#!/bin/bash
# mklist v 1.12 (c) Steve McIntyre <stevem@chiark.greenend.org.uk>
# Released under GPL 12 Mar 1999
# See the file COPYING for license details
# Released as part of the slink_cd package, not much use standalone
#
# $1 is Debian-mirror location
# $2 is start directory location (where the scripts live)
# $3 is tmpdir location
# $4 is the binary arch
# $5 is debug level

VERBOSE=$5

. $2/vecho # Local definitions for vecho, vvecho, vvvecho

vecho Creating file lists for CDs 1 and 2

cd $3

rm -rf list
mkdir list

if [ -f $1/dists/slink/main/binary-$4/Packages.gz ]; then
	PACKAGES=$1/dists/slink/main/binary-$4/Packages.gz
elif [ -f $1/dists/slink/main/binary-$4/Packages ]; then
	PACKAGES=$1/dists/slink/main/binary-$4/Packages
else
	echo "$0: Could not find packages file. Exit."
	exit 1
fi

# AAAAAAAAaargh! We're f**ked if the Packages file is out-of-date
# (e.g. borken mirror), so check if there are any newer files in the
# archive _now_. If so, we'll need to create a newer copy for our own
# purposes. Warn the user a _lot_, as this may be a symptom of a
# really broken setup...

find $1/dists/slink/main/binary-$4 -type f -follow -newer $PACKAGES >$TDIR/newer.packages
if [ -s $3/newer.packages ] ; then
	PACKAGES=$3/Packages.regenerated
	echo "   Awooga!"
	echo "   Awooga!"
	echo "   Awooga!"
	echo "   Awooga! The main Packages file in the mirror is out of date."
	echo "   Generating a new one in $PACKAGES."
	echo "   Your md5sums results will be bogus, and so might your images."
	echo "   Awooga!"
	echo "   Awooga!"
	echo "   Awooga!"
	echo "   Awooga!"
	(cd $1/dists/slink/main && \
		dpkg-scanpackages binary-$4 $1/indices/override.slink.gz \
		dists/stable/main/ > $PACKAGES)
	ls -al $PACKAGES
fi

# optional/extra packages that should be on disc 1 should be listed in
# slink1.needed. Important/required/standard packages will
# automatically go there, and duplication is OK. slink1.needed no
# longer needs to be up-to-date with the boot-floppies package, as we
# generate it from their master file now.

# Remove hashed-out lines to allow hash comments...
# And use EOP as a header between sections
# the first section should be the list of packages to go on CD 1
# the second section should be the list of packages to remove _completely_
# the third is the Packages file, i.e. the raw data that we're going to use.
# Output will go to list1 for CD 1, list2 for CD 2, EXCLUDED for removed.

zcat -f $3/slink1.needed $3/slink1.useful $2/EOP $2/exclude-$4 $2/EOP $PACKAGES | grep -v '^#' | awk -v verbose=$VERBOSE '

/END_OF_PROCESSING/       {leave_done++}

/.*/            {
					if(!leave_done) 
					{
						leave[$1]++
					} 
					else 
					{
						if(leave_done==1)
						{
							remove[$1]++
						}
					}
				}

/^Package:/     {
					if(leave_done==2) 
					{
						priority="unlisted"; 
						package=$2; 
						list="list2" 
					}
				} 

/^Priority:/	{
					if(leave_done==2) 
					{
						priority = $2;
						if(leave[package]) 
						{ 
							list="list1" 
						} 
						if(remove[package])
						{ 
							list="EXCLUDED" 
						}
					}
				}

/^Filename:/    {
					if(leave_done==2) 
					{
						filename = $2
					}
				}

/^Size:/        {if(leave_done==2) 
	{
		if(match(priority,"^required"))    {list="list1"}
		if(match(priority,"^important"))   {list="list1"}
		if(match(priority,"^standard"))    {list="list1"}
		if(verbose>1) {print priority, "package ", package, "moved to", list}
		print package,filename," ",$2 >>"list/"list
		print package,filename," ",$2 >>"list/"priority
	}
}
'

cd list

for file in *
do
    sort $file -o $file.1
    mv $file.1 $file
done

vecho "Sizes for each priority in main binary-$4 (bytes):"
for file in required important standard optional extra unlisted
do 
	if [ -e $file ] ; then
		vecho -n $file
		if [ "$VERBOSE" -gt 0 ] ; then
			cat $file | awk '{size=size+$3};END{print " ",size}'
		fi
	fi
done

vecho
vecho "Sizes for each disc (bytes):"
for file in list1 list2 EXCLUDED
do 
	touch $file # so we don't get ENOENT
	vecho -n $file
	if [ "$VERBOSE" -gt 0 ] ; then
		cat $file | awk '{size=size+$3};END{print " ",size}'
	fi
done
vecho "These only count packages in main binary-$4. Other"
vecho "sections will fill these two discs up further"

cd $1
for i in 1 2
do
	vecho "Filling in binary-all links... $i"
    for file in `cat $3/list/list$i | sed 's/frozen/slink/g;s/stable/slink/g' | awk -v ARCH=$4 '{print $2; REPL=sprintf("binary-%s/",ARCH);gsub(REPL,"binary-all/",$2); print $2}'`
    do
	if [ -f $file ] ; then
		vvecho $file
	    echo $file >>/$3/list/OUT$i
	fi
    done
done


