#!/bin/sh
#
# startup.script.sample
#
#		This  should  show you,  how to keep an  IP connection
#		over a leased line (for example) up and alive.
#		It also takes care, that there are never two dip's try
#		to access  the same line  and  tries  to detect a died
#		dip.
#
#
# Version:	@(#)samples/startup.script.sample 1.02	1.July.1995
#
#
# Author:	Joachim Bartz, <injb@sun.rz.tu-clausthal.de>
#
#
#		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.
#			675 Mass Ave
#			Cambridge, MA 02139, USA
#

PIDFILE='sl0'

LOGFILE='/var/log/SLIP-'$PIDFILE'.log'

HOSTNM=`/bin/hostname`

DSCRIPT='/etc/dialout.script.sample'
SSCRIPT='/etc/setup.script.sample'

FPIDFILE='/var/run/dip.'$PIDFILE'.pid'


# Is there already a dip daemon for this setup running?
#
echo Looking for a dip daemon assigned to pid-file $FPIDFILE...
if [ -f $FPIDFILE ]
then
  echo -n Ooops, this pid-file exists.
  DIPSPID=`/bin/cat $FPIDFILE`
  echo ' 'Searching for pid $DIPSPID...

  LOOKUP=`/bin/ps | /usr/bin/grep $PIDFILE | /usr/bin/grep $DIPSPID`

  if [ "$LOOKUP" = "" ]
  then
    echo Seems there is no other dip daemon $PIDFILE up and running.
    echo -n Removing orphan $FPIDFILE
    /bin/rm -f $FPIDFILE
    echo ' 'done!

    OUTSTR=`/bin/date +'%b %e %X'`' '$HOSTNM
    OUTSTR1=$OUTSTR' DIP: Removed orphan pid file '$FPIDFILE
    OUTSTR1=$OUTSTR' DIP: Containing pid '$DIPSPID
    echo ''	  >> $LOGFILE
    echo $OUTSTR1 >> $LOGFILE
  else
    echo The dip daemon is already up and running.
    exit 1
  fi
else
  echo Not found.
fi


# Try it again and again...
# Until the system goes down or the shell gets killed or something
# like that.
#
while [ 1 = 1 ]
do

if [ "$LOOKUP" = "" ]
then
  # OK, prepare to start a new daemon
  #
  OUTSTR=`/bin/date +'%b %e %X'`' '$HOSTNM
  OUTSTR1=$OUTSTR' DIP: Initialization.'
  OUTSTR2=$OUTSTR' DIP: Dialout script : '$DSCRIPT
  OUTSTR3=$OUTSTR' DIP: Setup script . : '$SSCRIPT
  echo ''	>> $LOGFILE
  echo $OUTSTR1 >> $LOGFILE
  echo $OUTSTR2 >> $LOGFILE
  echo $OUTSTR3 >> $LOGFILE

  # Now start the dip program
  #
  #/sbin/dip  -f $PIDFILE  -s $SSCRIPT  $DSCRIPT
  /root/mydip/dip  -f $PIDFILE  $DSCRIPT

  # Wait a few seconds give the daemon a chance to establish
  # and write its pid to the file
  #
  /bin/sleep 30

  OUTSTR=`/bin/date +'%b %e %X'`' '$HOSTNM
  if [ -f $FPIDFILE ]
  then
    OUTSTR1=$OUTSTR' DIP: Started with pid '`/bin/cat $FPIDFILE`
  else
    OUTSTR1=$OUTSTR' DIP: Started with an unknown pid'
  fi
  echo $OUTSTR1	>> $LOGFILE

  # Test, if the new daemon is working
  #
  if [ ! -f $FPIDFILE ]
  then
    echo Something terrible happened... Dip is not running!
    echo Trying again in 60 seconds!
    /bin/sleep 60
  else
    DIPSPID=`/bin/cat $FPIDFILE`
    LOOKUP=`/bin/ps | /usr/bin/grep $PIDFILE | /usr/bin/grep $DIPSPID`
    if [ "$LOOKUP" = "" ]
    then
      echo Something terrible happened... Dip is not running properly!
      echo Trying again in 20 seconds!
      /bin/sleep 20
    else

      # Pooh, dip seems to run.
      # Now we go to bed and only have a frequently look, if all is
      # allright.
      #
      echo ''
      echo Connection established!

      while [ 1 = 1 ]
      do
	/bin/sleep 180

	if [ -f $FPIDFILE ]
	then
	  DIPSPID=`/bin/cat $FPIDFILE`
	  LOOKUP=`/bin/ps | /usr/bin/grep $PIDFILE | /usr/bin/grep $DIPSPID`
	  if [ "$LOOKUP" = "" ]
	  then
	    OUTSTR=`/bin/date +'%b %e %X'`' '$HOSTNM
	    OUTSTR1=$OUTSTR' DIP: Process '$DIPSPID' died? Trying to restart!'
	    echo $OUTSTR1 >> $LOGFILE
	    break
	  fi
	else
	  # If we wake up and the pid file is gone, this must be done
	  # by intention (dip -k).  So we leave the script via  exit.
	  #
	  OUTSTR=`/bin/date +'%b %e %X'`' '$HOSTNM
	  OUTSTR1=$OUTSTR' DIP: Process '$DIPSPID' terminated.'
	  echo $OUTSTR1 >> $LOGFILE
	  echo ''	>> $LOGFILE
	  exit 0
	fi
      done
    fi
  fi
fi

done
