#!/bin/sh
#
# DipEye:	This shell script attempts to keep dip online.
#
# Version:	@(#)/usr/local/bin/dipeye	0.65	11/04/94
#
# Author:	Adam Dace, <glide@mcs.com>

# Default files
DIP="/sbin/dip"
DIP_LOG="/var/adm/dipeye.log"
HOST="mercury.mcs.com"
MOUNT_WUARCHIVE="/bin/mount -o ro,noexec,nosuid wuarchive.wustl.edu:/archive /wuarchive"
PING="/bin/ping -c 1"
POP_MAIL="/usr/local/bin/popclient -3 -s -u glide -p password -o /var/spool/mail/thekind mercury.mcs.com"
SPARE_VT="/dev/tty11"
TRACEROUTE="/usr/bin/traceroute"

# The wonderful world of flags
typeset -i ONLINE
ONLINE=0

start_dip()
{
  # First off, if there are any copies of dip or ping currently running,
  # kill them.

  PID=`ps ax | grep "/sbin dip /home/thekind/bin/mcs.dip" | grep -v grep | cut -c -5`
  if [ "X${PID}" != "X" ]
  then
    kill ${PID}
    sleep 30s
  fi

  # Now boot dip.
  $DIP /home/thekind/bin/mcs.dip &> /dev/null &

  # Wait for dip to try connecting.
  sleep 7m

  # Did dip succeed?
  CONNECT=`$PING $HOST 2>&1 | tail +2 | grep "64 bytes"`
  if [ "X${CONNECT}" != "X" ]
  then
    ONLINE=1
    SERVER=`$TRACEROUTE $HOST 2>&1 | tail +4 | cut -c 5-21`
    echo -n "Successful DIP To " >> $DIP_LOG
    echo -n $SERVER >> $DIP_LOG
    echo -n ":	 	" >> $DIP_LOG
    date >> $DIP_LOG
    echo -n "Successful DIP To " >> $SPARE_VT
    echo -n $SERVER >> $SPARE_VT
    echo -n ":		" >> $SPARE_VT
    date >> $SPARE_VT

    # Start up various services
    /usr/sbin/netdate $HOST &> /dev/null
    $POP_MAIL &> /dev/null
    $MOUNT_WUARCHIVE &> /dev/null
else
    ONLINE=0
    echo -n "UnSuccessful DIP Attempt:				" >> $DIP_LOG
    date >> $DIP_LOG
    echo -n "UnSuccessful DIP Attempt:				" >> $SPARE_VT
    date >> $SPARE_VT

    # Hunt down my dip process and zaps it.
    PID=`ps ax | grep "/sbin dip /home/thekind/bin/mcs.dip" | grep -v grep | cut -c -5`
    if [ "X${PID}" != "X" ]
    then
      kill ${PID}
    fi
  fi

}

# Keep an eye on dip, testing every 5 minutes to make sure it's up
watch()
{
  while [ $ONLINE -eq 1 ]
  do
    PID=`ps ax | grep "/sbin dip /home/thekind/bin/mcs.dip" | grep -v grep | cut -c -5`
    if [ "X${PID}" = "X" ]
    then 
      ONLINE=0
      echo -n "DIP Connection Faliure:				" >> $DIP_LOG
      date >> $DIP_LOG
      echo -n "DIP Connection Faliure:				" >> $SPARE_VT
      date >> $SPARE_VT
      # Umount the now non-existant NFS filesystems
      /bin/umount /wuarchive &> /dev/null
      /bin/umount /ais_net &> /dev/null
    else
      # I like my mail checked about every 5 minutes ;)
      # "Ping-Bot?  What Ping-Bot?"
      $POP_MAIL &> /dev/null
      sleep 5m
    fi
  done
}

#---------------------------------
#main
#---------------------------------

# Check to see if dip exists and boot it if it does.  If not, exit.
if [ -f $DIP ]
then start_dip
else
  echo "Can't find Dip!  Exiting..."
  exit 1
fi

# If dip doesn't connect the first time, wait 5 minutes and retry.
if [ $ONLINE -eq 0 ]
then
  sleep 5m
  start_dip
fi

# Go into an infinite loop to maintain dip, waiting 1, and then 15 minutes
# between failures.
while true
do
  watch
  sleep 1m
  start_dip

  if [ $ONLINE -eq 0 ]
  then
    sleep 15m
    start_dip
  fi
done
