#!/bin/sh
#
# $Id: update-cd,v 1.9 2002/12/23 10:03:45 93sam Exp $
# 
# (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=3.0r1

# 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 woody cd set ...
CODENAME=woody

# 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/$CODENAME/r0-r1.diff #~/r2.diff

REL=Debian"$VER"
CLOG=dists/$CODENAME/ChangeLog
NUCLOG=dists/$CODENAME/non-US/ChangeLog
UPD=$TDIR/$CODENAME-update
DATE=`date +%Y%m%d`
BASEDIR=`pwd`
SECTS="main non-free contrib"
VERBOSE=2
export VERBOSE BASEDIR SECTS

ARCHLIST="alpha arm hppa i386 ia64 m68k mips mipsel powerpc s390 sparc src" # all dealt with specially

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
addfiles=$BASEDIR/tools/add_files

adddirs () {
    ROOTDIR=$1
    shift
    FILELIST=$1
    for FILE in `cat $FILELIST`
    do
        DIR=`echo $FILE | sed 's?/[-_\.A-Za-z0-9\+]*$??'`
        if [ ! -d $ROOTDIR/$DIR ] ; then
            mkdir -p $ROOTDIR/$DIR
        fi
    done
}

copy_trees () {
    CDDIR=$1
    shift
    SRCLIST=$1
    shift
    BINLIST=$1
    shift
    ARCHES=$*
    echo "    Copying \"$ARCHES\" files into $UPD/$CDDIR"
    for ARCH in $ARCHES
    do
        case "$ARCH" in
            src)
                NUM=`cat $SRCLIST | wc -l`
                if [ $NUM -gt 0 ] ; then
                    echo "      $ARCH into $UPD/$CDDIR"
                    adddirs $UPD/$CDDIR $SRCLIST
                    cat $SRCLIST | xargs $addfiles $UPD/$CDDIR $MIRROR
                else
                    echo "      No updates needed for $ARCH"
                fi                
                ;;
            *)
                NUM=`cat $BINLIST | grep _$ARCH.deb | wc -l`
                if [ $NUM -gt 0 ] ; then
                    echo "      $ARCH into $UPD/$CDDIR"
                    adddirs $UPD/$CDDIR $BINLIST
                    cat $BINLIST | grep _$ARCH.deb | xargs $addfiles $UPD/$CDDIR $MIRROR
                else
                    echo "      No updates needed for $ARCH"
                fi                
                ;;
        esac
    done
}

create_control_files () {
    CDDIR=$1
    shift
    ARCHES=$*
    echo "  Creating control files for \"$ARCHES\" on $CDDIR"
    for ARCH in $ARCHES
    do
        case "$ARCH" in
            src)
                echo "    src"
                for SECT in $SECTS
                do
                    mkdir -p $CDDIR/dists/$CODENAME/$SECT/source
                    mkdir -p $CDDIR/dists/$CODENAME/non-US/$SECT/source
                done
                cd $UPD
                $scansources $CDDIR >/dev/null 2>&1
                ;;
            all)
                ;;
            *)
                echo "    $ARCH"
                for SECT in $SECTS
                do
                    mkdir -p $CDDIR/dists/$CODENAME/$SECT/binary-$ARCH
                    mkdir -p $CDDIR/dists/$CODENAME/non-US/$SECT/binary-$ARCH
                done
                ARCH=$ARCH $scanpackages scan $CDDIR >/dev/null 2>&1
                ARCH=$ARCH $scanpackages -noPackages.cd install $CDDIR >/dev/null 2>&1
                rm -f $CDDIR/*.Packages* #>/dev/null 2>&1
                ;;
        esac
    done
}

echo Cleaning up
rm -rf $UPD
mkdir $UPD
CDNUM=1

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 \\.gz$ -e \\.dsc$ $UPD/list >$UPD/src-list
else
	egrep -e ^dists.*\\.deb$ $CLOG >$UPD/bin-list
	egrep -e \\.gz$ -e \\.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

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

fi

for THISARCH in $ARCHLIST
do
    if [ $THISARCH = "src" ] ; then
        echo "Creating CD$CDNUM for source"
    else
        echo "Creating CD$CDNUM for binary-$THISARCH and binary-all"
    fi
    mkdir $UPD/CD$CDNUM $UPD/CD$CDNUM/.disk
    INFO="Debian GNU/Linux $VER Update CD $DATE: $THISARCH"
    echo $INFO > $UPD/CD$CDNUM/.disk/info
    echo "  Copying normal files"
    if [ $THISARCH = "src" ] ; then
        copy_trees CD$CDNUM $UPD/src-list $UPD/bin-list $THISARCH
    else
        copy_trees CD$CDNUM $UPD/src-list $UPD/bin-list $THISARCH all
    fi
    if [ "$NONUS"x != ""x ] ; then
        echo "  Copying non-US files"
        if [ $THISARCH = "src" ] ; then
            copy_trees CD$CDNUM $UPD/src-nu-list $UPD/bin-nu-list $THISARCH
        else
            copy_trees CD$CDNUM $UPD/src-nu-list $UPD/bin-nu-list $THISARCH all
        fi
    fi
    echo "  Creating Packages and Sources files"
    cd $UPD
    create_control_files CD$CDNUM $THISARCH
    echo "  Creating image for CD$CDNUM"
    echo -n "    "
    ${MKISOFS} -J -r -V "Debian $VER update CD" -o $OUT/$CODENAME-update-$THISARCH.raw $UPD/CD$CDNUM 2>&1 | grep "extents written"
    CDNUM=$(($CDNUM + 1))
done



