#!/usr/bin/env sh

##############################################################################
##
##  riskscape 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

APP_NAME="riskscape"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and RISKSCAPE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS="-Driskscape.app_home=${APP_HOME} """

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
    echo "$*"
}

die ( ) {
    echo
    echo "$*"
    echo
    exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
  CYGWIN* )
    cygwin=true
    ;;
  Darwin* )
    darwin=true
    ;;
  MINGW* )
    msys=true
    ;;
  NONSTOP* )
    nonstop=true
    ;;
esac

CLASSPATH=$APP_HOME/lib/app.jar:$APP_HOME/lib/core.jar:$APP_HOME/lib/common.jar:$APP_HOME/lib/api.jar:$APP_HOME/lib/i18n.jar:$APP_HOME/lib/openstack4j-httpclient-3.1.0.jar:$APP_HOME/lib/openstack4j-core-3.1.0.jar:$APP_HOME/lib/gt-arcgrid-31.2.jar:$APP_HOME/lib/gt-geotiff-31.2.jar:$APP_HOME/lib/gt-geopkg-31.2.jar:$APP_HOME/lib/gt-coverage-31.2.jar:$APP_HOME/lib/jt-zonal-1.1.25.jar:$APP_HOME/lib/jt-stats-1.1.25.jar:$APP_HOME/lib/json-patch-1.9.jar:$APP_HOME/lib/jackson-coreutils-1.6.jar:$APP_HOME/lib/guava-33.0.0-jre.jar:$APP_HOME/lib/jul-to-slf4j-1.7.25.jar:$APP_HOME/lib/logback-classic-1.5.3.jar:$APP_HOME/lib/metrics-core-4.0.0.jar:$APP_HOME/lib/imageio-ext-cog-reader-1.4.12.jar:$APP_HOME/lib/imageio-ext-cog-streams-1.4.12.jar:$APP_HOME/lib/ehcache-3.4.0.jar:$APP_HOME/lib/slf4j-api-2.0.12.jar:$APP_HOME/lib/jline-terminal-jna-3.21.0.jar:$APP_HOME/lib/jline-reader-3.21.0.jar:$APP_HOME/lib/jline-terminal-3.21.0.jar:$APP_HOME/lib/failureaccess-1.0.2.jar:$APP_HOME/lib/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:$APP_HOME/lib/msg-simple-1.1.jar:$APP_HOME/lib/btf-1.2.jar:$APP_HOME/lib/jsr305-3.0.2.jar:$APP_HOME/lib/postgresql-42.3.10.jar:$APP_HOME/lib/checker-qual-3.41.0.jar:$APP_HOME/lib/error_prone_annotations-2.23.0.jar:$APP_HOME/lib/groovy-all-2.4.21.jar:$APP_HOME/lib/json-20180130.jar:$APP_HOME/lib/jts-io-common-1.19.0.jar:$APP_HOME/lib/javacsv-2.0.jar:$APP_HOME/lib/ini4j-0.5.4.jar:$APP_HOME/lib/commons-math3-3.6.1.jar:$APP_HOME/lib/gt-shapefile-31.2.jar:$APP_HOME/lib/gt-wfs-ng-31.2.jar:$APP_HOME/lib/gt-cql-31.2.jar:$APP_HOME/lib/gt-csv-31.2.jar:$APP_HOME/lib/gt-geojson-31.2.jar:$APP_HOME/lib/gt-xsd-wfs-31.2.jar:$APP_HOME/lib/gt-xsd-fes-31.2.jar:$APP_HOME/lib/gt-xsd-ows-31.2.jar:$APP_HOME/lib/gt-xsd-filter-31.2.jar:$APP_HOME/lib/gt-xsd-gml3-31.2.jar:$APP_HOME/lib/gt-complex-31.2.jar:$APP_HOME/lib/gt-xsd-kml-31.2.jar:$APP_HOME/lib/gt-app-schema-resolver-31.2.jar:$APP_HOME/lib/gt-xsd-gml2-31.2.jar:$APP_HOME/lib/gt-xsd-core-31.2.jar:$APP_HOME/lib/gt-xml-31.2.jar:$APP_HOME/lib/gt-gml-geometry-streaming-31.2.jar:$APP_HOME/lib/gt-http-commons-31.2.jar:$APP_HOME/lib/net.opengis.wfs-31.2.jar:$APP_HOME/lib/gt-jdbc-31.2.jar:$APP_HOME/lib/gt-main-31.2.jar:$APP_HOME/lib/gt-epsg-wkt-31.2.jar:$APP_HOME/lib/gt-epsg-hsql-31.2.jar:$APP_HOME/lib/gt-referencing-31.2.jar:$APP_HOME/lib/gt-http-31.2.jar:$APP_HOME/lib/imageio-ext-cog-commons-1.4.12.jar:$APP_HOME/lib/imageio-ext-tiff-1.4.12.jar:$APP_HOME/lib/jt-affine-1.1.25.jar:$APP_HOME/lib/jt-algebra-1.1.25.jar:$APP_HOME/lib/jt-bandmerge-1.1.25.jar:$APP_HOME/lib/jt-bandselect-1.1.25.jar:$APP_HOME/lib/jt-bandcombine-1.1.25.jar:$APP_HOME/lib/jt-warp-1.1.25.jar:$APP_HOME/lib/jt-format-1.1.25.jar:$APP_HOME/lib/jt-border-1.1.25.jar:$APP_HOME/lib/jt-buffer-1.1.25.jar:$APP_HOME/lib/jt-crop-1.1.25.jar:$APP_HOME/lib/jt-mosaic-1.1.25.jar:$APP_HOME/lib/jt-lookup-1.1.25.jar:$APP_HOME/lib/jt-rescale-1.1.25.jar:$APP_HOME/lib/jt-scale-1.1.25.jar:$APP_HOME/lib/jt-scale2-1.1.25.jar:$APP_HOME/lib/jt-nullop-1.1.25.jar:$APP_HOME/lib/jt-translate-1.1.25.jar:$APP_HOME/lib/jt-binarize-1.1.25.jar:$APP_HOME/lib/jt-colorconvert-1.1.25.jar:$APP_HOME/lib/jt-errordiffusion-1.1.25.jar:$APP_HOME/lib/jt-orderdither-1.1.25.jar:$APP_HOME/lib/jt-colorindexer-1.1.25.jar:$APP_HOME/lib/jt-imagefunction-1.1.25.jar:$APP_HOME/lib/jt-classifier-1.1.25.jar:$APP_HOME/lib/jt-piecewise-1.1.25.jar:$APP_HOME/lib/jt-rlookup-1.1.25.jar:$APP_HOME/lib/jt-vectorbin-1.1.25.jar:$APP_HOME/lib/jt-shadedrelief-1.1.25.jar:$APP_HOME/lib/jt-utilities-1.1.25.jar:$APP_HOME/lib/jt-iterators-1.1.25.jar:$APP_HOME/lib/imageio-ext-arcgrid-1.4.12.jar:$APP_HOME/lib/gt-metadata-31.2.jar:$APP_HOME/lib/imageio-ext-geocore-1.4.12.jar:$APP_HOME/lib/imageio-ext-streams-1.4.12.jar:$APP_HOME/lib/imageio-ext-utilities-1.4.12.jar:$APP_HOME/lib/net.opengis.fes-31.2.jar:$APP_HOME/lib/net.opengis.ows-31.2.jar:$APP_HOME/lib/org.w3.xlink-31.2.jar:$APP_HOME/lib/gt-api-31.2.jar:$APP_HOME/lib/jai_core-1.1.3.jar:$APP_HOME/lib/json-simple-1.1.1.jar:$APP_HOME/lib/jna-5.9.0.jar:$APP_HOME/lib/logback-core-1.5.3.jar:$APP_HOME/lib/jt-zonalstats-1.6.0.jar:$APP_HOME/lib/jt-utils-1.6.0.jar:$APP_HOME/lib/jts-core-1.19.0.jar:$APP_HOME/lib/opencsv-5.0.jar:$APP_HOME/lib/commons-text-1.12.0.jar:$APP_HOME/lib/jackson-dataformat-yaml-2.17.1.jar:$APP_HOME/lib/jackson-databind-2.17.1.jar:$APP_HOME/lib/jackson-annotations-2.17.1.jar:$APP_HOME/lib/jackson-core-2.17.1.jar:$APP_HOME/lib/re2j-1.6.jar:$APP_HOME/lib/ejml-ddense-0.41.jar:$APP_HOME/lib/jai_imageio-1.1.jar:$APP_HOME/lib/commons-io-2.16.1.jar:$APP_HOME/lib/commons-lang3-3.14.0.jar:$APP_HOME/lib/sqlite-jdbc-3.41.2.2.jar:$APP_HOME/lib/httpclient-4.5.14.jar:$APP_HOME/lib/hsqldb-2.7.2.jar:$APP_HOME/lib/commons-dbcp-1.4.jar:$APP_HOME/lib/commons-pool-1.5.4.jar:$APP_HOME/lib/jgridshift-core-1.3.jar:$APP_HOME/lib/GeographicLib-Java-1.49.jar:$APP_HOME/lib/ejml-core-0.41.jar:$APP_HOME/lib/aircompressor-0.27.jar:$APP_HOME/lib/jai_codec-1.1.3.jar:$APP_HOME/lib/bigint-0.7.1.jar:$APP_HOME/lib/commons-collections4-4.4.jar:$APP_HOME/lib/xml-commons-resolver-1.2.jar:$APP_HOME/lib/org.eclipse.emf.ecore.xmi-2.15.0.jar:$APP_HOME/lib/org.eclipse.emf.ecore-2.15.0.jar:$APP_HOME/lib/org.eclipse.emf.common-2.15.0.jar:$APP_HOME/lib/picocontainer-1.2.jar:$APP_HOME/lib/commons-jxpath-1.3.jar:$APP_HOME/lib/org.eclipse.xsd-2.12.0.jar:$APP_HOME/lib/snakeyaml-2.2.jar:$APP_HOME/lib/jaxb-runtime-2.4.0-b180830.0438.jar:$APP_HOME/lib/jaxb-api-2.4.0-b180830.0359.jar:$APP_HOME/lib/javax.activation-api-1.2.0.jar:$APP_HOME/lib/httpcore-4.4.16.jar:$APP_HOME/lib/commons-logging-1.2.jar:$APP_HOME/lib/commons-codec-1.11.jar:$APP_HOME/lib/systems-common-2.1.jar:$APP_HOME/lib/indriya-2.1.3.jar:$APP_HOME/lib/txw2-2.4.0-b180830.0438.jar:$APP_HOME/lib/istack-commons-runtime-3.0.7.jar:$APP_HOME/lib/stax-ex-1.8.jar:$APP_HOME/lib/FastInfoset-1.2.15.jar:$APP_HOME/lib/si-units-2.1.jar:$APP_HOME/lib/si-quantity-2.1.jar:$APP_HOME/lib/uom-lib-common-2.1.jar:$APP_HOME/lib/unit-api-2.1.3.jar:$APP_HOME/lib/javax.inject-1.jar:$APP_HOME/lib/apiguardian-api-1.1.1.jar:$APP_HOME/lib/jakarta.annotation-api-1.3.4.jar

# Determine the Java command to use to start the JVM.
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
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD="java"
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ] ; then
        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
            MAX_FD="$MAX_FD_LIMIT"
        fi
        ulimit -n $MAX_FD
        if [ $? -ne 0 ] ; then
            warn "Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
    JAVACMD=`cygpath --unix "$JAVACMD"`

    # We build the pattern for arguments to be converted via cygpath
    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
    SEP=""
    for dir in $ROOTDIRSRAW ; do
        ROOTDIRS="$ROOTDIRS$SEP$dir"
        SEP="|"
    done
    OURCYGPATTERN="(^($ROOTDIRS))"
    # Add a user-defined pattern to the cygpath arguments
    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
    fi
    # Now convert the arguments - kludge to limit ourselves to /bin/sh
    i=0
    for arg in "$@" ; do
        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option

        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
        else
            eval `echo args$i`="\"$arg\""
        fi
        i=$((i+1))
    done
    case $i in
        (0) set -- ;;
        (1) set -- "$args0" ;;
        (2) set -- "$args0" "$args1" ;;
        (3) set -- "$args0" "$args1" "$args2" ;;
        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
    esac
fi

# Escape application args
for s in "${@}" ; do
    s="\""$s"\""
    APP_ARGS=$APP_ARGS" "$s
done

# Collect JVM options
JVM_OPTS=$DEFAULT_JVM_OPTS" "$JVM_OPTS" "$RISKSCAPE_OPTS

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
  cd "$(dirname "$0")"
fi

eval "$JAVACMD" "$JVM_OPTS" -classpath "\"$CLASSPATH\"" nz.org.riskscape.engine.cli.Main "$APP_ARGS"
