#!/usr/bin/env bash
# Safer bash tips: https://elder.dev/posts/safer-bash/
set -o errexit
set -o nounset
set -o pipefail

SCRIPT_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
export ROOT_DIR=${SCRIPT_DIR}/..
export BIN=${SCRIPT_DIR}
export CONFIG=${ROOT_DIR}/packages
export REMOTE=http://tiny.dengtianshuo.com/dropship

function usage() {
    cat <<EOF
Description:
        Install emacs and tmux configs wherever you go.

Install location:
        $(dirname ${SCRIPT_DIR})

Usage: $(basename $0) <command>

Options:
        -h/-help       Show help.

Subcommands:
$(
a=(${BIN}/dropship-*)
a=("${a[@]/#${BIN}\/dropship-/}")
printf '\t%s\n' "${a[@]}"
)
EOF
}

function ensure_gum_installed() {
command -v gum >/dev/null || {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        brew install gum
    elif [[ -f /etc/debian_version ]]; then
        sudo apt install gum
    else
        echo "Please install gum first"; exit 1;
    fi
}
}

[[ ! $# -gt 0 ]] && { usage; exit 1; }
case $1 in
    -h|--help)
        usage
        exit 0
        ;;
    *)  # No argument accepted for this script
        cmd="$1"
        cmd_script="${BIN}/dropship-${cmd}"
        shift
        if [[ -f ${cmd_script} ]]; then
            source ${cmd_script}
            exit 0;
        else
            echo "Unknown argument: ${cmd}."
            usage
            exit 1
        fi
        ;;
esac

