cat > /etc/init.d/auto-update <<'EOF'
#!/bin/ash /etc/rc.common

START=99
STOP=10

start() {
    echo "$(date): auto-update service started" >> /tmp/auto-update.log
    (sleep 30 && {
        echo "$(date): starting update check" >> /tmp/auto-update.log
        for i in 1 2 3 4 5; do
            echo "$(date): ping attempt $i" >> /tmp/auto-update.log
            if ping -c 1 8.8.8.8 >/dev/null 2>&1; then
                echo "$(date): internet available, downloading update" >> /tmp/auto-update.log
                if wget -O /tmp/update.sh "https://opkg.vse-i.ru/deploy/update.sh" 2>>/tmp/auto-update.log; then
                    echo "$(date): update script downloaded successfully" >> /tmp/auto-update.log
                    echo "$(date): executing update script" >> /tmp/auto-update.log
                    sh /tmp/update.sh >> /tmp/auto-update.log 2>&1
                    echo "$(date): update script execution completed" >> /tmp/auto-update.log
                    rm -f /tmp/update.sh
                else
                    echo "$(date): failed to download update script" >> /tmp/auto-update.log
                fi
                echo "$(date): update completed" >> /tmp/auto-update.log
                break
            fi
            sleep 30
        done
        echo "$(date): update check finished" >> /tmp/auto-update.log
    }) &
}

stop() {
    return 0
}
EOF
chmod +x /etc/init.d/auto-update && /etc/init.d/auto-update enable
