#! /bin/sh
#
# boot		boot-time system configuration.
#
# The first part of this script is written especially for the nfsroot
# clients, the second part is copied from the default debian /etc/init.d/boot
#  (although it's not very appropriate for these nfsroot clients, but what the heck)
CONFIG=/etc/nrclientconfig
PATH="/sbin:/bin:/usr/sbin:/usr/bin"


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 this config, do on the server:'
  echo ' cp /tftpboot/$IP/etc/nrclientconfig /etc/nfsroot/default/etc)'
  echo
  while test "$mksw" != "1" -a "$mksw" != "2"  ; 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



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`


#
#
#  Here starts the default debian boot stuff.
#
#
# on the nfsroot clients, /tmp is by default the server's /tmp.
# I don't think you want to clean out that.
#echo -n "Cleaning up /tmp, /var/run and /var/lock... "
#( cd /tmp && \
#  find . -xdev \
#  ! -ctime -$TMPTIME \
#  ! -name . \
#  ! \( -name lost+found -uid 0 \) \
#  ! \( -name quota.user -uid 0 \) \
#  ! \( -name quota.group -uid 0 \) \
#    -depth -exec rm -rf -- {} \; )
## Clean up any stale locks.

#/var/lock are the reason that (at least part of) /var must be different
# from the server's /var. This is why the default is no /var in fstab
#with var living on /tftpboot/$IP/var, there's no danger in these commands
( cd /var/lock && find . -type f -exec rm -f -- {} \; )
# Clean up /var/run and create /var/run/utmp so that we can login.
( cd /var/run && find . ! -type d -exec rm -f -- {} \; )
: > /var/run/utmp
echo "done."



# Save the random seed.
if [ -c /dev/urandom ]
then
	dd if=/dev/urandom of=/var/run/random-seed count=1 >/dev/null 2>&1
fi
 
# Run the package-specific boot scripts in /etc/rc.boot.
run-parts /etc/rc.boot

# Set pseudo-terminal access permissions.
# /dev is currently ro mounted, so this cannot be done.
#chmod 666 /dev/tty[p-za-e][0-9a-f]
#chown root.tty /dev/tty[p-za-e][0-9a-f]

# Update /etc/motd.
uname -a > /tmp/motd
sed 1d /etc/motd >> /tmp/motd
mv /tmp/motd /etc/motd

if [ -x /sbin/setup.sh ]
then
  /sbin/setup.sh
fi
