#!/usr/bin/env bash
set -euo pipefail
package_root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
install_root=${DS1_INSTALL_ROOT:-$HOME/.local/share/ds1cli-rust}
operation=install
extra=()
encrypted_package=
while (($#)); do
  case "$1" in
    --package) encrypted_package=$2; shift 2;;
    --root) install_root=$2; shift 2;;
    --update) operation=update; shift;;
    --plan) extra+=(--plan); shift;;
    *) printf 'Unknown argument: %s\n' "$1" >&2; exit 2;;
  esac
done
if [[ -n $encrypted_package ]]; then
    export DS1CLI_PACKAGE_PATH=$encrypted_package DS1_INSTALL_ROOT=$install_root
    export DS1_INSTALL_MODE=$operation
    if ((${#extra[@]})); then export DS1CLI_INSTALL_PLAN=1; fi
    exec bash "$package_root/install-or-update.sh"
fi
(cd "$package_root" && sha256sum --strict -c package-files.sha256)
"$package_root/bin/ds1cli" package "$operation" --target "$install_root" "${extra[@]}"
printf 'Add this directory to PATH: %s/bin\n' "$install_root"
