#!/bin/sh
#
# add_debs
#
# Simple helper script for debian-cd
#
# Split out of the top-level Makefile by SAM 2005/12/10
#
# 1. Check that the lists of packages look valid (e.g. installer
# packages are on CD#1
#
# 2. Add deb files into each CD root
#
# 3. Create Packages files within each CD root

set -e

BDIR=$1
TDIR=$2
FIRSTDISKS=$3
ARCH=$4
BASE_INCLUDE=$5
BASE_EXCLUDE=$6
UDEB_INCLUDE=$7
UDEB_EXCLUDE=$8
ADDPACKAGES=$9
shift
SCANPACKAGES=$9

echo "Current disk usage on the binary CDs (before the debs are added):"
cd $BDIR && du -sm CD[0123456789]*
echo "Adding the selected packages to each CD:"

# Check that all packages required by debootstrap are included
# and create .disk/base_installable if yes
# Also create .disk/base_components
mkdir -p $TDIR

for DISK in $FIRSTDISKS
do 
    DISK=${DISK##CD}
	ok=yes
    for p in `debootstrap --arch $ARCH --print-debs $CODENAME $TDIR/debootstrap.tmp file:$MIRROR`
	do
		if ! grep -q ^$p$ $BDIR/$DISK.packages ; then
			if [ -n "$BASE_EXCLUDE" ] && grep -q ^$p$ $BASE_EXCLUDE ; then
				echo "Missing debootstrap-required $p but included in $BASE_EXCLUDE"
				continue
			fi
	        ok=no
	        echo "Missing debootstrap-required $p"
	    fi
    done

    rm -rf $TDIR/debootstrap.tmp

    if [ "$ok" = "yes" ] ; then
	    echo "CD$DISK contains all packages needed by debootstrap"
		touch $BDIR/CD$DISK/.disk/base_installable
    else
	    echo "CD$DISK missing some packages needed by debootstrap"
    fi

    echo 'main' > $BDIR/CD$DISK/.disk/base_components
    if [ -n "$LOCAL" ] ; then
        echo 'local' >> $BDIR/CD$DISK/.disk/base_components
    fi

	# Sort out the udeb include and exclude files
    if [ -n "$UDEB_INCLUDE" ] ; then
		if [ -r "$UDEB_INCLUDE" ] ; then
			cp -af "$UDEB_INCLUDE" "$BDIR/CD$DISK/.disk/udeb_include"
		else
			echo "ERROR: Unable to read UDEB_INCLUDE file $UDEB_INCLUDE"
		fi
    fi
    if [ -n "$UDEB_EXCLUDE" ] ; then
		if [ -r "$UDEB_EXCLUDE" ] ; then
			cp -af "$UDEB_EXCLUDE" "$BDIR/CD$DISK/.disk/udeb_exclude"
		else
			echo "ERROR: Unable to read UDEB_EXCLUDE file $UDEB_EXCLUDE"
		fi
    fi

	# Ditto the base include and exclude
    if [ -n "$BASE_INCLUDE" ] ; then
		if [ -r "$BASE_INCLUDE" ] ; then
			cp -af "$BASE_INCLUDE" "$BDIR/CD$DISK/.disk/base_include"
		else
			echo "ERROR: Unable to read BASE_INCLUDE file $BASE_INCLUDE"
		fi
    fi
    if [ -n "$BASE_EXCLUDE" ] ; then
		if [ -r "$BASE_EXCLUDE" ] ; then
			cp -af "$BASE_EXCLUDE" "$BDIR/CD$DISK/.disk/base_exclude"
		else
			echo "ERROR: Unable to read BASE_EXCLUDE file $BASE_EXCLUDE"
		fi
	fi
done

# Copy the debs in place, and generate Packages files as we go
for i in $BDIR/*.packages
do
	dir=${i%%.packages}
	n=${dir##$BDIR/}
	dir=$BDIR/CD$n
	echo "$n ... "

	# Copy the deb files in; addpackages also creates the Packages
	# files as it runs
  	sort $i | xargs -n 200 -r $ADDPACKAGES $dir
	if [ -x "$HOOK" ] ; then
	   cd $BDIR && $HOOK $n before-scanpackages
	fi

	# Create the Packages.gz files. Should we also make Packages.bz2?
	for file in `find $dir -name Packages`
	do
		echo "  $file.gz"
		gzip -9 < $file > $file.gz
	done

	# Now the Release files; has to be done last, as they contain the
	# md5sums of the Packages and Packages.gz files
	$SCANPACKAGES $dir

	echo "done."
done
