#!/bin/sh

# TURBO_KEEPUP v1.0
# (re)connection script for Belgacom Turboline ADSL
# Author Frederik Questier http://vub.vub.ac.be/~fquestie
# Thanks to Alain.Nissen@ulg.ac.be, the author of turbo_telnet
# Thanks to David Noble, the author of telnet.pl and sock.pl
# No Thanks to Turboline for the many disconnections.

# CHANGE next 2 lines or those hosts will die if this script becomes popular!
pinghost1="212.100.160.65" # Nearby host with high uptime e.g. smtp.easynet.be
pinghost2="194.7.3.50"     # Nearby host with high uptime e.g. ftp.uunet.be
ISP_IPnr="212.100"         # The IP_nr we expect for our host
turbopath="/usr/local/turboline/ea" # The path where these scripts are

# Launch connection
$turbopath/turbo_telnet
$turbopath/turbo_www
# Do some Dynamic DNS stuff here if you wish

sleep 20

# Check and keep up connection
while true
do
  # check if we still got connection
  # ping pinghost1
  if [ -z "`/bin/ping -c4 $pinghost1|grep ms`" ]
  then
    # ping pinghost2
    if [ -z "`/bin/ping -c4 $pinghost2|grep ms`" ]
    then
      # check if we still got IPnr of our provider, and not a Turboline one
      if [ -z "`/sbin/ifconfig|grep $ISP_IPnr`" ]
      then
        # Maybe we need a Turboline connection first
        $turbopath/turbo_telnet
        $turbopath/turbo_www
        # most often that says: Release Ipnr...
        # Next stuff seems heavy, but less was not reliable for me...
        # With pump it could be much easier...
        /sbin/dhcpcd -k
        killall -9 turbo_telnet
        /etc/rc.d/rc2.d/S10network restart
        # ... and log in again.
        $turbopath/turbo_telnet
        $turbopath/turbo_www
      else
        $turbopath/turbo_telnet
        $turbopath/turbo_www
      fi
      # Do some Dynamic DNS stuff here if you wish
    fi
  fi
  sleep 600
done
