#!/usr/bin/env bash

# Version 1.6.0

# Initialize number of attempts
reachable=9
echo >>/var/log/netcheck
echo "NetCheck starting: [`date +%T.%3N`]" >> /var/log/netcheck
while [ $reachable -ne 0 ]; do
 # Check host
 # ping -q -c 1 -W 1 "www.google.com" > /dev/null 2>&1
 echo >>/var/log/netcheck
 wget https://google.com/robots.txt --spider --tries 10 --output-document=/tmp/robots.txt --append-output=/var/log/netcheck
 # Check return code
 wget_exit_code=$? 
  if [ $wget_exit_code -eq 0 ]; then
    # Success, we can break with the return code
    break
  fi
  # Not reachable, decrement counter and try again
  let reachable-=1
  # Sleep for five seconds
  sleep 5
done

# Number of attempts exhausted, quiting
# Let's be very verbose for debuging

case "$wget_exit_code" in
                   0)
                        systemd-notify READY=1 --status="Network connectivity is available"
                        wget_exit_msg="No problems occurred (0)"
                        netcheck_status="Success"
                        exit_code=0
                        ;;

                   1)
                        systemd-notify ERRNO=101 --status='wget: Generic error code (1)'
                        wget_exit_msg="Generic error code (1)"
                        exit_code=1
                        ;;

                   2)
                        systemd-notify ERRNO=101 --status='wget: Parse error (2)'
                        wget_exit_msg="Parse error (2)"
                        exit_code=1
                        ;;

                   3)
                        systemd-notify ERRNO=101 --status='wget: File I/O error (3)'
                        wget_exit_msg="File I/O error (3)"
                        exit_code=74
                        ;;

                   4)
                        systemd-notify ERRNO=101 --status='wget: Network failure (4)'
                        wget_exit_msg="Network failure (4)"
                        netcheck_status="Failure"
                        exit_code=68
                        ;;

                   5)
                        systemd-notify ERRNO=101 --status='wget: SSL verification failure (5)'
                        wget_exit_msg="SSL verification failure (5)"
                        exit_code=1
                        ;;

                   6)
                        systemd-notify ERRNO=101 --status='wget: Username/password authentication failure (6)'
                        wget_exit_msg="Username/password authentication failure (6)"
                        exit_code=1
                        ;;

                  7)
                        systemd-notify ERRNO=101 --status='wget: Protocol errors (7)'
                        wget_exit_msg="Protocol errors (7)"
                        exit_code=1
                        ;;

                   8)
                        systemd-notify ERRNO=101 --status='wget: Server issued an error response (8)'
                        wget_exit_msg="Server issued an error response (9)"
                        exit_code=8
                        ;;

esac

echo >>/var/log/netcheck
echo "wget returned: $wget_exit_msg [`date +%T.%3N`]" >> /var/log/netcheck
echo >>/var/log/netcheck
echo "NetCheck: $netcheck_status [`date +%T.%3N`]" >> /var/log/netcheck
exit $exit_code

