#!/usr/bin/bash

# Copyright © Josep Bigorra <jjbigorra@gmail.com>
#
# maak is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# maak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with maak. If not, see <https://www.gnu.org/licenses/>.

# Commentary:
#
# This bash script provides a convenient and user-friendly wrapper
# around the command-line usage of `maak`, allowing the
# Guile Scheme application to behave and feel like any other
# executable or binary that users may install to their systems.
#
# This also helps installing `maak` in a convenient fashion
# to the Guix store and it allows us to decouple the user-facing
# command-line interface from the internal Guile Scheme arguments
# to the main program and library.
#
# Some of the rationale behind the `SCRIPT_DIR` and `STORE_DIR`
# can be explained due to the need of reliably and reproducibly
# deploying source code and other resources to the store
# and have access to those at runtime.

set -euo pipefail

script_dir=$(dirname "$0")
store_dir=$(readlink -f "$script_dir/..")
scheme_sources_dir="$store_dir/share/guile/site/3.0"
resources_dir="$store_dir/share/resources"
scripts_dir="$store_dir/share/scripts"

bold_text='\033[1m'
red_text='\033[0;31m'
reset_text='\033[0m'

get_timestamp() {
  date '+%Y-%m-%d %H:%M:%S%:z'
}

log_error() {
  if [ -z "${1}" ]; then
    log_error "log-error was called without a message."
    return 1
  fi

  echo -e "${red_text}[ERROR][$(get_timestamp)] ${1}${reset_text}" >&2
}

log_info() {
  if [ -z "${1}" ]; then
    log_error "log-info was called without a message."
    return 1
  fi

  echo -e "${bold_text}[INFO][$(get_timestamp)] ${1}${reset_text}"
}


show_help() {
  cat "$resources_dir/help.txt"
}

parsed_options=$(getopt -n "maak" -o lf:hnq --longoptions list,file,help,dry-run,quiet -- "$@")
eval set -- "$parsed_options"

file_arg=""

guile_cmd_args=(
  "-L" "${scheme_sources_dir}"
  "-c" "((@(maak main) main))"
  "--resources" "${resources_dir}"
)

while true; do
  case "${1}" in
  -f | --file)
    file_arg="${2}"
    guile_cmd_args+=("--file" "$(realpath "${file_arg}")")
    shift 2
    ;;
  -h | --help)
    show_help
    exit 0
    ;;
  -l | --list)
    guile_cmd_args+=("--list")
    shift
    ;;
  -n | --dry-run)
    guile_cmd_args+=("--dry-run")
    shift
    ;;
  -q | --quiet)
    guile_cmd_args+=("--quiet")
    shift
    ;;
  --)
    shift
    [ "$#" -gt 0 ] && guile_cmd_args+=("--tasks" "${@}")
    break
    ;;
  *)
    log_error "Error: unrecognized option '${1}' !"
    show_help
    exit 1
    ;;
  esac
done

if [[ -z "${file_arg}" ]]; then
  guile_cmd_args+=("--file" "$(realpath ./maak.scm)")
fi

# Run Guile application
exec guile "${guile_cmd_args[@]}"
