#!/bin/bash
# mklist v 1.04 (c) Steve McIntyre <stevem@chiark.greenend.org.uk>
# Released under GPL 22 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
# $5 is debug level

VERBOSE=$5

function vecho ()
{
	if [ $VERBOSE -gt 0 ] ; then
		echo $*
	fi
}

function vvecho ()
{
	if [ $VERBOSE -gt 1 ] ; then
		echo $*
	fi
}

function vvvecho ()
{
	if [ $VERBOSE -gt 2 ] ; then
		echo $*
	fi
}

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 ]; 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 -v verbose=$VERBOSE '

/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"}
		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
do 
	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 | 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
