#!/bin/bash

# A robust script to uninstall Netmaker Desktop.
# This script must be run with sudo privileges.

# Exit immediately if a command exits with a non-zero status.
set -e

# --- Configuration: Variables for all file paths ---
SERVICE_NAME="netmaker-desktop"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
DESKTOP_FILE="/usr/share/applications/${SERVICE_NAME}.desktop"
ICON_FILE="/usr/share/icons/hicolor/scalable/apps/${SERVICE_NAME}.png"
DAEMON_BINARY="/usr/bin/netmaker-desktop-daemon"
GUI_DIR="/opt/NetmakerDesktop"

# --- Functions ---

# Log a message with a timestamp.
log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] - $1"
}

# Stop and remove the systemd service for the daemon.
uninstall_service() {
    log "--- Stopping and removing systemd service ---"
    # Check if the service is running before trying to stop it
    if systemctl is-active --quiet "$SERVICE_NAME"; then
        log "Stopping ${SERVICE_NAME} service..."
        systemctl stop "$SERVICE_NAME"
    else
        log "Service '${SERVICE_NAME}' is not running."
    fi

    # Check if the service is enabled before trying to disable it
    if systemctl is-enabled --quiet "$SERVICE_NAME"; then
        log "Disabling ${SERVICE_NAME} service..."
        systemctl disable "$SERVICE_NAME"
    fi

    # Check if the service file exists before trying to remove it
    if [ -f "$SERVICE_FILE" ]; then
        log "Removing service file: ${SERVICE_FILE}"
        rm -f "$SERVICE_FILE"
        log "Reloading systemd daemon..."
        systemctl daemon-reload
        systemctl reset-failed
    else
        log "Service file not found. Skipping removal."
    fi
}

# Remove the application's .desktop entry and icon.
uninstall_shortcuts() {
    log "--- Removing application shortcuts and icons ---"
    if [ -f "$DESKTOP_FILE" ]; then
        log "Removing desktop entry: ${DESKTOP_FILE}"
        rm -f "$DESKTOP_FILE"
    else
        log "Desktop entry not found. Skipping removal."
    fi

    if [ -f "$ICON_FILE" ]; then
        log "Removing icon file: ${ICON_FILE}"
        rm -f "$ICON_FILE"
    else
        log "Icon file not found. Skipping removal."
    fi

    # Update the desktop database if the command exists
    if command -v update-desktop-database &> /dev/null; then
        log "Updating desktop database..."
        update-desktop-database
    fi
}

# Remove the main application binary and directories.
uninstall_files() {
    log "--- Removing application files ---"
    if [ -f "$DAEMON_BINARY" ]; then
        log "Removing daemon binary: ${DAEMON_BINARY}"
        rm -f "$DAEMON_BINARY"
    else
        log "Daemon binary not found. Skipping removal."
    fi

    if [ -d "$GUI_DIR" ]; then
        log "Removing GUI directory: ${GUI_DIR}"
        rm -rf "$GUI_DIR"
    else
        log "GUI directory not found. Skipping removal."
    fi
}

# --- Main Execution ---
main() {
    log "Starting Netmaker Desktop uninstallation."

    # Check for root privileges
    if [ "$(id -u)" -ne 0 ]; then
      log "ERROR: This script must be run as root. Please use sudo." >&2
      exit 1
    fi

    uninstall_service
    uninstall_shortcuts
    uninstall_files

    log "Uninstallation complete."
}

# Run the main function
main

