#!/bin/bash -e
# Handle port conflict with ttyd-bash

echo "Handling port conflicts..."

# Option 1: Disable ttyd-bash (default - recommended)
# This frees up port 8000 for Dangerous Pi
if systemctl is-enabled ttyd-bash 2>/dev/null; then
    systemctl disable ttyd-bash
    echo "Disabled ttyd-bash to avoid port conflict with Dangerous Pi (port 8000)"
fi

# Option 2: Change Dangerous Pi port to 8001
# Uncomment to use this option:
# if [ -f /opt/dangerous-pi/.env ]; then
#     sed -i 's/^PORT=.*/PORT=8001/' /opt/dangerous-pi/.env
#     echo "Changed Dangerous Pi port to 8001"
# fi

# Option 3: Change ttyd-bash port to 8002
# Uncomment to use this option:
# if [ -f /etc/systemd/system/ttyd-bash.service ]; then
#     sed -i 's/--port 8000/--port 8002/' /etc/systemd/system/ttyd-bash.service
#     echo "Changed ttyd-bash port to 8002"
# fi

# Port conflict handled automatically by disabling ttyd-bash
# Users can manually re-enable it and configure alternative ports if needed
# See /opt/dangerous-pi/scripts/resolve-port-conflict.sh for other options
echo "Port conflict resolution complete"
