#!/bin/bash

# --- CONFIGURATION ---
# Replace these with your actual Gitea details
GITEA_URL="http://your-gitea-instance:3000"
REG_TOKEN="YOUR_TOKEN_HERE"
RUNNER_NAME="uk-host-runner"
# Adding :host tells Gitea NOT to use Docker
LABELS="ubuntu-latest:host,linux_amd64:host,self-hosted:host"

echo "--- 1. Registering Runner ---"
/usr/local/bin/act_runner register \
  --instance "$GITEA_URL" \
  --token "$REG_TOKEN" \
  --name "$RUNNER_NAME" \
  --labels "$LABELS" \
  --no-interactive

echo "--- 2. Generating Default Config ---"
/usr/local/bin/act_runner generate-config > config.yaml

echo "--- 3. Creating Systemd Service ---"
cat <<EOF | sudo tee /etc/systemd/system/act_runner.service
[Unit]
Description=Gitea Actions Host Runner
After=network.target

[Service]
ExecStart=/usr/local/bin/act_runner daemon --config $(pwd)/config.yaml
WorkingDirectory=$(pwd)
Restart=always
RestartSec=5
User=root

[Install]
WantedBy=multi-user.target
EOF

echo "--- 4. Starting Service ---"
sudo systemctl daemon-reload
sudo systemctl enable act_runner --now

echo "--- Setup Complete ---"
sudo systemctl status act_runner --no-pager