# This script gets sourced from boot-hurd-i386.
#
# Do install stuff for hurd, including making bootable CDs
# Works with debian-installer
#
# $1 is the CD number
# $2 is the temporary CD build dir

. $BASEDIR/tools/boot/$DI_CODENAME/common.sh
. $BASEDIR/tools/boot/$DI_CODENAME/x86-desktop.sh

set -e
#set -x

# Have we been told to use just one boot method? If none listed,
# assume all are desired. Order also matters. Possible values are
# "BIOS", "EFI" and "BIOS EFI". We'll also accept "EFI BIOS" for
# completeness, but we will switch the order.
if [ "$BOOT_METHODS"x = ""x ] \
    || [ "$BOOT_METHODS"x = "BIOS EFI"x ] \
    || [ "$BOOT_METHODS"x = "EFI BIOS"x ] ; then
    BOOT_BIOS=1
    BOOT_EFI=2
elif [ "$BOOT_METHODS"x = "BIOS"x ] ; then
    BOOT_BIOS=1
    BOOT_EFI=0
elif [ "$BOOT_METHODS"x = "EFI"x ] ; then
    BOOT_EFI=1
    BOOT_BIOS=0
else
    echo "ERROR: Unrecognized boot method choice $BOOT_METHODS"
    exit 1
fi

N=$1
CDDIR=$2
INSTALLDIR="install"

# Common options for all disks
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-J -joliet-long"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-cache-inodes"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-l"

# Exit if this is not CD#1/DVD#1
if [ $N != "1" ]; then
    exit 0
fi

if [ "$DI_WWW_HOME" = "default" ]; then
    DI_WWW_HOME="https://d-i.debian.org/daily-images/hurd-i386/daily/"
    try_di_image_cache
else
    DI_WWW_HOME=$(echo $DI_WWW_HOME | sed "s,%ARCH%,$ARCH,")
fi

case "$MKISOFS" in
    *xorriso*)
        XORRISO_VER=$(xorriso_version)
        ;;
    *)
	echo "ERROR: debian-cd now depends on xorriso for making x86 bootable CDs."
	exit 1;
	;;
esac

cd $CDDIR/..

# Download boot images.
BOOT_IMAGES="cdrom/debian-cd_info.tar.gz cdrom/gnumach.gz cdrom/initrd.gz cdrom/gtk/initrd.gz cdrom/ext2fs.static cdrom/exec.static"

for image in $BOOT_IMAGES; do
       if [ ! -e "$image" ]; then
               dir=$(dirname $image)
               mkdir -p $dir
               if [ ! "$DI_WWW_HOME" ];then
                       if [ ! "$DI_DIR" ];then
                               DI_DIR="$MIRROR/dists/$DI_DIST/main/installer-$ARCH/current/images"
                       fi
                       cp "$DI_DIR/$image" $image
               else
                       $WGET "$DI_WWW_HOME/$image" -O $image
               fi
       fi
done

# Install kernel and initrd
mkdir -p $CDDIR/boot/kernel/
mkdir -p $CDDIR/boot/gtk/
cp "cdrom/gnumach.gz" "$CDDIR/boot/kernel/gnumach.gz"
cp "cdrom/initrd.gz" "$CDDIR/boot/initrd.gz"
cp "cdrom/gtk/initrd.gz" "$CDDIR/boot/gtk/initrd.gz"
cp "cdrom/ext2fs.static" "$CDDIR/boot/kernel/ext2fs.static"
cp "cdrom/exec.static" "$CDDIR/boot/kernel/exec.static"

# Install bootloader
mkdir -p boot$N
tar -C boot$N -zxf cdrom/debian-cd_info.tar.gz
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-J -joliet-long"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-b boot/grub/grub_eltorito"
[ -f boot$N/boot/grub/grub_embed ] && add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "--embedded-boot boot$N/boot/grub/grub_embed"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-c boot/boot.cat"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-no-emul-boot"

bls=4 # Specify 4 for BIOS boot, don't calculate it
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-boot-load-size $bls"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-boot-info-table"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-cache-inodes"
add_mkisofs_opt $CDDIR/../$N.mkisofs_dirs "boot$N"

# Add autorun
if [ -f $CDDIR/README.html ]; then
       todos > $CDDIR/autorun.inf <<EOF
[autorun]
open=autorun.bat
label=Install Debian GNU/Hurd
EOF
       todos > $CDDIR/autorun.bat <<EOF
@echo Starting "README.html"...
@start README.html
@exit
EOF
fi

# Cleanup
rm -rf cdrom

if [ $BOOT_EFI -ne 0 ] ; then
    echo "  Adding EFI boot code for $ARCH on CD$N"

    # Move GRUB files to the right place.
    mkdir -p $CDDIR/EFI/boot
    mcopy -n -s -i boot$N/grub/efi.img '::efi/*' $CDDIR/EFI
    mkdir -p $CDDIR/boot/grub
    mv boot$N/grub/* $CDDIR/boot/grub/
    rmdir boot$N/grub

    change_grub_cfg_uuid $CDDIR

    # Stuff the EFI boot files into a FAT filesystem, making it as
    # small as possible. We end up re-packing like this in case we're
    # making a multi-arch image

    # First, work out how many blocks we need
    blocks=$(calculate_efi_image_size $CDDIR)

    # Now make a new image to contain the files
    rm -f $CDDIR/boot/grub/efi.img
    mkfs.msdos -v -C "$CDDIR/boot/grub/efi.img" $blocks >/dev/null

    # And copy them into place
    mmd -i "$CDDIR/boot/grub/efi.img" ::efi
    mcopy -o -s -i "$CDDIR/boot/grub/efi.img" $CDDIR/EFI/* \
        "::efi"

    if [ $BOOT_EFI = 2 ] ; then
	# We're being added alongside (after) a BIOS boot record. Tell
	# xorriso to create a (secondary) ElTorito boot record for the
	# EFI bootloader. Otherwise, xorriso will create it as the
	# primary.
        add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-eltorito-alt-boot"
    fi

    add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-e boot/grub/efi.img -no-emul-boot"
    add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-isohybrid-gpt-basdat -isohybrid-apm-hfsplus"
else
    echo "  No EFI boot code for $ARCH on CD$N"
fi

# done
