#!/bin/bash

#
# Commands in isofs.txt files:
#
# EVERY		from	to		Put this on every disk
# DISK1		from	to		Put this on the 1st disk
# SINGLE	from	to		Put it on any disk
# SPLIT		from	to		You may split it up over many disks
#
# BOOT		boot-options		Add this mkisofs options for 1st disk
#
# If you want to add multiple 'BOOT' lines, use the tag-name 'BOOTx' for
# the 2nd and all further lines.
#
# SCRIPT	script-name	args	Run given script for each image
#
# Intended for image post-processing. The first attached argument is the CD
# number and the second the image file.
#

eval `grep rockver scripts/parse-config`

if [ $# -eq 0 ] ; then
	echo
	echo "Usage: $0 [ -size <MB> ] [ -source ] [ -mkdebug ] [ -nomd5 ] \\"
	echo "       ${0//?/ } <ISO-Prefix> <Config> [ <Config> [ .. ] ]"
	echo
	echo " Create ISO images ready to burn on CD/DVD from one or more target builds." 
	echo " Targets have to be built using ./scripts/Build-Target before you can create"
	echo " ISO images from them."
	echo
	echo "	-size  <MB>	limit the size of images to <MB> MB; default is 680"
	echo "	-source		include package sources from download/mirror on the images"
	echo "	-nodist		also include download/nodist sources (Check licenses!)"
	echo "	-mkdebug	create a small debug script for every image"
	echo "	-nomd5		don't add md5 checksums to images"
	echo
	echo "	<ISO-Prefix> 	the name of the resulting images"
	echo "	<Config>  	the target builds to include; the first has to be a"
	echo "			bootdisk target for the first image to be bootable" 
	echo
	echo "E.g.: $0 mycdset install generic"
	echo
	exit 1
fi

# default disk-size is 680 MB
dsize=$(( 680 * 1024 ))
src=0; mkdebug=0; implantisomd5=1; nodist=0

while true ; do
	case "$1" in
		-size)
			dsize=$(( $2 * 1024 )) ; shift ; shift ;;
		-source)
			src=1 ; shift ;;
		-mkdebug)
			mkdebug=1 ; shift ;;
		-nomd5)
			implantisomd5=0 ; shift ;;
		-nodist)
			nodist=1 ; shift ;;
		-* | '')
			$0 ; exit 1 ;;
		*)
			isoprefix=$1 ; shift ; break ;;
	esac
done

spacer="    "

if [ "$implantisomd5" = 1 -a ! -x src/isomd5sum/implantisomd5 ] ; then
	echo ; date "+  [%X] Creating ISO-MD5 tools ..."
	rm -rf src/isomd5sum; mkdir -p src/isomd5sum
	cp misc/isomd5sum/* src/isomd5sum/
	if ! make -C src/isomd5sum all ; then
		implantisomd5=0
		date "+  [%X] Could not create ISO-MD5 tools"
		date "+  [%X] Not implanting md5sum into isos"
	fi
fi

echo ; date "+  [%X] Removing old files with same prefix ..."
rm -rf ${isoprefix}_*

date "+  [%X] Reading configs and creating ISO index ..."
index=`mktemp` ; dat=`mktemp` ; pathspec=`mktemp`
rm -f ${pathspec}*

for cfg ; do
	id="`grep '^export ROCKCFG_ID=' config/$cfg/config | cut -f2 -d\'`"
	if ! cat build/$id/ROCK/isofs.txt >> $dat
	then rm -f $dat $index ${pathspec}_* ; exit 1 ; fi
done

# function to add a given file $1 to the place $2 on the resulting ISO
#
add() {
	local from="$1" to="$2" done=0
	if [ ! -f "$from" -a ! -d "$from" ] ; then
		echo "No such file or directory: $from"
		rm -f $dat $index ${pathspec}_* ; exit 1
	fi

	size="`du -sk $from | cut -f1`"
	if [ $size -gt $(( $dsize - $disk_0_size )) ] ; then
		echo "Chunk $from is too big!"
		rm -f $dat $index ${pathspec}_* ; exit 1
	fi

	for x in $disks ; do
		ds=disk_${x}_size ; dd=disk_${x}_data
		if [ $(( ${!ds} + $size )) -le \
		     $(( $dsize - $disk_0_size )) ] ; then
			eval "$ds=$(( ${!ds} + $size ))"
			echo "$to=$from" >> ${pathspec}_${disk_nr}
			done=1 ; break
		fi
	done

	if [ $done = 0 ] ; then
		disk_nr=$(( $disk_nr + 1 ))
		ds=disk_${disk_nr}_size ; eval "$ds=$size"

		# FIXME: if in a path-list files, mkisofs complains about
		#        missing isolinux. Should be a mkisofs bug!
		if [ "$to" = "isolinux/" -o "$from" = "isolinux/" ] ; then
			echo "$to=$from" >> ${pathspec}_iso
		else
			echo "$to=$from" >> ${pathspec}_${disk_nr}
		fi
		disks="$disks $disk_nr"
	fi
}

bootoptions=""
scripts=""
while read tag data ; do
	if [ $tag = BOOT ] ; then
		if [ "$bootoptions" ] ; then
			echo "Multiple boot options found!"
			rm -f $dat $index ${pathspec}_* ; exit 1
		else
			bootoptions="$data"
		fi
	elif [ $tag = SCRIPT ] ; then
		scripts="$scripts $data"
	fi
done < $dat
while read tag data ; do
	[ $tag = BOOTx ] && bootoptions="$bootoptions $data"
done < $dat

echo "$spacer parsing for EVERY disk."

disks="0" ; disk_nr=0 ; disk_0_size=0
echo "index.txt=${isoprefix}_index.txt" >> ${pathspec}_0
while read type from to ; do
	if [ $type = EVERY ] ; then
		add $from $to
		if [ $disk_nr -gt 0 ] ; then
			echo "Every-disk data is too big!"
			rm -f $dat $index ${pathspec}_* ; exit 1
		fi
	fi
done < $dat

echo "$spacer parsing for DISK1 disk."

disk_nr=0 ; disks=""
while read type from to ; do
	if [ $type = DISK1 ] ; then
		add $from $to
		if [ $disk_nr -gt 1 ] ; then
			echo "Disk 1 is too big!"
			rm -f $dat $index ${pathspec}_* ; exit 1
		fi
	fi
done < $dat

echo "$spacer parsing for SINGLE disk."

while read type from to ; do
	if [ $type = SINGLE ] ; then
		add $from $to
	fi
done < $dat

echo "$spacer parsing for SPLIT disk."

while read type from to ; do
	if [ $type = SPLIT ] ; then
		find $from -type f -printf '%P\n' | sort > $index
		grep ':dev' $index > ${index}_dev
		grep ':doc' $index > ${index}_doc
		grep -v ':dev' $index | grep -v ':doc' >${index}_new
		cat ${index}_new ${index}_doc ${index}_dev >$index
		rm ${index}_{dev,doc,new}
		while read p ; do
			add $from$p $to$p
		done < $index
	fi
done < $dat

if [ $src = 1 ] ; then
	echo "$spacer embedding source."

	./scripts/Create-SrcTar

	for x in Documentation/* rock-src-$rockver.tar.bz2 ; do
		[ ! -d $x ] && add $x ${x/Documentation\/}
	done

	while read from ; do
		if [ "${from#download/nodist/}" != "${from}" ] ; then
			[ "${nodist}" == "0" ] && continue
		fi
		# adaptions of the sed exp need to propagated into Download, too
		from="`echo $from | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,'`"
		if [ -e $from ] ; then
			add $from $from
		else
			echo "WARNING: File $from is missing!"
		fi
	done < <( ./scripts/Download -list )
fi

echo -n > $index
for x in 0 $disks ; do
	dd=disk_${x}_data
	for y in ${!dd} `cat ${pathspec}_${x} 2> /dev/null` ; do
		to=${y%=*} ; from=${y#*=}
		if [ -e $from ] ; then
			find $from -printf "disk$x	$to%P\\n" >> $index
		else
			echo "disk$x	$to" >> $index
		fi
	done
done
disk_0_size=$(( $disk_0_size + $( du -sk $index | cut -f1 ) ))

if [ -z "$bootoptions" ] ; then
	echo "WARNING: Disk1 is not bootable - no boot options defined."
fi

xxx() {
	if ! mkisofs "$@" &> $dat ; then
		echo ; echo "mkisofs $@" ; echo
		cat $dat ; rm -f $index $dat; exit 1
	fi
}

echo
total=0
for x in 0 $disks ; do
	ds=disk_${x}_size
	total=$(( $total + ${!ds} ))

	if [ $x = 0 ] ; then y="Shared"
	else y="`printf 'Disk%2d' $x`" ; fi

	z="$( grep "^disk$x	" $index | wc -l )"

	if [ -z "$bootoptions" -o $x != 1 ] ; then
		printf "%15s $y: %7s kB in %5d files\n" "" ${!ds} $z
	else
		printf "%15s $y: %7s kB in %5d files  (boot)\n" "" ${!ds} $z
	fi
done
printf "%15s Total: %8s kB in %5d files\n" "" $total $( wc -l < $index )
echo

date "+  [%X] Creating ${isoprefix}_index.txt ..."
sort -tk -n -k2 < $index > ${isoprefix}_index.txt

for x in $disks ; do
	ds=disk_${x}_size
	date "+  [%X] Creating ${isoprefix}_cd$x.iso ..."
	echo "This is disk #$x." > $dat
	# FIXME: current mkisofs does only accept one -path-list
	sort -u ${pathspec}_${x} ${pathspec}_0 > ${pathspec}_current 2> /dev/null
	xxx -q -r -T -J -l -o ${isoprefix}_cd$x.iso -A "ROCK Linux - Disk $x" \
	    -publisher 'The ROCK Linux Project - http://www.rocklinux.org' \
	    -V "ROCK Linux - Disk $x" \
	    -path-list ${pathspec}_current \
	    $bootoptions -graft-points disk$x.txt=$dat \
	    `cat ${pathspec}_iso 2>/dev/null`
	rm -f ${pathspec}_iso
	rm -f ${pathspec}_current

	bootoptions=""

	if [ "$scripts" ] ; then
		date "+  [%X] Running post-processing scripts on image ..."
		eval $scripts $x ${isoprefix}_cd$x.iso
	fi

	if [ "$implantisomd5" = 1 -a -x ./src/isomd5sum/implantisomd5 ] ; then
		date "+  [%X] Calculating and implanting MD5 checksum ..."
		./src/isomd5sum/implantisomd5 ${isoprefix}_cd$x.iso >/dev/null
	fi

	if [ "$mkdebug" = 1 ] ; then
		cat > ${isoprefix}_loop$x.sh << EOT
#!/bin/sh

if [ "\$1" -a -z "\${1//[0-9]/}" ] ; then
	[ "\$2" ] && umount \$2 > /dev/null 2>&1
	losetup -d /dev/loop/\$1 > /dev/null 2>&1
	losetup /dev/loop/\$1 ${isoprefix}_cd$x.iso
	[ "\$2" ] && mount /dev/loop/\$1 \$2
else
	echo "Usage: \$0 <loopback-dev-nr> [ <mount-point> ]"
	echo " Mount the corresponding ISO image at the given mount point using the given"
	echo " loopback device. If no mount point is given, the mount step is skipped."
	exit 1
fi
EOT
		chmod +x ${isoprefix}_loop$x.sh
	fi
done

date "+  [%X] Done. Have fun!"
echo

rm -f $index $dat ${pathspec}_*