#!/bin/bash
#
# Do install stuff for i386, 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

set -e

N=$1
CDDIR=$2
BOOTDIR=
if [ "$DI_WWW_HOME" = "default" ];then
   DI_WWW_HOME="http://people.debian.org/~joeyh/d-i/images/daily"
   if [ -n "$DI_DIR" ];then
      DI_DIR="$DI_DIR/~${DI_WWW_HOME#*~}"
      DI_WWW_HOME=""                     
   fi
fi
if [ ! "$DI_DIST" ]; then
   DI_DIST="$DI_CODENAME"
fi

cd $CDDIR/..

# This script is called with $1 (now $N) as the CD to 
# make bootable.  N may be in the form "n" or "n_NONUS"
# There may be more than 4 disks...support extras.

# Strip NONUS part of disk number
# NN will be 1...N so it can be used as an index
NN=`echo $N | sed -e 's/_NONUS//'`

# List of boot image for each CD
KTYPE[1]="" #isolinux multiboot in fact
KTYPE[2]="cdrom"
# XXX add net-image back when it's fixed
KTYPE[3]=""
KTYPE[4]=""
KTYPE[5]=""
KTYPE[6]=""
KTYPE[7]=""
KTYPE[8]=""
KTYPE[9]=""
KTYPE[10]=""

THISTYPE=${KTYPE[$NN]}

BOOT_IMAGES="cdrom/boot.img cdrom/initrd.gz cdrom/vmlinuz cdrom/debian-cd_info.tar.gz"
DISK_IMAGES="floppy/cd-drivers.img floppy/boot.img floppy/root.img"
# Only include extra disk images on full CDs, not on smaller images.
if [ "$INSTALLER_CD" != 1 ] && [ "$INSTALLER_CD" != 2 ]; then
	EXTRA_DISK_IMAGES="floppy/access/root.img floppy/access/boot.img floppy/access/cd-drivers.img floppy/net-drivers.cfg"
fi

# Download boot images and floppies.
for image in $BOOT_IMAGES $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"
	else
		wget "$DI_WWW_HOME/$image" -O "$image"
	fi
    fi
done

# Floppies that are not really required or may be unavailable.
for image in $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

if [ "$NN" = "1" ]; then
    echo "Using ISOLINUX boot-disks image on CD$N"
    mkdir -p boot$N/isolinux
    cp -f $BASEDIR/data/$DI_CODENAME/isolinux.bin boot$N/isolinux/
    cp -lf cdrom/vmlinuz $CDDIR/install/
    cp -lf cdrom/initrd.gz $CDDIR/install/
    echo -n "-cache-inodes -J -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table " > $N.mkisofs_opts
else
    if [ -n "$THISTYPE" ]; then
	echo "Using $THISTYPE boot-disks image on CD$N"
	mkdir -p boot$N/boot
	cp $THISTYPE/boot.img boot$N/boot/
	echo -n "-cache-inodes -J -b boot/boot.img "  > $N.mkisofs_opts
    else
	mkdir boot$N
	echo -n "-cache-inodes -J " > $N.mkisofs_opts
    fi
fi

# Only disk one gets the extra files installed
if [ "$NN" = "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/install/$dir
		cp -lf $disk $CDDIR/install/$dir
          fi
	done

	# Include Smart Boot Manager image for people where isolinux fails
	gzip -dc $BASEDIR/data/$DI_CODENAME/sbm.bin.gz > $CDDIR/install/sbm.bin
	# Keep the original file timestamp
	touch -r $BASEDIR/data/$DI_CODENAME/sbm.bin.gz $CDDIR/install/sbm.bin
	cp -p $BASEDIR/data/$DI_CODENAME/README.sbm $CDDIR/install/
	
	# Isolinux setup including config and help files comes from d-i.
	cat cdrom/debian-cd_info.tar.gz | (cd boot$N/isolinux/; tar zx)
	# TODO: is using the .with26 file as a flag really the best way?
	if [ -e boot$N/isolinux/f3.txt.with26 ];then
		mv boot$N/isolinux/f3.txt.with26 boot$N/isolinux/f3.txt
		for image in cdrom/2.6/initrd.gz cdrom/2.6/vmlinuz; 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
		cp -a cdrom/2.6 $CDDIR/install/2.6
		mv boot$N/isolinux/isolinux.cfg.with26 boot$N/isolinux/isolinux.cfg
	else 
		rm -f boot$N/isolinux/isolinux.cfg.with26
	fi
	if [ -e boot$N/isolinux/f4.txt.with26 ];then
		mv boot$N/isolinux/f4.txt.with26 boot$N/isolinux/f4.txt
	fi

	if [ "$SPLASHPNG" ] ; then
		# Insert our own splash screen.  Color index 15 is
		# background, and index 7 is foreground.  Set to black and
		# white respecively
		pngtopnm < $SPLASHPNG | ppmquant 16 | \
			ppmtolss16 "#ffffff=7" "#000000=15" > boot$N/isolinux/splash.rle
	fi

	if [ -n "$KERNEL_PARAMS" ]; then
		# Substitute custom kernel params into the isolinux config
		# file.
		sed -i "s/append /append $KERNEL_PARAMS/" boot$N/isolinux/isolinux.cfg
	fi
fi

# Add autorun if we have README.html

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

# write final lines to mkisofs_opts
if [ "$NN" = "1" ]; then
	echo -n "boot$N " >> $N.mkisofs_opts
else
	if [ -n "$THISTYPE" ]; then
		echo -n "-c boot/boot.catalog boot$N "  >> $N.mkisofs_opts
	fi
fi

# th,th, thats all
