#! /bin/sh
set -e

# Custom wrapper script for build.sh.
# See also CONF.sh for the meaning of variables used here.

# The directory that contains the debian-cd code
export BASEDIR=$(pwd)
# The path to the local mirror
export MIRROR=/srv/debmirror/debian

# The architecture for which to build the CD/DVD image
export ARCH=i386

# The suite the installed system will be based on
export CODENAME=etch
# The suite the udebs for the installer will be taken from
export DI_CODENAME=etch

# Take D-I images from this location
#export DI_DIR=/srv/d-i_images/i386

# Setting for sid CDs (to use daily d-i images from http://people.debian.org)
# See tools/boot/etch/boot-$ARCH scripts
export DI_WWW_HOME=default

# We build only the first CD/DVD by default
# Uncomment/change lines for full/dvd targets below if you want to build more
export MAXCDS=1

# Set variables that determine the type of image to be built
export DISKTYPE="$1"
case $DISKTYPE in
    BC)
	export INSTALLER_CD=1
	;;
    NETINST)
	export INSTALLER_CD=2
	;;
    CD)
	unset INSTALLER_CD
	#export MAXCDS=3
	;;
    DVD)
	export INSTALLER_CD=3
	#export MAXCDS=1
	;;
    *)
	echo "Usage: mybuild BC|NETINST|CD|DVD"
	exit 1
	;;
esac

# We should use the correct version of debootstrap to build the CD image.
DEBOOTUDEB=$(zcat $MIRROR/dists/$CODENAME/main/debian-installer/binary-$ARCH/Packages.gz | \
	     /bin/sed -n \
	     's/Filename: \(pool\/main\/d\/debootstrap\/debootstrap-udeb.*udeb\)$/\1/p')
if [ -z "$DEBOOTUDEB" ]; then
	exit 1
fi
echo Using ${DEBOOTUDEB##*/} to get packages used by d-i.

#DEBOOTDIR=/srv/debian-cd/debootstrap
DEBOOTDIR=/srv/debmirror/debian-cd.tmp/debootstrap
rm -rf $DEBOOTDIR
dpkg -x "$MIRROR/$DEBOOTUDEB" $DEBOOTDIR
export PATH="$DEBOOTDIR/usr/sbin:$PATH"
export DEBOOTSTRAP_DIR="$DEBOOTDIR/usr/lib/debootstrap/"


# Upgrade debian-installer's task files
if [ -d tasks ]; then
	cd tasks
	../tools/generate_di_list
	../tools/generate_di+k_list
	cd ..
else
	echo "Error: cannot find tasks directory"
	exit 1
fi

# Set configuration file to be used by build.sh
#export CF=CONF-dzongkha.sh

./build.sh $ARCH

# Revert local changes
cd $BASEDIR
svn revert tasks/debian-installer-etch
svn revert tasks/debian-installer+kernel-etch
