#!/bin/sh /etc/rc.common

START=99
STOP=10

start() {
    echo "Starting router identity detection..."
    (
        WAIT_INTERVAL=30
        MAX_WAIT=600
        elapsed=0

        while [ $elapsed -lt $MAX_WAIT ]; do
            if curl -s --max-time 5 https://supabase.co > /dev/null 2>&1; then
                echo "Internet available after ${elapsed}s, running detection..." >> /var/log/router_startup.log 2>&1
                /root/scripts/detect_router_identity.sh >> /var/log/router_startup.log 2>&1
                exit 0
            fi
            sleep $WAIT_INTERVAL
            elapsed=$((elapsed + WAIT_INTERVAL))
        done

        echo "Internet not available after ${MAX_WAIT}s, running detection anyway..." >> /var/log/router_startup.log 2>&1
        /root/scripts/detect_router_identity.sh >> /var/log/router_startup.log 2>&1
    ) &
}

stop() {
    echo "Stopping router identity detection service..."
    pkill -f "detect_router_identity.sh"
}
