#!/bin/sh

# $Id: update-cd,v 1.5 2001/12/16 23:22:11 93sam Exp $
# $Progeny: update-cd,v 1.3 2002/02/12 00:00:35 epg Exp $

set -ex

# (c) Steve McIntyre <stevem@chiark.greenend.org.uk> Released under
# GNU GPL v2 1st January 2001
#
# Quick and dirty script to create update CDs for people to upgrade
# from potato (2.2r0) to the latest release level
#
# Configuration goes here.
# Where is your mirror?
MIRROR=${MIRROR:-/mirror/debian}

# If you have a non-US mirror, where is it? Leave blank if you don't have one.
NONUS=${NONUS:-$MIRROR/non-US}

# Do you want non-free? 1 for yes, 0 for no
NONFREE=1

# What release version is this?
VER=2.2r4

# Path to use with mkisofs/mkhybrid
MKISOFS=${MKISOFS:-"mkhybrid"}

# The working directory to use. MUST be on the same partition as the mirror.
TDIR=/mirror/debian-cd

# Building potato cd set ...
CODENAME=potato

# Path where the images will be written
OUT=/mirror/debian-cd

# Location of the diff file to use to determine the changes. If you leave
# this blank, we'll try to determine the changes from the ChangeLog files,
# which is probably less accurate. 
DIFF=/mirror/lists/r0-r4.diff #~/r2.diff

REL=Debian"$VER"
CLOG=dists/potato/ChangeLog
NUCLOG=dists/potato/non-US/ChangeLog
UPD=$TDIR/potato-update
DATE=`date +%Y%m%d`
BASEDIR=`pwd`
INFO1="Debian GNU/Linux $VER Update CD $DATE for Alpha, Arm and i386"
INFO2="Debian GNU/Linux $VER Update CD $DATE for M68k, PowerPC and Sparc"
INFO3="Debian GNU/Linux $VER Update CD $DATE source"
ARCH1="alpha arm i386"
ARCH2="m68k powerpc sparc"

export TDIR NONFREE NONUS VER MIRROR CODENAME OUT BASEDIR

scanpackages=$BASEDIR/tools/scanpackages.old
scansources=$BASEDIR/tools/scansources.old
set_mkisofs_opts=$BASEDIR/tools/set_mkisofs_opts
adddirs=$BASEDIR/tools/add_dirs
addfiles=$BASEDIR/tools/add_files

echo Cleaning up
rm -rf $UPD
mkdir $UPD $UPD/CD1 $UPD/CD2 $UPD/CD3

echo Creating directory trees
for ARCH in $ARCH1
do
	export ARCH
	$adddirs $UPD/CD1
done

for ARCH in $ARCH2
do
	export ARCH
	$adddirs $UPD/CD2
done

$adddirs $UPD/CD3

cd $MIRROR

echo Creating main-section list

if [ -e $DIFF ] ; then
	grep -v non-US $DIFF >$UPD/list
	egrep -e \\.deb$ $UPD/list >$UPD/bin-list
	egrep -e ^dists.*\\.gz$ -e ^dists.*\\.dsc$ $UPD/list >$UPD/src-list
else
	egrep -e ^dists.*\\.deb$ $CLOG >$UPD/bin-list
	egrep -e ^dists.*\\.gz$ -e ^dists.*\\.dsc$ $CLOG >$UPD/src-list
fi
if [ "$NONFREE"x != "1"x ] ; then
	echo Removing non-free
	grep -v non-free $UPD/bin-list > $UPD/bin-list1
	mv -f $UPD/bin-list1 $UPD/bin-list
	grep -v non-free $UPD/src-list > $UPD/src-list1
	mv -f $UPD/src-list1 $UPD/src-list
fi

echo Creating trees
cat $UPD/bin-list | xargs $addfiles $UPD/CD1 $MIRROR
cat $UPD/bin-list | xargs $addfiles $UPD/CD2 $MIRROR
cat $UPD/src-list | xargs $addfiles $UPD/CD3 $MIRROR

if [ "$NONUS"x != ""x ] ; then
	echo Creating non-US list
	cd $NONUS
	if [ -e $DIFF ] ; then
		grep non-US $DIFF >$UPD/nu-list
		egrep -e \\.deb$ $UPD/nu-list >$UPD/bin-nu-list
		egrep -e ^dists.*\\.gz$ -e ^dists.*\\.dsc$ $UPD/nu-list >$UPD/src-nu-list
	else
		egrep -e ^dists.*\\.deb$ $NUCLOG | sed 's?^stable?dists/potato?g' > $UPD/bin-nu-list
		egrep -e ^dists.*\\.gz$ -e ^dists.*\\.dsc$ $NUCLOG | sed 's?^stable?dists/potato?g' > $UPD/src-nu-list
	fi

	if [ "$NONFREE"x != "1"x ] ; then
		echo Removing non-free
		grep -v non-free $UPD/bin-nu-list > $UPD/bin-nu-list1
		mv -f $UPD/bin-nu-list1 $UPD/bin-nu-list
		grep -v non-free $UPD/src-nu-list > $UPD/src-nu-list1
		mv -f $UPD/src-nu-list1 $UPD/src-nu-list
	fi

	echo Creating non-US trees
	cat $UPD/bin-nu-list | xargs $addfiles $UPD/CD1 $MIRROR
	cat $UPD/bin-nu-list | xargs $addfiles $UPD/CD2 $MIRROR
	cat $UPD/src-nu-list | xargs $addfiles $UPD/CD3 $MIRROR
fi

# The full update will not fit on a single CD, so split it by architecture, arbitrarily
# CD 1 gets alpha/arm/i386
# CD 2 gets m68k/sparc/powerpc
# binary-all goes on both
# CD 3 gets source

cd $UPD

echo Splitting binary dirs between trees

for ARCH in $ARCH2
do
	find CD1 -type d | egrep -e binary-$ARCH$ | xargs rm -r
done
find CD1 -type d | grep source$ | xargs rm -r

for ARCH in $ARCH1
do
	find CD2 -type d | egrep -e binary-$ARCH$ | xargs rm -r
done
find CD2 -type d | grep source$ | xargs rm -r

for ARCH in $ARCH1 $ARCH2 all
do
	find CD3 -type d | grep -e binary-$ARCH$ | xargs rm -r >/dev/null 2>&1
done

# Now create the binary-all symlinks

echo Creating binary-all symlinks
for DIR in `find . -name binary-all`
do
	cd $DIR
	for CAT in *
	do
		if [ $CAT != '*' ] ; then
			if [ -d $CAT ] ; then
				cd $CAT
			fi
			for FILE in *.deb
			do
				if [ $FILE != '*.deb' ] ; then
					for ARCH in alpha arm i386 m68k powerpc sparc
					do
						if [ -d ../../binary-$ARCH ] ; then
							if [ ! -d ../../binary-$ARCH/$CAT ] ; then
								mkdir ../../binary-$ARCH/$CAT
							fi
						ln -s ../../binary-all/$CAT/$FILE ../../binary-$ARCH/$CAT
						fi
					done
				fi
			done
			if [ -d ../$CAT ] ; then
				cd ..
			fi
		fi
	done
	cd $UPD
done

echo Creating .disk/info files
echo $INFO1 >CD1/.disk/info
echo $INFO2 >CD2/.disk/info
echo $INFO3 >CD3/.disk/info

echo Creating Packages files
for ARCH in $ARCH1
do
	echo $ARCH
	ARCH=$ARCH $scanpackages scan CD1
	ARCH=$ARCH $scanpackages -noPackages.cd install CD1
	rm -f CD1/*.Packages*
done

for ARCH in $ARCH2
do
	echo $ARCH
	ARCH=$ARCH $scanpackages scan CD2
	ARCH=$ARCH $scanpackages -noPackages.cd install CD2
	rm -f CD2/*.Packages*
done

echo Creating Sources files
$scansources CD3

rm CD?/CD?.*

${MKISOFS} -J -r -V "Debian $VER update CD" -o $OUT/potato-update-1.iso CD1
${MKISOFS} -J -r -V "Debian $VER update CD" -o $OUT/potato-update-2.iso CD2
${MKISOFS} -J -r -V "Debian $VER update CD" -o $OUT/potato-update-3.iso CD3
