#!/bin/sh

# Thanks to Peter Chiocchetti <girbal@tacheles.de>
# for this script
#
# connect to the (austrian telekom) adsl line
# return 0 on full run, 1 if already connected, 2 if ANT down
#
# - test whether pppd is running,
# - test whether pptp is running,
# - test wheter ANT is up,
#   log failure
# - start pptp
#   log success

# set this to the ip-address of the ANT
ANT=10.0.0.138
# set this to some nonexistant file
TMP=/tmp/prozesse
# use /dev/null to disable logging
LOG=/var/log/adsl
# use /dev/console to watch progress
OUT=/dev/null

#
# how are we ?
#
if [ ! -f /var/run/ppp0.pid ]; then
        echo 'pppd is down' > $OUT
elif [ ! -S /var/run/pptp/$ANT ]; then
        echo 'pptp is down' > $OUT
else # everything seems fine
        exit 1
fi

echo -n 'testing ANT... ' > $OUT
ping -c 1 $ANT >/dev/null 2>&1
if [ "$?" != "0" ]; then
        echo $ANT 'not responding, turn on the ANT.' > $OUT
        # log this only once
        tail -1 $LOG | grep ANT
        if [ "$?" != "0" ]; then
                echo -n "ANT down - " >> $LOG; date >> $LOG
        fi
        exit 2
fi
echo 'is fine.' > $OUT

#
# clean up !
#
if [ -f /var/run/ppp0.pid ]; then
        echo -n 'shutting down pppd' > $OUT
        kill `cat /var/run/ppp0.pid`
        while [ -f /var/run/ppp0.pid ]; do
                echo -n '.' > $OUT
                sleep 1
        done
        echo '.' > $OUT
fi

if [ -S /var/run/pptp/$ANT ]; then
        echo -n 'shutting down pptp' > $OUT
        ps ax >$TMP
        kill `awk '/pptp: call manager/ { print $1 } ' $TMP`
        rm -f $TMP
        while [ -S /var/run/pptp/$ANT ]; do
                echo -n '.' > $OUT
                sleep 1
        done
        echo '.' > $OUT
fi

#
# connect !
#
echo -n 'bringing up pptp... ' > $OUT
        /usr/local/sbin/pptp $ANT >/dev/null 2>&1
wait
echo -n "PPTP up  - " >> $LOG; date >> $LOG
echo 'done.' > $OUT
#eof


