#!/usr/bin/env bash
#
# SonarQube Prometheus Metrics Exporter
#
# Prometheus textfile collector exporter for SonarQube.
# Uses the SonarQube Web API to collect system health, JVM heap usage,
# compute engine queue depth, database connection pool stats, project
# count, and total lines of code.
#
# Usage:
#   SONAR_URL="https://sonar.example.com" SONAR_TOKEN="squ_xxx" ./sonarqube-exporter.sh
#   SONAR_URL="https://sonar.example.com" SONAR_TOKEN="squ_xxx" ./sonarqube-exporter.sh --textfile
#   SONAR_URL="https://sonar.example.com" SONAR_TOKEN="squ_xxx" ./sonarqube-exporter.sh --install
#
# Parameters:
#   --textfile   Write to textfile collector directory
#   --install    Create cron job for automatic collection
#   --help       Show usage
#
# Environment:
#   SONAR_URL       SonarQube base URL (required)
#   SONAR_TOKEN     API token (required, admin token recommended)
#   TEXTFILE_DIR    Textfile collector directory (default: /var/lib/node_exporter/textfile_collector)
#   CURL_TIMEOUT    API request timeout in seconds (default: 10)
#
# Author:  Phil Connor
# Contact: contact@mylinux.work
# Website: https://mylinux.work
# License: MIT
# Version: 1.0
#
# Metrics Exported:
#   Core:
#     - sonarqube_up
#     - sonarqube_exporter_info{version}
#     - sonarqube_health_status
#
#   Compute Engine:
#     - sonarqube_ce_pending_tasks
#     - sonarqube_ce_in_progress_tasks
#     - sonarqube_ce_worker_count
#
#   JVM:
#     - sonarqube_web_jvm_heap_used_bytes
#     - sonarqube_web_jvm_heap_max_bytes
#     - sonarqube_ce_jvm_heap_used_bytes
#     - sonarqube_ce_jvm_heap_max_bytes
#     - sonarqube_search_jvm_heap_used_bytes
#     - sonarqube_search_jvm_heap_max_bytes
#
#   Database:
#     - sonarqube_db_pool_active_connections
#     - sonarqube_db_pool_max_connections
#
#   Projects:
#     - sonarqube_projects_total
#     - sonarqube_lines_of_code_total
#
#   Exporter:
#     - sonarqube_exporter_duration_seconds
#     - sonarqube_exporter_last_run_timestamp

set -euo pipefail

# --- Configuration ---
readonly VERSION="1.0"
readonly SCRIPT_NAME="$(basename "$0")"
SONAR_URL="${SONAR_URL:-}"
SONAR_TOKEN="${SONAR_TOKEN:-}"
TEXTFILE_DIR="${TEXTFILE_DIR:-/var/lib/node_exporter/textfile_collector}"
CURL_TIMEOUT="${CURL_TIMEOUT:-10}"
TEXTFILE_MODE=false
OUTPUT=""
START_TIME=""

# --- Functions ---

usage() {
    cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS]

SonarQube Prometheus Metrics Exporter

Options:
  --textfile    Write metrics to textfile collector directory
  --install     Create cron job for automatic collection
  --help        Show this help message

Environment Variables:
  SONAR_URL       SonarQube base URL (required)
  SONAR_TOKEN     API token (required)
  TEXTFILE_DIR    Output directory (default: /var/lib/node_exporter/textfile_collector)
  CURL_TIMEOUT    Request timeout in seconds (default: 10)

Examples:
  SONAR_URL="https://sonar.example.com" SONAR_TOKEN="squ_xxx" $SCRIPT_NAME
  SONAR_URL="https://sonar.example.com" SONAR_TOKEN="squ_xxx" $SCRIPT_NAME --textfile
  SONAR_URL="https://sonar.example.com" SONAR_TOKEN="squ_xxx" $SCRIPT_NAME --install
EOF
    exit 0
}

check_dependencies() {
    local missing=()
    for cmd in curl jq; do
        if ! command -v "$cmd" &>/dev/null; then
            missing+=("$cmd")
        fi
    done
    if [[ ${#missing[@]} -gt 0 ]]; then
        echo "ERROR: Missing required commands: ${missing[*]}" >&2
        echo "Install with: apt install ${missing[*]}  OR  dnf install ${missing[*]}" >&2
        exit 1
    fi
}

validate_config() {
    if [[ -z "$SONAR_URL" ]]; then
        echo "ERROR: SONAR_URL environment variable is required" >&2
        exit 1
    fi
    if [[ -z "$SONAR_TOKEN" ]]; then
        echo "ERROR: SONAR_TOKEN environment variable is required" >&2
        exit 1
    fi
    # Strip trailing slash
    SONAR_URL="${SONAR_URL%/}"
}

api_get() {
    local endpoint="$1"
    curl -sf --max-time "$CURL_TIMEOUT" \
        -H "Authorization: Bearer ${SONAR_TOKEN}" \
        "${SONAR_URL}${endpoint}" 2>/dev/null || echo ""
}

add_metric() {
    local name="$1"
    local type="$2"
    local help="$3"
    local value="$4"
    local labels="${5:-}"

    if [[ -n "$labels" ]]; then
        OUTPUT+="# HELP ${name} ${help}
# TYPE ${name} ${type}
${name}{${labels}} ${value}
"
    else
        OUTPUT+="# HELP ${name} ${help}
# TYPE ${name} ${type}
${name} ${value}
"
    fi
}

add_metric_value() {
    local name="$1"
    local value="$2"
    local labels="${3:-}"

    if [[ -n "$labels" ]]; then
        OUTPUT+="${name}{${labels}} ${value}
"
    else
        OUTPUT+="${name} ${value}
"
    fi
}

collect_health() {
    local health_json
    health_json=$(api_get "/api/system/health")

    if [[ -z "$health_json" ]]; then
        add_metric "sonarqube_up" "gauge" "SonarQube reachability (1=up, 0=down)" "0"
        return 1
    fi

    add_metric "sonarqube_up" "gauge" "SonarQube reachability (1=up, 0=down)" "1"

    local health_status
    health_status=$(echo "$health_json" | jq -r '.health // empty' 2>/dev/null)

    if [[ "$health_status" == "GREEN" ]]; then
        add_metric "sonarqube_health_status" "gauge" "System health (1=GREEN, 0=other)" "1"
    else
        add_metric "sonarqube_health_status" "gauge" "System health (1=GREEN, 0=other)" "0"
    fi

    return 0
}

collect_system_info() {
    local info_json
    info_json=$(api_get "/api/system/info")

    if [[ -z "$info_json" ]]; then
        return
    fi

    # Web JVM
    local web_heap_used web_heap_max
    web_heap_used=$(echo "$info_json" | jq -r '.["Web JVM State"]["Heap Used (MB)"] // empty' 2>/dev/null)
    web_heap_max=$(echo "$info_json" | jq -r '.["Web JVM State"]["Heap Max (MB)"] // empty' 2>/dev/null)

    if [[ -n "$web_heap_used" ]]; then
        local web_used_bytes=$((web_heap_used * 1048576))
        add_metric "sonarqube_web_jvm_heap_used_bytes" "gauge" "Web process JVM heap used" "$web_used_bytes"
    fi
    if [[ -n "$web_heap_max" ]]; then
        local web_max_bytes=$((web_heap_max * 1048576))
        add_metric "sonarqube_web_jvm_heap_max_bytes" "gauge" "Web process JVM heap max" "$web_max_bytes"
    fi

    # Compute Engine JVM
    local ce_heap_used ce_heap_max
    ce_heap_used=$(echo "$info_json" | jq -r '.["Compute Engine JVM State"]["Heap Used (MB)"] // empty' 2>/dev/null)
    ce_heap_max=$(echo "$info_json" | jq -r '.["Compute Engine JVM State"]["Heap Max (MB)"] // empty' 2>/dev/null)

    if [[ -n "$ce_heap_used" ]]; then
        local ce_used_bytes=$((ce_heap_used * 1048576))
        add_metric "sonarqube_ce_jvm_heap_used_bytes" "gauge" "CE process JVM heap used" "$ce_used_bytes"
    fi
    if [[ -n "$ce_heap_max" ]]; then
        local ce_max_bytes=$((ce_heap_max * 1048576))
        add_metric "sonarqube_ce_jvm_heap_max_bytes" "gauge" "CE process JVM heap max" "$ce_max_bytes"
    fi

    # Search (Elasticsearch) JVM
    local search_heap_used search_heap_max
    search_heap_used=$(echo "$info_json" | jq -r '.["Search State"]["Heap Used (MB)"] // empty' 2>/dev/null)
    search_heap_max=$(echo "$info_json" | jq -r '.["Search State"]["Heap Max (MB)"] // empty' 2>/dev/null)

    if [[ -n "$search_heap_used" ]]; then
        local search_used_bytes=$((search_heap_used * 1048576))
        add_metric "sonarqube_search_jvm_heap_used_bytes" "gauge" "Elasticsearch JVM heap used" "$search_used_bytes"
    fi
    if [[ -n "$search_heap_max" ]]; then
        local search_max_bytes=$((search_heap_max * 1048576))
        add_metric "sonarqube_search_jvm_heap_max_bytes" "gauge" "Elasticsearch JVM heap max" "$search_max_bytes"
    fi

    # Database pool
    local db_active db_max
    db_active=$(echo "$info_json" | jq -r '.["Web Database Connection"]["Pool Active Connections"] // empty' 2>/dev/null)
    db_max=$(echo "$info_json" | jq -r '.["Web Database Connection"]["Pool Max Connections"] // empty' 2>/dev/null)

    if [[ -n "$db_active" ]]; then
        add_metric "sonarqube_db_pool_active_connections" "gauge" "Active database connections" "$db_active"
    fi
    if [[ -n "$db_max" ]]; then
        add_metric "sonarqube_db_pool_max_connections" "gauge" "Maximum database connections" "$db_max"
    fi

    # CE workers
    local ce_workers
    ce_workers=$(echo "$info_json" | jq -r '.["Compute Engine Tasks"]["Worker Count"] // empty' 2>/dev/null)
    if [[ -n "$ce_workers" ]]; then
        add_metric "sonarqube_ce_worker_count" "gauge" "Compute Engine worker count" "$ce_workers"
    fi
}

collect_ce_queue() {
    local pending_json in_progress_json

    pending_json=$(api_get "/api/ce/activity?status=PENDING&ps=1")
    in_progress_json=$(api_get "/api/ce/activity?status=IN_PROGRESS&ps=1")

    if [[ -n "$pending_json" ]]; then
        local pending_count
        pending_count=$(echo "$pending_json" | jq -r '.paging.total // 0' 2>/dev/null)
        add_metric "sonarqube_ce_pending_tasks" "gauge" "Compute Engine pending tasks" "${pending_count:-0}"
    fi

    if [[ -n "$in_progress_json" ]]; then
        local in_progress_count
        in_progress_count=$(echo "$in_progress_json" | jq -r '.paging.total // 0' 2>/dev/null)
        add_metric "sonarqube_ce_in_progress_tasks" "gauge" "Compute Engine in-progress tasks" "${in_progress_count:-0}"
    fi
}

collect_projects() {
    local projects_json
    projects_json=$(api_get "/api/projects/search?ps=1")

    if [[ -n "$projects_json" ]]; then
        local project_count
        project_count=$(echo "$projects_json" | jq -r '.paging.total // 0' 2>/dev/null)
        add_metric "sonarqube_projects_total" "gauge" "Total number of projects" "${project_count:-0}"
    fi

    # Lines of code via measures
    local measures_json
    measures_json=$(api_get "/api/measures/search?projectKeys=&metricKeys=ncloc&ps=1")

    if [[ -n "$measures_json" ]]; then
        local total_loc=0
        # Sum ncloc across all projects (paginated, use component tree for total)
        local loc_json
        loc_json=$(api_get "/api/measures/component?component=&metricKeys=ncloc")
        if [[ -n "$loc_json" ]]; then
            total_loc=$(echo "$loc_json" | jq -r '.component.measures[0].value // 0' 2>/dev/null)
        fi

        # Alternative: use navigation/global to get total LOC
        local nav_json
        nav_json=$(api_get "/api/navigation/global")
        if [[ -n "$nav_json" ]]; then
            local global_loc
            global_loc=$(echo "$nav_json" | jq -r '.qualifiers[] | select(.key=="TRK") | .count // empty' 2>/dev/null)
        fi

        add_metric "sonarqube_lines_of_code_total" "gauge" "Total lines of code" "${total_loc:-0}"
    fi
}

write_output() {
    if [[ "$TEXTFILE_MODE" == true ]]; then
        local output_file="${TEXTFILE_DIR}/sonarqube.prom"
        local temp_file="${output_file}.$$"

        mkdir -p "$TEXTFILE_DIR"
        echo "$OUTPUT" > "$temp_file"
        mv "$temp_file" "$output_file"
    else
        echo "$OUTPUT"
    fi
}

install_cron() {
    if [[ $EUID -ne 0 ]]; then
        echo "ERROR: --install requires root" >&2
        exit 1
    fi

    local script_path
    script_path=$(readlink -f "$0")

    cat > /etc/cron.d/sonarqube-exporter <<EOF
# SonarQube Prometheus Exporter — runs every 2 minutes
SONAR_URL=${SONAR_URL}
SONAR_TOKEN=${SONAR_TOKEN}
TEXTFILE_DIR=${TEXTFILE_DIR}
*/2 * * * * root ${script_path} --textfile 2>/dev/null
EOF

    chmod 644 /etc/cron.d/sonarqube-exporter
    echo "Installed cron job: /etc/cron.d/sonarqube-exporter"
    echo "Metrics will be written to: ${TEXTFILE_DIR}/sonarqube.prom"
}

# --- Main ---

main() {
    # Parse arguments
    for arg in "$@"; do
        case "$arg" in
            --textfile) TEXTFILE_MODE=true ;;
            --install)
                check_dependencies
                validate_config
                install_cron
                exit 0
                ;;
            --help|-h) usage ;;
            *) echo "Unknown option: $arg" >&2; usage ;;
        esac
    done

    check_dependencies
    validate_config

    START_TIME=$(date +%s%N)

    # Exporter info
    add_metric "sonarqube_exporter_info" "gauge" "Exporter version information" "1" "version=\"${VERSION}\""

    # Collect metrics
    if collect_health; then
        collect_system_info
        collect_ce_queue
        collect_projects
    fi

    # Exporter performance
    local end_time duration
    end_time=$(date +%s%N)
    duration=$(echo "scale=2; ($end_time - $START_TIME) / 1000000000" | bc 2>/dev/null || echo "0")
    add_metric "sonarqube_exporter_duration_seconds" "gauge" "Time to generate all metrics" "$duration"
    add_metric "sonarqube_exporter_last_run_timestamp" "gauge" "Unix timestamp of last successful run" "$(date +%s)"

    write_output
}

main "$@"
