#!/bin/bash

# ============================================================
# Argus — Predictive Disk Health Monitor
# Command-line launcher
# ============================================================

VERSION="1.0.0"

ARGUS_ROOT="/opt/argus"
ARGUS_SCRIPTS="${ARGUS_ROOT}/scripts"
ARGUS_CONFIG="/etc/argus/argus.conf"

PYTHON="/usr/bin/python3"

COLLECTOR="${ARGUS_SCRIPTS}/argus-collector.py"
ANALYZER="${ARGUS_SCRIPTS}/argus-analyzer.py"
WATCHER="${ARGUS_SCRIPTS}/argus-watcher.py"
EXPORTER="${ARGUS_SCRIPTS}/argus-exporter.py"


# ------------------------------------------------------------
# Usage
# ------------------------------------------------------------

usage() {

    cat <<EOF

Argus — Predictive Disk Health Monitor

Usage:
    argus <command> [options]

Commands:

    --argus-collector
        Collect SMART data from configured disks.

    --argus-analyzer
        Analyze collected disk health data.

    --argus-watcher
        Watch disk health and send alerts.

    --argus-exporter
        Start Prometheus metrics exporter.

    --status
        Show current disk health status.

    --version
        Show Argus version.

    --help
        Show this help message.


Examples:

    sudo argus --argus-collector

    argus --argus-analyzer

    argus --argus-watcher

    sudo argus --argus-exporter

    argus --status

    argus --version


Configuration:
    ${ARGUS_CONFIG}

Application:
    ${ARGUS_ROOT}

Scripts:
    ${ARGUS_SCRIPTS}

EOF
}


# ------------------------------------------------------------
# Version
# ------------------------------------------------------------

version() {

    echo "Argus ${VERSION}"

}


# ------------------------------------------------------------
# Check Python
# ------------------------------------------------------------

check_python() {

    if [[ ! -x "${PYTHON}" ]]; then

        echo "[argus] ERROR: Python 3 was not found:"
        echo "        ${PYTHON}"

        exit 2

    fi

}


# ------------------------------------------------------------
# Check configuration
# ------------------------------------------------------------

check_config() {

    if [[ ! -f "${ARGUS_CONFIG}" ]]; then

        echo "[argus] ERROR: Configuration file not found:"
        echo "        ${ARGUS_CONFIG}"

        echo
        echo "Create or restore the configuration file."

        exit 2

    fi

}


# ------------------------------------------------------------
# Check component
# ------------------------------------------------------------

check_script() {

    local script="$1"

    if [[ ! -f "${script}" ]]; then

        echo "[argus] ERROR: Component not found:"
        echo "        ${script}"

        exit 2

    fi

}


# ------------------------------------------------------------
# Run Python component
# ------------------------------------------------------------

run_component() {

    local script="$1"

    shift

    check_python
    check_config
    check_script "${script}"

    exec "${PYTHON}" "${script}" \
        --config "${ARGUS_CONFIG}" \
        "$@"

}


# ============================================================
# Main
# ============================================================

case "${1:-}" in

    --argus-collector)

        shift

        run_component \
            "${COLLECTOR}" \
            "$@"

        ;;


    --argus-analyzer)

        shift

        run_component \
            "${ANALYZER}" \
            "$@"

        ;;


    --argus-watcher)

        shift

        run_component \
            "${WATCHER}" \
            "$@"

        ;;


    --argus-exporter)

        shift

        run_component \
            "${EXPORTER}" \
            "$@"

        ;;


    --status)

        check_python
        check_config
        check_script "${ANALYZER}"

        exec "${PYTHON}" \
            "${ANALYZER}" \
            --config "${ARGUS_CONFIG}"

        ;;


    --version|-v)

        version

        ;;


    --help|-h|"")

        usage

        ;;


    *)

        echo "[argus] ERROR: Unknown option: ${1}"

        echo
        echo "Run:"
        echo "    argus --help"

        exit 2

        ;;

esac
