#!/bin/bash
set -e

echo "=== Installing sing-box server configuration ==="

# Copy certificates
echo "Copying TLS certificates..."
sudo cp /tmp/sing-box-certs/cert.pem /etc/sing-box/cert.pem
sudo cp /tmp/sing-box-certs/key.pem /etc/sing-box/key.pem
sudo chmod 600 /etc/sing-box/key.pem
sudo chmod 644 /etc/sing-box/cert.pem

# Backup existing config
if [ -f /etc/sing-box/config.json ]; then
    echo "Backing up existing config..."
    sudo cp /etc/sing-box/config.json /etc/sing-box/config.json.bak
fi

# Copy new config
echo "Installing new configuration..."
sudo cp /home/andy/singbox-setup/server-config.json /etc/sing-box/config.json

# Validate config
echo "Validating configuration..."
sing-box check -c /etc/sing-box/config.json

# Restart sing-box service
echo "Restarting sing-box service..."
sudo systemctl restart sing-box
sudo systemctl enable sing-box

# Check status
echo "Checking service status..."
sudo systemctl status sing-box --no-pager

echo ""
echo "=== Setup complete! ==="
echo ""
echo "Listening on:"
echo "  - VLESS + Reality: port 443 (TCP)"
echo "  - Hysteria2: port 8443 (UDP)"
echo ""
echo "Make sure your firewall allows these ports:"
echo "  sudo ufw allow 443/tcp"
echo "  sudo ufw allow 8443/udp"
