#!/bin/sh

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

# Common variables used to ease maintenance of this script
OPENAL_SOFT_TARBALL="openal-soft-1.13.tar.bz2"
OPENAL_SOFT_TARBALL_CHECKSUM="dc735b8b2ab21f6ec54b4262f150a7e0527ae42aa975a1965b9342df1520443c"
OPENAL_SOFT_VERSION="1.13"
OPENAL_SOFT_ORIG_TARBALL="$(echo "$OPENAL_SOFT_TARBALL" | \
    sed -e s/soft-$OPENAL_SOFT_VERSION/soft_$OPENAL_SOFT_VERSION/ | \
    sed -e s/\.tar\.bz2/.orig.tar.bz2/)"

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

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

make_current_tarball() {
    # Download the tarball if it's not available in the current directory
    [ -f $OPENAL_SOFT_TARBALL ] || \
        wget -c http://kcat.strangesoft.net/openal-releases/$OPENAL_SOFT_TARBALL

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

    echo "Renaming $OPENAL_SOFT_TARBALL to $OPENAL_SOFT_ORIG_TARBALL"
    mv $OPENAL_SOFT_TARBALL $OPENAL_SOFT_ORIG_TARBALL
}

make_current_tarball
