SYSLINUX=$IMGPATH/usr/lib/syslinux/syslinux-nomtools

if [ ! -f $SYSLINUX ]; then
  echo "Warning: nomtools syslinux doesn't exist"
  SYSLINUX=$IMGPATH/usr/bin/syslinux
  if [ ! -f $SYSLINUX ]; then
    echo "$SYSLINUX doesn't exist"
    exit 1
  fi
fi

# prepare a directory with the kernel, initrd, and various message files
# used to populate a boot image
prepareBootTree() {
	(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
	pngtopnm $BOOTDISKDIR/syslinux-splash.png | ppmtolss16 \#000000=0 \#cdcfd5=7 \#c90000=2 \#ffffff=15 \#5b6c93=9 > $BOOTDISKDIR/splash.lss
	if [ $? != 0 ]; then
	   echo $0: Failed to create splash.lss
	   exit 1
	fi	
	cp $BOOTDISKDIR/splash.lss $MBD_BOOTTREE/splash.lss
	rm -f $MBD_BOOTTREE/syslinux-splash.png
	rm -f $MBD_BOOTTREE/memtest*
	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
	    rm -rf $MBD_BOOTTREE $MBD_TMPIMAGE
	    exit 1
	fi
}


# LATEUSBMODS go in the second stage.  
# FIXME: bleah, have to include sd_mod for usb-storage
USBMODS="ohci-hcd uhci-hcd ehci-hcd hid usb-storage sd_mod sr_mod"
# usb-storage goes on both so we can reload it.  *sigh*
LATEUSBMODS="usb-storage"
IDEMODS="ide-cd"
SCSIMODS="sd_mod sg sr_mod st"

FSMODS="msdos vfat ext3 reiserfs jfs xfs"
LVMMODS="dm-mod dm-zero dm-snapshot dm-mirror"
RAIDMODS="md raid0 raid1 raid5 raid6"
SECSTAGE="agpgart $RAIDMODS $LVMMODS $FSMODS $IDEMODS $SCSIMODS $LATEUSBMODS st parport_pc parport"

BTERMMODULES="vga16fb"
COMMONMODULES="vfat loop cramfs floppy edd lapic_status virtio_pci $USBMODS $BTERMMODULES"
LOCALMODULES="$COMMONMODULES aic7xxx megaraid_mbox sym53c8xx $IDEMODS $SCSIMODS"
NETWORKMODULES="$COMMONMODULES nfs 3c59x 
	       e100 tulip pcnet32 8139too"
#	       sis900 e100 hp100 natsemi"

PCMCIAMAINMODULES="$COMMONMODULES nfs $IDEMODS $SCSIMODS"

# images we only want on the CD (usually for space reasons)
ISOLINUXMODULES="ehci-hcd ieee1394 ohci1394 sbp2"

mkdir -p $IMAGEPATH/pxeboot

# 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
        aironet4500_cs
        orinoco_cs
        orinoco
        orinoco_old_cs
        wavelan_cs
        wvlan_cs
"
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"`

mkdir -p $IMAGEPATH/pxeboot

makeBootImages() {
  if [ "$kerneltag" != "xenU" ]; then
    echo "Building initrd-everything.img"
    makeinitrd --initrdto $IMAGEPATH/pxeboot/initrd.img \
	--pcmcia \
	--initrdsize 8192 \
	--loaderbin loader \
	--modules "$LOCALMODULES $NETWORKMODULES $PCMCIAMAINMODULES $PCMCIAMODULES $ISOLINUXMODULES =scsi =net"
    [ $? = 0 ] || exit 1

    if [ -f $IMGPATH/usr/lib/syslinux/isolinux.bin ]; then
	echo "Building isolinux directory"
	MBD_BOOTTREE=$TOPDESTPATH/isolinux
	MBD_FSIMAGE=$IMAGEPATH/pxeboot/initrd.img
	mkdir $MBD_BOOTTREE
	cp $IMGPATH/usr/lib/syslinux/isolinux.bin $MBD_BOOTTREE/isolinux.bin

	prepareBootTree

#Override with files in $SITE/build/isolinux/
        if [ -z $SITE ] ; then
            BUILDLOC=$DEFAULT
        else
            BUILDLOC=""
        fi
        if [ -s $TOPDESTPATH/$BUILDLOC/build/isolinux/boot.msg ] ; then
           cp $TOPDESTPATH/$BUILDLOC/build/isolinux/boot.msg $MBD_BOOTTREE
        fi
        if [ -s $TOPDESTPATH/$BUILDLOC/build/isolinux/isolinux.cfg ] ; then
           cp $TOPDESTPATH/$BUILDLOC/build/isolinux/isolinux.cfg $MBD_BOOTTREE
        fi
        if [ -s $TOPDESTPATH/$BUILDLOC/build/isolinux/syslinux.cfg ] ; then
           cp $TOPDESTPATH/$BUILDLOC/build/isolinux/syslinux.cfg $MBD_BOOTTREE
        fi
        if [ -s $TOPDESTPATH/$BUILDLOC/build/isolinux/splash.lss ] ; then
           cp $TOPDESTPATH/$BUILDLOC/build/isolinux/splash.lss $MBD_BOOTTREE
        fi
#end of override

	# isolinux needs the config file to be isolinux.cfg
	mv $MBD_BOOTTREE/syslinux.cfg $MBD_BOOTTREE/isolinux.cfg

	# copy in memtest if present
	if [ -f $IMGPATH/usr/lib/anaconda-runtime/boot/memtest* ]; then
           cp $IMGPATH/usr/lib/anaconda-runtime/boot/memtest* $MBD_BOOTTREE/memtest
           echo -e "label memtest86\n  kernel memtest\n  append -\n" >> $MBD_BOOTTREE/isolinux.cfg
	fi
    else
	echo "No isolinux binaries.  Skipping isolinux creation"
    fi

    # symlink the kernel for pxe dir
    ln $TOPDESTPATH/isolinux/vmlinuz $IMAGEPATH/pxeboot/vmlinuz

    # now make a boot iso
    mkdir -p $IMAGEPATH/isopath
    cp -rl $TOPDESTPATH/isolinux $IMAGEPATH/isopath
    mkisofs -quiet -o $IMAGEPATH/boot.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -V "$PRODUCT" -T $IMAGEPATH/isopath
    rm -rf $IMAGEPATH/isopath

    # now make a boot pendrive image
    mkdir -p $IMAGEPATH/fobpath
    dd if=/dev/zero of=$IMAGEPATH/diskboot.img bs=1M count=12 2>/dev/null
    mkdosfs $IMAGEPATH/diskboot.img
    $SYSLINUX $IMAGEPATH/diskboot.img
    mount -o loop,sync $IMAGEPATH/diskboot.img $IMAGEPATH/fobpath
    cp -r $TOPDESTPATH/isolinux/* $IMAGEPATH/fobpath/
    grep -v local $IMAGEPATH/fobpath/isolinux.cfg > $IMAGEPATH/fobpath/syslinux.cfg
    rm -f $IMAGEPATH/fobpath/isolinux.cfg
    sync; sync;
    umount -f $IMAGEPATH/fobpath
    rmdir $IMAGEPATH/fobpath

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

The following files are available:
vmlinuz - the kernel used for the installer
initrd.img - an initrd with support for all install methods and
    drivers supported for installation of $PRODUCT
EOF
  else # set up the boot stuff for the xen guest kernel
    echo "Building $KERNELARCH guest initrd.img"
    mkdir -p $IMAGEPATH/images/xen
    makeinitrd --initrdto $IMAGEPATH/images/xen/initrd.img \
	--initrdsize 8192 \
	--loaderbin loader \
	--modules  "$LOCALMODULES $NETWORKMODULES $PCMCIAMAINMODULES $BOOTPCMCIAMODULES $PCMCIAMODULES $ISOLINUXMODULES =scsi =net"
    [ $? = 0 ] || exit 1
    cp $KERNELROOT/boot/vmlinuz-*xen* $IMAGEPATH/images/xen/vmlinuz
  fi
}

makeSecondStage() {
    echo "Building main moduleball"
    makemainmodules "=scsi =net $SECSTAGE"
    [ $? = 0 ] || exit 1

    echo "Building netstg.img"
    makeinstimage "netstg" "=scsi $SECSTAGE"
    [ $? = 0 ] || exit 1
    echo "Building hdstg.img"
    makeinstimage "hdstg" "=net $SECSTAGE"
    [ $? = 0 ] || exit 1
    echo "Building stage2.img"
    makemainimage "stage2" "cramfs"
    [ $? = 0 ] || exit 1
}