#!/bin/bash
# mklist v 1.03 (c) Steve McIntyre <stevem@chiark.greenend.org.uk>
# Released under GPL 21 Jan 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

echo Creating file lists for CDs 1 and 2

cd $3

rm -rf list
mkdir list

if [ -f $1/dists/slink/main/binary-$4/Packages ]; then
	PACKAGES=$1/dists/slink/main/binary-$4/Packages
elif [ -f $1/dists/slink/main/binary-$4/Packages.gz ]; then
	cp $1/dists/slink/main/binary-$4/Packages.gz $3/list/Packages-bin-$4.gz
	gunzip $3/list/Packages-bin-$4.gz
	PACKAGES=$3/list/Packages-bin-$4
else
	exit 1
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.
# Make sure the list slink1.needed is up-to-date with the
# boot-floppies package.

# Remove hashed-out lines to allow hash comments...
cat $3/slink1.needed $3/slink1.useful $2/EOP $PACKAGES | grep -v '^#' | awk '

/END_OF_PROCESSING/       {leave_done++}

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

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

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

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

/^Size:/        {if(leave_done) 
	{
		if(match(priority,"^required"))    {list="list1"}
		if(match(priority,"^important"))   {list="list1"}
		if(match(priority,"^standard"))    {list="list1"}
		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

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

echo
echo "Sizes for each disc (bytes):"
for file in list1 list2
do 
    echo -n $file
    cat $file | awk '{size=size+$3};END{print " ",size}'
done
echo "These only count packages in main binary-$4. Other"
echo "sections will fill these two discs up further"

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