#!/bin/sh
# ------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

set -o errexit


#
# Initialization.
#
BASE_DIR="`dirname "$( readlink -e "$0"; )";`/.."
. "$BASE_DIR/conf/setenv.sh"

SIS_DATA="${SIS_DATA:-$BASE_DIR/data}"
export SIS_DATA
unset  SIS_HOME


#
# Check requirements. If no path to FX is specified,
# try the location used by some package managers.
#
MIN_FX_VERSION=20
if [ -z "$PATH_TO_FX" ]
then
    PATH_TO_FX=/usr/lib/jvm/openjfx
fi


#
# Check for a sentinel OpenJFX file and its version number.
# If too old, we will process as if no OpenJFX is installed.
#
if [ -r $PATH_TO_FX/javafx.controls.jar ]
then
    FX_VERSION=`java --module-path=$PATH_TO_FX --describe-module=javafx.controls | grep --only-matching --max-count=1 "[0-9]\+" | head --lines=1`
    if [ $FX_VERSION -lt $MIN_FX_VERSION ]
    then
        echo "Found OpenJFX $FX_VERSION in $PATH_TO_FX but requires OpenJFX $MIN_FX_VERSION."
        unset PATH_TO_FX
    else
        export PATH_TO_FX
    fi
else
    unset PATH_TO_FX
fi


#
# If no suitable OpenJFX version has been found, start the installation wizard.
#
if [ -z "$PATH_TO_FX" ]
then
    java --class-path "$BASE_DIR/lib/app/org.apache.sis.gui.jar" org.apache.sis.gui.setup.FXFinder $BASE_DIR/conf/setenv.sh
    if [ $? -ne 0 ]
    then
        exit
    fi
    . "$BASE_DIR/conf/setenv.sh"
fi

#
# Optional UCAR dependencies, if present, need to be added to the environment.
#
shopt -s nullglob
files=("$BASE_DIR"/lib/cdm-core-*.jar)
if [ ${#files[@]} -gt 0 ]; then
  ADD_OPTIONAL_MODULES="--add-modules cdm.core,udunits,com.google.common"
else
  ADD_OPTIONAL_MODULES=
fi
shopt -u nullglob

#
# Execute SIS with any optional JAR that the user may put in the `lib` directory.
#
java -splash:"$BASE_DIR/lib/logo.jpg" \
     --module-path "$PATH_TO_FX:$BASE_DIR/lib:$BASE_DIR/lib/app/org.apache.sis.gui.jar" \
     -Djava.util.logging.config.class="org.apache.sis.util.logging.Initializer" \
     -Djava.util.logging.config.file="$BASE_DIR/conf/logging.properties" \
     -Dderby.stream.error.file="$BASE_DIR/log/derby.log" \
     $ADD_OPTIONAL_MODULES \
     --module org.apache.sis.gui/org.apache.sis.gui.DataViewer \
     $SIS_OPTS "$@"
