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

INSTALL_DIR="${EXECUTOR_INSTALL_DIR:-$HOME/.executor/bin}"
RUNTIME_DIR="${EXECUTOR_RUNTIME_DIR:-$HOME/.executor/runtime}"
EXECUTOR_HOME="${EXECUTOR_HOME_DIR:-$HOME/.executor}"
SERVICE_DIR="$RUNTIME_DIR/services"
BACKEND_PID_FILE="$SERVICE_DIR/backend.pid"
WEB_PID_FILE="$SERVICE_DIR/web.pid"
ASSUME_YES="0"

usage() {
  cat <<EOF
Executor uninstaller

Usage:
  bash executor/uninstall [--yes]

Options:
  -y, --yes     Skip confirmation prompt
  -h, --help    Show this help
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    -y|--yes)
      ASSUME_YES="1"
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown option: $1"
      exit 1
      ;;
  esac
done

stop_pid_file() {
  local pid_file="$1"
  if [[ ! -f "$pid_file" ]]; then
    return
  fi

  local pid
  pid="$(cat "$pid_file" 2>/dev/null || true)"
  if [[ -n "$pid" ]] && kill -0 "$pid" >/dev/null 2>&1; then
    kill "$pid" >/dev/null 2>&1 || true
    sleep 1
    if kill -0 "$pid" >/dev/null 2>&1; then
      kill -9 "$pid" >/dev/null 2>&1 || true
    fi
  fi

  rm -f "$pid_file"
}

if [[ "$ASSUME_YES" != "1" ]]; then
  echo "This will remove:"
  echo "  - $INSTALL_DIR/executor"
  echo "  - $RUNTIME_DIR"
  echo ""
  read -r -p "Continue? [y/N] " response
  if [[ ! "$response" =~ ^[Yy]$ ]]; then
    echo "Cancelled."
    exit 0
  fi
fi

stop_pid_file "$WEB_PID_FILE"
stop_pid_file "$BACKEND_PID_FILE"

rm -f "$INSTALL_DIR/executor"
rm -rf "$RUNTIME_DIR"

if [[ -d "$INSTALL_DIR" ]]; then
  rmdir "$INSTALL_DIR" 2>/dev/null || true
fi

if [[ -d "$EXECUTOR_HOME" ]]; then
  rmdir "$EXECUTOR_HOME" 2>/dev/null || true
fi

echo "Executor uninstall complete."
echo ""
echo "If you previously added PATH manually, remove this line from your shell rc:"
echo "  export PATH=$INSTALL_DIR:\$PATH"
