#! /bin/sh

export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
umask 022

# Uncomment this stuff if you want a sulogin to be spawned from
# this script *before anything else* with a timeout, like on SCO.
#
echo
echo "Running /etc/init.d/boot..."
echo

echo mounting filesystems..
export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
CONFIG=/etc/nrclientconfig
rm -f /etc/mtab

mount -av

if test -e $CONFIG; then
  source $CONFIG
  if test "$SWAP" = "y"; then
    mount -t "$FSTYPE" "$SWAPFILEDEV" /mnt
    if test ! -f /mnt/"$SWAPFILENAME"; then
      dd if=/dev/zero of=/mnt/"$SWAPFILENAME" bs=1024 count="$SWAPFILESIZE" &&\
      mkswap /mnt/"$SWAPFILENAME" "$SWAPFILESIZE" &&\
      sync 
    fi
    swapon /mnt/"$SWAPFILENAME"    
  fi
else
  echo 'this is the first time this client boots.'
  echo 'will ask you some questions now, but they will'
  echo 'be saved to the server /tftpboot/$IP/etc/nrclientconfig'
  echo 'directory, so you will only need to do this once.'
  echo '  (if you want all later clients to have the config, do on the server:'
  echo ' cp /tftpboot/$IP/etc/nrclientconfig /etc/nfsroot/default/etc'
  echo
  while test "$mksw" != "1" -o "$mksw" != "1"  ; do
    echo Total current memory usage:
    free
    echo "Check memory usage above; total mem should be at least 6M (or more)"
    echo "Do you want to:"
    echo "  1 mount a partition (msdos or otherwise), and make a"
    echo "    swapfile on it"
    echo "  2 continue, don't create a swapfile"
    echo "  s shell"
    read mksw
    if test "$mksw" = "s"; then
      sh
    fi
  done
  case "$mksw" in
    "1")
	while test "$DEVERR" != "0" -a "$ANS" != "q"; do
          echo "Enter device you want to use for swapping, like /dev/hda1 (for DOS C:)"
	  echo "or /dev/hda2 for second partition on first disk, or /dev/hdb1 for second disk"
  	  echo  '(or "q" to quit mounting partition altogether) '
	  echo -n 'Devicename: '
	  read ANS
	  SWAPFILEDEV="$ANS"
	  dd if="$SWAPFILEDEV" of=/dev/null count=1
	  DEVERR=$?
	  if test "$DEVERR" != "0"; then
	    echo "something seems to be wrong with the device"
	    echo '(eigher kernel support hasnt been added for this device,'
	    echo "or the /dev entry doesn't exist on the server, or???)"
	  fi
        done
	DEVERR=noerr
	while test "$DEVERR" != "0" -a "$ANS" != "q"; do
	  echo "what's the filesystem type on this device?"
	  echo '(type "msdos" for MSDOS FAT; "q" to quit)'
	  echo -n "fs type: "
	  read ANS
	  FSTYPE="$ANS"
	  mount -v -t "$FSTYPE" $SWAPFILEDEV /mnt
	  echo  mount -v -t "$FSTYPE" $SWAPFILEDEV /mnt
	  DEVERR=$?
	  if test "$DEVERR" != "0"; then
            echo "something went wrong when mounting the fs -- please try again"
	  fi
	done
	if  test "$ANS" != "q"; then
	  df $SWAPFILEDEV
	  echo "memory usage:"
	  free
	  echo 'Please give a nice size for the swapfile ("q"= quit).'
	  read ANS
	  SWAPFILESIZE="$ANS"
	fi
	if  test "$ANS" != "q"; then
	  echo 'Please give a name for the swapfile ("q" = quit)'
	  echo ' (if its on a DOS disk C:\lin_swap, say "lin_swap" here)'
	  read ANS
	  SWAPFILENAME="$ANS"
	fi
	if  test "$ANS" != "q"; then
  	  dd if=/dev/zero of=/mnt/"$SWAPFILENAME" bs=1024 count="$SWAPFILESIZE" &&\
	  mkswap /mnt/"$SWAPFILENAME" "$SWAPFILESIZE" &&\
	  sync &&\
	  swapon /mnt/"$SWAPFILENAME"
	  DEVERR=$?
	fi
	if test "$ANS" != "q"; then
	  echo using:
	  echo "a filesystem of type $FSTYPE, mounted on $SWAPFILEDEV"
	  echo "with the swapfile $SWAPFILENAME of size $SWAPFILESIZE"
	  echo "do you want to save this to the file /tfpboot/IP$CONFIG"
	  echo 'on the server? (type "y" to save)'
	  read ANS
	  if test "$ANS" = "y" -o "$ANS" = "Y"; then
	    echo "#if you want the client to swap, say SWAP=y here" >$CONFIG
	    echo "SWAP=y"                                          >>$CONFIG
	    echo "#the device with the swapfile on it"             >>$CONFIG
	    echo "SWAPFILEDEV=$SWAPFILEDEV"                        >>$CONFIG
	    echo "FSTYPE=$FSTYPE"                                  >>$CONFIG
	    echo "SWAPFILESIZE=$SWAPFILESIZE"                      >>$CONFIG
	    echo "SWAPFILENAME=$SWAPFILENAME"                      >> $CONFIG
	  fi
	fi
      ;;
    *)
      echo "not using any swap. Do you want this option to be saved"
      echo "to the server's /tftpboot/IP/etc/$CONFIG, so that you"
      echo "will not be promted for this any more?"
      read ANS
      if test "$ANS" = "y" -o "$ANS" = "Y"; then
        echo "SWAP=n" > $CONFIG
      fi
      ;;
  esac
fi

rm -f /var/run/X* /var/run/*.pid
echo Obtaining hostname:
ipnumber=`/sbin/ifconfig eth0|sed -n -e "2s/inet addr://" -e "2s/ Bcast.*//p"`
name=`nslookup   $ipnumber SERVERIP|sed -n -e "4s/Name: *//p"`
hostname `echo $name |cut -d. -f1`

echo Starting inetd
/usr/sbin/inetd 

chmod a+wr /dev/{fd0,zero,null}
insmod lp
lpd

echo starting X
while /usr/X11R6/bin/X -indirect SERVERIP; do echo restarting X...; done

echo X failed, giving you a shell.
echo 

bash

