#!/bin/sh

sleepdef=90
sleeptime=$sleepdef
mailer=/bin/mail
maintainer=gad

while (echo) ; do
  if [ -f authtemp ] ; then
     mv authtemp authserv
     chmod +x authserv
  fi
  date
  ./authserv $*
  case $? in
    2) echo "There is a non-recognizable error, exiting"
       date | $mailer -s DEAD_AUTH $maintainer
       exit 1
       ;;
    4) echo "Waiting $sleeptime seconds for server to cycle"
       sleep $sleeptime
       sleeptime=`expr $sleeptime + $sleepdef + $sleepdef`
       if test $sleeptime -gt 3600; then
         date | $mailer -s STALLED_AUTH $maintainer
         sleeptime = 3600
       fi
       ;;
    8) echo "Waiting $sleepdef seconds to make sure server is up"
       sleeptime=$sleepdef
       sleep $sleepdef
       ;;
    *) echo $? `date` | $mailer -s STRANGE_EXIT $maintainer
       echo "Very strange exit code"
       echo "Waiting $sleeptime seconds to wait for things to clear up"
       sleep $sleeptime
       sleeptime=`expr $sleeptime \* 2`
       if test $sleeptime -gt 3600; then
         sleeptime = 3600
       fi
       ;;
  esac
done
