#!/bin/sh

set -e

MIRROR=$1
SUITE=$2
TMPDIR=$3
BASEDIR=$4
export LC_ALL=C

if [ "$MIRROR"x = ""x ] || \
	[ "$SUITE"x = ""x ] || \
	[ "$TMPDIR"x = ""x ] || \
	[ "$BASEDIR"x = ""x ] ; then
	echo "$0: Need parameters"
	exit 1
fi

TASKFILE=$BASEDIR/tasks/firmware

rm -rf $TMPDIR/firmware
mkdir -p $TMPDIR/firmware

FILES=`( cpp -undef $TASKFILE | grep -v -e "^#" -e "^$" ; \
	echo DONE ; 
	zcat $MIRROR/dists/$SUITE/non-free/binary-*/Packages.gz ) | \
	awk '
    /DONE/ {
        in_packages = 1
        matched = 0
        next
    }
    /.*/ {
        if (!in_packages) {
            firmware[$1] = num_fw
            num_fw++
            next
        }
    }
    /^Package: / {
        matched = 0
        if (in_packages) {
            for (fw in firmware) {
                if (fw == $2) {
                    matched = 1
                }
            }
            next
        }
    }
    /^Filename: / {
        if (matched) {
            print $2
        }
    }' | sort -u`

for file in $FILES; do
	cp $MIRROR/$file $TMPDIR/firmware/
done	

cd $TMPDIR/firmware
tar czf ../firmware.tar.gz .
zip -9rq ../firmware.zip .
cd ..
ls -l $PWD/firmware.tar.gz $PWD/firmware.zip
