#!/bin/bash
#
# Purpose : HamPack's smoketest health checks. Run in a test VM with HamPack
#           installed; prints what it checked, and exits 1 if anything's wrong.
#           The Windows applications aren't checked: the smoketest skips them.

HAMPACK_DIR="${HAMPACK_DIR:-$HOME/.local/share/siblings/HamPack}"
APPLICATIONS_DIR="$HOME/.local/share/applications"
# shellcheck source=../lib.sh
source "$HAMPACK_DIR/lib.sh"

problems=()
problem() {
  echo "PROBLEM: $*"
  problems+=("$*")
}

# Arch packages (or the packages providing them) and groups
packages_installed() {
  local p
  while read -r p; do
    pacman -T "$p" > /dev/null 2>&1 || pacman -Qg "$p" > /dev/null 2>&1 || problem "package $p isn't installed"
  done < <(source "$HAMPACK_DIR/packages.conf"; printf '%s\n' "${UTILITIES[@]}" "${APPLICATIONS[@]}")
  echo "checked packages.conf's packages"
}

flatpaks_installed() {
  local id
  while read -r id; do
    flatpak info "$id" > /dev/null 2>&1 || problem "Flatpak $id isn't installed"
  done < <(source "$HAMPACK_DIR/packages.conf"; printf '%s\n' "${FLATPAKS[@]}")
  echo "checked packages.conf's Flatpaks"
}

# True if FILE is an ELF binary
is_elf() {
  [ "$(head -c 4 "$1" 2> /dev/null | tail -c 3)" = ELF ]
}

# parse_conf callback for binaries.conf and compile.conf: the app is where it
# should be, at the version in the conf, with every library it links to, and
# with its desktop file
check_app() {
  local app="${CONF[_name]}" path recorded missing desktop=""
  path="$(expand_vars "${CONF[install]}")"
  if [ ! -e "$path" ]; then
    problem "$app isn't installed at $path"
    return
  fi
  recorded="$(get_recorded_version "$app")"
  [ "$recorded" = "${CONF[version]}" ] \
    || problem "$app is recorded at version ${recorded:-(none)}, not ${CONF[version]}"
  if is_elf "$path"; then
    missing="$(ldd "$path" 2> /dev/null | grep 'not found')"
    [ -z "$missing" ] || problem "$app can't find libraries: $(echo "$missing" | awk '{print $1}' | tr '\n' ' ')"
  fi
  if [ -n "${CONF[desktop]}" ]; then
    desktop="$APPLICATIONS_DIR/$(basename "$(expand_vars "${CONF[desktop]}")")"
  elif [ "$1" = binary ] && [ "${CONF[gui]}" = true ]; then
    desktop="$APPLICATIONS_DIR/$app.desktop"
  fi
  [ -z "$desktop" ] || [ -f "$desktop" ] || problem "$app has no desktop file at $desktop"
  echo "checked $app"
}
check_binary() { check_app binary; }
check_compiled() { check_app compiled; }

packages_installed
flatpaks_installed
parse_conf "$HAMPACK_DIR/binaries.conf" check_binary
parse_conf "$HAMPACK_DIR/compile.conf" check_compiled

systemctl is-enabled --quiet graywolf 2> /dev/null || problem "graywolf's service isn't enabled"
uv tool list 2> /dev/null | grep -q '^not1mm ' || problem "not1mm isn't installed"
[ -f "$APPLICATIONS_DIR/k6gte-not1mm.desktop" ] || problem "not1mm has no desktop file"
[ -x "$HOME/.local/bin/hampackremove" ] || problem "hampackremove isn't installed"
echo "checked graywolf's service, not1mm and hampackremove"

if (( ${#problems[@]} > 0 )); then
  echo "${#problems[@]} problem(s)"
  exit 1
fi
echo "HamPack is healthy"
