#!/bin/bash

# The best thing about pingpon is it's name!
# Otherwise, it is a simple script to automatically reconnect your internet
# when it goes down, e.g. if you are using wireless broadband.

# WARNING! Don't use this script with dialup if you are paying for the phone
# calls, you need a different script that will stop or back exponentially if it
# can't reconnect. Otherwise you might get a large phone bill.

. get_root
peer="dialup2"
host="${host:-ucm.dev}"
count="${count:-10}"
sleep="${sleep:-10}"
timeout="${timeout:-8}"
while true; do
	while true; do
		ping -c "$count" -w "$timeout" "$host" 2>&1 | perl -ne 'BEGIN { $exit = 1; } if (/(\d+) received/ && $1 > 0) { $exit = 0; } END { exit $exit; }' || break
		sleep "$sleep"
	done
	xmessage -timeout 10 'the network went down!' &
	poff ; while pidof pppd; do sleep 1 ; done; sleep 5; pon "$peer" ; sleep 40
done
