#!/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=/org/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 $BASEDIR

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

# 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
