#!/bin/bash

# NUPST Uninstaller Script
# Completely removes NUPST from the system

# Check if running as root
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root (sudo nupst uninstall or sudo ./uninstall.sh)"
  exit 1
fi

# This script can be called directly or through the CLI
# When called through the CLI, environment variables are set
# REMOVE_CONFIG=yes|no - whether to remove configuration files
# REMOVE_REPO=yes|no - whether to remove the repository

# If not set through CLI, use defaults
REMOVE_CONFIG=${REMOVE_CONFIG:-"no"}
REMOVE_REPO=${REMOVE_REPO:-"no"}

echo "NUPST Uninstaller"
echo "================="
echo "This will completely remove NUPST from your system."

# Find the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Step 1: Stop and disable the systemd service if it exists
if [ -f "/etc/systemd/system/nupst.service" ]; then
  echo "Stopping NUPST service..."
  systemctl stop nupst.service 2>/dev/null
  
  echo "Disabling NUPST service..."
  systemctl disable nupst.service 2>/dev/null

  echo "Removing systemd service file..."
  rm -f /etc/systemd/system/nupst.service
  
  echo "Reloading systemd daemon..."
  systemctl daemon-reload
fi

# Step 2: Remove global symlink
if [ -L "/usr/local/bin/nupst" ]; then
  echo "Removing global symlink..."
  rm -f /usr/local/bin/nupst
fi

# Step 3: Remove configuration if requested
if [ "$REMOVE_CONFIG" = "yes" ]; then
  echo "Removing configuration files..."
  rm -rf /etc/nupst
else
  # If not called through CLI, ask user
  if [ -z "$NUPST_CLI_CALL" ]; then
    read -p "Do you want to remove the NUPST configuration files? (y/N) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      echo "Removing configuration files..."
      rm -rf /etc/nupst
    fi
  fi
fi

# Step 4: Remove repository if requested
if [ "$REMOVE_REPO" = "yes" ]; then
  if [ -d "$SCRIPT_DIR/.git" ]; then
    echo "Removing NUPST repository directory..."
    
    # Get parent directory to remove it after the script exits
    PARENT_DIR=$(dirname "$SCRIPT_DIR")
    REPO_NAME=$(basename "$SCRIPT_DIR")
    
    # Create a temporary cleanup script
    CLEANUP_SCRIPT=$(mktemp)
    echo "#!/bin/bash" > "$CLEANUP_SCRIPT"
    echo "sleep 1" >> "$CLEANUP_SCRIPT"
    echo "rm -rf \"$SCRIPT_DIR\"" >> "$CLEANUP_SCRIPT"
    echo "echo \"NUPST repository has been removed.\"" >> "$CLEANUP_SCRIPT"
    chmod +x "$CLEANUP_SCRIPT"
    
    # Run the cleanup script in the background
    nohup "$CLEANUP_SCRIPT" > /dev/null 2>&1 &
    
    echo "NUPST repository will be removed after uninstaller exits."
  else 
    echo "No git repository found."
  fi
else
  # If not requested, just display info
  if [ -d "$SCRIPT_DIR/.git" ]; then
    echo
    echo "NUPST repository at $SCRIPT_DIR will remain intact."
  fi
fi

# Check for npm global installation
NODE_PATH=$(which node 2>/dev/null)
if [ -n "$NODE_PATH" ]; then
  NPM_PATH=$(dirname "$NODE_PATH")/npm
  if [ -x "$NPM_PATH" ]; then
    echo
    echo "If you installed NUPST via npm, you may want to uninstall it with:"
    echo "  npm uninstall -g @serve.zone/nupst"
  fi
fi

echo
echo "NUPST has been uninstalled from your system."