#!/usr/bin/env bash
#
# Gitea/Forgejo Prometheus Metrics Exporter
#
# Prometheus textfile collector exporter for Gitea and Forgejo.
# Uses the Gitea REST API to collect repository count, user count,
# organization count, issue and pull request stats, runner status,
# mirror sync health, and system resource usage.
#
# Usage:
#   GITEA_URL="https://git.example.com" GITEA_TOKEN="xxx" ./gitea-exporter.sh
#   GITEA_URL="https://git.example.com" GITEA_TOKEN="xxx" ./gitea-exporter.sh --textfile
#   GITEA_URL="https://git.example.com" GITEA_TOKEN="xxx" ./gitea-exporter.sh --install
#
# Parameters:
#   --textfile   Write to textfile collector directory
#   --install    Create cron job for automatic collection
#   --help       Show usage
#
# Environment:
#   GITEA_URL       Gitea/Forgejo base URL (required)
#   GITEA_TOKEN     API token with admin scope (required)
#   TEXTFILE_DIR    Textfile collector directory (default: /var/lib/node_exporter/textfile_collector)
#   CURL_TIMEOUT    API request timeout in seconds (default: 10)
#   MAX_REPOS       Maximum repositories to collect per-repo metrics for (default: 50)
#
# Author:  Phil Connor
# Contact: contact@mylinux.work
# Website: https://mylinux.work
# License: MIT
# Version: 1.0
#
# Metrics Exported:
#   Core:
#     - gitea_up
#     - gitea_exporter_info{version}
#     - gitea_version_info{version}
#
#   Counts:
#     - gitea_users_total
#     - gitea_organizations_total
#     - gitea_repositories_total
#
#   Per-Repository:
#     - gitea_repo_stars{repo}
#     - gitea_repo_forks{repo}
#     - gitea_repo_open_issues{repo}
#     - gitea_repo_open_pull_requests{repo}
#     - gitea_repo_size_bytes{repo}
#     - gitea_repo_is_mirror{repo}
#
#   Runners:
#     - gitea_runners_total
#     - gitea_runners_online
#     - gitea_runners_offline
#
#   Exporter:
#     - gitea_exporter_duration_seconds
#     - gitea_exporter_last_run_timestamp

set -euo pipefail

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

# --- Functions ---

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

Gitea/Forgejo 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:
  GITEA_URL       Gitea/Forgejo base URL (required)
  GITEA_TOKEN     API token with admin scope (required)
  TEXTFILE_DIR    Output directory (default: /var/lib/node_exporter/textfile_collector)
  CURL_TIMEOUT    Request timeout in seconds (default: 10)
  MAX_REPOS       Max repositories for per-repo metrics (default: 50)

Examples:
  GITEA_URL="https://git.example.com" GITEA_TOKEN="xxx" $SCRIPT_NAME
  GITEA_URL="https://git.example.com" GITEA_TOKEN="xxx" $SCRIPT_NAME --textfile
  GITEA_URL="https://git.example.com" GITEA_TOKEN="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 "$GITEA_URL" ]]; then
        echo "ERROR: GITEA_URL environment variable is required" >&2
        exit 1
    fi
    if [[ -z "$GITEA_TOKEN" ]]; then
        echo "ERROR: GITEA_TOKEN environment variable is required" >&2
        exit 1
    fi
    # Strip trailing slash
    GITEA_URL="${GITEA_URL%/}"
}

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

api_get_with_headers() {
    local endpoint="$1"
    local response
    response=$(curl -sD - --max-time "$CURL_TIMEOUT" \
        -H "Authorization: token ${GITEA_TOKEN}" \
        "${GITEA_URL}${endpoint}" 2>/dev/null) || { echo ""; return; }

    local headers body
    headers=$(echo "$response" | sed '/^\r$/q')
    body=$(echo "$response" | sed '1,/^\r$/d')

    local total_count
    total_count=$(echo "$headers" | grep -i '^X-Total-Count:' | tr -d '\r' | awk '{print $2}')

    echo "${total_count:-0}"
    echo "$body"
}

sanitize_label() {
    local value="$1"
    echo "$value" | sed 's/[^a-zA-Z0-9_\/.-]/_/g'
}

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_version() {
    local version_json
    version_json=$(api_get "/api/v1/version")

    if [[ -z "$version_json" ]]; then
        add_metric "gitea_up" "gauge" "Gitea reachability (1=up, 0=down)" "0"
        return 1
    fi

    add_metric "gitea_up" "gauge" "Gitea reachability (1=up, 0=down)" "1"

    local version
    version=$(echo "$version_json" | jq -r '.version // empty' 2>/dev/null)

    if [[ -n "$version" ]]; then
        add_metric "gitea_version_info" "gauge" "Gitea/Forgejo version" "1" "version=\"${version}\""
    fi

    return 0
}

collect_users() {
    local response
    response=$(api_get_with_headers "/api/v1/admin/users?limit=1")

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

    local total_count
    total_count=$(echo "$response" | head -1)

    if [[ -n "$total_count" && "$total_count" != "0" ]]; then
        add_metric "gitea_users_total" "gauge" "Total number of users" "$total_count"
    fi
}

collect_organizations() {
    local response
    response=$(api_get_with_headers "/api/v1/admin/orgs?limit=1")

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

    local total_count
    total_count=$(echo "$response" | head -1)

    if [[ -n "$total_count" ]]; then
        add_metric "gitea_organizations_total" "gauge" "Total number of organizations" "$total_count"
    fi
}

collect_repositories() {
    local response
    response=$(api_get_with_headers "/api/v1/repos/search?limit=1")

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

    local total_count
    total_count=$(echo "$response" | head -1)

    if [[ -n "$total_count" ]]; then
        add_metric "gitea_repositories_total" "gauge" "Total number of repositories" "$total_count"
    fi
}

collect_repo_details() {
    local page=1
    local per_page=50
    local collected=0
    local first_page=true

    # Add HELP/TYPE lines for per-repo metrics
    OUTPUT+="# HELP gitea_repo_stars Number of stars for the repository
# TYPE gitea_repo_stars gauge
# HELP gitea_repo_forks Number of forks for the repository
# TYPE gitea_repo_forks gauge
# HELP gitea_repo_open_issues Number of open issues for the repository
# TYPE gitea_repo_open_issues gauge
# HELP gitea_repo_open_pull_requests Number of open pull requests for the repository
# TYPE gitea_repo_open_pull_requests gauge
# HELP gitea_repo_size_bytes Repository size in bytes
# TYPE gitea_repo_size_bytes gauge
# HELP gitea_repo_is_mirror Whether the repository is a mirror (1=yes, 0=no)
# TYPE gitea_repo_is_mirror gauge
"

    while [[ $collected -lt $MAX_REPOS ]]; do
        local remaining=$((MAX_REPOS - collected))
        local fetch_count=$((remaining < per_page ? remaining : per_page))

        local repos_json
        repos_json=$(api_get "/api/v1/repos/search?limit=${fetch_count}&page=${page}")

        if [[ -z "$repos_json" ]]; then
            break
        fi

        local repo_count
        repo_count=$(echo "$repos_json" | jq -r '.data | length // 0' 2>/dev/null)

        if [[ "$repo_count" == "0" || -z "$repo_count" ]]; then
            break
        fi

        local i
        for ((i = 0; i < repo_count && collected < MAX_REPOS; i++)); do
            local full_name stars forks open_issues size mirror has_pull_requests
            full_name=$(echo "$repos_json" | jq -r ".data[$i].full_name // empty" 2>/dev/null)
            stars=$(echo "$repos_json" | jq -r ".data[$i].stars_count // 0" 2>/dev/null)
            forks=$(echo "$repos_json" | jq -r ".data[$i].forks_count // 0" 2>/dev/null)
            open_issues=$(echo "$repos_json" | jq -r ".data[$i].open_issues_count // 0" 2>/dev/null)
            size=$(echo "$repos_json" | jq -r ".data[$i].size // 0" 2>/dev/null)
            mirror=$(echo "$repos_json" | jq -r ".data[$i].mirror // false" 2>/dev/null)
            has_pull_requests=$(echo "$repos_json" | jq -r ".data[$i].has_pull_requests // true" 2>/dev/null)

            if [[ -z "$full_name" ]]; then
                continue
            fi

            local safe_name
            safe_name=$(sanitize_label "$full_name")
            local label="repo=\"${safe_name}\""

            # Size: API returns KB, convert to bytes
            local size_bytes=$((size * 1024))

            # Mirror: convert bool to 0/1
            local mirror_val=0
            if [[ "$mirror" == "true" ]]; then
                mirror_val=1
            fi

            # Open PRs: fetch from repo API if pull requests are enabled
            local open_prs=0
            if [[ "$has_pull_requests" == "true" ]]; then
                local owner repo_name
                owner=$(echo "$repos_json" | jq -r ".data[$i].owner.login // empty" 2>/dev/null)
                repo_name=$(echo "$repos_json" | jq -r ".data[$i].name // empty" 2>/dev/null)
                if [[ -n "$owner" && -n "$repo_name" ]]; then
                    local pr_response
                    pr_response=$(api_get_with_headers "/api/v1/repos/${owner}/${repo_name}/pulls?state=open&limit=1")
                    if [[ -n "$pr_response" ]]; then
                        open_prs=$(echo "$pr_response" | head -1)
                    fi
                fi
            fi

            add_metric_value "gitea_repo_stars" "$stars" "$label"
            add_metric_value "gitea_repo_forks" "$forks" "$label"
            add_metric_value "gitea_repo_open_issues" "$open_issues" "$label"
            add_metric_value "gitea_repo_open_pull_requests" "${open_prs:-0}" "$label"
            add_metric_value "gitea_repo_size_bytes" "$size_bytes" "$label"
            add_metric_value "gitea_repo_is_mirror" "$mirror_val" "$label"

            collected=$((collected + 1))
        done

        # If we got fewer than requested, we've reached the end
        if [[ $repo_count -lt $fetch_count ]]; then
            break
        fi

        page=$((page + 1))
    done
}

collect_runners() {
    local runners_json
    runners_json=$(api_get "/api/v1/admin/runners")

    # Runner endpoint may 404 if Actions is not enabled — skip gracefully
    if [[ -z "$runners_json" ]]; then
        return
    fi

    # Validate we got a JSON array
    local is_array
    is_array=$(echo "$runners_json" | jq -r 'if type == "array" then "yes" else "no" end' 2>/dev/null)

    if [[ "$is_array" != "yes" ]]; then
        return
    fi

    local total online offline
    total=$(echo "$runners_json" | jq 'length' 2>/dev/null)
    online=$(echo "$runners_json" | jq '[.[] | select(.status == "online")] | length' 2>/dev/null)
    offline=$(echo "$runners_json" | jq '[.[] | select(.status != "online")] | length' 2>/dev/null)

    add_metric "gitea_runners_total" "gauge" "Total number of registered runners" "${total:-0}"
    add_metric "gitea_runners_online" "gauge" "Number of online runners" "${online:-0}"
    add_metric "gitea_runners_offline" "gauge" "Number of offline runners" "${offline:-0}"
}

write_output() {
    if [[ "$TEXTFILE_MODE" == true ]]; then
        local output_file="${TEXTFILE_DIR}/gitea.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/gitea-exporter <<EOF
# Gitea/Forgejo Prometheus Exporter — runs every 2 minutes
GITEA_URL=${GITEA_URL}
GITEA_TOKEN=${GITEA_TOKEN}
TEXTFILE_DIR=${TEXTFILE_DIR}
*/2 * * * * root ${script_path} --textfile 2>/dev/null
EOF

    chmod 644 /etc/cron.d/gitea-exporter
    echo "Installed cron job: /etc/cron.d/gitea-exporter"
    echo "Metrics will be written to: ${TEXTFILE_DIR}/gitea.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 "gitea_exporter_info" "gauge" "Exporter version information" "1" "version=\"${VERSION}\""

    # Collect metrics
    if collect_version; then
        collect_users
        collect_organizations
        collect_repositories
        collect_repo_details
        collect_runners
    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 "gitea_exporter_duration_seconds" "gauge" "Time to generate all metrics" "$duration"
    add_metric "gitea_exporter_last_run_timestamp" "gauge" "Unix timestamp of last successful run" "$(date +%s)"

    write_output
}

main "$@"
