# This script gets sourced from boot-i386 and boot-amd64.
#
# Do install stuff for x86, 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

# Workaround for #823881:
export MTOOLS_SKIP_CHECK=1

# 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
BOOTDIR=
if [ "$DI_WWW_HOME" = "default" ]; then
    # Tempting as it might be to merge these two definitions using $ARCH,
    # do *not* do that - these defs are parsed out by other scripts that
    # won't cope with that
    if [ "$ARCH" = amd64 ]; then
        DI_WWW_HOME="https://d-i.debian.org/daily-images/amd64/daily"
    else
        DI_WWW_HOME="https://d-i.debian.org/daily-images/i386/daily"
    fi
    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/..

# This script is called with $1 (now $N) as the CD to make bootable.
# Exit if this is not a boot disk
if [ $N != 1 ] ; then exit 0; fi

INSTALLDIR_amd64="install.amd"
INSTALLDIR_i386="install.386"

if [ "$ARCH" = amd64 ]; then
        INSTALLDIR="$INSTALLDIR_amd64"
else
        INSTALLDIR="$INSTALLDIR_i386"
fi

BOOT_IMAGES="cdrom/initrd.gz cdrom/vmlinuz cdrom/debian-cd_info.tar.gz"

# Download boot images.
for image in $BOOT_IMAGES; do
        if [ ! -e "$image" ]; then
                dir=$(dirname $image)
                mkdir -p $dir
                if [ -n "$LOCAL"  -a -f "${LOCALDEBS:-$MIRROR}/dists/$DI_DIST/local/installer-$ARCH/current/images/$image" ]; then
                        cp "${LOCALDEBS:-$MIRROR}/dists/$DI_DIST/local/installer-$ARCH/current/images/$image" "$image"
                elif [ ! "$DI_WWW_HOME" ];then
                        if [ ! "$DI_DIR" ];then
                                DI_DIR="$MIRROR/dists/$DI_DIST/main/installer-$ARCH/current/images"
                        fi
#                       echo "Copying images from $DI_DIR"
                        cp "$DI_DIR/$image" "$image"
                else
                        $WGET "$DI_WWW_HOME/$image" -O "$image"
                fi
        fi
done

# Images that are not really required or may be unavailable.
for image in $DISK_IMAGES $EXTRA_DISK_IMAGES; do
        if [ ! -e "$image" ]; then
                dir=$(dirname $image)
                mkdir -p $dir
                if [ -n "$LOCAL"  -a -f "${LOCALDEBS:-$MIRROR}/dists/$DI_DIST/local/installer-$ARCH/current/images/$image" ]; then
                        cp "${LOCALDEBS:-$MIRROR}/dists/$DI_DIST/local/installer-$ARCH/current/images/$image" "$image"
                elif [ ! "$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" || true
                else
                        $WGET "$DI_WWW_HOME/$image" -O "$image" || rm -f "$image"
                fi
        fi
done

# Only disk one gets the extra files installed
if [ "$N" = "1" ]; then
        # populate the install directory as well
        for disk in $DISK_IMAGES $EXTRA_DISK_IMAGES; do
                if [ -e "$disk" ]; then
                        dir=$(dirname $disk)
                        mkdir -p $CDDIR/$INSTALLDIR/$dir
                        cp -lf $disk $CDDIR/$INSTALLDIR/$dir
                fi
        done

        if [ -e "$MIRROR/tools" ] && \
           [ ! -e $CDDIR/tools ] && \
           [ "$OMIT_DOC_TOOLS" != "1" ] ; then
                echo "    Adding tools to CD1"
                $BASEDIR/tools/add_files $CDDIR $MIRROR tools
                # Remove the win32-loader/ subdirectory from tools, as d-i already installs setup.exe
                rm -Rf $CDDIR/tools/win32-loader
        fi
fi

case "$DESKTOP" in
    $UNSPEC_DESKTOP_DEFAULT)
        # default from tasksel 
        DESKTOP=
        ;;
    all)
        # default from tasksel 
        DESKTOP=
        ;;
    light)
        DESKTOP=xfce
        ;;
esac
# desktop boot parameter gets set separately through DESKTOP variable
KERNEL_PARAMS="$(echo "$KERNEL_PARAMS" | sed -r "s/desktop=[^ ]* ?//")"

mkdir -p $CDDIR/$INSTALLDIR
cp -lf cdrom/vmlinuz $CDDIR/$INSTALLDIR/
cp -lf cdrom/initrd.gz $CDDIR/$INSTALLDIR/
if [ -e $CDDIR/tools/loadlin.exe ]; then
        echo "\\tools\\loadlin.exe vmlinuz initrd=initrd.gz" | todos > $CDDIR/$INSTALLDIR/install.bat
fi

# In case of a multi-arch CD the script will be called two times. The
# first time the isolinux dir gets set up for single arch; if it is
# called a second time the isolinux dir already exists and we know it
# will be multi-arch. We then throw away the first version and just
# configure things from scratch for multi-arch.

MULTIARCH=
if [ -e boot$N/isolinux/isolinux.cfg ]; then
        MULTIARCH=1
        rm -r boot$N/isolinux
fi

mkdir -p boot$N/isolinux
# Isolinux setup including config and help files comes from d-i.
cat cdrom/debian-cd_info.tar.gz | (cd boot$N/isolinux/; tar zx)

# Not everything in the tarball is isolinux stuff
if [ -e boot$N/isolinux/win32-loader.ini ] ; then
    echo "  Moving extra loader files into place:"
    for file in boot$N/isolinux/g2ldr* boot$N/isolinux/setup.exe boot$N/isolinux/win32-loader.ini; do
        if [ -e $file ] ; then
	    printf "    "
            mv -v $file CD$N/
        fi
    done
    # Modify win32-loader.ini for the "current" arch
    sed -i "s|install/|$INSTALLDIR/|" CD$N/win32-loader.ini
    if [ "${DESKTOP}" ] ; then
        sed -i "s|^\(default_desktop\)=.*|\1=${DESKTOP}|" CD$N/win32-loader.ini
    fi
fi

# Add autorun
if [ -f CD$N/setup.exe ]; then
# when win32-loader is present, use that (it already checks for README.html)
    todos > $CDDIR/autorun.inf <<EOF
[autorun]
open=setup.exe
icon=setup.exe,0
label=Install Debian GNU/Linux

[Content]
MusicFiles=false
PictureFiles=false
VideoFiles=false
EOF
elif [ -f $CDDIR/README.html ]; then
    todos > $CDDIR/autorun.inf <<EOF
[autorun]
open=autorun.bat
label=Install Debian GNU/Linux

[Content]
MusicFiles=false
PictureFiles=false
VideoFiles=false
EOF
    todos > $CDDIR/autorun.bat <<EOF
@echo Starting "README.html"...
@start README.html
@exit
EOF
fi

# Common mkisofs options when creating CDs
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-J -joliet-long"
add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-cache-inodes"

# BIOS-boot specific stuff. Even when we're *not* doing BIOS boot, we
# still create all the isolinux config files as they're parsed and
# used later. We just don't grab all the syslinux files, then make
# sure that we *don't* set up BIOS boot on the xorriso command line.
if [ "$BOOT_BIOS" = 1 ] ; then
    echo "  Adding BIOS boot code for $ARCH on CD$N"
    mkdir -p $CDDIR/../syslinux
    SYSLINUXDEB=$(find_pkg_file syslinux)
    if [ -z "$SYSLINUXDEB" ]; then
        echo "ERROR: syslinux package is required" >&2
        exit 1
    fi
    (dpkg --fsys-tarfile "$MIRROR/$SYSLINUXDEB" | \
        tar xf - -C $CDDIR/../syslinux ./usr/lib)

    if [ -n "$ARCHIVE_EXTRACTED_SOURCES" ]; then
        echo $SYSLINUXDEB >> $CDDIR/../$N.pkgs_extracted
        find_pkg_file syslinux source >> $CDDIR/../$N.pkgs_extracted
    fi

    ISOLINUXDEB=$(find_pkg_file isolinux)
    if [ -z "$ISOLINUXDEB" ]; then
        echo "ERROR: isolinux package is required" >&2
        exit 1
    fi
    (dpkg --fsys-tarfile "$MIRROR/$ISOLINUXDEB" | \
        tar xf - -C $CDDIR/../syslinux ./usr/lib)
    
    if [ -n "$ARCHIVE_EXTRACTED_SOURCES" ]; then
        echo $ISOLINUXDEB >> $CDDIR/../$N.pkgs_extracted
        find_pkg_file isolinux source >> $CDDIR/../$N.pkgs_extracted
    fi
    add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-isohybrid-mbr syslinux/usr/lib/ISOLINUX/isohdpfx.bin"

    cp -f $CDDIR/../syslinux/usr/lib/ISOLINUX/isolinux.bin boot$N/isolinux/
    cp -f $CDDIR/../syslinux/usr/lib/syslinux/modules/bios/vesamenu.c32 boot$N/isolinux/
    cp -f $CDDIR/../syslinux/usr/lib/syslinux/modules/bios/ldlinux.c32 boot$N/isolinux/
    cp -f $CDDIR/../syslinux/usr/lib/syslinux/modules/bios/libcom32.c32 boot$N/isolinux/
    cp -f $CDDIR/../syslinux/usr/lib/syslinux/modules/bios/libutil.c32 boot$N/isolinux/

    if [ ! -z "$MULTIARCH" ]; then
        # Mandatory for 64bits capability detection
        if [ -f $CDDIR/../syslinux/usr/lib/syslinux/modules/bios/ifcpu64.c32 ] ; then
            cp -f $CDDIR/../syslinux/usr/lib/syslinux/modules/bios/ifcpu64.c32 boot$N/isolinux/
        else
            cp -f $CDDIR/../syslinux/usr/lib/syslinux/ifcpu64.c32 boot$N/isolinux/
        fi
    fi

    # If we don't have grub stuff from d-i, tell xorriso to add a
    # partition offset for the isohybrid code
    if [ ! -d boot$N/isolinux/grub ] || [ $BOOT_EFI = 0 ] ; then
        add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-partition_offset 16"
    fi

    # Add the normal options to make an ElTorito bootable CD/DVD using isolinux
    add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-b isolinux/isolinux.bin"
    add_mkisofs_opt $CDDIR/../$N.mkisofs_opts "-c isolinux/boot.cat"
    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 "-no-emul-boot"

    # We explicitly put the boot bits in boot$N (as opposed to CD$N)
    # and list it first on the eventual mkisofs command line. That
    # means they'll be placed nearer the front of the eventual image;
    # some older BIOSes apparently have a limitation on what they'll
    # boot here.
    add_mkisofs_opt $CDDIR/../$N.mkisofs_dirs "boot$N"
else
    echo "  No BIOS boot code for $ARCH on CD$N"
fi

di_syslinux_ver="$(di_syslinux_version)"
case "$di_syslinux_ver" in
    2.0)
        : ;;
    1.*)
        echo "Error: syslinux config versions before 2.0 are not supported"
        exit 1 ;;
    *)
        echo "Error: unrecognized syslinux config version '$di_syslinux_ver'"
        exit 1 ;;
esac

if [ -e boot$N/isolinux/f3.txt.withgtk ]; then
    extra_image gtk/vmlinuz         "../vmlinuz"
    extra_image gtk/initrd.gz       "../initrd.gz"
    mv boot$N/isolinux/f3.txt.withgtk boot$N/isolinux/f3.txt
    mv boot$N/isolinux/f4.txt.withgtk boot$N/isolinux/f4.txt
    if [ -e boot$N/isolinux/isolinux.cfg.withgtk ]; then
        mv boot$N/isolinux/isolinux.cfg.withgtk boot$N/isolinux/isolinux.cfg
    fi
    if [ -e $CDDIR/tools/loadlin.exe ]; then
        echo "\\tools\\loadlin.exe \\$INSTALLDIR\\vmlinuz initrd=initrd.gz vga=788" | todos > $CDDIR/$INSTALLDIR/gtk/install.bat
    fi
fi
rm -f boot$N/isolinux/isolinux.cfg.with*

if variant_enabled "xen" ; then
    extra_image xen/vmlinuz         "../vmlinuz   ../gtk/vmlinuz"
    extra_image xen/initrd.gz       "../initrd.gz ../gtk/initrd.gz"
    extra_image xen/debian.cfg || extra_image xen/xm-debian.cfg
fi

if [ -z "$MULTIARCH" ]; then
    # Set up isolinux dir for only the current arch
    grep -lr '%install%' boot$N/isolinux/* | xargs -r sed -i "s|%install%|$INSTALLDIR|"
    
    # Remove the x86*.cfg files to avoid them being loaded on a disc
    # that does not have both amd64 and i386 dirs.
    find boot$N/isolinux/ -name "x86*.cfg" -delete
else
    # Use the syslinux.cfg with architecture detection
    mv boot$N/isolinux/x86syslinux.cfg boot$N/isolinux/isolinux.cfg

    # Set up isolinux dir for multi-arch
    grep -lr '%install[-x86]*%' boot$N/isolinux/* | \
        xargs -r sed -i -e "s|%install%|$INSTALLDIR_amd64|" \
                        -e "s|%install-x86%|$INSTALLDIR_i386|"

    for file in boot$N/isolinux/f3.txt* boot$N/isolinux/f4.txt*; do
        sed -i "4 s/^$/(note: amd64 is default; prepend 'x86-' to use an i386 kernel instead)/" $file
    done

    if [ -e CD$N/win32-loader.ini ]; then
        # Modify win32-loader.ini to also cover the "other" arch
        if [ "$ARCH" = amd64 ]; then
            sed -i -e "/^arch=/d
                       /^amd64\//p; s/^amd64/i386/; s/=$INSTALLDIR_amd64/=$INSTALLDIR_i386/g" \
                           CD$N/win32-loader.ini
        else
            sed -i -e "/^arch=/d
                       /^i386\//p; s/^i386/amd64/; s/=$INSTALLDIR_i386/=$INSTALLDIR_amd64/g" \
                           CD$N/win32-loader.ini
        fi
    fi

    if [ "$BOOT_BIOS" = 1 ] ; then
        # Add another ifcpu64.c32 launch to let the 'enter' in help menus work correctly
        sed -i "/^include menu.cfg/ a\include instsel.cfg" \
                boot$N/isolinux/prompt.cfg
        cat >boot$N/isolinux/instsel.cfg <<EOF
default install-select
label install-select
    kernel ifcpu64.c32
    append installgui -- x86-installgui
EOF
    fi
fi

if [ "$SPLASHPNG" ] ; then
    cp $SPLASHPNG boot$N/isolinux/splash.png
fi

# used by Debian Edu
if [ "$LOGOPNG" ] ; then
    # Add custom logo to the initrd.gz file, replacing old image
    GTKINITRD=$CDDIR/$INSTALLDIR/gtk/initrd
    GTKINITRDGZ=$GTKINITRD.gz
    echo "info: Adding $LOGOPNG to $GTKINITRD"
    INITRDDIR=$TDIR/initrd-$$
    # Repack initrd with new image
    mkdir -p $INITRDDIR
    (
        cd $INITRDDIR
	# There may be multiple hardlinks to the initrd.gz, so
	# explicitly break the links here
        zcat $GTKINITRDGZ > $GTKINITRD
	rm -f $GTKINITRDGZ
        mkdir -p $INITRDDIR/usr/share/graphics
        cp $LOGOPNG $INITRDDIR/usr/share/graphics/logo_debian.png
        echo usr/share/graphics/logo_debian.png | \
            cpio -oA -H newc -F $GTKINITRD
	gzip -9 $GTKINITRD
    )
    rm -rf $INITRDDIR
fi

sed -i "s|built on|built $BUILD_DATE; d-i|" boot$N/isolinux/f1.txt

if [ -n "$KERNEL_PARAMS" ]; then
    # Substitute custom kernel params into the isolinux config file(s)
    for file in boot$N/isolinux/*.cfg; do
        case "$(basename $file)" in
            instsel.cfg)
                continue ;;
        esac
        sed -i "/^[[:space:]]\+append .*---/ s|append|append $KERNEL_PARAMS|" \
            $file
    done
fi

set_default_desktop
rm -rf cdrom

if [ -d boot$N/isolinux/grub ] && [ $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/isolinux/grub/efi.img '::efi/*' $CDDIR/EFI
    mkdir -p $CDDIR/boot/grub
    mv boot$N/isolinux/grub/* $CDDIR/boot/grub/
    rmdir boot$N/isolinux/grub

    # If we're only doing EFI, copy the splash file from the BIOS boot dir
    if [ $BOOT_EFI = 1 ] ; then
        mkdir -p $CDDIR/isolinux
        cp boot$N/isolinux/splash.png $CDDIR/isolinux
    fi

    # Create grub menu entries to match the isolinux ones
    sed -i '/^menuentry/Q' $CDDIR/boot/grub/grub.cfg;
    $BASEDIR/tools/boot/$DI_CODENAME/parse_isolinux \
        boot$N/isolinux $CDDIR $BASEDIR/data/$DI_CODENAME/grub-theme.in "$DISKINFO_DISTRO" "$DEBIAN_KERNEL" "$DEBVERSION" \
        >> $CDDIR/boot/grub/grub.cfg
    cp -v $BASEDIR/data/$DI_CODENAME/hl*png $CDDIR/boot/grub/theme
    NUM_ENTRIES=$(grep menuentry $CDDIR/boot/grub/grub.cfg | wc -l)
    echo "    CD$N/boot/grub/grub.cfg has $NUM_ENTRIES boot entries defined"

    # 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

# th,th, thats all

