#! /bin/sh

set -x

if [ $# -eq 0 ]; then
	echo "Usage:   $(basename $0) AREAS BRANCHES DISTS ARCHS"
	echo "         (multiple values must \"be quoted\" to look as one on the commandline)"
	echo "Example: $(basename $0) misc \"default unstable source\" 'potato sid' i386"
	exit 1
fi

BASE=~/public_websites/debian.jones.dk

AREAS=$1

BRANCHES="$2"

DISTS="$3"

ARCHS="$4"

for ARCH in $ARCHS; do
 for DIST in $DISTS; do
  for BRANCH in $BRANCHES; do
   for AREA in $AREAS; do
	case $BRANCH in
	    default)
		DIR="dists/$DIST/$AREA/binary-$ARCH"
		;;
	    source)
		DIR="dists/$DIST/$AREA/source"
		;;
	    *-source)
		DIR="dists/$DIST/$BRANCH/$AREA/source"
		;;
	    *)
		DIR="dists/$DIST/$BRANCH/$AREA/binary-$ARCH"
		;;
	esac
	echo "Checking directories..."
	if [ -d $BASE/$DIR ]; then
		echo "  $DIR looks fine."
	else
		echo "$0: $BASE/$DIR is not a directory"
		echo
		echo "AREA must exist."
		echo 
		echo "BRANCH can be either default or source, special ones like unstable or private,"
		echo "or space-delimited combinations. Default is \"default\"."
		echo
		echo "DIST can be \"potato\", \"potato woody\" or similar. Default is \"stable\""
		echo
		echo "ARCH is \"i386\" or whatever you have. Default is \"i386\"."
		echo
		echo "The corresponding directory must exist: $BASE/$DIR."
		exit 1
	fi
   done
  done
 done
done
for ARCH in $ARCHS; do
 for DIST in $DISTS; do
  for BRANCH in $BRANCHES; do
   for AREA in $AREAS; do
	SRC=""
	case $BRANCH in
	    default)
		DIR="dists/$DIST/$AREA/binary-$ARCH"
		;;
	    source)
		DIR="dists/$DIST/$AREA/source"
		SRC="1"
		;;
	    *-source)
		DIR="dists/$DIST/$AREA/$BRANCH/source"
		SRC="1"
		;;
	    *)
		DIR="dists/$DIST/$BRANCH/$AREA/binary-$ARCH"
		;;
	esac
	echo "Scanning $DIR..."
	if [ $SRC ]; then
		(cd $BASE; \
			$BASE/tools/dpkg-scansources $DIR /dev/null > $DIR/Sources; \
			touch $DIR/Sources; \
			rm -f $DIR/Sources.gz; \
			gzip -c $DIR/Sources > $DIR/Sources.gz; \
			bzip2 -c $DIR/Sources > $DIR/Sources.bz2; \
		)
	else
		(cd $BASE; \
			$BASE/tools/dpkg-scanpackages $DIR /dev/null > $DIR/Packages; \
			touch $DIR/Packages; \
			rm -f $DIR/Packages.gz; \
			gzip -c $DIR/Packages > $DIR/Packages.gz; \
			bzip2 -c $DIR/Packages > $DIR/Packages.bz2; \
		)
	fi
   done
  done
 done
done
