#!/bin/sh
# Alpine 5G Router – modem/WAN diagnostic (run on device to debug “modem not up”)
# Rev: 1  (see REVISION in repo root)
# Usage: ./diag-modem-up.sh   or   /usr/local/bin/diag-modem-up.sh

CONFIG="/etc/5g-router.conf"
WAN_IF="${WAN_IF:-eth1}"
AT_PORT="${AT_PORT:-/dev/ttyUSB1}"
LOG_FILE="/var/log/5g-router.log"
[ -f "$CONFIG" ] && . "$CONFIG"

echo "=== Alpine 5G Router – modem/WAN diagnostic ==="
echo ""

# 1) Service
echo "--- 5g-router service ---"
if command -v rc-service >/dev/null 2>&1; then
    rc-service 5g-router status 2>&1 || true
else
    echo "OpenRC not found; service status skipped"
fi
echo ""

# 2) Config
echo "--- Config ($CONFIG) ---"
if [ -f "$CONFIG" ]; then
    echo "WAN_IF=$WAN_IF  AT_PORT=$AT_PORT"
    grep -E '^APN=|^WAN_IF=|^AT_PORT=' "$CONFIG" 2>/dev/null || true
else
    echo "Config file not found (using defaults)"
fi
echo ""

# 3) Modem USB
echo "--- Modem (lsusb) ---"
MODEM_LINE=$(lsusb 2>/dev/null | grep -i fibocom || lsusb 2>/dev/null | grep "0e8d" || true)
if [ -n "$MODEM_LINE" ]; then
    echo "$MODEM_LINE"
    if echo "$MODEM_LINE" | grep -q "7127"; then
        echo "  -> Mode 41 (0e8d:7127): AT port may not work; use Mode 40 (0e8d:7126)"
    elif echo "$MODEM_LINE" | grep -q "7126"; then
        echo "  -> Mode 40 (RNDIS): AT port should be available"
    fi
else
    echo "No Fibocom / 0e8d device – modem not seen by USB"
fi
echo ""

# 4) WAN interface
echo "--- WAN interface ($WAN_IF) ---"
if ip link show "$WAN_IF" >/dev/null 2>&1; then
    ip link show "$WAN_IF"
    WAN_STATE=$(ip link show "$WAN_IF" 2>/dev/null | grep -oE 'state [A-Z]+' | awk '{print $2}')
    WAN_IP=$(ip -4 addr show "$WAN_IF" 2>/dev/null | grep -oE 'inet [0-9.]+' | awk '{print $2}')
    echo "  State: $WAN_STATE   IP: ${WAN_IP:-none}"
    if [ "$WAN_STATE" = "DOWN" ]; then
        echo "  -> Interface is DOWN (connect-5g.sh may have failed or not run)"
    fi
    if [ -z "$WAN_IP" ]; then
        echo "  -> No IP (PDP not activated or modem did not assign address)"
    fi
else
    echo "  Interface $WAN_IF does not exist (wrong name or modem not bound?)"
fi
echo ""

# 5) Default route
echo "--- Default route ---"
DEFAULT=$(ip route show default 2>/dev/null | head -1)
if [ -n "$DEFAULT" ]; then
    echo "$DEFAULT"
    DEF_DEV=$(echo "$DEFAULT" | awk '{print $3}')
    if [ "$DEF_DEV" != "$WAN_IF" ]; then
        echo "  -> Default is via $DEF_DEV, not $WAN_IF (5G may be down or failover active)"
    fi
else
    echo "  No default route"
fi
echo ""

# 6) AT port
echo "--- AT port ($AT_PORT) ---"
if [ -c "$AT_PORT" ]; then
    echo "  Exists (character device)"
    out=$(timeout 4 sh -c "
        cat $AT_PORT 2>/dev/null &
        _p=\$!
        sleep 0.3
        echo -e 'AT\r' > $AT_PORT 2>/dev/null
        sleep 1
        kill \$_p 2>/dev/null
    " 2>/dev/null)
    if echo "$out" | grep -q 'OK'; then
        echo "  AT response: OK"
    else
        echo "  AT response: no OK (raw: $(echo "$out" | head -1))"
    fi
else
    echo "  Not available (missing or not a char device) – connect-5g.sh waits for this"
    for p in /dev/ttyUSB0 /dev/ttyUSB1 /dev/ttyUSB2; do
        [ -c "$p" ] && echo "  Found: $p"
    done
fi
echo ""

# 7) Last log lines
echo "--- Last 20 lines of $LOG_FILE ---"
if [ -f "$LOG_FILE" ]; then
    tail -20 "$LOG_FILE" 2>/dev/null | sed 's/^/  /'
else
    echo "  Log file not found"
fi
echo ""

# 8) Connectivity
echo "--- Connectivity (ping 8.8.8.8) ---"
if ping -c 1 -W 3 8.8.8.8 >/dev/null 2>&1; then
    echo "  Reachable (modem/route may be OK)"
else
    echo "  Not reachable (WAN down or no default via 5G)"
fi
echo ""
echo "=== End diagnostic ==="
echo ""
echo "Typical fixes:"
echo "  - Modem not in lsusb: power-cycle modem or USB; check cable"
echo "  - Mode 41 (7127): reboot modem or run AT+GTUSBMODE=40 then AT+CFUN=1,1"
echo "  - AT port missing: wait for modem to expose ttyUSB (can take 30s after boot)"
echo "  - AT no OK: wrong port – set AT_PORT in $CONFIG (e.g. /dev/ttyUSB0)"
echo "  - No modem IP: check APN in $CONFIG; check SIM/network registration"
echo "  - Start connection: service 5g-router start   or   /usr/local/bin/connect-5g.sh"
