#!/bin/bash
# Uninstallation script for Dangerous Pi systemd service

set -e

echo "Uninstalling Dangerous Pi systemd service..."

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

# Variables
SERVICE_NAME="dangerous-pi"
SERVICE_FILE="dangerous-pi.service"
SYSTEMD_DIR="/etc/systemd/system"

# Stop the service if running
if systemctl is-active --quiet "$SERVICE_NAME"; then
    echo "Stopping $SERVICE_NAME service..."
    systemctl stop "$SERVICE_NAME"
fi

# Disable the service
if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
    echo "Disabling $SERVICE_NAME service..."
    systemctl disable "$SERVICE_NAME"
fi

# Remove service file
if [ -f "$SYSTEMD_DIR/$SERVICE_FILE" ]; then
    echo "Removing service unit file..."
    rm "$SYSTEMD_DIR/$SERVICE_FILE"
fi

# Reload systemd daemon
echo "Reloading systemd daemon..."
systemctl daemon-reload
systemctl reset-failed || true

echo ""
echo "✅ Uninstallation complete!"
echo ""
echo "Note: Application files in /opt/dangerous-pi were NOT removed."
echo "To remove them manually: sudo rm -rf /opt/dangerous-pi"
echo ""
