#!/sbin/sh
#
# Andrei.Ryjov@post.ch AKA aryzhov@excite.com
#
# start/stop net boot services
# 
# This script is to run on the boot server on remote sites.
#
# We don't want rarpd, tftpd, bootparamd and NFS stuff,
# except for short periods when the client is to be booted
#
#CPLATFORM=sun4m          # Temnorary setting for ss10
CPLATFORM=sun4u           # Waiting for Ultra-III surprises from Sun
UPLATFORM=SUN4U


NSLOOKUP=/bin/false
#NSLOOKUP=`/usr/sbin/ping 138.191.37.119 2 >&- 2>&- && echo /usr/sbin/nslookup || echo /bin/false`
JHOST=196.1.1.11
JHOME=/export/Jumpstart
REAL_TFTPDIR=$JHOME/config/tftpboot
ITF0=/etc/inet/inetd.conf
ITF1=$ITF0.pre-bootserver.`date '+%Y-%m%d-%H:%M'`
TFTPLINE="tftp    dgram   udp     wait    root    /usr/sbin/in.tftpd      in.tftpd -s /tftpboot"
MYADDR=`echo \`${NSLOOKUP} \\\`uname -n\\\` 2>&-\` | cut -d' ' -f8`
[ -z "$MYADDR"  ] && MYADDR=`grep loghost /etc/hosts | awk '{print $1}' | head -1`
[ -z "$MYADDR"  ] && {
  echo "
    Can not determine the IP address of this boot server - exiting
    exit 1
  "
}


[ `id | cut -d= -f2 | cut -d\( -f1` -ne 0 ] && { echo Must be root ; exit 1 ; }

INETD_PID=`/usr/bin/ps -e -u 0|/usr/bin/fgrep inetd |/usr/bin/awk '{print $1}'`
RARPD_PID=`ps -eflo pid,comm | grep in.rarpd   | /usr/bin/awk '{print $1}'`
BOOTP_PID=`ps -eflo pid,comm | grep bootparamd | /usr/bin/awk '{print $1}'`

#
# Kill the NFS services at both start and stop,
# but if only Jumpstart is shared. We shall start them later, if needed
#
[ -d $JHOME/media  -a  -z "`/usr/sbin/share 2>&- | grep -v \`basename $JHOME\``" ] && {
   echo Halting NFS services
   /usr/sbin/unshareall
   kill $MOUNTD_PID $NFSD_PID 2>&-
   #
   # Kill the NFS client services, if nothing is mounted remotely
   #
   [ -z "`/usr/sbin/mount | grep remote`" ] || kill $STATD_PID $LOCKD_PID 2>&-
}

case "$1" in

'restart')
	$0 stop
	$0 start
	;;

'start')

	HLIST="`egrep '^[0-9a-h]+:[0-9a-h]+:' /etc/ethers | awk '{print $2}' | cut -d. -f1 | sort | uniq`"
	CLIST="=`echo \`for i in $HLIST ; do echo $i $i.domain.ch ; done\` | sed 's/ /:/g'`"

	[ "$CLIST" = "=" ] && { echo Describe the client in /etc/ethers first ; exit 2 ; }
	NETZONE=`echo $MYADDR | cut -d. -f-2`
	[ -d $JHOME/root/POST-specific ] && JHOST=$MYADDR

	echo Enabling TFTP
	[ -d $REAL_TFTPDIR ] || mkdir -p $REAL_TFTPDIR || exit 2
	rm -f /tftpboot || exit 3
	ln -s $REAL_TFTPDIR /tftpboot
	#
	# In 138.191, we have to temporarily mount $JHOST
	# in order to steal the proper version of inetboot image
	# We can not get it from local host, as the Solaris release may be different from
	# the one to be booted
	#
	[ "$NETZONE" = 138.191   -a    ! -d $JHOME/media ] && {
	  STATD_PID=`ps -eflo pid,comm | grep nfs/statd  | /usr/bin/awk '{print $1}'`
	  LOCKD_PID=`ps -eflo pid,comm | grep nfs/lockd  | grep -v rpc |/usr/bin/awk '{print $1}'`
	  [ -z "$STATD_PID"  ]  && /usr/lib/nfs/statd
	  [ -z "$LOCKD_PID"  ]  && /usr/lib/nfs/lockd
	  mount -o ro ${JHOST}:$JHOME $JHOME
	}

	for PLATF in `cd $JHOME/root/usr/platform && ls -d sun*`  ; do
	    UPLATF=`echo $PLATF | tr "[:lower:]" "[:upper:]"`
	    cp $JHOME/root/usr/platform/$PLATF/lib/fs/nfs/inetboot /tftpboot/inetboot.$UPLATF
	done

	umount $JHOME 2>&-

	for i in $HLIST ; do
	  CIP=`echo \`${NSLOOKUP} $i 2>&-\` | cut -d' ' -f8`
	  [ -z "$CIP" ] && CIP=`grep $i /etc/hosts | awk '{print $1}' | head -1`
	  [ -z "$CIP" ] && {
	    echo "
	       IP Address for $i not found in hosts or DNS.
	       Please enter it here: \c"
	       read CIP
	       echo "$CIP\t\t$i\t$i.post.ch" >>/etc/hosts
	  }

	  HCIP=`echo $CIP | awk -F. '{printf("%02x%02x%02x%02x",$1,$2,$3,$4)}' | dd conv=ucase 2>&-`
	  [ -z "$CIP" -o -z "$HCIP" -o "$HCIP" = 00000000 ] && {
	    echo "
	      Incorrect or empty address for $i ...
	      Please snoop for TFTP broadcasts later
	      and add the link in /tftpboot by hand
	    "
	  }

	  rm -f /tftpboot/$HCIP /tftpboot/00000000

	  # Need a better UPLATFORM algorithm here - at least
	  # try to figure out by ethernet address... silly, I know
	  #
	  # EADDR=`cat /etc/ethers | awk '{if($2=="'"$i"'") print $1}'`
	  # Q31=`echo $EADDR | cut -d: -f3`
	  # DQ3=`expr \`echo $Q3 | cut -b1` \* 16
	  #
	  # if(
	  #

	  for l in "" ".${CPLATFORM}" ".${UPLATFORM}" ; do
	    [ -r /tftpboot/${HCIP}$l ] && continue
	    ln -s ./inetboot.${UPLATFORM} /tftpboot/${HCIP}$l
	  done

	done
	#
	# Modify inetd.conf and HUP the daemon
	#
	cp $ITF0 $ITF1
	{ grep -v in.tftpd $ITF1 ; echo "$TFTPLINE" ; } >$ITF0
	[ ! -z "$INETD_PID" ] && /usr/bin/kill -HUP ${INETD_PID}

	echo Starting RARP
	/usr/sbin/in.rarpd -a
	
	echo Starting BOOTPARAMD
	#
	# If the bootparams file exists, don't touch it.
	# But... make a copy, for logging at least
	#
	# Otherwise, create a new one. For 138.191.*.* the NFS server will be hcwe8l,
	# for 192.*, the NFS server is the machine where the script is being run
	#
	#
	cat /etc/bootparams >/etc/bootparams.`date +%y-%m-%d`

	[ ! -s /etc/bootparams ] && echo "\n*               \\
	  root=${JHOST}:${JHOME}/root                       \\
	  install=${JHOST}:${JHOME}/media                   \\
	  install_config=${JHOST}:${JHOME}/config           \\
	  sysid_config=${JHOST}:${JHOME}/config

	" >/etc/bootparams

	/usr/sbin/rpc.bootparamd

	STATD_PID=`ps -eflo pid,comm | grep nfs/statd  | /usr/bin/awk '{print $1}'`
	LOCKD_PID=`ps -eflo pid,comm | grep nfs/lockd  | grep -v rpc |/usr/bin/awk '{print $1}'`
	MOUNTD_PID=`ps -eflo pid,comm| grep nfs/mountd | /usr/bin/awk '{print $1}'`
	NFSD_PID=`ps -eflo pid,comm  | grep nfs/nfsd   | /usr/bin/awk '{print $1}'`

	[  -d $JHOME/media   -a   -z "`/usr/sbin/share 2>&- | grep -v $JHOME`" ] && {
	   echo Starting NFS services
	   [ -z "$STATD_PID"  ]  && /usr/lib/nfs/statd
	   [ -z "$LOCKD_PID"  ]  && /usr/lib/nfs/lockd
	   [ -z "$MOUNTD_PID" ]  && /usr/lib/nfs/mountd
	   [ -z "$NFSD_PID"   ]  && /usr/lib/nfs/nfsd -a 16
	   sleep 1
	   [ `uname -n | cut -d. -f1` = hcwe8l ] && CLIST=""
	   /usr/sbin/share -o ro${CLIST},anon=0 $JHOME
	} || {
	  echo "WARNING:
	    NFS was not restarted and Jumpstart directory not shared,
	    probably because machine may be running NFS for other filesystems.

	    There is also a possibility that NFS server is on another
	    machine or even another subnet.

	    Please restart NFS if necessary and share Jumpstart by hand with

	       share -o ro=CLIENT1:CLIENT2,anon=0 $JHOME

	  "
	}

	;;

'stop')
	#
	# NFS services are already killed before analysing the ARGV
	#
	echo Killing BOOTPARAMD
	[ ! -z "$BOOTP_PID" ] && /usr/bin/kill ${BOOTP_PID}

	echo Disabling tftpd
	cp $ITF0 $ITF1
	{ grep -v in.tftpd $ITF1 ; echo "# $TFTPLINE" ; } >$ITF0
	[ ! -z "$INETD_PID" ] && /usr/bin/kill -HUP ${INETD_PID}
	rm -f /tftpboot

	echo Killing RARP
	[ ! -z "$RARPD_PID" ] && /usr/bin/kill ${RARPD_PID}

	;;
*)
	echo "Usage: /etc/init.d/`basename $0` { start | stop }"
	;;

esac
exit 0
