#!/bin/sh

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

# Common variables used to ease maintenance of this script
BEANSBINDING_TARBALL="beansbinding-1.2.1-src.zip"
BEANSBINDING_TARBALL_CHECKSUM="faf50715f7ca020bbac2c4dad63c2595ac00f1f4"
BEANSBINDING_VERSION="1.2.1+repack1"

USAGE="\n\
This script is used to generate the orig tarball used in building\n\
Debian packages for beansbinding-$BEANSBINDING_VERSION.\n\
Usage: beansbinding-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 $BEANSBINDING_TARBALL ] || \
        wget -c https://beansbinding.dev.java.net/files/documents/6779/73673/$BEANSBINDING_TARBALL

    # Verify the checksum
    COMPUTED_CHECKSUM=`sha1sum $BEANSBINDING_TARBALL | cut -d ' ' -f 1`
    if [ $BEANSBINDING_TARBALL_CHECKSUM != $COMPUTED_CHECKSUM ] ; then
        echo "Checksum verification failed. Checksum was $COMPUTED_CHECKSUM
    Expected checksum $BEANSBINDING_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 $BEANSBINDING_TARBALL beansbinding_${BEANSBINDING_VERSION}.orig.zip
    jh_repack --upstream-version ${BEANSBINDING_VERSION} beansbinding_${BEANSBINDING_VERSION}.orig.zip
    if [ $REMOVE_UPSTREAM_TARBALL ]; then
        echo -n "Removing upstream tarball..."
        rm $BEANSBINDING_TARBALL
        echo "done."
    fi
}

make_current_tarball
