#!/bin/bash

##############################################################################

INROUTE1=0
#INROUTE2=0

TRY=1
JOBID=0
OLDJOB=0

NAMESERVER1=$2
#NAMESERVER2=$3

DEVICE=/dev/modem
#DEVICE=/dev/ttyI1
#DEVICE=/dev/ttyS1
#DEVICE=/dev/cua1

HOMEDIR=~/.tkAutoPPP

##############################################################################

cat >${HOMEDIR}/ppp.tmp<<EOF
ABORT BUSY
ABORT "NO CARRIER"
ABORT ERROR
TIMEOUT 70
"" ATDT$1
CONNECT "" 
EOF

echo -n "ogin:--ogin: ">>${HOMEDIR}/ppp.tmp
cat<${HOMEDIR}/login>>${HOMEDIR}/ppp.tmp
echo -n " word: ">>${HOMEDIR}/ppp.tmp
cat<${HOMEDIR}/password>>${HOMEDIR}/ppp.tmp     

#############################################################
# ATTENTION: YOU'LL HAVE TO REPLACE THE                     #
# '/usr/sbin/chat -f ~/.tkAutoPPP/ppp.tmp'                  #
#                    ^^^^^^^^^^^^                           #
# PART IF YOU CHANGED THE DEFAULT INSTALLATION-DIRECTORY !  #
#############################################################
/usr/sbin/pppd debug crtscts defaultroute \
 ${DEVICE} 38400 connect '/usr/sbin/chat -f ~/.tkAutoPPP/ppp.tmp' \
 file ${HOMEDIR}/options


sleep 1
JOBID=`ps -a | grep "pppd"| grep -v grep | cut -b 1-6`
OLDJOB=$JOBID
# removing ppp.tmp since we don't need it anymore and 
# it contains secret data
rm ${HOMEDIR}/ppp.tmp



# while there exists a pppd-task and our
# providers ip is not in the routingtable
while [ $JOBID  ] ; do
   JOBID=`ps -a | grep pppd |grep -v grep | cut -b 1-6`
   INROUTE1=`netstat -rn|grep -c $NAMESERVER1`
#   INROUTE2=`netstat -rn|grep -c $NAMESERVER2`
   if [ ! $JOBID ] ; then

# echo 0 -> no Connect.
      echo 0
      exit 0
   fi

   if [ $INROUTE1 -gt 0 ] ; then
      echo 1
      exit 0
   fi
   if [ $INROUTE2 -gt 0 ] ; then
      echo 1
      exit 0
   fi

done

# Successful connected.
echo 1
