#!/bin/bash 
############################################################################
## UUCP-Call via SLIP
## ------------------
## 
## Copyright (c) 1994 by Bodo Bauer
##  
##  Permission to use, copy, modify and distribute this software
##  and its documentation for any purpose is hereby granted without fee,
##  provided that the above copyright notice appear in all copies and
##  that both that copyright notice and this permission notice appear in
##  supporting documentation, and that may name is not used in
##  advertising or publicity pertaining to distribution of the software
##  without specific, written prior permission. I make no representations
##  about the suitability of this software for any purpose. It is
##  provided "as is" without express or implied warranty.
##
## $Id: uupoll,v 1.12 1994/05/26 20:24:05 root Exp $
############################################################################

############################################################################
## Etwas Hilfe...
############################################################################

givehelp () {
echo "Options:"
echo " 	-v          Be verbose an say what's going on  "
echo " 	-s <system> Connect to <system>"
echo " 	-x <n>      Give uucico option -x <n>"
echo "	-g          Do not hangup after uucico has finished"
echo "	-t          Poll via TCP/IP and use dip to connect"
echo "	            to the remote system"
echo " 	-f <file>   Use 'dip <file>' to set up SLIP"
echo "	-q          Be quiet, don't even display error messages"
echo "	-d <n>      Wait n seconds between two tries (defaults to"
echo "	            30 seconds)"
echo "	-m <n>      Set max. number of tries to n (default is a"
echo "	            maximum of 10 tries)"
exit -1;
}

############################################################################
## weitere Funktionen....
############################################################################

output () {
	if ${verbose}
	then 
		echo "`basename $0`: [`date +"%d.%m.%y %H:%M"`] $*"
	fi
}

error () {
	if ${quiet}
	then
		:
	else
		echo "`basename $0`: [`date +"%d.%m.%y %H:%M"`] ERROR -> $*" >& 2
	fi
}

##
## set up IP connection
##
setupIP() {
	succ=true
	output Setting up IP connection to system ${system}...

	##
	## Ist ein passendes Dip-Skript da?
	##
        if [ -f "${dippath}/${filename}.dip" ]
        then
                file=${dippath}/${filename}.dip
        else
                if [ -f "${dippath}/${system}.dip" ]
                then
                        file=${dippath}/${system}.dip
                fi
        fi

	if [ -f "${dippath}/${system}.dip" ]
	then
		output  "found Dip-Skript ${file}"

		##
		## Skript starten. Es sollte bei geglueckter Verbindung den String 
		## 'CONNECTED' ausgeben!
		##

		output "Execute \"$dip $file\""

		${dip}  $file > /tmp/slp.$$ 2>&1

		if cat /tmp/slp.$$ | grep CONNECTED  > /dev/null 2>&1
		then
			output "CONNECTED to ${system}"
			rm /tmp/slp.$$ > /dev/null 2>&1
		else
			rm /tmp/slp.$$ > /dev/null 2>&1
			error "Connection to ${system} failed!"
			succ=false
		fi

	else
		error "Dip-Skript $file not found"
		succ=false
		exit -1
	fi

}

##
## close IP connection
##
closeIP () {
	##
	## SLIP-Verbindung wieder kappen
	##
	output "Kill dip with: \"${dip} -k\""
	${dip} -k > /dev/null 2>&1
}

## 
## UUCP-Call durchfuehren
##
uucall () {
	succ=true
	##
	## Jetzt uucico aufrufen und Poll-Vorgang starten
	##

	output "Execute \"${uucico} -D -S${system}\""

	su uucp -c "${uucico} -D -S${system} > /dev/null" > /dev/null 2>&1
}

##
## war UUCP-Call erfolgreich?
##
uucheck () {
	if ${uustat} -m | grep ${system} | grep "SUCCESSFUL" > /dev/null 2>&1
	then
		succ=true
	else
		error "uucico failed"
		succ=false
	fi
}

############################################################################
## Vorbelegungen
############################################################################
dip=/sbin/dip 
dippath=/etc/dip
uucico=/usr/lib/uucp/uucico
uustat=/usr/bin/uustat
succ=false
goon=true
try=0

verbose=false
system=xxxx
filename=xxxx
file=xxxxx
hangup=true
tcp=false
quiet=false
delay=30
maxtry=10

version='$Revision: 1.12 $'
############################################################################
## Optionen parsen
############################################################################

while getopts m:d:tx:vs:f:hg option
do
	case "$option"
	in
		v)	verbose=true;;
		s)	system=$OPTARG;;
                f)      filename=$OPTARG;;
		x)      uucico="${uucico} -x$OPTARG";;
		h)	givehelp;;
		g)      hangup=false;;
		t)      tcp=true;;
		q)      quiet=true;;
		d)      delay=$OPTARG;;
		m)      maxtry=$OPTARG;;
		\?)	givehelp;;
	esac
done

############################################################################
## Das Hauptprogramm
############################################################################

if [ "${system}" = "xxxx" ]
then
	givehelp
fi

while ${goon}
do
	output "prepare for try #${try}"
	if ${tcp}
	then
		setupIP
	else
		succ=true
	fi

	if ${succ}
	then
	
		uucall

		if ${tcp}
		then
			if ${hangup}
			then
				closeIP
			else
				output "Holding the line..."
			fi
		fi
	
		uucheck
		if ${succ}
		then
			goon=false
		else
			goon=true
		fi
	else
		if ${tcp}
		then		
				closeIP
		fi
	fi

	if ${goon}
	then
		try=$[$try+1]
		if [ $try -eq ${maxtry} ]
		then
			goon=false
			error "max. number of tries reached"
			succ=false
		else

			if ${goon}	
			then
				output "next try in ${delay} seconds."
				sleep ${delay}
			fi
		fi
	fi
done

##
## Status-Meldung ausgeben
##
output `${uustat} -m | grep ${system}`

if   ${succ}
then
	output "polling succeeded!"
	exit 0
else
	error "polling failed!"
	exit -1
fi

