#!/bin/csh
## This Shell script was written by:
##  Christian Holtje with help from Ben Galliart
##  <docwhat@uiuc.edu>           <bgallia@orion.it.luc.edu>
##
set LINE=cua3
set ALTLINE=ttyS3
set SLIPLOCK=/var/lock/slip
set SWITCH=
set DEBUG=0
set DYNAMIC=0
set TEMPFILE=/tmp/dip000.tmp
set PROG=`basename $0`

###############
#  Help Code 
###############
if ( $1 == "-h" || $1 == "-?" || $1 == "help" ) then	# Are we to help?
  echo "Usage: slip link  (installs the links below)
  echo "Usage: slipup [-h][-d][-v]"
  echo "       -h          This help"
  echo "       -d          Dynamic slip connection"
  echo "       -v          Verbose/Debugging"
  echo
  echo "Usage: slipdown [-h][f][-d <time>]"
  echo "       -h          This help"
  echo "       f           Force a slipdown"
  echo "       -w <time>   Amount of time to wait prior to slipping down"
  echo
  echo "Usage: sliplock [-h]"
  echo "       slipunlock [-h]"
  echo "       -h          This help"
  exit(0)
endif 						# End: Are we to help?


# This is for setups where you are always dynamic.
set DYNAMIC=1

################
# Slipup Code
################
if ( $PROG == "slipup" ) then			# Are we running slipup?

  ## Set up stuff...
  rm -f $TEMPFILE
  if ( `cat /etc/HOSTNAME` != `hostname` ) then
	hostname localhost
  endif

  if !( -f /var/lock/LCK..$LINE ) then		# Is the modem already busy?
    if ( "$1" == "-d" || "$2" == "-d" ) then
      set SCRIPT=/etc/nwu.dip
      #	set SCRIPT=/etc/uiuc.dynamic.dip
      set DYNAMIC=1
    else
      set SCRIPT=/etc/nwu.dip
      # set SCRIPT=/etc/uiuc.dip
    endif

    if ( "$1" == "-v" || "$2" == "-v" ) then
      set SWITCH=-v
      set DEBUG=1
    endif


    if ( $DEBUG == 1 ) then
      echo " Running Dip, using: "
      echo " script = $SCRIPT      switch = $SWITCH "
      /usr/bin/dip $SWITCH $SCRIPT 
    else
      /usr/bin/dip $SWITCH $SCRIPT > $TEMPFILE
      cat $TEMPFILE
    endif

    ## Show the user what's going on....
    ##
    sleep 4s

    ## The following are daemons and other services that should only
    ## be run while the slip is up
    /usr/bin/ping -i 400 ux4 >& /dev/null &

    ### Setup stuff for dynamic slip....
    if ( $DYNAMIC != 0 && $DEBUG == 0 ) then
      setenv INODE `awk '$3 == "iNode:" { print $4 }' $TEMPFILE`
      hostname `nslookup $INODE | grep Name: | cut -d":" -f2 | cut -d"." -f1`
    endif

    else
      echo "$PROG: Sorry, modem busy. "
    endif 					# End: Is the modem busy?

  exit(0)
endif						# End: Are we using slipup?




##################
#  Slipdown Code
##################
if ( $PROG == "slipdown" ) then  #Are we using slipdown?

  if ( -f $SLIPLOCK ) then
    if ( $1 != 'f' ) then
      echo "$PROG: Slipup was locked up. "
    else
      rm -f $SLIPLOCK
      echo " Removing slip lock. "
      slipdown $2
    endif
  else

  if ( -f /var/lock/LCK..$LINE ) then
    if  ($1 == '-w') then
      echo " Linux Box Ranma is going off net in $2 ! " | wall
      sleep $2
    endif
      echo " Linux Box Ranma is going off net NOW\!\!\!\!\! " | wall
      ## TERMinate processes used for slip.
      killall -15 ping
      killall -15 dip
      rm -f /var/lock/LCK..$LINE
      rm -f /var/lock/LCK..$ALTLINE
  else
      echo "$PROG: Modem is not in use. "
  endif 					# End: is modem being used?

  endif
  exit(0)
endif 						# End: are we using slipdown?



################
# SlipLock Code
################
if ( $PROG == "sliplock" ) then
  touch $SLIPLOCK
  echo "  Slip is locked."
  exit(0)
endif

##################
# SlipUnlock Code
##################
if ( $PROG == "slipunlock" ) then
  if !( -f $SLIPLOCK ) then
    echo "$PROG: Slip is not locked!"
  else
    rm -f $SLIPLOCK
    echo " Lock Removed. "
  endif
  exit(0)
endif

##################
# Slip link Code
##################
## This is to install the links for slip
if ( $PROG == "slip" ) then
  if ( $1 == "link" ) then
    set DIR = `dirname $0`
    ln -s $0 $DIR/slipup
    ln -s $0 $DIR/slipdown
    ln -s $0 $DIR/sliplock
    ln -s $0 $DIR/slipunlock
    echo The appropiate links have been made!
  exit(0)
endif


################
# Other Code
################
## This is if someone makes a wrong link or trys to run slip
##
echo "Usage: slip link  (installs the links below)"
echo "       slipup [-h][-d][-v]"
echo "       slipdown [-h][f][-w <time>]"
echo "       sliplock [-h]"
echo "       slipunlock [-h]"

#End of slip!