#!/bin/sh
# Rebuilds the package index and signs with GPG.  That means you will need
# a GPG key.  
# In $root/dists/$distro it expects to find a file called Release.boiler 
# containing the fixed material of the Release  file, e.g. "Origin: Nokia", 
# "Label: Maemo", etc. (change for your own organization).  

# Author: James F. Carter, 2006-08-05

# "Official" subdirectories are: 
# repository.maemo.org: user/office user/other user/system-tools user/tools
# catalogue.tableteer.nokia.com: user/games user/themes

arch=binary-armel
distro=mistral

distdir=dists/$distro
root=`dirname $0`
root=`(cd $root ; pwd)`
sections=`(cd $root/$distdir ; echo *)`
over=`mktemp /tmp/scanpkg-over.XXXXXX`

for sect0 in $sections ; do
    pkgdir=$distdir/$sect0/$arch
    if [ ! -d $root/$pkgdir ] ; then continue ; fi
    echo "Doing section '$sect0'"
    cd $root/$pkgdir
		# Format of override file:
		# pkg-basename priority section
		# For this cache, use
		# pkg-basename extra user
    cp /dev/null $over
    find . -name "*.deb" -print | \
    while read f junk ; do
	section=`dirname $f | sed -e 's/\.\/*//'`
	if [ -z "$section" ] ; then section=$sect0 ; fi
	dpkg-deb -f $f Package | sed -e 's%$% extra '"$sect0/$section%" >> $over
    done

    cd $root
    dpkg-scanpackages $pkgdir $over > $pkgdir/Packages
    gzip -9c < $pkgdir/Packages > $pkgdir/Packages.gz
done

cd $root/$distdir
cp -p Release.boiler Release
echo "Date: `date`" >> Release
t=`find */* \( -name "Release*" -o -name "Packages*" \) -print`

# $1 = section label; $2 = program to make sums; subsequent args are the
# filenames to be summed in the Release file.
function addsums () {
    local title pgm
    title=$1 ; shift ; pgm=$1 ; shift
    echo "$title:" >> Release
    for f in $* ; do 
	size=`stat -c "%s" $f`
                # md5sum and sha1sum produce output in the form "SUM  fname"
		# with 2 blanks separating.  However, the Release file format
		# appears to tolerate repeated blanks, so I don't worry.
	$pgm $f | sed -e "s/ / $size /" -e 's/^/ /' >> Release
    done
}

addsums MD5Sum  md5sum  $t
addsums SHA1    sha1sum $t
rm -f Release.gpg
# gpg-1.4.2 emits a return code of 1 if the key agent was attempted and not
# found.  Hiss, boo. 
if ( gpg -a -o Release.gpg -u jimc --detach-sign Release || \
    [ -f Release.gpg ] ) && \
    gpg --verify Release.gpg Release
then : ; else
    echo "Failed to sign $PWD/Release, you need to fix this. ($?)"
fi

rm -f $over
