#!/bin/bash

set -e

function download-nanopub-jar {
  >&2 echo "Getting latest nanopub version..."
  NANOPUB_LATEST_LOCATION=$(
    curl --head -s https://github.com/Nanopublication/nanopub-java/releases/latest \
    | egrep -i '^location:'
  )
  NANOPUB_VERSION=${NANOPUB_LATEST_LOCATION##*-}
  NANOPUB_VERSION="${NANOPUB_VERSION%"${NANOPUB_VERSION##*[![:space:]]}"}" 
  >&2 echo "Downloading nanopub jar file version $NANOPUB_VERSION..."
  echo "$SCRIPTDIR/nanopub-${NANOPUB_VERSION}-jar-with-dependencies.jar"
  curl -L --output "$SCRIPTDIR/nanopub-${NANOPUB_VERSION}-jar-with-dependencies.jar" "https://github.com/Nanopublication/nanopub-java/releases/download/nanopub-${NANOPUB_VERSION}/nanopub-${NANOPUB_VERSION}-jar-with-dependencies.jar"
}

WORKINGDIR=`pwd`
cd "$( dirname "${BASH_SOURCE[0]}" )"
SCRIPTDIR=`pwd`

if [ "$1" == "--download" ]; then
  download-nanopub-jar
  exit
fi

cd -P ..
PROJECTDIR=`pwd`
# for Cygwin:
PROJECTDIR=${PROJECTDIR#/cygdrive/?}
cd $WORKINGDIR

NANOPUBJAR=$(find $SCRIPTDIR -maxdepth 1 -name "nanopub-*-jar-with-dependencies.jar" 2>/dev/null | sort -n | tail -1)

JAVAPARAMS="-Dsun.jnu.encoding=utf8 -Dfile.encoding=utf8"

if [ ! -z "$NANOPUBJAR" ]; then
  exec java $JAVAPARAMS -jar $NANOPUBJAR "$@"
fi

NANOPUBJAR=$(find /usr/share/java/ -maxdepth 1 -name "nanopub-*-jar-with-dependencies.jar" 2>/dev/null | sort -n | tail -1)

if [ ! -z "$NANOPUBJAR" ]; then
  exec java $JAVAPARAMS -jar $NANOPUBJAR "$@"
fi

NANOPUBJAR=$(find $PROJECTDIR/target/ -maxdepth 1 -name "nanopub-*-jar-with-dependencies.jar" 2>/dev/null | sort -n | tail -1)

if [ ! -z "$NANOPUBJAR" ]; then
  exec java $JAVAPARAMS -jar $NANOPUBJAR "$@"
fi

>&2 echo "No nanopub jar file found. Downloading it from the web..."
download-nanopub-jar

NANOPUBJAR=$(find $SCRIPTDIR -maxdepth 1 -name "nanopub-*-jar-with-dependencies.jar" 2>/dev/null | sort -n | tail -1)

if [ ! -z "$NANOPUBJAR" ]; then
  exec java $JAVAPARAMS -jar $NANOPUBJAR "$@"
fi

>&2 echo "ERROR: Failed to find or download nanopub jar file."
exit 1