#!/bin/sh
# Alpine 5G Router – install/deploy scripts and config to this device
# Run from repo root: ./scripts/install.sh
# Or from scripts/: ./install.sh (uses script dir to find repo root)
# Rev: 2  (see REVISION in repo root)
#
# NOTE: As of Rev 2, 5g-webgui handles both Web GUI AND 5G connection management.
#       The standalone 5g-router service is kept for manual/fallback use but is
#       NOT enabled by default. Use 5g-webgui instead.

set -e

# Repo root (directory containing etc/ and scripts/)
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ETC="$ROOT/etc"
SCRIPTS="$ROOT/scripts"
BIN="/usr/local/bin"
INITD="/etc/init.d"
IPTABLES_SAVE="/etc/iptables"
CONFIG="/etc/5g-router.conf"
CONFIG_EXAMPLE="$ETC/5g-router.conf.example"
LOG_DIR="/var/log"

echo "Installing Alpine 5G Router from $ROOT"

# Config: copy example if no config exists
if [ ! -f "$CONFIG" ]; then
    cp "$CONFIG_EXAMPLE" "$CONFIG"
    echo "Created $CONFIG – please edit APN and interfaces if needed."
else
    echo "Keeping existing $CONFIG"
fi

# Scripts (kept for manual use and diagnostics)
install -m 755 "$SCRIPTS/connect-5g.sh"    "$BIN/connect-5g.sh"
install -m 755 "$SCRIPTS/status-5g.sh"      "$BIN/status-5g.sh"
install -m 755 "$SCRIPTS/modem-status-at.sh" "$BIN/modem-status-at.sh"
install -m 755 "$SCRIPTS/healthcheck-5g.sh" "$BIN/healthcheck-5g.sh"
install -m 755 "$SCRIPTS/speedtest-5g.sh"   "$BIN/speedtest-5g.sh"
install -m 755 "$SCRIPTS/rotate-5g-log.sh" "$BIN/rotate-5g-log.sh"
install -m 755 "$SCRIPTS/diag-at-port.sh" "$BIN/diag-at-port.sh"
install -m 755 "$SCRIPTS/diag-modem-up.sh" "$BIN/diag-modem-up.sh"
install -m 755 "$SCRIPTS/troubleshoot-5g.sh" "$BIN/troubleshoot-5g.sh"
echo "Installed scripts to $BIN"

# Optional: configure_fm350_5g.sh for manual runs
if [ -f "$ROOT/configure_fm350_5g.sh" ]; then
    install -m 755 "$ROOT/configure_fm350_5g.sh" "$BIN/configure_fm350_5g.sh"
    echo "Installed configure_fm350_5g.sh"
fi

# Legacy 5g-router service (kept for manual/fallback use, NOT enabled by default)
install -m 755 "$ETC/init.d/5g-router" "$INITD/5g-router"
echo "Installed $INITD/5g-router (legacy – use 5g-webgui instead)"

# iptables rules (restore at boot – ensure iptables-restore service exists)
mkdir -p "$IPTABLES_SAVE"
install -m 644 "$ETC/iptables/rules.v4" "$IPTABLES_SAVE/rules.v4"
echo "Installed $IPTABLES_SAVE/rules.v4"
if [ -d /etc/init.d ] && [ ! -f /etc/init.d/iptables-restore ]; then
    echo "Tip: enable iptables restore at boot: rc-update add iptables-restore"
fi

# Log files
touch "$LOG_DIR/5g-router.log" 2>/dev/null && chmod 644 "$LOG_DIR/5g-router.log" || true
touch "$LOG_DIR/speedtest-5g.log" 2>/dev/null && chmod 644 "$LOG_DIR/speedtest-5g.log" || true
touch "$LOG_DIR/5g-webgui.log" 2>/dev/null && chmod 644 "$LOG_DIR/5g-webgui.log" || true

# Web GUI with integrated 5G connection management
WEBGUI_SRC="$ROOT/web"
WEBGUI_DEST="/usr/local/share/5g-webgui"
if [ -d "$WEBGUI_SRC" ] && [ -f "$WEBGUI_SRC/app.py" ]; then
    mkdir -p "$WEBGUI_DEST"
    cp -r "$WEBGUI_SRC"/* "$WEBGUI_DEST/"
    chmod 755 "$WEBGUI_DEST/run.sh" 2>/dev/null || true
    install -m 755 "$ETC/init.d/5g-webgui" "$INITD/5g-webgui"

    # Enable 5g-webgui at boot (this now handles connection management)
    if command -v rc-update >/dev/null 2>&1; then
        rc-update add 5g-webgui default 2>/dev/null || true
        echo "Added 5g-webgui to default runlevel"

        # Disable old 5g-router if it was enabled
        rc-update del 5g-router default 2>/dev/null || true
    fi

    echo ""
    echo "Installed Web GUI with 5G connection management at $WEBGUI_DEST"
    echo "  Install dependencies: apk add python3 py3-flask && pip install pyserial"
    echo "  Start service: service 5g-webgui start"
    echo "  Login at http://<device-ip>:5000 (default: admin/admin, support/support – change passwords)"
else
    echo "Web GUI source not found; skipping."
    echo "Enabling legacy 5g-router service instead..."
    if command -v rc-update >/dev/null 2>&1; then
        rc-update add 5g-router default 2>/dev/null || true
        echo "Added 5g-router to default runlevel"
    fi
fi

echo ""
echo "Done. Edit $CONFIG if needed."
echo ""
echo "To start:"
echo "  service 5g-webgui start   (recommended: includes Web GUI + 5G connection)"
echo "  OR"
echo "  service 5g-router start   (legacy: 5G connection only, no Web GUI)"
echo ""
echo "Status: Visit http://<device-ip>:5000 or run: $BIN/status-5g.sh"
