#!/bin/sh

DIRNAME=`dirname $0`
PROGNAME=`basename $0`
OUTDIR=$1
FORCE=$2

if [ ! "$OUTDIR" ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME <output directory> [ -f ]
EOF
    exit 1
fi

if [ "$FORCE" != '-f' ]; then
    FORCE=
fi

if [ ! -e "$OUTDIR" ]; then
    mkdir -p "$OUTDIR"
fi

for TAG in `hg tags | grep ^rel- | cut -f 1 -d ' '` ; do
    NUM=`echo "$TAG" | sed 's/rel-//;s/-/./g'`
    OUTFILE="$OUTDIR/imip-agent-$NUM.tar.bz2"
    if [ ! -e "$OUTFILE" ] || [ "$FORCE" ]; then
        hg archive -t tbz2 -r "$TAG" "$OUTFILE"
    fi
    echo "$OUTFILE"
done
