#!/bin/sh
#
# Debian-cd helper script for making ISO / jigdo images
#
# Split out from the top-level Makefile SAM 2005/12/08

set -e

DIR=$1
ARCHES="$2"
OUT=$3
DOJIGDO=$4
DEBVERSION=$5
MIRROR=$6
MKISOFS=$7
MKISOFS_OPTS=$8
JIGDO_OPTS=$9
shift
JIGDO_CLEANUP=$9

NUM_ARCHES=`echo $ARCHES | wc -w`

if [ "$CD"x = ""x ] ; then
	echo "Generating the $ARCHES iso/jigdo images ..."
	FILES=$DIR/*.volid
else
	echo "Generating $ARCHES iso/jigdo image number $CD ..."
	FILES=$DIR/$CD.volid
fi

NUM_CDS=`ls -1 $FILES | wc -l`

last_minute_update () {
    echo "  Last-minute updates:"
    # Aaargh. Only now that we know how many CDs we're making can we
    # fill in the TOTALNUM number in README.{html,txt} (and therefore also
    # update the md5sum.txt entries for those files)
    for file in README.html README.txt
    do
        echo "    $file"
        OLD_MD5=`md5sum ./$file`
        sed -i "s?TOTALNUM?$NUM_CDS?" $file
        NEW_MD5=`md5sum ./$file`
        sed -i "s?$OLD_MD5?$NEW_MD5?" md5sum.txt
    done
}

for file in $FILES
do
	dir=${file%%.volid}
	n=${dir##$DIR/}
	num=$n
	dir=$DIR/CD$n

    cd $dir
    # Anything last-minute that can only be done now?
    last_minute_update

	cd $dir/..

	opts=`cat $DIR/$n.mkisofs_opts` || true
	dirs=`cat $DIR/$n.mkisofs_dirs` || true
	volid=`cat $DIR/$n.volid`
	relname=`echo $DEBVERSION | sed -e 's/[. ]//g'`
	DISKINFO=`cat $DIR/$n.diskinfo`

	ARCHLIST=`echo "$ARCHES" | tr ' ' '-'`
	OUTFILE="${CDNAME:-debian}-$relname-$ARCHLIST-$DISKTYPE-$n"

	# Clean up any old files
	rm -f $OUT/$OUTFILE.iso $OUT/$OUTFILE.jigdo $OUT/$OUTFILE.template

	date

	# Actually make the ISO/jigdo images. Long command lines
	# here... :-(
	case $DOJIGDO in
	
		0) # No jigdo files, just straight ISO
			echo $MKISOFS $MKISOFS_OPTS -V "$volid" -o $OUT/$OUTFILE.iso $opts $dirs CD$n
			echo $MKISOFS $MKISOFS_OPTS -V "$volid" -o $OUT/$OUTFILE.iso $opts $dirs CD$n > CD$n/.disk/mkisofs
			$MKISOFS $MKISOFS_OPTS -V "$volid" -o $OUT/$OUTFILE.iso $opts $dirs CD$n
			;;


		1) # jigdo files _and_ ISO
			echo $MKISOFS $MKISOFS_OPTS -V "$volid" \
				-o $OUT/$OUTFILE.iso \
				-jigdo-jigdo $OUT/$OUTFILE.jigdo \
				-jigdo-template $OUT/$OUTFILE.template \
				-jigdo-map Debian=$MIRROR/ \
				-jigdo-exclude boot$n \
				-md5-list $DIR/md5-check \
				$JIGDO_OPTS $opts $dirs CD$n
			echo $MKISOFS $MKISOFS_OPTS -V "$volid" \
				-o $OUT/$OUTFILE.iso \
				-jigdo-jigdo $OUT/$OUTFILE.jigdo \
				-jigdo-template $OUT/$OUTFILE.template \
				-jigdo-map Debian=$MIRROR/ \
				-jigdo-exclude boot$n \
				-md5-list $DIR/md5-check \
				$JIGDO_OPTS $opts $dirs CD$n > CD$n/.disk/mkisofs
			$MKISOFS $MKISOFS_OPTS -V "$volid" \
				-o $OUT/$OUTFILE.iso \
				-jigdo-jigdo $OUT/$OUTFILE.jigdo \
				-jigdo-template $OUT/$OUTFILE.template \
				-jigdo-map Debian=$MIRROR/ \
				-jigdo-exclude boot$n \
				-md5-list $DIR/md5-check \
				$JIGDO_OPTS $opts $dirs CD$n
			;;

		2) # jigdo only, no ISO
			echo $MKISOFS $MKISOFS_OPTS -V "$volid" \
				-o /dev/null \
				-jigdo-jigdo $OUT/$OUTFILE.jigdo \
				-jigdo-template $OUT/$OUTFILE.template \
				-jigdo-map Debian=$MIRROR/ \
				-jigdo-exclude boot$n \
				-md5-list $DIR/md5-check \
				$JIGDO_OPTS $opts $dirs CD$n
			echo $MKISOFS $MKISOFS_OPTS -V "$volid" \
				-o /dev/null \
				-jigdo-jigdo $OUT/$OUTFILE.jigdo \
				-jigdo-template $OUT/$OUTFILE.template \
				-jigdo-map Debian=$MIRROR/ \
				-jigdo-exclude boot$n \
				-md5-list $DIR/md5-check \
				$JIGDO_OPTS $opts $dirs CD$n > CD$n/.disk/mkisofs
			$MKISOFS $MKISOFS_OPTS -V "$volid" \
				-o /dev/null \
				-jigdo-jigdo $OUT/$OUTFILE.jigdo \
				-jigdo-template $OUT/$OUTFILE.template \
				-jigdo-map Debian=$MIRROR/ \
				-jigdo-exclude boot$n \
				-md5-list $DIR/md5-check \
				$JIGDO_OPTS $opts $dirs CD$n
			;;
	esac

	# If we've made jigdo files, tweak them with extra info now
	if [ "$DOJIGDO" != "0" ] ; then
		$JIGDO_CLEANUP $OUT/$OUTFILE.jigdo \
			$OUTFILE.iso $DIR/CD$n \
			"`echo "$JIGDOTEMPLATEURL" | sed -e 's|%ARCH%|$ARCH|g'`$OUTFILE.template" \
			"$DISKINFO" \
			$JIGDOFALLBACKURLS
	fi
done