SYSLINUX=$IMGPATH/usr/bin/syslinux

if [ ! -f $SYSLINUX ]; then
    echo "$SYSLINUX doesn't exist"
    exit 1
fi

prepareBootImage() {
        dd if=/dev/zero bs=1k count=$BOOTDISKSIZE of=/$MBD_TMPIMAGE 2>/dev/null
	mkdosfs -r 16 -C $MBD_TMPIMAGE $BOOTDISKSIZE >/dev/null
	$SYSLINUX $MBD_TMPIMAGE
	mount -o loop -t msdos $MBD_TMPIMAGE $MBD_BOOTTREE

	(cd $BOOTDISKDIR; find . ! -name "*.msg" -maxdepth 1 ! -type d | cpio --quiet -p $MBD_BOOTTREE)
	
	cp $MBD_FSIMAGE $MBD_BOOTTREE/initrd.img
	cp $KERNELROOT/boot/vmlinuz-* $MBD_BOOTTREE/vmlinuz
	if [ -d $BOOTDISKDIR/$BOOTLANG ]; then
	    # fb console and kon don't get along...
	    if [ "$BOOTLANG" = "ja_JP" ]; then
		sed -e "s/ vga=[0-9]*//g" < $MBD_BOOTTREE/syslinux.cfg > $MBD_BOOTTREE/syslinux.cf2
		mv -f $MBD_BOOTTREE/syslinux.cf2 $MBD_BOOTTREE/syslinux.cfg
	    fi
	    if [ -n $BOOTDIR ]; then
		sed -e "s/initrd.img/initrd.img lang=$BOOTDIR/g" < $MBD_BOOTTREE/syslinux.cfg > $MBD_BOOTTREE/syslinux.cf2
		mv -f $MBD_BOOTTREE/syslinux.cf2 $MBD_BOOTTREE/syslinux.cfg
	    fi
	    mkdir /tmp/messages
	    for file in $BOOTDISKDIR/$BOOTLANG/*.msg; do
		filename=`basename $file`
		sed -e "s/@VERSION@/$VERSION/g" $file > /tmp/messages/$filename
	    done
	    for file in /tmp/messages/*.msg; do
		filename=`basename $file`
		sed -e "s/Red Hat/Fermi/g" $file > $MBD_BOOTTREE/$filename
	    done
	    /bin/rm -r /tmp/messages/
	    if [ $? != 0 ]; then
		echo $0: Failed to copy messages from $BOOTDISKDIR/$BOOTLANG to $MBD_BOOTTREE.
		umount $MBD_BOOTTREE
		rm -rf $MBD_BOOTTREE $MBD_TMPIMAGE
		exit 1
	    fi
	else
	    for file in $BOOTDISKDIR/*.msg; do
		filename=`basename $file`
		sed -e "s/@VERSION@/$VERSION/g" $file > $MBD_BOOTTREE/$filename
	    done
	    if [ $? != 0 ]; then
		echo $0: Failed to copy messages from $BOOTDISKDIR to $MBD_BOOTTREE.
		umount $MBD_BOOTTREE
#CJS just so that we can see what failed
#		rm -rf $MBD_BOOTTREE $MBD_TMPIMAGE
		exit 1
	    fi
	fi
}

mkdir -p $TOPDESTPATH/images
cat > $TOPDESTPATH/images/README <<EOF

The files in this directory are raw disk images.  Some are boot disks
for booting the Red Hat Linux installation program.  Others are driver
disks to support loading the installation program from less common
hardware.

Follow the instructions in the Red Hat Linux Installation Guide,
Chapter 2, section "Making Installation Diskettes" to create the
disks.

The drivers.img driver disk image has been split into several disks
because of the increasing size of the supported modules.

The following boot images images available:

boot.img - boot image for CD and hard drive based install
bootnet.img - boot image for network based install
pcmcia.img - boot image for installing on laptops

The following driver disks are available:

EOF

# LATEUSBMODS go in the second stage
USBMODS="usb-ohci usb-uhci hid keybdev usb-storage"
# usb-storge in both so we can reload it
LATEUSBMODS="mousedev usb-storage"
IDEMODS=""
SCSIMODS="sd_mod sr_mod"

FSMODS="msdos vfat ext3 reiserfs"
SECSTAGE="agpgart raid0 raid1 raid5 $IDEMODS $SCSIMODS $LATEUSBMODS $FSMODS"

COMMONMODULES="vfat $USBMODS"
LOCALMODULES="$COMMONMODULES BusLogic aic7xxx $IDEMODS $SCSIMODS 
             sym53c8xx megaraid"
NETWORKMODULES="$COMMONMODULES nfs 3c59x eepro100 tulip pcnet32
               ne2k-pci 8139too"

PCMCIAMAINMODULES="$COMMONMODULES nfs $IDEMODS $SCSIMODS"

makeinitrd --initrdto $TOPDESTPATH/dosutils/autoboot/initrd.img \
	   --initrdsize 2250 \
	   --loaderbin loader-local \
	   --modules "$LOCALMODULES"

makeinitrd --initrdto $TOPDESTPATH/images/pxeboot/initrd.img \
	   --initrdsize 2250 \
	   --loaderbin loader-network \
	   --modules "$NETWORKMODULES"

# XXX hack hack
PCMCIAMODULES_EXCLUDED="
	apa1480_cb
	iflash2+_mtd
	iflash2_mtd
	memory_cb
	memory_cs
	parport_cs
	parport_pc
	parport
	serial_cs
	serial_cb
	sram_mtd
"
PCMCIAMODULES_EXCLUDED_SED="sed"
for m in $PCMCIAMODULES_EXCLUDED
do
   PCMCIAMODULES_EXCLUDED_SED="$PCMCIAMODULES_EXCLUDED_SED -e 's/$m//g'"
done
PCMCIAMODULES=`echo $PCMCIAMODULES | eval "$PCMCIAMODULES_EXCLUDED_SED"`

makeinitrd --initrdto $TOPDESTPATH/images/initrd-pcmcia.img \
	   --pcmcia \
	   --initrdsize 2350 \
	   --loaderbin loader-pcmcia \
	   --modules "$PCMCIAMAINMODULES"

#for I in $BOOTDISKDIR; do
TIER1LANGS="de_DE|fr_FR|it_IT|es_ES|ja_JP"
for I in $BOOTDISKDIR `find $BOOTDISKDIR -type d | egrep "($TIER1LANGS)"`; do
    BOOTLANG=`basename $I`
    BOOTDIR=`basename $I | cut -d'_' -f1`

    if [ $BOOTLANG = "boot" ]; then
	BOOTLANG=""
	BOOTDIR=""
    fi

    makebootdisk --kernelto $TOPDESTPATH/dosutils/autoboot/vmlinuz  \
		 --bootdisksize 1440 \
		 --imagename $BOOTDIR/boot.img \
		 --initrd $TOPDESTPATH/dosutils/autoboot/initrd.img

    makebootdisk --kernelto $TOPDESTPATH/images/pxeboot/vmlinuz  \
		 --bootdisksize 1440 \
		 --imagename $BOOTDIR/bootnet.img \
		 --initrd $TOPDESTPATH/images/pxeboot/initrd.img

    makebootdisk --imagename $BOOTDIR/pcmcia.img \
	         --bootdisksize 1440 \
		 --initrd $TOPDESTPATH/images/initrd-pcmcia.img
done

rm -f $TOPDESTPATH/images/initrd-pcmcia.img

makedriverdisk --padsize 1440 "Supplemental Block Device Drivers" "drvblock" "+scsi"
makedriverdisk --padsize 1440 "Supplemental Network Drivers" "drvnet" "+net"

makedriverdisk --padsize 1440 "Supplemental CDROM controllers" "oldcdrom" "parport_pc parport +cdrom"
echo "Just made oldcdrom" 
makedriverdisk --padsize 1440 "PCMCIA Driver Diskette" "pcmciadd" "$PCMCIAMODULES"
echo "Just made pcmciadd" 
echo "now making dosutils/autoboot/initrd.img"
unset BOOTLANG
unset BOOTDIR
makeinitrd --initrdto $TOPDESTPATH/dosutils/autoboot/initrd.img \
	   --initrdsize 4100 \
	   --pcmcia \
	   --loaderbin loader-pcmcia \
	   --modules "$COMMONMODULES $PCMCIAMODULES =scsi $IDEMODS $SCSIMODS"
#	   --modules "$COMMONMODULES $PCMCIAMODULES nfs =scsi $IDEMODS $SCSIMODS" 
echo "now making dosutils/autoboot/vmlinuz"
makebootdisk --kernelto $TOPDESTPATH/dosutils/autoboot/vmlinuz  \
	     --bootdisksize 2880 \
	     --imagename ../dosutils/autoboot/cdboot.img \
	     --initrd $TOPDESTPATH/dosutils/autoboot/initrd.img

echo "now making pxeboot/initrd-everything.img"

makeinitrd --initrdto $TOPDESTPATH/images/pxeboot/initrd-everything.img \
	   --pcmcia \
	   --initrdsize 4000 \
	   --loaderbin loader-pcmcia \
	   --modules "$LOCALMODULES $NETWORKMODULES $PCMCIAMAINMODULES $PCMCIAMODULES =scsi =net"
echo "Just made images/pxeboot/initrd-everything.img"
makemainmodules "=scsi =net $SECSTAGE"
echo "just did makemainmodules"
makeinstimage "netstg" "=scsi $SECSTAGE"
makeinstimage "hdstg" "=net $SECSTAGE"
makemainimage "stage2" "cramfs"


cat > $TOPDESTPATH/images/pxeboot/README <<EOF
The files in this directory are useful for booting a machine via PXE.  

The following files are available:
vmlinuz - the BOOT kernel used for the installer
initrd.img - the regular network initrd used by the bootnet images
initrd-everything.img - an initrd with support for all install methods and
    drivers supported for installation of Red Hat Linux
EOF
