#!/bin/bash

set -euo pipefail

PROJECT_ID="autonas"
ORG_ID="xdev"
RUNTIME_DIR="/usr/local/lib/${ORG_ID}/${PROJECT_ID}"
LEGACY_RUNTIME_DIR="/usr/local/lib/autonas"
COMMAND_PATH="/usr/local/sbin/autonas"
UNINSTALL_WRAPPER="/usr/local/sbin/${ORG_ID}-${PROJECT_ID}-uninstall"
DOC_DIR="/usr/local/share/doc/${ORG_ID}/${PROJECT_ID}"
DEFAULTS_PATH="/etc/default/${ORG_ID}-${PROJECT_ID}"
CONFIG_DIR="/etc/pve/autonas"
ATTACH_UNIT="autonas-attach@.service"
MAIN_UNIT="autonas.service"
BOOT_SCAN_UNIT="autonas-boot-scan.service"
DISK_RULES="99-autonas-disk.rules"
INTERFACE_RULES="98-autonas-interfaces.rules"
NFS_DROPIN_DIR="/etc/systemd/system/nfs-server.service.d"
NFS_DROPIN_FILE="${NFS_DROPIN_DIR}/50-autonas-self-hosted-proxmox.conf"

FORCE_MODE=0

log_message() {
    if [[ "${FORCE_MODE}" -eq 0 ]]; then
        echo "$@"
    fi
}

require_root() {
    if [[ "${EUID}" -ne 0 ]]; then
        echo "ERROR: this script must be run as root" >&2
        exit 1
    fi
}

main() {
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --force)
                FORCE_MODE=1
                shift
                ;;
            -h|--help)
                echo "Usage: $0 [--force]"
                exit 0
                ;;
            *)
                echo "ERROR: unknown option: $1" >&2
                exit 1
                ;;
        esac
    done

    require_root

    log_message "=== Uninstalling AutoNAS ==="

    systemctl disable --now autonas-boot-scan.service >/dev/null 2>&1 || true
    systemctl disable --now autonas.service >/dev/null 2>&1 || true
    systemctl stop "autonas-attach@*.service" >/dev/null 2>&1 || true
    systemctl disable "autonas-attach@*.service" >/dev/null 2>&1 || true

    rm -f "/etc/systemd/system/${ATTACH_UNIT}"
    rm -f "/etc/systemd/system/${MAIN_UNIT}"
    rm -f "/etc/systemd/system/${BOOT_SCAN_UNIT}"
    rm -f "${NFS_DROPIN_FILE}"
    rmdir "${NFS_DROPIN_DIR}" 2>/dev/null || true
    systemctl daemon-reload

    rm -f "/etc/udev/rules.d/${DISK_RULES}"
    rm -f "/etc/udev/rules.d/${INTERFACE_RULES}"
    udevadm control --reload-rules >/dev/null 2>&1 || true

    rm -f "${UNINSTALL_WRAPPER}"
    rm -f "${COMMAND_PATH}"
    rm -f "${DEFAULTS_PATH}"
    rm -rf "${DOC_DIR}"
    rm -rf "${RUNTIME_DIR}"

    rm -f /usr/local/bin/autonas
    rm -f /usr/local/bin/autonas.sh
    rm -f /usr/local/bin/autonas-core.sh
    rm -f /usr/local/bin/autonas-disk-handler.sh
    rm -f /usr/local/bin/autonas-udev-wrapper.sh
    rm -f /usr/local/bin/autonas-boot-scan.sh
    rm -f /usr/local/bin/autonas-network-handler.sh
    rm -f /usr/local/bin/autonas-media-importer.sh
    rm -f /usr/local/bin/autonas-uninstall.sh
    rm -f /usr/local/bin/autonas-interface-debug.sh
    rm -f /usr/local/bin/autonas-test-interface-stability.sh
    rm -rf "${LEGACY_RUNTIME_DIR}"

    rmdir "/usr/local/lib/${ORG_ID}" 2>/dev/null || true
    rmdir "/usr/local/share/doc/${ORG_ID}" 2>/dev/null || true

    log_message "Preserved cluster-shared config:"
    log_message "  - ${CONFIG_DIR}/"
    if [[ -f "${CONFIG_DIR}/disks.conf" ]]; then
        log_message "  - ${CONFIG_DIR}/disks.conf"
    fi

    if mount | grep -q "/mnt/autonas/"; then
        log_message "Preserved mounted data paths under /mnt/autonas/"
    elif [[ -d "/mnt/autonas" ]] && [[ -z "$(ls -A /mnt/autonas 2>/dev/null)" ]]; then
        rmdir /mnt/autonas 2>/dev/null || true
    fi

    if [[ -f /etc/exports ]] && grep -q "/mnt/autonas/" /etc/exports 2>/dev/null; then
        log_message "NFS exports referencing /mnt/autonas/ were preserved for manual review."
    fi

    log_message "Uninstall complete."
}

main "$@"
