#!/bin/sh

# upstream has set 1024
# I had use cases that required more than 2048
MAXMEM=3072

# Remark: There should be no need to specify CLASSPATH
# explicitely because it is set in manifest - however, this
# does not work

# Remark: Adding /usr/lib/$(dpkg-architecture -qDEB_BUILD_MULTIARCH)/jni
#         to java.library.path helped solving the following problem:
#  $ beast-mcmc -beagle_info >/dev/null
#  Failed to load BEAGLE library: no hmsbeagle-jni in java.library.path
#  Failed to load BEAGLE library: no hmsbeagle-jni in java.library.path
#  Failed to load BEAGLE library: no hmsbeagle-jni in java.library.path
#
# Using the setting above this turns into
#  BEAGLE resources available:
#  0 : CPU
#      Flags: PRECISION_SINGLE PRECISION_DOUBLE COMPUTATION_SYNCH EIGEN_REAL EIGEN_COMPLEX SCALING_MANUAL SCALING_AUTO SCALING_ALWAYS SCALERS_RAW SCALERS_LOG VECTOR_SSE VECTOR_NONE THREADING_NONE PROCESSOR_CPU FRAMEWORK_CPU

BEAST_LIB=/usr/share/beast2-mcmc/
if echo $@ | grep -q -- -template ; then
   TEMPLATE=""
else
   TEMPLATE="-template ${BEAST_LIB}/templates/Standard.xml"
fi

if [ "$https_proxy" != "" ] ; then
   HTTPSPROXYHOST=`echo $https_proxy | sed -e 's?^https*:/*??' -e 's/:[0-9]*//'`
   HTTPSPROXYPORT=`echo $https_proxy | sed -e 's/^.*:\([0-9]\+\)$/\1/'`
   SETHTTPS="-Dhttps.proxyHost=${HTTPSPROXYHOST} -Dhttps.proxyPort=${HTTPSPROXYPORT}"
fi

if [ "$http_proxy" != "" ] ; then
   HTTPPROXYHOST=`echo $http_proxy | sed -e 's?^http:/*??' -e 's/:[0-9]*//'`
   HTTPPROXYPORT=`echo $http_proxy | sed -e 's/^.*:\([0-9]\+\)$/\1/'`
   SETHTTP="-Dhttp.proxyHost=${HTTPPROXYHOST} -Dhttp.proxyPort=${HTTPPROXYPORT}"
   if [ "$SETHTTPS" = "" ] ; then
      SETHTTPS="-Dhttps.proxyHost=${HTTPPROXYHOST} -Dhttps.proxyPort=${HTTPPROXYPORT}"
   fi
fi

BEAST_LIB="/usr/share/beast2-mcmc"
DEBJAR="/usr/share/java"
java -Xms64m -Xmx${MAXMEM}m -Djava.library.path="$BEAST_LIB" $SETHTTP $SETHTTPS \
   -classpath "${CLASSPATH}:${DEBJAR}/jebl.jar:${DEBJAR}/jam.jar:${DEBJAR}/jdom1.jar:${DEBJAR}/colt.jar:${BEAST_LIB}/beast.jar:$BEAST_LIB/launcher.jar" \
   beast.app.beauti.BeautiLauncher \
   ${TEMPLATE} $*
