#!/bin/sh

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

# Common variables used to ease maintenance of this script
INI4J_TARBALL="ini4j-0.5.2-SNAPSHOT-src.zip"
INI4J_TARBALL_CHECKSUM="5a7a172a4e1ccaa149f3db8ed0a15bf20dbd56df"
INI4J_VERSION="0.5.2-SNAPSHOT"

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

while [ "$#" -gt "0" ]
do
    case "$1" in
        --remove-upstream-tarball)
            REMOVE_UPSTREAM_TARBALL=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
    [ -f $INI4J_TARBALL ] || \
        wget -c http://downloads.sourceforge.net/project/ini4j/ini4j-src/0.5.2-SNAPSHOT/$INI4J_TARBALL

    # Verify the checksum
    COMPUTED_CHECKSUM=`sha1sum $INI4J_TARBALL | cut -d ' ' -f 1`
    if [ $INI4J_TARBALL_CHECKSUM != $COMPUTED_CHECKSUM ] ; then
        echo "Checksum verification failed. Checksum was $COMPUTED_CHECKSUM
    Expected checksum $INI4J_TARBALL_CHECKSUM."
        exit 1
    else
        echo "Checksum verified. Checksum is $COMPUTED_CHECKSUM."
    fi

    echo "Copying upstream tarball with different name and running jh_repack."
    cp -f $INI4J_TARBALL ini4j_${INI4J_VERSION}.orig.zip
    jh_repack --upstream-version ${INI4J_VERSION} ini4j_${INI4J_VERSION}.orig.zip
    if [ $REMOVE_UPSTREAM_TARBALL ]; then
        echo -n "Removing upstream tarball..."
        rm $INI4J_TARBALL
        echo "done."
    fi
}

make_current_tarball
