#!/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
source ./sdata/lib/dist-determine.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.
  virtmon        (For dev only) Create virtual monitors for testing multi-monitors.
  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|virtmon)
    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)
    for function in ${print_os_group_id_functions[@]}; do
      $function
    done
    pause
    if [[ "${SKIP_ALLGREETING}" != true ]]; then
      source ${SUBCMD_DIR}/0.greeting.sh
    fi
    if [[ "${SKIP_ALLDEPS}" != true ]]; then
      source ${SUBCMD_DIR}/1.deps-router.sh
    fi
    if [[ "${SKIP_ALLSETUPS}" != true ]]; then
      source ${SUBCMD_DIR}/2.setups.sh
    fi
    if [[ "${SKIP_ALLFILES}" != true ]]; then
      source ${SUBCMD_DIR}/3.files.sh
    fi
    ;;
  install-deps)
    for function in ${print_os_group_id_functions[@]}; do
      $function
    done
    pause
    if [[ "${SKIP_ALLDEPS}" != true ]]; then
      source ${SUBCMD_DIR}/1.deps-router.sh
    fi
    ;;
  install-setups)
    for function in ${print_os_group_id_functions[@]}; do
      $function
    done
    pause
    if [[ "${SKIP_ALLSETUPS}" != true ]]; then
      source ${SUBCMD_DIR}/2.setups.sh
    fi
    ;;
  install-files)
    for function in ${print_os_group_id_functions[@]}; do
      $function
    done
    pause
    if [[ "${SKIP_ALLFILES}" != true ]]; then
      source ${SUBCMD_DIR}/3.files.sh
    fi
    ;;
  *)
    source ${SUBCMD_DIR}/0.run.sh
    exit
    ;;
esac
