#!/bin/sh

# Name:		ppp-on
# Revision:	1.1.1
# Programmer	Bernie Karmilowicz (IntEn Corporation: karmilow@intencorp.com)
#
# History:	2006-01-08  1.1.1  Increased interface test delay to 5 seconds.
#		2003-08-31  1.1.0  Added printing of first comment line. 
#		2001-11-12  1.0.1  Added 15 second delay for modem reset.
#		2001-05-25  1.0.0  Created
#
# Purpose:	pppd dialup script which loops thru a list of chat script files
#		(containing connection parameters (phone numbers,
#		username/password pairs, etc)) until a successful connection is
#		obtained or all the listed chat script files have been tried.
#
# Usage:	ppp-on listname
#		  listname	- both the /etc/ppp/peers/ option file name, and the
#				  /etc/ppp/peers/ chat script file list name prefix.
#
#		ppp-on expects an option file (with the chatlist name) in
#		/etc/ppp/peers/, and one or more chat files (named as
#		listname0.chat, listname1.chat, ... , listname9.chat).
#
#		For example, to run thru a list of cheapestISP's local modem
#		numbers (one number per chat script file), use the command:
#
#		    ppp-on cheapestISP
#
#		thereby invoking pppd using the options in
#		/etc/ppp/peers/cheapestISP, and the chat script files
#		/etc/ppp/peers/cheapestISP[0-9].chat.
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#             
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#             
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


killall -INT pppd 2>/dev/null
rm -f /var/lock/LCK* /var/run/ppp*.pid

ISP=$1
echo "Calling $ISP..." 
set `ls /etc/ppp/peers/$ISP[0-9].chat`

while [ $1 ]
  do
    echo -n "  Trying $1"
    LOCATION=`grep "#" $1 | awk '{print $2$3$4}'`

    if [ $LOCATION != "" ] ; then
      echo -n " ("
      echo -n $LOCATION
      echo -n "): "
    else
      echo -n ": "
    fi

    /usr/sbin/pppd call $ISP connect "/usr/sbin/chat -f $1" 1>/dev/null

    while [ 1 ]
      do
        sleep 2
        ps -ew f | grep -A 1 "/usr/sbin/pppd call $ISP" | grep -v grep | grep "\_ /usr/sbin/chat" | grep -vq grep

        if [ $? -eq 0 ] ; then
          echo -n "."
        else
          break
        fi
      done

    sleep 5
    PPPIF=`grep "Using interface" /etc/ppp/pppsetup.txt | awk '{print $5}'`
    ifconfig | grep -q $PPPIF

    if [ $? -eq 0 ] ; then
      echo " success"
      /usr/bin/play /data1/sounds/ajtalks/aj1-6.wav &
      sleep 5
      exit 0
    else
      echo " failed. waiting 15 seconds for modem to reset."
      sleep 15
      shift
    fi
  done

exit 1
