#!/usr/bin/env bash
# Instala bridge Community opencli → API FOSSBilling (porta 18087)
set -euo pipefail
DIR="$(cd "$(dirname "$0")" && pwd)"
BRIDGE_DIR="${DIR}/openpanel-community-bridge"

install -d "$BRIDGE_DIR"
chmod +x "$BRIDGE_DIR/bridge.py"
cp "$BRIDGE_DIR/openpanel-foss-bridge.service" /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now openpanel-foss-bridge.service
# FOSS Docker → bridge no host (CSF)
csf -a 172.19.0.0/16 2>/dev/null || iptables -I INPUT -s 172.19.0.0/16 -p tcp --dport 18087 -j ACCEPT
sleep 1
curl -sf -X POST "http://127.0.0.1:18087/api/" \
  -H "Content-Type: application/json" \
  -d '{"username":"ligboxadmin","password":"LbOpen805353"}' | grep -q access_token
echo "Bridge OK em http://10.10.10.123:18087"
echo "FOSS servidor: porta 18087, secure=No (HTTP bridge Community)"
