#!/bin/bash
################################################################################
# Script Name: duplicati-exporter.sh
# Version: 1.0
# Description: Prometheus exporter for Duplicati backups — backup job status,
#              last run time, backup age, file counts, and size metrics
#
# Author: Phil Connor
# Contact: contact@mylinux.work
# Website: https://mylinux.work
# License: MIT
#
# Prerequisites:
#   - curl installed
#   - jq installed
#   - netcat (nc) for HTTP mode
#
# Usage:
#   ./duplicati-exporter.sh --textfile
#   ./duplicati-exporter.sh --http -p 9203
#   ./duplicati-exporter.sh --url http://myhost:8200 --password secret
#   DUPLICATI_PASSWORD=secret ./duplicati-exporter.sh --textfile
#
# Configuration:
#   Default HTTP port: 9203
#   Default Duplicati URL: http://localhost:8200
#   Textfile directory: /var/lib/node_exporter
#
################################################################################

EXPORTER_VERSION="1.0"
TEXTFILE_DIR="/var/lib/node_exporter"
OUTPUT_FILE=""
HTTP_MODE=false
HTTP_PORT=9203
DUPLICATI_URL="http://localhost:8200"
DUPLICATI_PASS="${DUPLICATI_PASSWORD:-}"

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

Export Duplicati backup statistics as Prometheus metrics.

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

OPTIONS:
    -p, --port      HTTP port (default: 9203)
    -o, --output    Output file path
    --url           Duplicati server URL (default: http://localhost:8200)
    --password      Duplicati server password (or DUPLICATI_PASSWORD env var)
    -h, --help      Show this help message

EXAMPLES:
    $0 --textfile
    $0 --http -p 9203
    $0 --url http://myhost:8200 --password secret --textfile
    DUPLICATI_PASSWORD=secret $0 --textfile

EOF
    exit 0
}

parse_args() {
    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help) show_usage ;;
            --textfile) OUTPUT_FILE="$TEXTFILE_DIR/duplicati_backup.prom"; shift ;;
            --http) HTTP_MODE=true; shift ;;
            -p|--port) HTTP_PORT="$2"; shift 2 ;;
            -o|--output) OUTPUT_FILE="$2"; shift 2 ;;
            --url) DUPLICATI_URL="$2"; shift 2 ;;
            --password) DUPLICATI_PASS="$2"; shift 2 ;;
            *) echo "Unknown option: $1" >&2; exit 1 ;;
        esac
    done
    # Strip trailing slash from URL
    DUPLICATI_URL="${DUPLICATI_URL%/}"
}

check_dependencies() {
    local missing=0
    for cmd in curl jq; do
        if ! command -v "$cmd" >/dev/null 2>&1; then
            echo "ERROR: $cmd not found" >&2
            missing=1
        fi
    done
    return "$missing"
}

# Authenticate with Duplicati and store cookie jar
duplicati_auth() {
    COOKIE_JAR=$(mktemp /tmp/.duplicati_cookies.XXXXXX)
    trap 'rm -f "$COOKIE_JAR"' EXIT

    # If no password, try unauthenticated access
    if [ -z "$DUPLICATI_PASS" ]; then
        return 0
    fi

    # Get XSRF token first
    local xsrf_token
    xsrf_token=$(curl -s -c "$COOKIE_JAR" -b "$COOKIE_JAR" \
        "${DUPLICATI_URL}/api/v1/auth/refresh" 2>/dev/null | jq -r '.Token // empty')

    if [ -z "$xsrf_token" ]; then
        # Try fetching the login page to get cookies
        curl -s -c "$COOKIE_JAR" -b "$COOKIE_JAR" \
            "${DUPLICATI_URL}/" >/dev/null 2>&1
        xsrf_token=$(grep -i "xsrf" "$COOKIE_JAR" 2>/dev/null | awk '{print $NF}')
    fi

    # Authenticate with password
    local auth_response
    auth_response=$(curl -s -c "$COOKIE_JAR" -b "$COOKIE_JAR" \
        -X POST "${DUPLICATI_URL}/api/v1/auth/login" \
        -H "Content-Type: application/json" \
        ${xsrf_token:+-H "X-XSRF-Token: $xsrf_token"} \
        -d "{\"Password\":\"${DUPLICATI_PASS}\"}" 2>/dev/null)

    if echo "$auth_response" | jq -e '.Token' >/dev/null 2>&1; then
        AUTH_TOKEN=$(echo "$auth_response" | jq -r '.Token')
        return 0
    fi

    return 1
}

# Make an authenticated API call
api_call() {
    local endpoint="$1"
    curl -s -b "$COOKIE_JAR" \
        ${AUTH_TOKEN:+-H "Authorization: Bearer $AUTH_TOKEN"} \
        "${DUPLICATI_URL}/api/v1/${endpoint}" 2>/dev/null
}

# Map status string to numeric value
status_to_number() {
    case "$1" in
        Success|Completed) echo 1 ;;
        Warning)           echo 2 ;;
        Error|Failed)      echo 3 ;;
        Fatal)             echo 4 ;;
        *)                 echo 0 ;;
    esac
}

generate_metrics() {
    local script_start
    script_start=$(date +%s%N)

    if ! check_dependencies; then
        echo "# HELP duplicati_up Exporter status (1=up, 0=down)"
        echo "# TYPE duplicati_up gauge"
        echo "duplicati_up 0"
        return
    fi

    # Test server reachability
    local server_up=0
    if curl -s --connect-timeout 5 "${DUPLICATI_URL}/api/v1/systeminfo" >/dev/null 2>&1; then
        server_up=1
    fi

    # Authenticate
    AUTH_TOKEN=""
    if [ "$server_up" -eq 1 ]; then
        duplicati_auth
    fi

    echo "# HELP duplicati_up Duplicati server reachable (1=up, 0=down)"
    echo "# TYPE duplicati_up gauge"
    echo "duplicati_up $server_up"

    echo "# HELP duplicati_exporter_info Exporter version information"
    echo "# TYPE duplicati_exporter_info gauge"
    echo "duplicati_exporter_info{version=\"${EXPORTER_VERSION}\"} 1"

    if [ "$server_up" -eq 0 ]; then
        echo "# HELP duplicati_backup_count Total number of configured backup jobs"
        echo "# TYPE duplicati_backup_count gauge"
        echo "duplicati_backup_count 0"
        local script_end
        script_end=$(date +%s)
        echo "# HELP duplicati_exporter_duration_seconds Script execution time"
        echo "# TYPE duplicati_exporter_duration_seconds gauge"
        echo "duplicati_exporter_duration_seconds 0"
        echo "# HELP duplicati_exporter_last_run_timestamp Last successful run"
        echo "# TYPE duplicati_exporter_last_run_timestamp gauge"
        echo "duplicati_exporter_last_run_timestamp $script_end"
        return
    fi

    # Fetch all backups
    local backups_json
    backups_json=$(api_call "backups")

    if [ -z "$backups_json" ] || ! echo "$backups_json" | jq -e '.' >/dev/null 2>&1; then
        echo "# HELP duplicati_backup_count Total number of configured backup jobs"
        echo "# TYPE duplicati_backup_count gauge"
        echo "duplicati_backup_count 0"
        local script_end
        script_end=$(date +%s)
        echo "# HELP duplicati_exporter_duration_seconds Script execution time"
        echo "# TYPE duplicati_exporter_duration_seconds gauge"
        echo "duplicati_exporter_duration_seconds 0"
        echo "# HELP duplicati_exporter_last_run_timestamp Last successful run"
        echo "# TYPE duplicati_exporter_last_run_timestamp gauge"
        echo "duplicati_exporter_last_run_timestamp $script_end"
        return
    fi

    local backup_count
    backup_count=$(echo "$backups_json" | jq 'length')
    echo "# HELP duplicati_backup_count Total number of configured backup jobs"
    echo "# TYPE duplicati_backup_count gauge"
    echo "duplicati_backup_count ${backup_count:-0}"

    local now
    now=$(date +%s)

    # Collect per-backup metrics into arrays so HELP/TYPE appears once per metric
    local info_lines=()
    local last_run_lines=()
    local age_lines=()
    local duration_lines=()
    local status_lines=()
    local files_lines=()
    local size_lines=()
    local uploaded_lines=()
    local next_ts_lines=()
    local next_sec_lines=()
    local error_lines=()
    local warning_lines=()

    while IFS= read -r backup; do
        local id name target_url
        id=$(echo "$backup" | jq -r '.Backup.ID // empty')
        name=$(echo "$backup" | jq -r '.Backup.Name // empty')
        target_url=$(echo "$backup" | jq -r '.Backup.TargetURL // empty')

        [ -z "$name" ] && continue

        local safe_name="${name//\"/\\\"}"
        local safe_target="${target_url//\"/\\\"}"

        info_lines+=("duplicati_backup_info{id=\"${id}\",name=\"${safe_name}\",target_url=\"${safe_target}\"} 1")

        # Last run timestamp
        local last_run_ts=0
        local last_run_raw
        last_run_raw=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupDate" // empty')
        if [ -n "$last_run_raw" ]; then
            last_run_ts=$(date -d "$last_run_raw" +%s 2>/dev/null || echo 0)
        fi
        last_run_lines+=("duplicati_backup_last_run_timestamp{name=\"${safe_name}\"} $last_run_ts")

        # Age since last run
        local age=0
        if [ "$last_run_ts" -gt 0 ]; then
            age=$((now - last_run_ts))
        fi
        age_lines+=("duplicati_backup_last_run_age_seconds{name=\"${safe_name}\"} $age")

        # Last duration
        local duration
        duration=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupDuration" // "0"')
        local duration_seconds=0
        if [[ "$duration" =~ ^([0-9]+):([0-9]+):([0-9]+) ]]; then
            duration_seconds=$(( BASH_REMATCH[1] * 3600 + BASH_REMATCH[2] * 60 + BASH_REMATCH[3] ))
        elif [[ "$duration" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
            duration_seconds="${duration%%.*}"
        fi
        duration_lines+=("duplicati_backup_last_duration_seconds{name=\"${safe_name}\"} $duration_seconds")

        # Last status
        local status_raw status_num
        status_raw=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupResult" // "Unknown"')
        status_num=$(status_to_number "$status_raw")
        status_lines+=("duplicati_backup_last_status{name=\"${safe_name}\",status=\"${status_raw}\"} $status_num")

        # Files examined
        local files_total
        files_total=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupExaminedFiles" // "0"')
        files_lines+=("duplicati_backup_files_total{name=\"${safe_name}\"} ${files_total:-0}")

        # Files size
        local files_size
        files_size=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupSizeOfExaminedFiles" // "0"')
        size_lines+=("duplicati_backup_files_size_bytes{name=\"${safe_name}\"} ${files_size:-0}")

        # Uploaded bytes
        local uploaded
        uploaded=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupUploadedSize" // "0"')
        uploaded_lines+=("duplicati_backup_uploaded_bytes{name=\"${safe_name}\"} ${uploaded:-0}")

        # Next run timestamp
        local next_run_ts=0
        local next_run_raw
        next_run_raw=$(echo "$backup" | jq -r '.Schedule.Time // empty')
        if [ -n "$next_run_raw" ]; then
            next_run_ts=$(date -d "$next_run_raw" +%s 2>/dev/null || echo 0)
        fi
        next_ts_lines+=("duplicati_backup_next_run_timestamp{name=\"${safe_name}\"} $next_run_ts")

        # Seconds until next run
        local next_run_seconds=0
        if [ "$next_run_ts" -gt "$now" ]; then
            next_run_seconds=$((next_run_ts - now))
        fi
        next_sec_lines+=("duplicati_backup_next_run_seconds{name=\"${safe_name}\"} $next_run_seconds")

        # Error count
        local error_count
        error_count=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupErrors" // "0"')
        error_lines+=("duplicati_backup_error_count{name=\"${safe_name}\"} ${error_count:-0}")

        # Warning count
        local warning_count
        warning_count=$(echo "$backup" | jq -r '.Backup.Metadata."LastBackupWarnings" // "0"')
        warning_lines+=("duplicati_backup_warning_count{name=\"${safe_name}\"} ${warning_count:-0}")
    done < <(echo "$backups_json" | jq -c '.[]' 2>/dev/null)

    # Output each metric group with HELP/TYPE immediately before values
    if [ ${#info_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_info Backup job information"
        echo "# TYPE duplicati_backup_info gauge"
        printf '%s\n' "${info_lines[@]}"
    fi
    if [ ${#last_run_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_last_run_timestamp Unix timestamp of last backup run"
        echo "# TYPE duplicati_backup_last_run_timestamp gauge"
        printf '%s\n' "${last_run_lines[@]}"
    fi
    if [ ${#age_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_last_run_age_seconds Seconds since last backup run"
        echo "# TYPE duplicati_backup_last_run_age_seconds gauge"
        printf '%s\n' "${age_lines[@]}"
    fi
    if [ ${#duration_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_last_duration_seconds Duration of last backup run in seconds"
        echo "# TYPE duplicati_backup_last_duration_seconds gauge"
        printf '%s\n' "${duration_lines[@]}"
    fi
    if [ ${#status_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_last_status Last backup status (Success=1, Warning=2, Error=3, Fatal=4, Unknown=0)"
        echo "# TYPE duplicati_backup_last_status gauge"
        printf '%s\n' "${status_lines[@]}"
    fi
    if [ ${#files_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_files_total Total files examined in last backup"
        echo "# TYPE duplicati_backup_files_total gauge"
        printf '%s\n' "${files_lines[@]}"
    fi
    if [ ${#size_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_files_size_bytes Total size of examined files in bytes"
        echo "# TYPE duplicati_backup_files_size_bytes gauge"
        printf '%s\n' "${size_lines[@]}"
    fi
    if [ ${#uploaded_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_uploaded_bytes Bytes uploaded in last backup"
        echo "# TYPE duplicati_backup_uploaded_bytes gauge"
        printf '%s\n' "${uploaded_lines[@]}"
    fi
    if [ ${#next_ts_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_next_run_timestamp Next scheduled run unix timestamp"
        echo "# TYPE duplicati_backup_next_run_timestamp gauge"
        printf '%s\n' "${next_ts_lines[@]}"
    fi
    if [ ${#next_sec_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_next_run_seconds Seconds until next scheduled run"
        echo "# TYPE duplicati_backup_next_run_seconds gauge"
        printf '%s\n' "${next_sec_lines[@]}"
    fi
    if [ ${#error_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_error_count Number of errors in last backup run"
        echo "# TYPE duplicati_backup_error_count gauge"
        printf '%s\n' "${error_lines[@]}"
    fi
    if [ ${#warning_lines[@]} -gt 0 ]; then
        echo "# HELP duplicati_backup_warning_count Number of warnings in last backup run"
        echo "# TYPE duplicati_backup_warning_count gauge"
        printf '%s\n' "${warning_lines[@]}"
    fi

    local script_end script_duration_ns script_duration
    script_end=$(date +%s)
    script_duration_ns=$(( $(date +%s%N) - script_start ))
    script_duration=$(( script_duration_ns / 1000000000 ))

    echo "# HELP duplicati_exporter_duration_seconds Script execution time"
    echo "# TYPE duplicati_exporter_duration_seconds gauge"
    echo "duplicati_exporter_duration_seconds $script_duration"
    echo "# HELP duplicati_exporter_last_run_timestamp Last successful run"
    echo "# TYPE duplicati_exporter_last_run_timestamp gauge"
    echo "duplicati_exporter_last_run_timestamp $script_end"
}

run_http_server() {
    echo "Starting Duplicati 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
    while true; do
        {
            read -r request
            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
                echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r"
                echo "<html><head><title>Duplicati Exporter</title></head><body><h1>Duplicati Prometheus Exporter</h1><p><a href=\"/metrics\">Metrics</a></p></body></html>"
            fi
        } | nc -l -p "$HTTP_PORT" -q 1 2>/dev/null
    done
}

main() {
    parse_args "$@"
    if [ "$HTTP_MODE" = true ]; then
        run_http_server
    elif [ -n "$OUTPUT_FILE" ]; then
        local output_dir
        output_dir="$(dirname "$OUTPUT_FILE")"
        mkdir -p "$output_dir"
        local temp_file
        temp_file=$(mktemp "${output_dir}/.duplicati_metrics.XXXXXX")
        if ! generate_metrics > "$temp_file" 2>/dev/null; then
            rm -f "$temp_file"
            echo "ERROR: Failed to generate metrics" >&2
            exit 1
        fi
        chmod 644 "$temp_file"
        mv -f "$temp_file" "$OUTPUT_FILE"
        echo "Metrics written to $OUTPUT_FILE" >&2
    else
        generate_metrics
    fi
}

main "$@"
