#!/bin/bash
# from https://askubuntu.com/questions/3299/how-to-run-cron-job-when-network-is-up
# usage in cron
##  @reboot bash /usr/share/customscripts/ifnet "/usr/share/customscripts/webhook bootup" 2>> /var/log/cron.error.log
function check_online
{
    netcat -z -w 5 discord.com 80 && echo 1 || echo 0
}

# Initial check to see if we are online
IS_ONLINE=$(check_online)
# How many times we should check if we're online - this prevents infinite looping
MAX_CHECKS=10
# Initial starting value for checks
CHECKS=0

echo "running! IS_ONLINE: $IS_ONLINE"

# Loop while we're not online.
while [[ $IS_ONLINE -eq 0 ]]; do
    sleep 10;
    echo "is_online: $IS_ONLINE loop $CHECKS"
    IS_ONLINE=check_online

    CHECKS=$(($CHECKS + 1))
    if [ $CHECKS -gt $MAX_CHECKS ]; then
        break
    fi
done

if [[ $IS_ONLINE -eq 0 ]]; then
    # We never were able to get online. Kill script.
    exit 1
fi

bash $1 2>> /var/log/cron.error.log