#!/bin/sh
#Zu beachten:

#Ausgaben mit echo muessen auf /dev/console umgeleitet werden, sonst gehen
#sie zum Modem und stoeren die Kommunikation.

#Ausgaben von chat -V auf stderr muessen wohl auch auf /dev/console geleitet
#werden?

#Zwischen dem Scheitern der Anwahl von Nummer 1 und der Anwahl von Nummer 2
#muss gewartet werden, sonst erkennt das Modem das Kommando nicht!

#Niemals einen zu kleinen Timeout-Wert fuer chat setzen. Kommt die
#Leitung naemlich tatsaechlich zustande, dann kann das Modem die
#Verbindung gar nicht mehr aufbauen, da chat auflegt. Eine Einheit ist
#dann natuerlich verschwendet.


###################################################################
#
# These parameters control the attack dialing sequence.
#
# Maximum number of attempts to reach the telephone number(s)
MAX_ATTEMPTS=100

# Delay between each of the attempts. This is a parameter to sleep
# so use "15s" for 15 seconds, "1m" for 1 minute, etc.
SLEEP_DELAY=2s

###################################################################
#
# This is a list of telephone numbers. Add new numbers if you wish
# and see the function 'callall' below for the dial process.
PHONE1=280797
PHONE2=282159

###################################################################
#
# If you use the ppp-on script, then these are passed to this routine
# automatically. There is no need to define them here. If not, then
# you will need to set the values.
#
ACCOUNT=ug201cj
PASSWORD=Mar95te

###################################################################
#
# Function to initialize the modem and ensure that it is in command
# state. This may not be needed, but it doesn't hurt.
#
initialize ()
{
    echo -e "\nInitializing modem ..." >/dev/console
    chat -V TIMEOUT 3 '' ATZ 'OK-+++\c-OK' 2>/dev/console
    return
}

###################################################################
#
# Script to dial a telephone
#
callnumber ()
{
chat	-V						\
	ABORT		BUSY				\
	ABORT		'NO ANSWER'			\
	ABORT		'NO CARRIER'			\
	''		ATDT$1				\
	CONNECT		''				\
	ogin:--ogin:	$ACCOUNT			\
	assword:	$PASSWORD			\
	punkt:		5				\
2>/dev/console

#
# If the connection was successful then end the whole script with a
# success.
#
    if [ "$?" = "0" ]; then
       exit 0
    fi

    return
}

###################################################################
#
# Script to dial any telephone number
#
callall ()
{
   echo -e "\ndialing attempt number: $1" >/dev/console
   callnumber $PHONE1
   sleep "$SLEEP_DELAY"
   callnumber $PHONE2
}

###################################################################
#
# Initialize the modem to ensure that it is in the command state
#
initialize
if [ ! "$?" = "0" ]; then
   exit 1
fi

#
# Dial telephone numbers until one answers
#
attempt=0
while : ; do
    attempt=`expr $attempt + 1`
    callall $attempt
    if [ "$attempt" = "$MAX_ATTEMPTS" ]; then
	exit 1
    fi	
    sleep "$SLEEP_DELAY"
done
