#!/usr/bin/env bash
set -euo pipefail

# Gitea Actions Runner Unregistration Script
# This script unregisters the runner from the Gitea instance

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}Gitea Actions Runner Unregistration${NC}"
echo "========================================"

# Check if runner is registered
if [ ! -f ./data/.runner ]; then
    echo -e "${YELLOW}Warning: No registration file found${NC}"
    echo "Runner may not be registered"
    exit 0
fi

echo ""
read -p "Are you sure you want to unregister this runner? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Unregistration cancelled"
    exit 0
fi

# Stop services
echo ""
echo -e "${YELLOW}Stopping services...${NC}"
docker compose down

# Remove registration file
echo "Removing registration file..."
rm -f ./data/.runner

# Remove runner data
echo ""
read -p "Do you want to remove all runner data? (y/N): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "Removing runner data..."
    rm -rf ./data/*
    echo -e "${GREEN}✓ Runner data removed${NC}"
fi

echo ""
echo -e "${GREEN}✓ Runner unregistered successfully!${NC}"
echo ""
echo "Note: You may need to manually remove the runner from Gitea UI:"
echo "  Go to: Site Administration > Actions > Runners"
echo "  Find the runner and click 'Delete'"
echo ""
