#!/usr/bin/env sh

##############################################################################
#  This file is part of the SIRIUS Software for analyzing MS and MS/MS data
#
#  Copyright (C) 2013-2020 Kai Dührkop, Markus Fleischauer, Marcus Ludwig, Martin A. Hoffman, Fleming Kretschmer, Marvin Meusel and Sebastian Böcker,
#  Chair of Bioinformatics, Friedrich-Schilller University.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Affero General Public License
#  as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with SIRIUS.
#  If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>
##############################################################################

##############################################################################
##
##  SIRIUS start up script for UN*X
##
##############################################################################

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
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
SAVED="`pwd`"
cd "`dirname \"$PRG\"`" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

# Add default JVM options here. You can also use JAVA_OPTS and SIRIUS_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS="-Xms1G -XX:MaxRAMPercentage=85 -Djava.library.path=\"$GUROBI_HOME/lib\":\"$CPLEX_HOME/bin/x86-64_linux\":\"$APP_HOME/lib/native\":\"$APP_HOME/lib/runtime/lib\""

# Native library path
export LD_LIBRARY_PATH="$GUROBI_HOME/lib:$CPLEX_HOME/bin/x86-64_linux:$APP_HOME/lib/native:$APP_HOME/lib/runtime/lib:$LD_LIBRARY_PATH"

# Add location of jar files for Classpath
# Add location of jar files for Classpath (STANDARD THIRDPARTY configuration)
JAR_HOME="$APP_HOME/../../../All/Sirius/app"

if ! [ -d "$JAR_HOME" ]; then
  # $JAR_HOME does not exist: assuming FLAT THIRDPARTY configuration
  JAR_HOME="$APP_HOME/app"
fi

# Add full lib dir to classpath
CLASSPATH="\"$JAR_HOME/*\":\"$GUROBI_HOME/lib/gurobi.jar\":\"$CPLEX_HOME/lib/cplex.jar\""

# Determine the Java command to use to start the JVM.
JAVACMD="$APP_HOME/lib/runtime/bin/java"

# Main class to run the app
MAIN_CLASS="de.unijena.bioinf.ms.frontend.SiriusCLIApplication"

# Escape application args
save () {
    for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
    echo " "
}
APP_ARGS=`save "$@"`

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- "$DEFAULT_JVM_OPTS" "$JAVA_OPTS" "$SIRIUS_OPTS" -classpath "$CLASSPATH" "$MAIN_CLASS" "$APP_ARGS"

exec "$JAVACMD" "$@"
