#!/usr/bin/env bash
set -uo pipefail;

####################################
# Ensure we can execute standalone #
####################################

function early_death() {
  echo "[FATAL] ${0}: ${1}" >&2;
  exit 1;
};

if [ -z "${TOFUENV_ROOT:-""}" ]; then
  # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
  readlink_f() {
    local target_file="${1}";
    local file_name;

    while [ "${target_file}" != "" ]; do
      cd "${target_file%/*}" || early_death "Failed to 'cd \$(${target_file%/*})' while trying to determine TOFUENV_ROOT";
      file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"' while trying to determine TOFUENV_ROOT";
      target_file="$(readlink "${file_name}")";
    done;

    echo "$(pwd -P)/${file_name}";
  };
  TOFUENV_SHIM=$(readlink_f "${0}")
  TOFUENV_ROOT="${TOFUENV_SHIM%/*/*}";
  [ -n "${TOFUENV_ROOT}" ] || early_death "Failed to determine TOFUENV_ROOT";

else
  TOFUENV_ROOT="${TOFUENV_ROOT%/}";
fi;
export TOFUENV_ROOT;

if [ -n "${TOFUENV_HELPERS:-""}" ]; then
  log 'debug' 'TOFUENV_HELPERS is set, not sourcing helpers again';
else
  [ "${TOFUENV_DEBUG:-0}" -gt 0 ] && >&2 echo "[DEBUG] Sourcing helpers from ${TOFUENV_ROOT}/lib/tofuenv-helpers.sh";
  if source "${TOFUENV_ROOT}/lib/tofuenv-helpers.sh"; then
    log 'debug' 'Helpers sourced successfully';
  else
    early_death "Failed to source helpers from ${TOFUENV_ROOT}/lib/tofuenv-helpers.sh";
  fi;
fi;

# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
  case ":${PATH}:" in
    *:${TOFUENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TOFUENV_ROOT}/${dir}', not adding it again";;
    *)
      log 'debug' "\$PATH does not contain '${TOFUENV_ROOT}/${dir}', prepending and exporting it now";
      export PATH="${TOFUENV_ROOT}/${dir}:${PATH}";
      ;;
  esac;
done;

#####################
# Begin Script Body #
#####################

declare arg="${1:-""}";

log 'debug' "Setting TOFUENV_DIR to ${PWD}";
export TOFUENV_DIR="${PWD}";

abort() {
  log 'debug' 'Aborting...';
  {
    if [ "${#}" -eq 0 ]; then
      cat -;
    else
      echo "tofuenv: ${*}";
    fi;
  } >&2;
};

log 'debug' "tofuenv argument is: ${arg}";

case "${arg}" in
  "")
    log 'debug' 'No argument provided, dumping version and help and aborting';
    {
      tofuenv---version;
      tofuenv-help;
    } | abort && exit 1;
exit 1;
    ;;
  -v | --version )
    log 'debug' 'tofuenv version requested...';
    exec tofuenv---version;
    ;;
  -h | --help )
    log 'debug' 'tofuenv help requested...';
    exec tofuenv-help;
    ;;
  *)
    log 'debug' "Long argument provided: ${arg}";
    command_path="$(command -v "tofuenv-${arg}" || true)";
    log 'debug' "Resulting command-path: ${command_path}";
    if [ -z "${command_path}" ]; then
      {
        echo "No such command '${arg}'";
        tofuenv-help;
      } | abort && exit 1;
    fi;
    shift 1;
    log 'debug' "Exec: \"${command_path}\" \"$*\"";
    exec "${command_path}" "$@";
    ;;
esac;

log 'error' 'This line should not be reachable. Something catastrophic has occurred';
