#!/bin/bash
# Quick script to install Cloudflare Tunnel service
# Usage: ./INSTALL_TUNNEL.sh <TUNNEL_TOKEN>

if [ -z "$1" ]; then
    echo "Error: Tunnel token required!"
    echo ""
    echo "Usage: $0 <TUNNEL_TOKEN>"
    echo ""
    echo "Get your token from Cloudflare Dashboard:"
    echo "  Zero Trust → Networks → Tunnels → Create tunnel → Copy token"
    exit 1
fi

TUNNEL_TOKEN="$1"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
CLOUDFLARED_VMID="${CLOUDFLARED_VMID:-102}"

echo "Installing Cloudflare Tunnel service..."
echo "Container: VMID $CLOUDFLARED_VMID"

# Stop existing DoH service if running
ssh root@${PROXMOX_HOST} "pct exec $CLOUDFLARED_VMID -- systemctl stop cloudflared 2>/dev/null || true"

# Install tunnel service
ssh root@${PROXMOX_HOST} "pct exec $CLOUDFLARED_VMID -- cloudflared service install $TUNNEL_TOKEN"

# Enable and start
ssh root@${PROXMOX_HOST} "pct exec $CLOUDFLARED_VMID -- systemctl enable cloudflared"
ssh root@${PROXMOX_HOST} "pct exec $CLOUDFLARED_VMID -- systemctl start cloudflared"

# Check status
echo ""
echo "Checking tunnel status..."
ssh root@${PROXMOX_HOST} "pct exec $CLOUDFLARED_VMID -- systemctl status cloudflared --no-pager | head -10"

echo ""
echo "✅ Tunnel service installed!"
echo ""
echo "Next steps:"
echo "1. Configure routes in Cloudflare Dashboard"
echo "2. Update DNS records to CNAME pointing to tunnel"
echo "3. See: docs/04-configuration/CLOUDFLARE_TUNNEL_QUICK_SETUP.md"

