#!/bin/sh
# simple script to execute java with the appropriate options to run Mauve
# Change JAVA_CMD to the location of your java binary if necessary
# Change the values of the -Xms and -Xmx parameters if you are getting
# out of memory errors
#
# (c) 2003 Aaron Darling

# JAVA8_CMD should be set before running this script.
# The best place to do that is in $BIRCH/local/local.profile.source
# For testing purposes, you can uncomment the line below and
# modify it for the appropriate Java command on your system.
#JAVA8_CMD="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.322.b06-6.fc35.x86_64/jre/bin/java"


if [ ! -z "$JAVA8_CMD" ]
then
    JAVA_CMD="$JAVA8_CMD"
else    
    JAVA_CMD="java"
fi

echo "JAVA_CMD: " $JAVA_CMD

# Check for Java8
JVERSION=`$JAVA_CMD -version 2>&1 | head -n 1 | cut -f2 -d'"' | cut -f2 -d"."`
echo "JVERSION: "$JVERSION

if [ "$JVERSION" = "8" ] 
then
    JAVA_ARGS="-Xms200M -Xmx500M"

    # this section taken from GPL BEAST code
    ## resolve links - $0 may be a link to application
    PRG="$0"

    # need this for relative symlinks
    while [ -h "$PRG" ] ; do
        ls=`ls -ld "$PRG"`
        link=`expr "$ls" : '.*-> \(.*\)$'`
        if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
        else
    PRG="`dirname "$PRG"`/$link"
        fi
    done

    # make it fully qualified
    saveddir=`pwd`
    MAUVE0=`dirname "$PRG"`
    MAUVE=`cd "$MAUVE0" && pwd`
    cd "$saveddir"
    #end BEAST section

    $JAVA_CMD $JAVA_ARGS -DmauveDir=$MAUVE/ -jar $MAUVE/Mauve.jar $1 $2 $3 $4 $5
else
    MESSAGE="Mauve requires Java8."
    TITLE="Dependencies"
    java -jar $BIRCH/script/OkayBox2.jar "$MESSAGE" "$TITLE"
    chooseviewer.py $BIRCH/public_html/birchadmin/dependencies/dependencies.html
fi