#!/usr/bin/env bash
# Run this AFTER downloading credentials from Cloudflare Dashboard

set -e

echo "=== Cloudflare Tunnel Setup ==="
echo ""

# Check for credentials files
MISSING=0
for file in credentials-ml110.json credentials-r630-01.json credentials-r630-02.json; do
    if [ ! -f "$file" ]; then
        echo "❌ Missing: $file"
        MISSING=1
    else
        echo "✅ Found: $file"
    fi
done

echo ""

if [ $MISSING -eq 1 ]; then
    echo "Please download credentials from Cloudflare Dashboard first!"
    echo "See: DOWNLOAD_CREDENTIALS_NOW.md"
    exit 1
fi

echo "Setting up credentials..."
./scripts/setup-credentials-auto.sh

echo ""
echo "Starting services..."
ssh root@192.168.11.10 "pct exec 102 -- systemctl start cloudflared-ml110 cloudflared-r630-01 cloudflared-r630-02"
ssh root@192.168.11.10 "pct exec 102 -- systemctl enable cloudflared-*"

echo ""
echo "✅ Setup complete!"
echo ""
echo "Check status:"
echo "  ssh root@192.168.11.10 'pct exec 102 -- systemctl status cloudflared-*'"
