#!/bin/sh
# =============================================================================
# epc670_cleanup_legacy
# Removes old scripts, services, configs, and other artifacts from previous
# versions of the epc670-tofhat-utils package.
# Intended to be run on a live system from /usr/bin.
# =============================================================================
set -e

EPC_HOME="/home/epc"
OLD_SCRIPTS="epc-set-resolution.sh epc-versions epc-update epc-notifier"
OLD_SYS_SERVICES="epc-set-resolution.service"
OLD_USER_SERVICES="epc-notifier.service epc-notifier.timer"
OLD_DESKTOP_ENTRIES="docs.desktop epc-docs.desktop update.desktop TOFcam670.desktop GUI-TOFcam-670.desktop"
OLD_AUTOSTART_ENTRIES="epc-set-resolution.desktop TOFcam670.desktop GUI-TOFcam-670.desktop"
OLD_CONFIGS="waybar/config"
OLD_LAUNCHER="/usr/lib/epc670/tofcam670-startup.sh"
OLD_PASS="$EPC_HOME/.epc_sudo"
OLD_MONITORED_PACKAGES="/etc/epc/monitored-packages.conf"
OLD_HOTPLUG_RULE="/etc/udev/rules.d/99-hdmi-hotplug.rules"

log() {
  echo "epc670_cleanup_legacy: $*"
}

# -----------------------------------------------------------------------------
# Scripts
# -----------------------------------------------------------------------------
for script in $OLD_SCRIPTS; do
  if [ -f "/usr/local/bin/$script" ]; then
    log "Removing old script /usr/local/bin/$script"
    rm -f "/usr/local/bin/$script"
  fi
done

# -----------------------------------------------------------------------------
# System services
# -----------------------------------------------------------------------------
for service in $OLD_SYS_SERVICES; do
  if [ -f "/etc/systemd/system/$service" ]; then
    log "Removing old system service $service"
    systemctl stop "$service" 2>/dev/null || true
    rm -f "/etc/systemd/system/$service"
  fi
done

# -----------------------------------------------------------------------------
# User services, desktop entries, autostart entries, and config files
# (all scoped to the epc user — skip if the user does not exist)
# -----------------------------------------------------------------------------
if id "epc" >/dev/null 2>&1; then
  EPC_UID=$(id -u "epc")

  for service in $OLD_USER_SERVICES; do
    if [ -f "/usr/lib/systemd/user/$service" ]; then
      log "Removing old user service $service"
      XDG_RUNTIME_DIR="/run/user/$EPC_UID" \
        systemctl --user stop "$service" 2>/dev/null || true
      rm -f "/usr/lib/systemd/user/$service"
    fi
  done

  for desktop in $OLD_DESKTOP_ENTRIES; do
    path="$EPC_HOME/Desktop/$desktop"
    if [ -f "$path" ]; then
      log "Removing old desktop shortcut $path"
      rm -f "$path"
    fi
  done

  for entry in $OLD_AUTOSTART_ENTRIES; do
    path="$EPC_HOME/.config/autostart/$entry"
    if [ -f "$path" ]; then
      log "Removing old autostart entry $path"
      rm -f "$path"
    fi
  done

  for cfg in $OLD_CONFIGS; do
    path="$EPC_HOME/.config/$cfg"
    if [ -f "$path" ]; then
      log "Removing old config $path"
      rm -f "$path"
    fi
  done
else
  log "User 'epc' not found — skipping user-scoped cleanup."
fi

# -----------------------------------------------------------------------------
# Miscellaneous files (not scoped to the epc user)
# -----------------------------------------------------------------------------
for path in "$OLD_LAUNCHER" "$OLD_MONITORED_PACKAGES" "$OLD_HOTPLUG_RULE"; do
  if [ -f "$path" ]; then
    log "Removing old artifact $path"
    rm -f "$path"
  fi
done

# OLD_PASS lives under EPC_HOME, so only attempt it if the user exists
if id "epc" >/dev/null 2>&1 && [ -f "$OLD_PASS" ]; then
  log "Removing old artifact $OLD_PASS"
  rm -f "$OLD_PASS"
fi

log "Legacy cleanup complete."