#!/usr/bin/env bash
set -euo pipefail

SCRIPT_NAME="$(basename "$0")"
MODE="keep"
DRY_RUN=0

usage() {
  cat <<USAGE
Uninstall OpenPhotos installed by the universal Linux installer.

Usage:
  ${SCRIPT_NAME} [options]

Options:
  --keep-data          Keep /var/lib/openphotos and /opt/openphotos/models (default).
  --purge             Remove data, config, logs, models, and runtime files.
  --uninstall         Alias for --keep-data.
  --uninstall-purge   Alias for --purge.
  --dry-run           Print what would be removed.
  -h, --help          Show this help.
USAGE
}

note() {
  printf '%s: %s\n' "$SCRIPT_NAME" "$*"
}

fail() {
  printf '%s: error: %s\n' "$SCRIPT_NAME" "$*" >&2
  exit 1
}

has_command() {
  command -v "$1" >/dev/null 2>&1
}

require_command() {
  has_command "$1" || fail "required command not found: $1"
}

as_root() {
  if [[ "${EUID:-$(id -u)}" -eq 0 ]]; then
    "$@"
  else
    sudo "$@"
  fi
}

run_systemctl_if_available() {
  if has_command systemctl; then
    as_root systemctl "$@" >/dev/null 2>&1 || true
  fi
}

print_plan() {
  printf 'Would stop and disable services: openphotos.service rustus.service\n'
  printf 'Would remove service units and binaries.\n'
  if [[ "$MODE" == "purge" ]]; then
    printf 'Would remove /opt/openphotos, /opt/rknn, /etc/openphotos, /var/lib/openphotos, /var/log/openphotos, and /var/tmp/openphotos-installer.\n'
    printf 'Would remove system user/group when possible: openphotos\n'
  else
    printf 'Would remove program files but keep /var/lib/openphotos, /opt/openphotos/models, /etc/openphotos, and /var/log/openphotos.\n'
  fi
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --keep-data|--uninstall)
      MODE="keep"
      shift
      ;;
    --purge|--uninstall-purge)
      MODE="purge"
      shift
      ;;
    --dry-run)
      DRY_RUN=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      fail "unknown argument: $1"
      ;;
  esac
done

[[ "$(uname -s)" == "Linux" ]] || fail "this uninstaller supports Linux only."
require_command bash
if [[ "$DRY_RUN" == "1" ]]; then
  print_plan
  exit 0
fi
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
  require_command sudo
fi

note "stopping OpenPhotos services..."
run_systemctl_if_available disable --now openphotos.service rustus.service
run_systemctl_if_available stop openphotos.service rustus.service

note "removing OpenPhotos service units and binaries..."
as_root rm -f \
  /etc/systemd/system/openphotos.service \
  /etc/systemd/system/rustus.service \
  /usr/local/bin/openphotos \
  /usr/local/bin/rustus \
  /usr/local/bin/openphotos-uninstall
run_systemctl_if_available daemon-reload
run_systemctl_if_available reset-failed openphotos.service rustus.service

if [[ "$MODE" == "purge" ]]; then
  note "removing OpenPhotos data, models, config, logs, and runtime files..."
  as_root rm -rf \
    /opt/openphotos \
    /opt/rknn \
    /etc/openphotos \
    /var/lib/openphotos \
    /var/log/openphotos \
    /var/tmp/openphotos-installer
  if id -u openphotos >/dev/null 2>&1; then
    as_root userdel openphotos >/dev/null 2>&1 || true
  fi
  if getent group openphotos >/dev/null 2>&1; then
    as_root groupdel openphotos >/dev/null 2>&1 || true
  fi
else
  note "removing OpenPhotos program files and keeping data/models..."
  as_root rm -rf \
    /opt/openphotos/bin \
    /opt/openphotos/defaults \
    /opt/openphotos/lib \
    /opt/openphotos/web-photos \
    /opt/rknn \
    /var/tmp/openphotos-installer
fi

note "uninstall complete"
