#!/bin/bash
# Check Cloudflare Tunnel Status

set -euo pipefail

NGINX_IP="${1:-20.160.58.99}"

echo "=========================================="
echo "Cloudflare Tunnel Status Check"
echo "=========================================="
echo ""

ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no besuadmin@$NGINX_IP << 'EOF'
echo "1. Cloudflared Installation:"
which cloudflared && cloudflared --version 2>/dev/null | head -1 || echo "  ✗ Not installed"

echo ""
echo "2. Cloudflared Service Status:"
sudo systemctl status cloudflared --no-pager -l 2>/dev/null | head -10 || echo "  ⊘ Service not running"

echo ""
echo "3. Tunnel Configuration:"
if [ -f /etc/cloudflared/config.yml ]; then
    echo "  ✓ Config file exists"
    sudo cat /etc/cloudflared/config.yml | head -20
else
    echo "  ✗ Config file not found"
fi

echo ""
echo "4. Tunnel List:"
sudo cloudflared tunnel list 2>&1 | head -10 || echo "  ⊘ Not authenticated or no tunnels"

echo ""
echo "5. Tunnel Credentials:"
if [ -d /root/.cloudflared ]; then
    echo "  ✓ Credentials directory exists"
    ls -la /root/.cloudflared/ 2>/dev/null | head -5 || echo "  ⊘ No credential files"
else
    echo "  ✗ Credentials directory not found"
fi

EOF

echo ""
echo "=========================================="
echo "Status Check Complete"
echo "=========================================="

