#!/usr/bin/env bash
cd "$(dirname "$0")"
# Use REPO_ROOT instead of base - when scripts are sourced they do not need export to inherit vars
REPO_ROOT="$(pwd)"
source ./sdata/lib/environment-variables.sh
source ./sdata/lib/functions.sh
source ./sdata/lib/package-installers.sh

prevent_sudo_or_root
set -e

#####################################################################################
showhelp_global(){
printf "${STY_CYAN}NOTE:
  The old \"./install.sh\"   is now \"./setup install\"
  The old \"./update.sh\"    is now \"./setup exp-update\"
  The old \"./uninstall.sh\" is now \"./setup exp-uninstall\"${STY_RST}

[$0]: Handle setup for illogical-impulse.

Syntax:
  $0 <subcommand> [OPTIONS]...

Subcommands:
  install        (Re)Install/Update illogical-impulse.
                 Note: To update to the latest, manually run \"git pull\" first.
  install-deps   Run the install step \"1. Install dependencies\"
  install-setups Run the install step \"2. Setup for permissions/services etc\"
  install-files  Run the install step \"3. Copying config files\"

  exp-uninstall  (Experimental) Uninstall illogical-impulse.
  exp-update     (Experimental) Update illogical-impulse without fully reinstall.
  exp-update-old (Experimental) exp-update but use behaves like old version.

  checkdeps      (For dev only) Check whether pkgs exist in AUR or repos of Arch.
  help           Show this help message.

For each <subcommand>, use -h for details:
  $0 <subcommand> -h

${STY_BOLD}${STY_CYAN}Access ${STY_UNDERLINE}https://ii.clsty.link${STY_RST} ${STY_BOLD}${STY_CYAN}for documentation about illogical-impulse.${STY_RST}
"
}
case $1 in
  # Global help
  ""|help|--help|-h)showhelp_global;exit;;
  # Correct subcommand
  install|exp-uninstall|exp-update|exp-update-old|checkdeps)
    SUBCMD_NAME=$1
    SUBCMD_DIR=./sdata/subcmd-$1
    shift;;
  # Correct subcommand but not using ./sdata/subcmd-$1
  install-deps|install-setups|install-files)
    SUBCMD_NAME=$1
    SUBCMD_DIR=./sdata/subcmd-install
    shift;;
  # Wrong subcommand
  *)printf "${STY_RED}Unknown subcommand \"$1\".${STY_RST}\n";showhelp_global;exit 1;;
esac
#####################################################################################
if [[ -f "${SUBCMD_DIR}/options.sh" ]];
  then source "${SUBCMD_DIR}/options.sh"
fi
case ${SUBCMD_NAME} in
  install)
    if [[ "${SKIP_ALLGREETING}" != true ]]; then
      source ${SUBCMD_DIR}/0.greeting.sh
    fi
    if [[ "${SKIP_ALLDEPS}" != true ]]; then
      source ${SUBCMD_DIR}/1.deps-selector.sh
    fi
    if [[ "${SKIP_ALLSETUPS}" != true ]]; then
      source ${SUBCMD_DIR}/2.setups-selector.sh
    fi
    if [[ "${SKIP_ALLFILES}" != true ]]; then
      source ${SUBCMD_DIR}/3.files.sh
    fi
    ;;
  install-deps)
    if [[ "${SKIP_ALLDEPS}" != true ]]; then
      source ${SUBCMD_DIR}/1.deps-selector.sh
    fi
    ;;
  install-setups)
    if [[ "${SKIP_ALLSETUPS}" != true ]]; then
      source ${SUBCMD_DIR}/2.setups-selector.sh
    fi
    ;;
  install-files)
    if [[ "${SKIP_ALLFILES}" != true ]]; then
      source ${SUBCMD_DIR}/3.files.sh
    fi
    ;;
  *)
    source ${SUBCMD_DIR}/0.run.sh
    exit
    ;;
esac
