#!/usr/bin/env bash
# Astitvaams Print Helper - remove it completely.
#
# The install puts three things on this computer and nothing else:
#   * a folder, ~/.ams-print-helper (Python and the helper live inside it -
#     nothing is installed system-wide, nothing needs a password)
#   * a login item: a LaunchAgent on macOS, a systemd --user unit on Linux
#   * the running process itself
# This removes all three, in that order, then checks the port is quiet.
#
# Nothing outside those three is touched. Other software on this computer,
# including anything else that uses Python, is left exactly as it was.
#
# Double-click it, or run ./uninstall.sh. Pass -y to skip the question when
# calling it from a script.
set -uo pipefail

APP="ams-print-helper"
DIR="${AMS_HELPER_DIR:-$HOME/.$APP}"
AGENT="com.astitva.$APP"
# What older installers wrote. Left behind, it keeps trying to start a helper
# that is no longer there, every time the machine is logged into.
LEGACY_AGENT="com.aams.printhelper"
PORT=18726

ASSUME_YES=0
case "${1:-}" in
  -y|--yes) ASSUME_YES=1 ;;
esac

echo
echo "  Astitvaams Print Helper - Remove"
echo "  ================================"
echo
echo "  This will remove:"
echo "    - the helper folder:  $DIR"
echo "    - the login item that starts it"
echo "    - the running program"
echo
echo "  Astitvaams keeps working. Printing falls back to the browser's own"
echo "  print dialog until the helper is installed again."
echo

installed=0
[ -d "$DIR" ] && installed=1
[ -f "$HOME/Library/LaunchAgents/$AGENT.plist" ] && installed=1
[ -f "$HOME/.config/systemd/user/$APP.service" ] && installed=1
if [ "$installed" -eq 0 ]; then
  echo "  Nothing to remove - the print helper is not installed on this computer."
  exit 0
fi

# One keypress, because a double-click should not be able to uninstall by
# accident - this file sits in the same folder as install.sh. Skipped when
# there is no terminal to ask in, so piping it into a shell still works.
if [ "$ASSUME_YES" -eq 0 ] && [ -t 0 ]; then
  printf "  Type Y and press Enter to remove it (anything else cancels): "
  read -r answer
  case "$answer" in
    [Yy]*) ;;
    *) echo; echo "  Cancelled. Nothing was changed."; exit 0 ;;
  esac
fi

# -- 1. Stop it starting again ------------------------------------------------
echo
echo "  Removing the login item..."
if [ "$(uname -s)" = "Darwin" ]; then
  for A in "$AGENT" "$LEGACY_AGENT"; do
    PLIST="$HOME/Library/LaunchAgents/$A.plist"
    if [ -f "$PLIST" ]; then
      launchctl unload "$PLIST" >/dev/null 2>&1 || true
      rm -f "$PLIST"
      echo "    removed: $A"
    fi
  done
else
  if [ -f "$HOME/.config/systemd/user/$APP.service" ]; then
    systemctl --user disable --now "$APP.service" >/dev/null 2>&1 || true
    rm -f "$HOME/.config/systemd/user/$APP.service"
    systemctl --user daemon-reload >/dev/null 2>&1 || true
    echo "    removed: $APP.service"
  fi
fi

# -- 2. Stop what is running now ----------------------------------------------
# Matched on the command line and scoped to this user: killing every python
# would take other people's programs down with it.
echo "  Stopping the helper..."
pkill -u "$(id -u)" -f "ams_print_helper" >/dev/null 2>&1 || true
sleep 2

# -- 3. Delete the folder -----------------------------------------------------
if [ -d "$DIR" ]; then
  echo "  Deleting $DIR ..."
  rm -rf "$DIR"
  if [ -d "$DIR" ]; then
    echo
    echo "  Could not delete the folder. Delete it yourself:"
    echo "      rm -rf \"$DIR\""
    exit 1
  fi
fi

# -- 4. Prove it is gone ------------------------------------------------------
# The install is only really undone when the port stops answering. Saying
# "removed" while an old copy still serves on 18726 is the one failure that
# would send someone in circles.
echo "  Checking..."
still_up=0
if command -v curl >/dev/null 2>&1; then
  curl -fsS --max-time 3 "http://127.0.0.1:$PORT/ping" >/dev/null 2>&1 && still_up=1
fi
if [ "$still_up" -eq 1 ]; then
  echo
  echo "  Something is still answering on port $PORT."
  echo "  Log out and back in, then run ./uninstall.sh again."
  exit 1
fi

echo
echo "  Astitvaams Print Helper removed."
echo
echo "  To install it again, download it from Astitvaams and run install.sh."
