#!/bin/ksh
LOGFILE=/var/adm/log/asppp.log

# How long do we want to wait for PPP connection to be established 
# before we decide it's not gonna happen
TIMEOUT=40
 
quiet=0
provider=""
max_dials=1
typeset -i max_dials

setup_redial()
{
    if [ $quiet -eq 0 ] 
    then
       echo
       echo "Redialing....."
    fi
    id=`ps -e | grep aspppd | awk '{print $1}'`
    if test -n "$id"
    then
       kill -HUP $id
    fi
}

usage()
{
   echo "Usage: attach [-q|-r] [ISP name]"
   echo "   where  -q   Attach quietly"
   echo "          -r   Attempt dialing in $MAX_DIAL_ATTEMPTS times before givin
g up"
}

while [ $# -gt 0 ]
do
   case "$1" in
      -q )
          quiet=1
          ;;
      -r)
          max_dials=MAX_DIAL_ATTEMPTS
          ;;
      -h)
          usage
          exit 0
          ;;
       * )
          if [ "$provider" = "" ]
          then
             provider=$1
          else
             usage
             exit 1
          fi
          ;;
   esac
   shift
done
 
# Check UID
if [ "`/usr/ucb/whoami`" != "root" ]
then
   echo "Error: You must be root to execute `basename $0`"
   exit 1
fi
 
# By default we will connect to INFO2000
if [ "$provider" = "" ]
then
   provider="info2000"
fi
 
case "$provider" in
   info2000)
        isp_host=pm1
        ;;
      *)
        echo "Error: Unknown ISP ($provider)"
        exit 1
        ;;
esac
 
# Try to obtain a lock first
/opt/local/bin/ppplock
if [ $? -ne 0 ]
then
   if [ $quiet -ne 1 ]
   then
      echo "A PPP connection is already active"
   fi
   exit 2
fi
 
# First of all make sure we are in initialized state
if [ $quiet -eq 0 ]
then
   /opt/local/bin/detach -n $provider
else
   /opt/local/bin/detach -n -q $provider
fi
 
# Up the ipdptp interface
ifc_cmd=`egrep "^ifconfig.*$isp_host" /etc/asppp.cf`
ifc=`echo $ifc_cmd | awk '{print $2}'`
ifconfig $ifc up
 
redialing=1
no_dials=0
typeset -i no_dials
while [ $redialing -eq 1 ]
do
   #This ping will cause PPP to dial out
   ping $isp_host > /dev/null
 
   # We need to wait for the PPP interface to be configured. We will 
   # time it while we wait
   typeset -i timer
   timer=0
   tail -3 $LOGFILE | grep start_ip > /dev/null 2> /dev/null
   ppp_done=$?
   while [ $ppp_done -ne 0 ]
   do
      isp_busy=0
      sleep 1
      timer=timer+1
      if [ $quiet -eq 0 ] 
      then
         printf "\b\b$timer"
      fi
      if [ timer -gt $TIMEOUT ]
      then
         ppp_done=0
      else
         tail -3 $LOGFILE | grep start_ip > /dev/null 2> /dev/null
         ppp_done=$?
         if [ $ppp_done -ne 0 ]
         then
            tail -3 $LOGFILE | grep -i busy > /dev/null 2> /dev/null
            if [ $? -eq 0 ]
            then
               isp_busy=1
               ppp_done=0
               no_dials=no_dials+1
               if [ no_dials -ge max_dials ]
               then
                  redialing=0
               else
                  setup_redial
               fi
            fi
	 else
	    redialing=0
         fi
      fi
   done
done
if [ $quiet -eq 0 ] 
then
   echo
fi
ppp_config=`ifconfig $ifc`
my_ip=`echo $ppp_config | awk '{print $6}'`
 
# Check if PPP connection was successful. If it was, our IP address
# should not be 0.0.0.1 any more. For Solaris 2.4, look for 
# 0.0.0.0 instead.
if [ "$my_ip" = "0.0.0.1" ] 
then
   if [ $quiet -eq 0 ] 
   then
      echo "PPP connection failed"
      if [ $isp_busy -eq 1 ]
      then
         echo "Reason: ISP modems are busy"
      fi
   fi
   if [ $quiet -eq 0 ]
   then
      /opt/local/bin/detach $provider
   else
      /opt/local/bin/detach -q $provider
   fi
   exit 1
fi
 
# We have a successful PPP connection. Let us set up the routing table
if [ $quiet -eq 0 ]
then
   echo "My IP address set to $my_ip"
fi
 
route add default $isp_host 1 > /dev/null
# Add any other route you may need. Also you may need to set up your
# /etc/resolv.conf here depending upon your setup.
 
if [ $quiet -eq 0 ]
then
   echo "Routing table after manipulation:"
   netstat -r -n
fi
exit 0