#!/sbin/openrc-run
# Alpine 5G Router – Web GUI with integrated 5G connection management.
# Rev: 2  (see REVISION in repo root)
#
# This service now handles both the Web GUI and 5G modem connection.
# The standalone 5g-router service is no longer needed.

description="5G Router Web GUI with connection management (port 5000)"
command="/usr/local/share/5g-webgui/run.sh"
command_background="yes"
pidfile="/run/5g-webgui.pid"
output_log="/var/log/5g-webgui.log"
error_log="/var/log/5g-webgui.log"

depend() {
    need net
    after bootmisc
    # Note: 5g-router service is no longer needed; connection is managed by this service
}

start_pre() {
    if [ ! -f /usr/local/share/5g-webgui/app.py ]; then
        eend 1 "Web GUI not installed at /usr/local/share/5g-webgui"
        return 1
    fi
    if ! command -v python3 >/dev/null 2>&1; then
        eend 1 "python3 not found. Install: apk add python3 py3-flask py3-serial"
        return 1
    fi
    # Ensure pyserial is available
    if ! python3 -c "import serial" 2>/dev/null; then
        ewarn "pyserial not found. Install: pip install pyserial"
    fi
    return 0
}
