#!/bin/sh

# This script is used to download the upstream source for bsaf
# and generate it into an orig source tarball for Debian.

# Common variables used to ease maintenance of this script
BSAF_VERSION="1.9"
SVN_REVISION="174"

USAGE="\n\
This script is used to generate the orig tarball used in building\n\
Debian packages for bsaf-$BSAF_VERSION.\n\
Usage: bsaf-get-orig-source [OPTION]\n\
\n\
 -h, --help                 Display this help message.\n\
 --remove-upstream-source   Remove upstream sources.\n"

while [ "$#" -gt "0" ]
do
    case "$1" in
        --remove-upstream-source)
            REMOVE_UPSTREAM_SOURCE=1
            shift
            ;;
        -h|--help|*)
            echo "${USAGE}"
            exit 1
            ;;
    esac
done

make_current_tarball() {
    # Download the tarball if it's not available in the current directory
    [ -d "bsaf-1.9" ] || \
        svn export -r $SVN_REVISION "https://svn.kenai.com/svn/bsaf~main/framework/tags/bsaf-1.9"

    tar --exclude-vcs -czf "bsaf_$BSAF_VERSION.orig.tar.gz" "bsaf-1.9/"
    jh_repack --upstream-version $BSAF_VERSION "bsaf_$BSAF_VERSION.orig.tar.gz"

    if [ $REMOVE_UPSTREAM_SOURCE ]; then
        rm -rf "bsaf-1.9"
    fi
}

make_current_tarball
