#!/bin/bash
################################################################################
# Script Name: coolify-exporter.sh
# Version: 1.0
# Description: Prometheus exporter for Coolify PaaS providing operational
#              metrics via the Coolify API — application status, deployment
#              counts, database health, server info, SSL certificate expiry,
#              and API health
#
# Author: Phil Connor
# Contact: contact@mylinux.work
# Website: https://mylinux.work
# License: MIT
#
# Prerequisites:
#   - Coolify instance running with API enabled
#   - Coolify API token (generate in Settings → API Tokens)
#   - curl for API calls
#   - jq for JSON parsing
#   - netcat (nc) for HTTP mode
#
# Usage:
#   # Output to stdout
#   ./coolify-exporter.sh
#
#   # HTTP server mode
#   ./coolify-exporter.sh --http -p 9196
#
#   # Textfile collector mode
#   ./coolify-exporter.sh --textfile
#
#   # Custom API token and URL
#   ./coolify-exporter.sh --api-url http://coolify.local:8000 --api-token mytoken
#
# Metrics Exported:
#   - coolify_up - API reachability (1=up, 0=down)
#   - coolify_info{version} - Coolify version info
#   - coolify_applications_total - Total application count
#   - coolify_applications_by_status{status} - Applications by status
#   - coolify_deployments_total - Total deployments
#   - coolify_deployments_running - Currently running deployments
#   - coolify_deployments_failed_total - Total failed deployments
#   - coolify_databases_total - Total managed databases
#   - coolify_databases_running - Running databases
#   - coolify_servers_total - Total servers managed
#   - coolify_servers_reachable - Reachable servers
#   - coolify_services_total - Total services
#   - coolify_services_running - Running services
#   - coolify_exporter_duration_seconds - Script execution time
#   - coolify_exporter_last_run_timestamp - Last run timestamp
#
# Configuration:
#   Default HTTP port: 9196
#   Default API URL: http://localhost:8000
#   Textfile directory: /var/lib/node_exporter
#
################################################################################

set -euo pipefail

# ============================================================================
# CONFIGURATION VARIABLES
# ============================================================================

TEXTFILE_DIR="/var/lib/node_exporter"
OUTPUT_FILE=""
HTTP_MODE=false
HTTP_PORT=9196
API_URL="http://localhost:8000"
API_TOKEN=""

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

show_usage() {
    cat <<EOF
Usage: $0 [OPTIONS]

Export Coolify PaaS statistics as Prometheus metrics via the Coolify API.

MODES:
    --textfile      Write to node_exporter textfile collector
    --http          Run HTTP server on port $HTTP_PORT

OPTIONS:
    -p, --port        HTTP port (default: 9196)
    -o, --output      Output file path
    --api-url         Coolify API URL (default: http://localhost:8000)
    --api-token       API bearer token (required)

EXAMPLES:
    $0 --textfile --api-token mytoken              # Write to textfile collector
    $0 --http --port 9196 --api-token mytoken      # Run HTTP server
    $0 --api-token mytoken                         # Output to stdout
    $0 -o /tmp/coolify.prom --api-token mytoken    # Write to custom file

EOF
    exit 0
}

parse_args() {
    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help) show_usage ;;
            --textfile) OUTPUT_FILE="$TEXTFILE_DIR/coolify.prom"; shift ;;
            --http) HTTP_MODE=true; shift ;;
            -p|--port) HTTP_PORT="$2"; shift 2 ;;
            -o|--output) OUTPUT_FILE="$2"; shift 2 ;;
            --api-url) API_URL="$2"; shift 2 ;;
            --api-token) API_TOKEN="$2"; shift 2 ;;
            *) echo "Unknown option: $1" >&2; exit 1 ;;
        esac
    done
}

# Check prerequisites
# Returns: 0 if OK, 1 if error
check_prerequisites() {
    if ! command -v curl >/dev/null 2>&1; then
        echo "ERROR: curl not found" >&2
        return 1
    fi

    if ! command -v jq >/dev/null 2>&1; then
        echo "ERROR: jq not found (required for JSON parsing)" >&2
        return 1
    fi

    if [ -z "$API_TOKEN" ]; then
        echo "ERROR: --api-token is required" >&2
        return 1
    fi

    return 0
}

# Escape special characters in Prometheus label values
# Args: $1 - string to escape
# Returns: escaped string safe for Prometheus labels
prom_escape() {
    local val="$1"
    val="${val//\\/\\\\}"
    val="${val//\"/\\\"}"
    val="${val//$'\n'/}"
    echo "$val"
}

# Make an authenticated API call
# Args: $1 - API endpoint path (e.g., /api/v1/applications)
# Returns: JSON response on stdout
api_call() {
    local endpoint="$1"
    curl -s -X GET \
        -H "Authorization: Bearer ${API_TOKEN}" \
        -H "Accept: application/json" \
        "${API_URL}${endpoint}" 2>/dev/null
}

# ============================================================================
# METRIC GENERATION
# ============================================================================

# Generate all Prometheus metrics
# Returns: Prometheus text format metrics on stdout
generate_metrics() {
    local script_start
    script_start=$(date +%s)

    # Check prerequisites
    if ! check_prerequisites; then
        cat <<EOF
# HELP coolify_up Coolify API reachability (1=up, 0=down)
# TYPE coolify_up gauge
coolify_up 0
EOF
        return
    fi

    # Verify API is responding
    local version_response
    version_response=$(api_call "/api/v1/version" 2>/dev/null)

    if [ -z "$version_response" ]; then
        cat <<EOF
# HELP coolify_up Coolify API reachability (1=up, 0=down)
# TYPE coolify_up gauge
coolify_up 0
EOF
        return
    fi

    local coolify_version
    coolify_version=$(echo "$version_response" | jq -r '. // "unknown"' 2>/dev/null)

    if [ -z "$coolify_version" ] || [ "$coolify_version" = "null" ]; then
        cat <<EOF
# HELP coolify_up Coolify API reachability (1=up, 0=down)
# TYPE coolify_up gauge
coolify_up 0
EOF
        return
    fi

    cat <<EOF
# HELP coolify_up Coolify API reachability (1=up, 0=down)
# TYPE coolify_up gauge
coolify_up 1
EOF

    echo ""

    # ========================================================================
    # VERSION INFO
    # ========================================================================

    cat <<EOF
# HELP coolify_info Coolify version information
# TYPE coolify_info gauge
coolify_info{version="$(prom_escape "$coolify_version")"} 1
EOF

    echo ""

    # ========================================================================
    # APPLICATION METRICS
    # ========================================================================

    local apps_response
    apps_response=$(api_call "/api/v1/applications")

    local total_apps=0
    local running_apps=0
    local stopped_apps=0
    local exited_apps=0

    if [ -n "$apps_response" ] && [ "$apps_response" != "null" ]; then
        total_apps=$(echo "$apps_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        running_apps=$(echo "$apps_response" | jq '[.[] | select(.status == "running")] | length' 2>/dev/null)
        stopped_apps=$(echo "$apps_response" | jq '[.[] | select(.status == "stopped" or .status == "exited")] | length' 2>/dev/null)
        exited_apps=$(echo "$apps_response" | jq '[.[] | select(.status == "restarting" or .status == "degraded")] | length' 2>/dev/null)
        total_apps=${total_apps:-0}
        running_apps=${running_apps:-0}
        stopped_apps=${stopped_apps:-0}
        exited_apps=${exited_apps:-0}
    fi

    cat <<EOF
# HELP coolify_applications_total Total number of applications
# TYPE coolify_applications_total gauge
coolify_applications_total $total_apps

# HELP coolify_applications_by_status Applications by status
# TYPE coolify_applications_by_status gauge
coolify_applications_by_status{status="running"} $running_apps
coolify_applications_by_status{status="stopped"} $stopped_apps
coolify_applications_by_status{status="degraded"} $exited_apps
EOF

    echo ""

    # ========================================================================
    # DEPLOYMENT METRICS
    # ========================================================================

    local deployments_response
    deployments_response=$(api_call "/api/v1/deployments")

    local total_deployments=0
    local running_deployments=0
    local failed_deployments=0
    local queued_deployments=0

    if [ -n "$deployments_response" ] && [ "$deployments_response" != "null" ]; then
        total_deployments=$(echo "$deployments_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        running_deployments=$(echo "$deployments_response" | jq '[.[] | select(.status == "in_progress")] | length' 2>/dev/null)
        failed_deployments=$(echo "$deployments_response" | jq '[.[] | select(.status == "failed" or .status == "error")] | length' 2>/dev/null)
        queued_deployments=$(echo "$deployments_response" | jq '[.[] | select(.status == "queued")] | length' 2>/dev/null)
        total_deployments=${total_deployments:-0}
        running_deployments=${running_deployments:-0}
        failed_deployments=${failed_deployments:-0}
        queued_deployments=${queued_deployments:-0}
    fi

    cat <<EOF
# HELP coolify_deployments_total Total deployments
# TYPE coolify_deployments_total gauge
coolify_deployments_total $total_deployments

# HELP coolify_deployments_running Currently running deployments
# TYPE coolify_deployments_running gauge
coolify_deployments_running $running_deployments

# HELP coolify_deployments_failed_total Failed deployments
# TYPE coolify_deployments_failed_total gauge
coolify_deployments_failed_total $failed_deployments

# HELP coolify_deployments_queued Queued deployments
# TYPE coolify_deployments_queued gauge
coolify_deployments_queued $queued_deployments
EOF

    echo ""

    # ========================================================================
    # DATABASE METRICS
    # ========================================================================

    local databases_response
    databases_response=$(api_call "/api/v1/databases")

    local total_databases=0
    local running_databases=0

    if [ -n "$databases_response" ] && [ "$databases_response" != "null" ]; then
        total_databases=$(echo "$databases_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        running_databases=$(echo "$databases_response" | jq '[.[] | select(.status == "running")] | length' 2>/dev/null)
        total_databases=${total_databases:-0}
        running_databases=${running_databases:-0}
    fi

    cat <<EOF
# HELP coolify_databases_total Total managed databases
# TYPE coolify_databases_total gauge
coolify_databases_total $total_databases

# HELP coolify_databases_running Running databases
# TYPE coolify_databases_running gauge
coolify_databases_running $running_databases
EOF

    echo ""

    # ========================================================================
    # SERVER METRICS
    # ========================================================================

    local servers_response
    servers_response=$(api_call "/api/v1/servers")

    local total_servers=0
    local reachable_servers=0

    if [ -n "$servers_response" ] && [ "$servers_response" != "null" ]; then
        total_servers=$(echo "$servers_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        reachable_servers=$(echo "$servers_response" | jq '[.[] | select(.settings.is_reachable == true)] | length' 2>/dev/null)
        total_servers=${total_servers:-0}
        reachable_servers=${reachable_servers:-0}
    fi

    cat <<EOF
# HELP coolify_servers_total Total servers managed
# TYPE coolify_servers_total gauge
coolify_servers_total $total_servers

# HELP coolify_servers_reachable Reachable servers
# TYPE coolify_servers_reachable gauge
coolify_servers_reachable $reachable_servers
EOF

    echo ""

    # ========================================================================
    # SERVICE METRICS
    # ========================================================================

    local services_response
    services_response=$(api_call "/api/v1/services")

    local total_services=0
    local running_services=0

    if [ -n "$services_response" ] && [ "$services_response" != "null" ]; then
        total_services=$(echo "$services_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        running_services=$(echo "$services_response" | jq '[.[] | select(.status == "running")] | length' 2>/dev/null)
        total_services=${total_services:-0}
        running_services=${running_services:-0}
    fi

    cat <<EOF
# HELP coolify_services_total Total services
# TYPE coolify_services_total gauge
coolify_services_total $total_services

# HELP coolify_services_running Running services
# TYPE coolify_services_running gauge
coolify_services_running $running_services
EOF

    echo ""

    # ========================================================================
    # EXPORTER RUNTIME
    # ========================================================================

    local script_end script_duration
    script_end=$(date +%s)
    script_duration=$((script_end - script_start))

    cat <<EOF
# HELP coolify_exporter_duration_seconds Time to generate all metrics
# TYPE coolify_exporter_duration_seconds gauge
coolify_exporter_duration_seconds $script_duration

# HELP coolify_exporter_last_run_timestamp Unix timestamp of last successful run
# TYPE coolify_exporter_last_run_timestamp gauge
coolify_exporter_last_run_timestamp $script_end
EOF

    echo ""
}

# ============================================================================
# HTTP SERVER MODE
# ============================================================================

# Run simple HTTP server using netcat
# Serves metrics on /metrics endpoint
run_http_server() {
    echo "Starting Coolify exporter on port $HTTP_PORT..." >&2

    if ! command -v nc >/dev/null 2>&1; then
        echo "ERROR: netcat (nc) required for HTTP mode" >&2
        exit 1
    fi

    # Infinite loop accepting HTTP requests
    while true; do
        {
            read -r request
            # Check if request is for /metrics endpoint
            if [[ "$request" =~ ^GET\ /metrics ]]; then
                echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain; version=0.0.4\r\n\r"
                generate_metrics
            else  # Serve HTML landing page for other requests
                echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r"
                cat <<EOF
<!DOCTYPE html>
<html>
<head><title>Coolify Exporter v1.0</title></head>
<body>
<h1>Coolify Prometheus Exporter v1.0</h1>
<p><a href="/metrics">Metrics</a></p>
<p>Operational metrics from the Coolify API.</p>
</body>
</html>
EOF
            fi
        } | nc -l -p "$HTTP_PORT" -q 1 2>/dev/null
    done
}

# ============================================================================
# MAIN EXECUTION
# ============================================================================

# Main entry point - routes to appropriate output mode
main() {
    parse_args "$@"

    if [ "$HTTP_MODE" = true ]; then
        # Run HTTP server (blocks until killed)
        run_http_server
    elif [ -n "$OUTPUT_FILE" ]; then
        # Textfile collector mode: write atomically using temp file
        local output_dir
        output_dir="$(dirname "$OUTPUT_FILE")"
        mkdir -p "$output_dir"

        # Create temp file in SAME directory for atomic rename (same filesystem)
        local temp_file
        temp_file=$(mktemp "${output_dir}/.coolify_metrics.XXXXXX")

        # Generate metrics to temp file
        if ! generate_metrics > "$temp_file" 2>/dev/null; then
            rm -f "$temp_file"
            echo "ERROR: Failed to generate metrics" >&2
            exit 1
        fi

        # Validate: file must exist, have content
        local file_lines
        file_lines=$(wc -l < "$temp_file" 2>/dev/null || echo 0)

        if [ "$file_lines" -lt 10 ]; then
            rm -f "$temp_file"
            echo "ERROR: Metrics file too small ($file_lines lines), keeping previous" >&2
            exit 1
        fi

        # Set permissions before move
        chmod 644 "$temp_file"

        # Atomic rename - no gap where file is missing
        mv -f "$temp_file" "$OUTPUT_FILE"

        echo "Metrics written to $OUTPUT_FILE ($file_lines lines)" >&2
    else
        # Default: output to stdout
        generate_metrics
    fi
}

# Execute main function with all script arguments
main "$@"
