#!/bin/sh
NAME="Optaplanner"
VERSION="6.4.0.Final"
URL="http://download.jboss.org/optaplanner/release/$VERSION/optaplanner-distribution-$VERSION.zip"
LOCAL="$HOME/optaplanner-distribution-$VERSION.zip"
DEPLOYMENT="/usr/share/java/optaplanner-distribution-$VERSION"

# Double checksum to protect against simple collisions
MD5="59535ecf7c7ebc6ed541402962d15b16"
SHA224="18488c8d456acde26681372de3d5baae31abf13e0f0dbc37f81ccc14"

### There should be no need to edit anything below this line ###

function manualdl() {
    echo "Please download $URL and execute $0 <downloaded file>"
}

if [ -r "$DEPLOYMENT" ]; then
    echo "It seems that $NAME $VERSION is already installed."
    exit 0
fi

if [ $# -gt 0 ]; then
    LOCAL=$(readlink -f $1)
    echo "Using local file $LOCAL."
else
    echo "Trying to download $NAME $VERSION [this might take a while]"
    curl $URL >$LOCAL

    if [ ! -r $LOCAL ]; then
        echo "$NAME $VERSION could not be downloaded."
        manualdl
        exit 1
    fi
fi

cat <<EOF | md5sum -c --status -
$MD5  $LOCAL
EOF

if [ $? -ne 0 ]; then
    echo "$NAME $VERSION archive is corrupted."
    manualdl
    exit 2
fi

cat <<EOF | sha224sum -c --status -
$SHA224  $LOCAL
EOF

if [ $? -ne 0 ]; then
    echo "$NAME $VERSION archive is corrupted."
    manualdl
    exit 3
fi

echo "Unpacking $NAME to the proper directory"
pushd /usr/share/java
unzip $LOCAL &>/dev/null

if [ $? -ne 0 ]; then
    echo "$NAME $VERSION archive could not be extracted to /usr/share/java, check your permissions."
    exit 4
else
  echo "$NAME is installed."
fi

popd


