#!/bin/sh

################################################################################
# Title: Force Field X.
#
# Description: Force Field X - Software for Molecular Biophysics.
#
# Copyright: Copyright (c) Michael J. Schnieders 2001-2026.
#
# This file is part of Force Field X.
#
# Force Field X is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3 as published by
# the Free Software Foundation.
#
# Force Field X 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
# Linking this library statically or dynamically with other modules is making a
# combined work based on this library. Thus, the terms and conditions of the
# GNU General Public License cover the whole combination.
#
# As a special exception, the copyright holders of this library give you
# permission to link this library with independent modules to produce an
# executable, regardless of the license terms of these independent modules, and
# to copy and distribute the resulting executable under terms of your choice,
# provided that you also meet, for each linked independent module, the terms
# and conditions of the license of that module. An independent module is a
# module which is not derived from or based on this library. If you modify this
# library, you may extend this exception to your version of the library, but
# you are not obligated to do so. If you do not wish to do so, delete this
# exception statement from your version.
################################################################################

BASEDIR=$(dirname "$0")/..
BASEDIR=$( (cd "$BASEDIR" || exit; pwd) )

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "$(uname)" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
             echo "Using Java version: $JAVA_VERSION"
           fi
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
           fi
           ;;
esac

if [ -z "$JAVA_HOME" ] ; then
  if [ -r /etc/gentoo-release ] ; then
    JAVA_HOME=$(java-config --jre-home)
  fi
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
  [ -n "$CLASSPATH" ] && CLASSPATH=$(cygpath --path --unix "$CLASSPATH")
fi

# If a specific java binary isn't specified search for the standard 'java' binary
if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=$(which java)
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

if [ -z "$REPO" ]
then
  REPO="$BASEDIR"
fi

if [ "$CLASSPATH_PREFIX" != "" ]; then
    CLASSPATH=$CLASSPATH_PREFIX:"$BASEDIR/lib/*"
else
    CLASSPATH="$BASEDIR/lib/*"
fi

EXTRA_JVM_ARGUMENTS="-Xms1G -Xmx1G -Xss1M"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$CLASSPATH" ] && CLASSPATH=$(cygpath --path --windows "$CLASSPATH")
  [ -n "$JAVA_HOME" ] && JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME")
  [ -n "$HOME" ] && HOME=$(cygpath --path --windows "$HOME")
  [ -n "$BASEDIR" ] && BASEDIR=$(cygpath --path --windows "$BASEDIR")
  [ -n "$REPO" ] && REPO=$(cygpath --path --windows "$REPO")
fi

exec "$JAVACMD" $JAVA_OPTS $EXTRA_JVM_ARGUMENTS \
  -classpath "$CLASSPATH" \
  -Dapp.name="FFX Job Scheduler" \
  -Dapp.pid="$$" \
  -Dapp.repo="$REPO" \
  -Dbasedir="$BASEDIR" \
  edu.rit.pj.cluster.JobScheduler \
  "$@"


