#!/bin/sh
# ver 0.7
# ifpoll, poll my boss node or the node given as argument 1
#
# Proccessed by LinuX Fido Point Packet V VERSION
#
# where point- and nodediffs coming in
INBOUND=/var/spool/fnet/inbound
OUTBOUND=/var/spool/fnet

# where "ifcico" and "ifpack" reside
FIDOPATH=/usr/local/lib/fnet

# logfile of ifcico
IFLOG=/var/adm/ifmail

# owner of "ifcico"
IFCICO_OWNER=USERNAME


# sysop of fido stuff
IFCICO_SYSOP=FULLNAME_FULLNAME1

# my boss node (default address to poll)
NODE="DEFAULTNODE.fidonet.org"

# how often should i try to call NODE?
MaxTry=100

# delay between outgoing calls in seconds
DELAY=60

# where to log processing - file or tty/console
INFO_TTY=/dev/tty7



echo "`date \"+%b %d %T\"` ifpoll[$$]: starting" >> $INFO_TTY

# remember me, not to run as root..
#
if [ `whoami` != "$IFCICO_OWNER" ]; then
	echo "*** run $0 as the owner of ifcico ***"
	echo "`date \"+%b %d %T\"` ifpoll[$$]: wrong uid (rc 2)" >> $INFO_TTY
	exit 2
fi

# argv[1] is the optional node to call
#
if [ "$1" != "" ]; then
	if [ "$1" = "-?" ] || [ "$1" = "-h" ]; then
		echo "usage: ifpoll [<node>]"
		exit 3
	else
		NODE=$1
	fi
fi

# let's pack the fido stuff..
#
$FIDOPATH/ifpack

# loop until ifcico could connect the node or MaxTry is encountered
#
i=1; errlv=1
while let 'i <= MaxTry' && let 'errlv != 0'
do
	echo -n "`date \"+%b %d %T\"` ifpoll[$$]: $i. try ($NODE) " >> $INFO_TTY
	#
	# start ifcico in master mode ..
	#
	$FIDOPATH/ifcico -r 1 $NODE
	errlv=$?
	if [ $errlv != "0" ]; then
		echo "failed" >> $INFO_TTY
		if [ $i != $MaxTry ]; then
			sleep $DELAY
		fi
		let i=i+1
	else
		echo "ok :)" >> $INFO_TTY
	fi
done

echo "`date \"+%b %d %T\"` ifpoll[$$]: finished (rc $errlv)" >> $INFO_TTY
#
# remove wrong outbound dirs
#
OLDDIR=$PWD
cd $OUTBOUND
#  rm -f -r *.[0-9]*
#
# processing node & pointdiffs
#
cd $INBOUND
if [ -f nodediff.* ]; then
  for i in nodediff.*
  do
    arc -x $i && rm $i
  done
fi
if [ -f pr24diff.* ]; then
  for i in pr24diff.*
  do
    arc -x $i && rm $i
  done
fi
if [ -f NODEDIFF.* ] || [ -f PR24DIFF.* ]; then
  fnlc
fi
cd $OLDDIR

# return the errorlevel of ifcico
exit $errlv













