#!/bin/sh

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

if [ ! "$OUTDIR" ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME <output filename> [ <page prefix> ]
EOF
    exit 1
fi

if [ -e "$OUTDIR" ]; then
    echo "Please remove $OUTDIR before generating a new package." 1>&2
    exit 1
fi

MANIFEST="$OUTDIR/MOIN_PACKAGE"

mkdir "$OUTDIR"
cat > "$MANIFEST" <<EOF
MoinMoinPackage|1
EOF

DOCS="$DIRNAME/../wiki"

for FILENAME in "$DOCS/"* ; do
    BASENAME=`basename "$FILENAME"`
    PAGENAME=`echo "$BASENAME" | sed 's/--/\//g'`
    if [ "$PREFIX" ]; then
        if [ "$PAGENAME" = "FrontPage" ]; then
            PAGENAME="$PREFIX"
        else
            PAGENAME="$PREFIX/$PAGENAME"
        fi
    fi
    echo "AddRevision|$BASENAME|$PAGENAME" >> "$MANIFEST"
done

cp "$DOCS/"* "$OUTDIR"
zip -j "$OUTDIR" "$OUTDIR/"*
