#!/bin/sh

set -e

OUT=/mnt/nfs-cdimage/unofficial/non-free/firmware
TOPDIR=~/build
SUITE=$1

. $TOPDIR/CONF.sh

if [ "$SUITE"x = ""x ] ; then
    echo "Need to know what to produce!"
    exit 1
fi

# Do stuff here!
export TDIR=/srv/cdbuilder.debian.org/dst/deb-cd/tmp/firmware
export BASEDIR=$TOPDIR/debian-cd
mkdir -p $TDIR
fakeroot $BASEDIR/tools/make-firmware-image $MIRROR $SUITE $TDIR

DATE=`date +%Y%m%d`
mkdir -p $OUT/$SUITE/$DATE
mv $TDIR/firmware*.* $TDIR/*SUMS $OUT/$SUITE/$DATE/

if [ "$RELEASE_BUILD"x = ""x ] ; then
    echo "Signing firmware checksums files using the automatic key"
    ~/build.stretch/sign-images $OUT/$SUITE/$DATE ""
fi

# Update the current link, and delete the old surplus builds. Keep up
# to 3
cd $OUT/$SUITE
rm -f current
ln -sf $DATE current

echo "Removing old firmware directories:"
NUM=`ls -d 20*/ 2>/dev/null |wc -l`
NUM=$(($NUM-6))
if [ "$NUM" -gt "0" ] ; then
    REMOVE=`ls -1d 20* 2>/dev/null|head -n $NUM`
    echo "  $REMOVE"
    rm -rf $REMOVE
fi
