#!/bin/bash
# git-gitea: Git commands over Gitea API
# Usage: git-gitea <command> [args...]
#
# This script translates git commands to Gitea API calls using the configured token.
# Environment variable GITEA_TOKEN must be set.
#
# Examples:
#   git-gitea ls-remote origin <repo>.git
#   git-gitea ls-tree -r --name-only HEAD
#   git-gitea show HEAD:path/to/file
#   git-gitea log --oneline
#
# See https://git.wholeworldcoding.com for Gitea API docs
# https://git.wholeworldcoding.com/api/swagger

GITEA_BASE="https://git.wholeworldcoding.com/api/v1"
GITEA_TOKEN="${GITEA_TOKEN}"

# Create header
HEADER="-H 'Authorization: Bearer ${GITEA_TOKEN}'"

# Get repo info
get_repo_info() {
    local owner="$1"
    local repo="$2"
    curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}" 2>/dev/null | python3 -
}

# List contents
contents() {
    local owner="$1"
    local repo="$2"
    shift 2
    local path="${1:-?}"
    curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/contents/${path}" 2>/dev/null | python3 -
}

# Read file content
read_file() {
    local owner="$1"
    local repo="$2"
    shift 2
    local path="${1:-?}"
    local ref="${2:-HEAD}"
    contents "${owner}" "${repo}" "${path}" | jq '.content' 2>/dev/null || curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/contents/${path}?ref=${ref}" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('content',''))"
}

# List commits/branches
branches() {
    local owner="$1"
    local repo="$2"
    curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/branches" 2>/dev/null | python3 -
}

# Show file content (returns markdown)
show_file() {
    local owner="$1"
    local repo="$2"
    shift 2
    local path="${1:-?}"
    local ref="${2:-HEAD}"
    read_file "${owner}" "${repo}" "${path}" "${ref}"
}

# Log
log() {
    local owner="$1"
    local repo="$2"
    shift 2
    local args="${1:-}"
    curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/commits" 2>/dev/null | python3 -
}

# Main git command router
case "${1:-}" in
    ls-remote)
        local owner="${2:-}"
        local repo="${3:-}"
        get_repo_info "${owner}" "${repo}" | python3 -c "
import sys, json
data = json.load(sys.stdin)
from git import Remote
remote = Remote('.')
for info in [data.get('default_branch', 'main')]:
    remote.set_url(f'https://git.wholeworldcoding.com/{data[\"owner\"][\"login\"]}/{data[\"name\"]}.git')
"
        ;;
    ls-tree)
        local owner="$1"
        local repo="$2"
        shift 2
        show_file "${owner}" "${repo}" "$@"
        ;;
    show)
        local owner="$1"
        local repo="$2"
        shift 2
        local ref="${1:-HEAD}"
        local path="${2:-}"
        show_file "${owner}" "${repo}" "${path:-$ref}" "${ref}"
        ;;
    log)
        local owner="$1"
        local repo="$2"
        shift 2
        log "${owner}" "${repo}" "$@"
        ;;
    diff)
        local owner="$1"
        local repo="$2"
        shift 2
        curl -s -X GET ${HEADER} "${GITEA_BASE}/repos/${owner}/${repo}/compare/${2:-master}...${1:-HEAD}" 2>/dev/null | python3 -
        ;;
    *)
        echo "Unknown git command: $1"
        echo "Supported commands: ls-remote, ls-tree, show, log, diff, pull, push, fetch"
        exit 1
        ;;
esac
