#!/usr/bin/env bash

set -e;

# If the script runs as root, assume this is a bare OS.
# shellcheck disable=SC2046
if [ $(id -u) = 0 ]; then
  apt update -y;
  DEBIAN_FRONTEND=noninteractive apt install sudo build-essential tzdata keyboard-configuration -y;
fi

# If the make package cannot be satisfied, assume the apt repositories need to
# be updated.
echo "[Prime APT repositories]";
if ! sudo apt satisfy make &> /dev/null; then sudo apt update -y; fi

# Ensure `make` is available.
echo "[Ensure installation of GNU Make]";
command -v make &> /dev/null || sudo apt install build-essential -y;

# If the Makefile is missing, assume this script is invoked directly and the
# repo itself needs to be downloaded.
echo "[Prepare installation files]";
if ! [ -f Makefile ]; then
  # Install curl and unzip, if required.
  (command -v curl &> /dev/null \
    && command -v unzip &> /dev/null \
    && (dpkg -l | tail -n+6 | awk '{ print "~"$2"~" }' | grep -q '~ca-certificates~') \
  ) || sudo apt install curl ca-certificates zip -y;

  # Download the archive of the main branch.
  curl -L https://github.com/elgentos/setup/archive/main.zip \
    --output /tmp/elgentos-setup.zip;

  # Empty the directory stack and remove the setup files when the script exits.
  trap 'while popd 2>/dev/null; do echo -n ""; done; rm -rf /tmp/setup-main' EXIT;

  # Extract the archive and push its output directory to the top of the stack.
  # This makes the Makefile and template files available for the rest of the
  # installation.
  pushd /tmp;
  unzip elgentos-setup.zip;
  rm -f elgentos-setup.zip;
  pushd setup-main;
fi

MAKE_OPTIONS=(
  --no-builtin-rules
  --no-builtin-variables
  "CI=${CI}"
)

# First test if we need to make the SSH key.
echo "[Ensure SSH key exists]";
if ! 2>/dev/null make ssh-key --quiet; then
  # shellcheck disable=SC2086
  make ssh-key ${MAKE_OPTIONS[*]};
  echo "";
  echo "Make sure your key is registered to your GitHub and GitLab accounts before proceeding.";
  echo "  https://github.com/settings/keys";
  echo "  https://gitlab.elgentos.nl/-/profile/keys";
  echo "";
  # shellcheck disable=SC2162
  cut -d: -f3 < /proc/1/cgroup | grep -q '/docker/' \
    || read -p "Press enter to continue, after installing the SSH key. . .";
fi

# Once the SSH key is installed, proceed with the rest of the installation.
TARGETS=(install)
OPTIONS="$*"

# If no targets are provided, ask for them.
#if [ $# -lt 1 ]; then
#  # Ask for installation targets.
#  echo "[Prepare optional software dialog]";
#  if ! 2>/dev/null make dialog --quiet; then
#    # shellcheck disable=SC2086
#    make dialog ${MAKE_OPTIONS[*]};
#  fi
#
#  options=()
#
#  for option in $(make -qp | grep 'optional:: |' | cut -d'|' -f2- | sort -u); do
#    if ! 2>/dev/null make -q "$option"; then
#      options+=("$option $option off")
#    fi
#  done
#
#  # shellcheck disable=SC2046
#  # shellcheck disable=SC2086
#  [ ${#options[@]} -gt 0 ] && OPTIONS=$(dialog \
#    --title 'Select software' \
#    --backtitle 'Optional workstation software' \
#    --separate-output \
#    --stdout \
#    --checklist 'Select optional software' \
#    0 0 0 ${options[*]} \
#  );
#fi

for target in $(echo "$OPTIONS" | tr ' ' '\n' | sort -u | uniq); do
  if 2>/dev/null make -q "$target"; then
    echo "[Skip $target] Already installed";
  else
    TARGETS+=("$target");
  fi
done

echo "[Make ${TARGETS[*]}]";
# shellcheck disable=SC2086
make -q ${TARGETS[*]} || make ${MAKE_OPTIONS[*]} ${TARGETS[*]};

echo "Finished";