#!/bin/sh

DOIT=echo

if [ $2 = "tag" -o $2 = "tar" ];
then
    VERSIONTAG="$1"
    VERSION=`echo $1 | sed 's/Ext-*//g;s/-/./g;'`
else
    case "$1" in
    Ext*) ;;
    *)  echo "VERSION must begin with Ext"
        exit 1 ;;
    esac

    VERSION="$1"
    VERSIONTAG=`echo Ext-$1 | sed 's/\./-/g;'`
fi

if [ $2 = "tag" ]; then
    echo "ERROR: tagging is now handled by the 'makerelease' script."
    exit 1
fi

if [ $2 = "tar" ]; then
    # gnu tar (as of 1.15.1) is unable to create portable tar archives, 
    # especially if long file names (>100 char) are present.
    # star is a better replacement.
    if [ -x /usr/bin/star ]; then
        TAR="/usr/bin/star -Hustar -list=- -s /^/net-snmp-$VERSION\// -z -c -f"
    elif [ -x /bin/tar ]; then
        echo "WARNING: about to create non-portable tar archives using GNU tar."
        echo "You'd better install /usr/bin/star and rerun."
        TAR="tar -T- --transform=s/^/net-snmp-$VERSION\// -czf"
    else
        echo "neither /usr/bin/star nor /bin/tar found."
        exit 1
    fi
    set -x
    git ls-tree --name-only -r HEAD | \
	grep -vE '^(agent/mibgroup/versiontag|ISSUES|makenosysdepend.pl|makefileindepend.pl|remove-files)$|^win32/dist' | \
	$TAR net-snmp-$VERSION.tar.gz
    md5sum net-snmp-$VERSION.tar.gz > net-snmp-$VERSION.tar.gz.md5
    set +x
fi

if [ $2 = "clean" ]; then
    $DOIT rm -fR net-snmp-[0-9]*
fi

exit 0
