#!/bin/bash
# SMirrors Uninstallation Script
# Removes SMirrors binary, service files, and optionally data/configuration

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
BINARY_NAME="smirrors"
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc/smirrors"
DATA_DIR="/var/lib/smirrors"
CACHE_DIR="/var/cache/smirrors"
LOG_DIR="/var/log/smirrors"
RUN_DIR="/var/run/smirrors"
SYSTEMD_DIR="/etc/systemd/system"

echo -e "${RED}SMirrors Uninstallation Script${NC}"
echo "================================"
echo

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
    exit 1
fi

# Confirm uninstallation
echo -e "${YELLOW}This will remove SMirrors from your system.${NC}"
read -p "Are you sure you want to continue? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Uninstallation cancelled."
    exit 0
fi

# 1. Stop and disable service
echo -e "${GREEN}[1/6]${NC} Stopping and disabling service..."
if systemctl is-active --quiet "${BINARY_NAME}.service"; then
    systemctl stop "${BINARY_NAME}.service"
    echo "      ✓ Service stopped"
fi

if systemctl is-active --quiet "${BINARY_NAME}.timer"; then
    systemctl stop "${BINARY_NAME}.timer"
    echo "      ✓ Timer stopped"
fi

if systemctl is-enabled --quiet "${BINARY_NAME}.service" 2>/dev/null; then
    systemctl disable "${BINARY_NAME}.service"
    echo "      ✓ Service disabled"
fi

if systemctl is-enabled --quiet "${BINARY_NAME}.timer" 2>/dev/null; then
    systemctl disable "${BINARY_NAME}.timer"
    echo "      ✓ Timer disabled"
fi

# 2. Remove systemd service files
echo -e "${GREEN}[2/6]${NC} Removing systemd service files..."
if [ -f "${SYSTEMD_DIR}/${BINARY_NAME}.service" ]; then
    rm -f "${SYSTEMD_DIR}/${BINARY_NAME}.service"
    echo "      ✓ Service file removed"
fi

if [ -f "${SYSTEMD_DIR}/${BINARY_NAME}.timer" ]; then
    rm -f "${SYSTEMD_DIR}/${BINARY_NAME}.timer"
    echo "      ✓ Timer file removed"
fi

systemctl daemon-reload
echo "      ✓ Systemd reloaded"

# 3. Remove binary
echo -e "${GREEN}[3/6]${NC} Removing binary..."
if [ -f "${INSTALL_DIR}/${BINARY_NAME}" ]; then
    rm -f "${INSTALL_DIR}/${BINARY_NAME}"
    echo "      ✓ Binary removed"
else
    echo "      ⓘ Binary not found"
fi

# 4. Remove runtime files
echo -e "${GREEN}[4/6]${NC} Removing runtime files..."
if [ -d "${RUN_DIR}" ]; then
    rm -rf "${RUN_DIR}"
    echo "      ✓ Runtime directory removed"
fi

if [ -d "${CACHE_DIR}" ]; then
    rm -rf "${CACHE_DIR}"
    echo "      ✓ Cache directory removed"
fi

# 5. Ask about data and logs
echo -e "${GREEN}[5/6]${NC} Data and logs..."
read -p "Remove data directory (${DATA_DIR})? This includes the database. [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    if [ -d "${DATA_DIR}" ]; then
        rm -rf "${DATA_DIR}"
        echo "      ✓ Data directory removed"
    fi
else
    echo "      ⓘ Data directory preserved"
fi

read -p "Remove log directory (${LOG_DIR})? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    if [ -d "${LOG_DIR}" ]; then
        rm -rf "${LOG_DIR}"
        echo "      ✓ Log directory removed"
    fi
else
    echo "      ⓘ Log directory preserved"
fi

# 6. Ask about configuration
echo -e "${GREEN}[6/6]${NC} Configuration..."
read -p "Remove configuration directory (${CONFIG_DIR})? This includes backups. [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    if [ -d "${CONFIG_DIR}" ]; then
        rm -rf "${CONFIG_DIR}"
        echo "      ✓ Configuration directory removed"
    fi
else
    echo "      ⓘ Configuration directory preserved"
fi

echo
echo -e "${GREEN}Uninstallation complete!${NC}"
echo

# Show preserved directories
PRESERVED=()
[ -d "${CONFIG_DIR}" ] && PRESERVED+=("Configuration: ${CONFIG_DIR}")
[ -d "${DATA_DIR}" ] && PRESERVED+=("Data: ${DATA_DIR}")
[ -d "${LOG_DIR}" ] && PRESERVED+=("Logs: ${LOG_DIR}")

if [ ${#PRESERVED[@]} -gt 0 ]; then
    echo "Preserved directories:"
    for dir in "${PRESERVED[@]}"; do
        echo "  - $dir"
    done
    echo
    echo "To completely remove SMirrors, delete these directories manually."
fi
