#
# mk-images.x86
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

SYSLINUX=$IMGPATH/usr/share/syslinux/syslinux-nomtools
CDLABEL=$PRODUCT

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 . -maxdepth 1 ! -name "*.msg" ! -type d | cpio --quiet -p $MBD_BOOTTREE)
	
	ln $MBD_FSIMAGE $MBD_BOOTTREE/initrd.img
	cp $KERNELROOT/boot/vmlinuz-* $MBD_BOOTTREE/vmlinuz
	cp $KERNELROOT/boot/xen-[0-9]* $MBD_BOOTTREE/xen.gz

	if [ -f $IMGPATH/usr/lib/anaconda-runtime/syslinux-vesa-splash.jpg ]; then
		cp $IMGPATH/usr/lib/anaconda-runtime/syslinux-vesa-splash.jpg $MBD_BOOTTREE/splash.jpg
		cp $IMGPATH/usr/share/syslinux/vesamenu.c32 $MBD_BOOTTREE/vesamenu.c32
		sed -i s'/default linux/default vesamenu.c32/g' $MBD_BOOTTREE/syslinux.cfg
		sed -i 's/prompt 1/#prompt 1/g' $MBD_BOOTTREE/syslinux.cfg
	elif [ -x $IMGPATH/usr/lib/anaconda-runtime/splashtolss.sh ]; then
		$IMGPATH/usr/lib/anaconda-runtime/splashtolss.sh $BOOTDISKDIR/syslinux-splash.png $BOOTDISKDIR/splash.lss
		if [ $? != 0 ]; then
		    echo $0: Failed to create splash.lss
		    exit 1
		fi    
		cp $BOOTDISKDIR/splash.lss $MBD_BOOTTREE/splash.lss
	elif [ -f $IMGPATH/usr/lib/anaconda-runtime/splash.lss ]; then
		cp $IMGPATH/usr/lib/anaconda-runtime/splash.lss $MBD_BOOTTREE/splash.lss
	fi

	rm -f $MBD_BOOTTREE/syslinux-splash.png
	sed -i "s/@PRODUCT@/$PRODUCT/g" $MBD_BOOTTREE/syslinux.cfg
	sed -i "s/@VERSION@/$VERSION/g" $MBD_BOOTTREE/syslinux.cfg

	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
}

makeBootImages() {
  local initrd="initrd.img"
  local kernelimage="vmlinuz"
  local xenimage="xen.gz"

  echo "Building $initrd"
  makeinitrd --initrdto $TOPDESTPATH/images/pxeboot/$initrd \
	--initrdsize 8192 \
	--loaderbin loader \
	--modules "$INITRDMODS"
  [ $? = 0 ] || exit 1

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

	prepareBootTree

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

	# set up the label for finding stage2 with a hybrid iso

	# insert XEN boot options
	sed -i 's/kernel vmlinuz/kernel mboot.c32/' $MBD_BOOTTREE/isolinux.cfg
	sed -i "s/append initrd=[^ ]*\(.*\)/append $xenimage --- $kernelimage stage2=hd:LABEL=\"$CDLABEL\" rdblacklist=aesni-intel\1 --- $initrd/" $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  menu label ^Memory test\n  kernel memtest\n  append -\n" >> $MBD_BOOTTREE/isolinux.cfg
	fi
  else
	echo "No isolinux binaries.  Skipping isolinux creation"
  fi

  if [ -n "$BOOTISO" ]; then echo "boot.iso = images/$BOOTISO" >> $TOPDESTPATH/.treeinfo ; fi    
}


doPostImages() {
    if [ -n "$BOOTISO" ]; then
        EFIARGS=""
        EFIGRAFT=""
        if [ -f $TOPDESTPATH/images/efiboot.img ]; then
            echo "Found efiboot.img, making an EFI-capable boot.iso"
            EFIARGS="-eltorito-alt-boot -e images/efiboot.img -no-emul-boot"
            EFIGRAFT="EFI/BOOT=$TOPDESTPATH/EFI/BOOT"
        else
            echo "No efiboot.img found, making BIOS-only boot.iso"
        fi
        BIOSARGS="-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
        mkisocmd="mkisofs -v -o $TOPDESTPATH/images/$BOOTISO $BIOSARGS $EFIARGS -R -J -V '$CDLABEL' -T -graft-points isolinux=$TOPDESTPATH/isolinux images=$TOPDESTPATH/images $EFIGRAFT"
        echo $PWD:\$ $mkisocmd
        eval $mkisocmd
        if [ -x /usr/bin/isohybrid ]; then
            isohybrid $TOPDESTPATH/images/$BOOTISO || echo "Unable to make hybrid boot.iso"
        fi
	implantisomd5 $TOPDESTPATH/images/$BOOTISO
    fi
}