#!/bin/sh
#
# mknetboot.sh 0.9					31.3.03 (cp@fli4l.de)
#
# Creates a fli4l-bootimage for use with netboot/etherboot-compiled bootcode
#
# requires installed mknbi-linux! 
# Download:  http://netboot.sourceforge.net


# configuration parameters:
 
IMAGEFILE=/tftpboot/fli4l.img		# full path of netboot-Image
#ROUTER=""				# hostname or IP-Adress of your router
ROUTER="192.168.0.1"			# this will reset your router automatically if defined!
#APPEND="console=ttyS1,9600n8"		# options for the kernel


# you normally don't have to edit these:

TMPMOUNT=/tmp/mnt
TMPFILE=/tmp/tmpfile


####################  some configuration before we start  #########################

# determine fli4l version and set $config accordingly

MAJOR_VERSION=`eval head -c1 ./version.txt`
VERSION=`eval head -c3 ./version.txt`

if [ "$1" != "" ]
then
  echo "using configuration file/directory $1 ..."
  config=$1
else
  if [ $MAJOR_VERSION -ge 2 ]
  then
    config=config
  else
    config=config.txt
  fi
fi


# determine which flavour of mknbi-linux is installed (netboot or etherboot?)

mknbi_binary=`which mknbi-linux`
if [ ! "$mknbi_binary" ]
then
  echo !!!   mknbi-linux not installed   !!!
  echo !!! Please read the documentation !!!
  exit
elif [ "`file -L $mknbi_binary | grep "perl script"`" ]
then
  mknbiversion=etherboot
else
  mknbiversion=netboot
fi
   
# append "boot=none " to the kernel in order to signal "no mountable boot-device"
# to fli4l's /etc/rc

appendoption="boot=none "$APPEND


################################  make fli4l...  ################################# 

cd unix
make mkfli4l
cd ..

set -e						# exit on error

unix/mkfli4l $config


###############################  make netboot image  #############################


# how to determine the filesize

filesize ()
{
  filesize=`eval ls -s $1 | tr -d $1 | tr -d " "`
}


# create opt archive and determine it's size before and after compression

cd opt
rm -f ../img/opt.tar ../img/opt.tar.gz
tar -cvT opt.tmp -f ../img/opt.tar
rm -f opt.tmp
cd ../img
filesize opt.tar
OPTTARSIZE=$filesize

gzip -9 opt.tar
filesize opt.tar.gz
OPTTGZSIZE=$filesize


# mount original rootfs and determine it's size

cd ../src
./mount-rootfs.sh
filesize rootfs.img
OLDROOTSIZE=$filesize


# calculate size and nr. of inodes for new rootfs

if [ $MAJOR_VERSION = 1 ]
then
  let ROOTSIZE=($OLDROOTSIZE+$OPTTGZSIZE+$OPTTARSIZE)
else
  let ROOTSIZE=($OLDROOTSIZE+$OPTTGZSIZE)
fi
let INODES=($ROOTSIZE/4+1024)


# create new rootfs

mkdir -p $TMPMOUNT

dd if=/dev/zero of=$TMPFILE bs=1k count=$ROOTSIZE

if [ $MAJOR_VERSION = 1 ] || [ "$VERSION" = "2.0" ]
then
  mke2fs -F -N $INODES $TMPFILE
  mount -t ext2 -o loop $TMPFILE $TMPMOUNT
else
  mkfs -t minix -i $INODES $TMPFILE
  mount -t minix -o loop $TMPFILE $TMPMOUNT
fi

(cd rootfs; tar cf - .) | (cd $TMPMOUNT; tar xvf -)


# the all important step: put in the rootfs what is normally copied to the boot-device

cd ../img
if [ $MAJOR_VERSION = 1 ]
then
  mv opt.tar.gz $TMPMOUNT/mnt/opt.tgz
else
  cp ../opt/etc/rc.cfg $TMPMOUNT/boot
  mv opt.tar.gz $TMPMOUNT/boot/opt.tgz
fi


# tidy up

../src/umount-rootfs.sh
umount $TMPFILE
rmdir $TMPMOUNT


# create compressed image of the new rootfs

dd if=$TMPFILE bs=1k count=$ROOTSIZE | gzip -v9 > rootfsnetboot.gz
rm $TMPFILE


# create the network bootable image...

if [ $mknbiversion = "netboot" ]
then
  mknbi-linux -a "$appendoption" -d initrd -r rootfsnetboot.gz -k kernel -o $IMAGEFILE
else 
  mknbi-linux --append "$appendoption" --rootdir=/dev/ram kernel rootfsnetboot.gz > $IMAGEFILE
fi


# Finished! Reset the router if defined...

set +e			# do not exit if ping yields an error

echo
echo "Done!"
echo

if [ ! "$ROUTER" ]
then
  echo "Reset the fli4l-router now to activate your new setup."
else
  ping -c1 -w1 $ROUTER >/dev/null
  if [ $? = 1 ]
  then
    echo "Router $ROUTER not reachable."
    echo "Abort!"
    exit
  fi 
  cd ../unix
  [ ! -f imonc ] && make imonc
  ./imonc $ROUTER <<'  END'
  5
  y
  0
  END
  echo "Your router has been restarted to boot the new setup!"
fi
echo
