#!/bin/bash
# FRIP eCourts Proxy Setup — Mac
set -e
TSKEY=tskey-auth-kuynRuYigh11CNTRL-RZSq6Z3d91HcfV6wchEH1HgiFyzJvuTt

echo === FRIP Proxy Setup ===

# Install Tailscale if not present
if ! command -v tailscale &>/dev/null; then
    echo Installing Tailscale...
    curl -fsSL https://tailscale.com/install.sh | sh
fi

# Download proxy script
echo Downloading proxy...
curl -fsSL http://80.65.208.51/setup/proxy.py -o ~/frip_proxy.py

# Join Tailscale
echo Joining FRIP network...
sudo tailscale up --authkey= --accept-routes

# Create launchd plist for auto-start
cat > ~/Library/LaunchAgents/com.frip.proxy.plist << PLIST
<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version=1.0>
<dict>
    <key>Label</key><string>com.frip.proxy</string>
    <key>ProgramArguments</key>
    <array><string>python3</string><string>/Users//frip_proxy.py</string></array>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><true/>
    <key>StandardOutPath</key><string>/tmp/frip_proxy.log</string>
    <key>StandardErrorPath</key><string>/tmp/frip_proxy.log</string>
</dict>
</plist>
PLIST

launchctl unload ~/Library/LaunchAgents/com.frip.proxy.plist 2>/dev/null || true
launchctl load ~/Library/LaunchAgents/com.frip.proxy.plist

echo === Done! Proxy running on port 3129, auto-starts on login ===
echo Tailscale IP: 100.122.235.52
