#!/usr/bin/env bash
# vim:et:ts=2:sts=2:sw=2

# Get absolute repository root (especially when symlinked)
ROOT="$(realpath "$(dirname "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")")")"

# Load accompanying bash library
source "$ROOT/lib/lib.bash"

SUBJECT="$1" # Subject to install (eg docs, sheets, rasters or "all")
VERSION="$2" # Version for the subject

if [[ "${1:-}" =~ ^(-h|--help)$ || -z "${SUBJECT:-}" ]]; then
  say "
  USAGE:
    $(g "${BASH_SOURCE##*/}")  [$(c subject)]  [$(c version)]

  DESCRIPTION:
    $(g "${BASH_SOURCE##*/}") installs a curriculum subject (or all subjects) into the Desktop

    If $(c subject) is omitted or 'all', all available local subjects will be installed.
    The lastest local version will be used by default for each subject if $(c version) is omitted.

  EXAMPLES:
    $(g "${BASH_SOURCE##*/}")  $(c docs)
    $(g "${BASH_SOURCE##*/}")  $(c shell)  $(c 0.1.20)
    $(g "${BASH_SOURCE##*/}")  $(c all) (default)

  NOTE:
    Use $(g "tl-comp-pull curriculum <subject>") to download curriculum subjects first.
"
  exit 1
fi

# Exit now if unless run as the admin user
assert-is-admin

DEST="${DEST:-/var/btrfs/@guest/Desktop}"
TEMP_BASE_DIR="$(mktemp -d)" # Global temporary directory

cleanup() { rm -rf "$TEMP_BASE_DIR"; }

install_subject() {
  local subject_name="$1"
  local subject_version="${2:-$("$ROOT"/bin/tl-comp-latest "curriculum" "$subject_name" 2>/dev/null)}"

  if [ -z "$subject_version" ]; then
    say "$(r WARNING) No local version found for subject $(c "$subject_name"). Skipping installation"
    return 0
  fi

  local SUBJECT_DOWNLOAD_DIR="/srv/curriculum/$subject_name"
  local ARCHIVE_PATH="$SUBJECT_DOWNLOAD_DIR/v$subject_version.tar.gz"

  if [ ! -f "$ARCHIVE_PATH" ]; then
    err "Curriculum archive not found for $(c "$subject_name") v$(c "$subject_version") at $(c "$ARCHIVE_PATH")"
    return 1
  fi

  local SUBJECT_TEMP_DIR="$(mktemp -d -p "$TEMP_BASE_DIR")" # Create temp dir inside global base
  say "Unpacking $(c "$subject_name") v$(c "$subject_version") into $SUBJECT_TEMP_DIR"
  run tar -xzf "$ARCHIVE_PATH" -C "$SUBJECT_TEMP_DIR"

  say "Replacing existing version of $(c "$subject_name") in $DEST/$subject_name"
  {
    run sudo rm -rf "$DEST/$subject_name"
    run sudo mkdir -p "$DEST/$subject_name"
    run sudo rsync -az "$SUBJECT_TEMP_DIR/" "$DEST/$subject_name/"

    run sudo chown -R guest:guest "$DEST/$subject_name"
  }

  run write "/srv/curriculum/$subject_name/.override" "$subject_version"
  say "Done. Curriculum $(c "$subject_name") v$(c "$subject_version") is installed at $(c "$DEST/$subject_name")"
  return 0
}

if [ -z "$SUBJECT" ] || [ "$SUBJECT" = "all" ]; then
  say "Installing all available curriculum subjects."
  for sub_dir in /srv/curriculum/*; do
    if [ -d "$sub_dir" ] && [ -f "$sub_dir/.v/local" ]; then
      subject_name=$(basename "$sub_dir")
      install_subject "$subject_name" "$VERSION" || say "$(r Installation failed for subjec) $(c "$subject_name")." # Continue even if one fails
    fi
  done
else
  install_subject "$SUBJECT" "$VERSION"
fi

say "All requested curriculum installations complete."
