#!/usr/bin/env bash
# ==============================================================================
# Git by Digital Forge Studios Inc. (https://git.dforge.ca)
# Unlimited Free BYO-Runner Installer (Linux x86_64/arm64 + macOS Apple Silicon)
#
# Usage:
#   curl -fsSL https://git.dforge.ca/runner.sh | bash -s -- \
#     --token <FORGEJO_RUNNER_TOKEN> \
#     --name my-runner \
#     --labels "ubuntu-latest:docker://ghcr.io/catthehacker/ubuntu:act-24.04"
# ==============================================================================
set -euo pipefail

INSTANCE_URL="https://git.dforge.ca"
RUNNER_VERSION="${FORGEJO_RUNNER_VERSION:-6.3.1}"
RUNNER_TOKEN=""
RUNNER_NAME="$(hostname -s 2>/dev/null || echo dforge-runner)"
RUNNER_LABELS=""
INSTALL_DIR="${HOME}/.local/share/dforge-runner"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --instance) INSTANCE_URL="$2"; shift 2 ;;
    --token)    RUNNER_TOKEN="$2"; shift 2 ;;
    --name)     RUNNER_NAME="$2"; shift 2 ;;
    --labels)   RUNNER_LABELS="$2"; shift 2 ;;
    --dir)      INSTALL_DIR="$2"; shift 2 ;;
    -h|--help)
      echo "Usage: runner.sh --token <TOKEN> [--name <NAME>] [--labels <LABELS>] [--instance <URL>]"
      exit 0 ;;
    *) echo "Unknown flag: $1" >&2; exit 1 ;;
  esac
done

if [[ -z "$RUNNER_TOKEN" ]]; then
  echo "ERROR: --token <FORGEJO_RUNNER_TOKEN> is required." >&2
  echo "Generate one in your https://git.dforge.ca org settings (Actions -> Runners) or via POST https://git.dforge.ca/api/v1/runners/token" >&2
  exit 1
fi

OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in
  x86_64|amd64) ARCH="amd64" ;;
  arm64|aarch64) ARCH="arm64" ;;
  *) echo "Unsupported CPU architecture: $ARCH" >&2; exit 1 ;;
esac

case "$OS" in
  linux)
    BIN_NAME="forgejo-runner-${RUNNER_VERSION}-linux-${ARCH}"
    DEFAULT_LABELS="ubuntu-latest:docker://ghcr.io/catthehacker/ubuntu:act-24.04,ubuntu-24.04:docker://ghcr.io/catthehacker/ubuntu:act-24.04"
    ;;
  darwin)
    BIN_NAME="forgejo-runner-${RUNNER_VERSION}-darwin-${ARCH}"
    DEFAULT_LABELS="macos-latest:host,macos-arm64:host"
    ;;
  *) echo "Unsupported OS: $OS" >&2; exit 1 ;;
esac

LABELS="${RUNNER_LABELS:-$DEFAULT_LABELS}"
mkdir -p "${INSTALL_DIR}/bin"
BIN_PATH="${INSTALL_DIR}/bin/forgejo-runner"

if [[ ! -x "$BIN_PATH" ]]; then
  DOWNLOAD_URL="https://code.forgejo.org/forgejo/runner/releases/download/v${RUNNER_VERSION}/${BIN_NAME}"
  echo "[dforge-runner] Downloading ${DOWNLOAD_URL} ..."
  curl -fsSL "$DOWNLOAD_URL" -o "$BIN_PATH"
  chmod 755 "$BIN_PATH"
fi

echo "[dforge-runner] Registering '${RUNNER_NAME}' against ${INSTANCE_URL} ..."
cd "$INSTALL_DIR"
"$BIN_PATH" register \
  --no-interactive \
  --instance "$INSTANCE_URL" \
  --token "$RUNNER_TOKEN" \
  --name "$RUNNER_NAME" \
  --labels "$LABELS"

cat > "${INSTALL_DIR}/config.yml" <<'YAML'
log:
  level: info
runner:
  file: .runner
  capacity: 2
  timeout: 3h
  insecure: false
  fetch_timeout: 5s
  fetch_interval: 2s
cache:
  enabled: true
container:
  network: ""
  privileged: false
  force_pull: false
YAML

echo ""
echo "[dforge-runner] Registered successfully!"
echo "Start now with:"
echo "  cd ${INSTALL_DIR} && ./bin/forgejo-runner daemon --config ./config.yml"
