#!/bin/sh
###############################################################################
##                                                                           ##
##                            make_toritoimage                               ##
##                                                                           ##
## This script takes the directory structure of _initrdfs/ (which represents ##
## the initial ramdisk initrd), gzips it and copies it on a floppy disk      ##
## mage. Then it prepares grub and writes the floppy image to                ##
## $ROOTFS/boot/boot.img.                                                    ##
## The Floppy image should then be a fully functional el torito boot image.  ##                                                                  ##
## The recipe to integrate grub into the set is heavily based on             ##
## information provided to me by Petr Konecny <pekon@informatics.muni.cz>.   ##
##                                                                           ##	  
## This program is free software; you can redistribute it and/or modify      ##
## it under the terms of the GNU General Public License as published by      ##
## the Free Software Foundation; either version 2 of the License, or         ##
## (at your option) any later version.                                       ##
##                                                                           ##
###############################################################################

. config_build
. "$SCRIPTS"/misc/lib/lib_fail

echo "----------------------------------"
echo "    creating boot floppy image    " 
echo "               grub               "
echo "----------------------------------"
echo 
echo

# this is here to override the FLOPPYSIZE parameter at runtime without
# the need of changing the config file.
# it's used by the utils/make_boot_disk script
if [ "$1" = 144 -o "$1" = 288 ]; then
    FLOPPYSIZE=$1
fi

# copy prepared 2.88MB floppy image file
cp "$SCRIPTS"/misc/files/grub_"$FLOPPYSIZE".img.gz $TMPDIR/grubfs.gz || fail
gunzip -f $TMPDIR/grubfs.gz || fail

losetup /dev/loop4 $TMPDIR/grubfs || fail
mount /dev/loop4 $MOUNTPOINT || fail
    
if [ "$CRAMFS" = "yes" ]; then
	cat "$SCRIPTS"/misc/files/grub_menu.lst                    \
		| sed "s|RAMSIZE|$RAMSIZE|;s|CRAMFS|cramfs|;s|CLOOP||" \
		> $MOUNTPOINT/grub/menu.lst
elif [ "$CLOOP" = "yes" ]; then
	cat "$SCRIPTS"/misc/files/grub_menu.lst                   \
		| sed "s|RAMSIZE|$RAMSIZE|;s|CRAMFS||;s|CLOOP|cloop|" \
		> $MOUNTPOINT/grub/menu.lst
else
	cat "$SCRIPTS"/misc/files/grub_menu.lst              \
		| sed "s|RAMSIZE|$RAMSIZE|;s|CRAMFS||;s|CLOOP||" \
		> $MOUNTPOINT/grub/menu.lst
fi

# copy kernel onto grubfs image
cp "$KERNEL"/vmlinuz $MOUNTPOINT || fail

# copy memtest86 image onto grubfs image
cp "$SCRIPTS"/misc/files/memtest86.bin $MOUNTPOINT/memtest.86 || fail

# move compressed initrdfs onto lilofs image
mv $TMPDIR/initrdfs.gz $MOUNTPOINT || fail
umount $MOUNTPOINT

losetup -d /dev/loop4
    
# move grubfs image to BURN_THIS/boot/boot.img
mv $TMPDIR/grubfs $ROOTFS/boot/boot.img

