#!/bin/bash
# Script to resolve port conflict between Dangerous Pi and ttyd-bash

set -e

echo "Dangerous Pi Port Conflict Resolution"
echo "======================================"
echo ""
echo "The existing pi-pm3 setup uses:"
echo "  - ttyd-bash on port 8000"
echo "  - ttyd-pm3 on port 8080"
echo "  - RaspAP on port 80"
echo ""
echo "Dangerous Pi wants to use port 8000 for the backend."
echo ""
echo "Choose a resolution option:"
echo "  1) Disable ttyd-bash (recommended - use Dangerous Pi's web terminal instead)"
echo "  2) Change Dangerous Pi to port 8001 (keep both services)"
echo "  3) Change ttyd-bash to port 8002 (keep both services)"
echo "  4) Cancel (manual configuration)"
echo ""

read -p "Enter option (1-4): " option

case $option in
    1)
        echo ""
        echo "Disabling ttyd-bash service..."

        if systemctl is-active --quiet ttyd-bash 2>/dev/null; then
            sudo systemctl stop ttyd-bash
            echo "✅ Stopped ttyd-bash"
        fi

        if systemctl is-enabled --quiet ttyd-bash 2>/dev/null; then
            sudo systemctl disable ttyd-bash
            echo "✅ Disabled ttyd-bash (won't start on boot)"
        fi

        echo ""
        echo "✅ Done! Dangerous Pi can now use port 8000."
        echo ""
        echo "To re-enable ttyd-bash later:"
        echo "  sudo systemctl enable ttyd-bash"
        echo "  sudo systemctl start ttyd-bash"
        ;;

    2)
        echo ""
        echo "Changing Dangerous Pi to port 8001..."

        # Update environment file
        if [ -f /opt/dangerous-pi/.env ]; then
            if grep -q "^PORT=" /opt/dangerous-pi/.env; then
                sudo sed -i 's/^PORT=.*/PORT=8001/' /opt/dangerous-pi/.env
            else
                echo "PORT=8001" | sudo tee -a /opt/dangerous-pi/.env
            fi
            echo "✅ Updated /opt/dangerous-pi/.env"
        else
            echo "⚠️  Environment file not found. Please edit manually."
        fi

        # Restart service if running
        if systemctl is-active --quiet dangerous-pi 2>/dev/null; then
            sudo systemctl restart dangerous-pi
            echo "✅ Restarted dangerous-pi service"
        fi

        echo ""
        echo "✅ Done! Dangerous Pi now uses port 8001."
        echo "   Access at: http://$(hostname -I | awk '{print $1}'):8001"
        ;;

    3)
        echo ""
        echo "Changing ttyd-bash to port 8002..."

        # Find and update ttyd-bash service file
        if [ -f /etc/systemd/system/ttyd-bash.service ]; then
            sudo sed -i 's/--port 8000/--port 8002/' /etc/systemd/system/ttyd-bash.service
            sudo systemctl daemon-reload
            echo "✅ Updated ttyd-bash service"

            if systemctl is-active --quiet ttyd-bash 2>/dev/null; then
                sudo systemctl restart ttyd-bash
                echo "✅ Restarted ttyd-bash"
            fi

            echo ""
            echo "✅ Done! ttyd-bash now uses port 8002."
            echo "   ttyd-bash: http://$(hostname -I | awk '{print $1}'):8002"
            echo "   Dangerous Pi can use port 8000"
        else
            echo "⚠️  ttyd-bash service file not found. Please configure manually."
        fi
        ;;

    4)
        echo ""
        echo "Cancelled. Manual configuration required."
        echo ""
        echo "Configuration files:"
        echo "  - Dangerous Pi: /opt/dangerous-pi/.env (PORT variable)"
        echo "  - ttyd-bash: /etc/systemd/system/ttyd-bash.service"
        ;;

    *)
        echo "Invalid option. Exiting."
        exit 1
        ;;
esac

echo ""
