#!/bin/bash
# Copyright (c) 2018-2020 "Graph Foundation,"
# Graph Foundation, Inc. [https://graphfoundation.org]
#
# This file is part of ONgDB.
#
# ONgDB is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -euo pipefail
[[ "${TRACE:-}" ]] && set -x

check_java() {
  _find_java_cmd

  version_command=("${JAVA_CMD}" "-version")
  [[ -n "${JAVA_MEMORY_OPTS:-}" ]] && version_command+=("${JAVA_MEMORY_OPTS[@]}")

  JAVA_VERSION=$("${version_command[@]}" 2>&1 | awk -F '"' '/version/ {print $2}')
  MAX_VERSION=`echo -e "$JAVA_VERSION\n1.8" | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -g | tail -1`
  if [[ "${MAX_VERSION}" = "1.8" ]]; then
    echo "ERROR! Java version ${JAVA_VERSION} is not supported. "
    _show_java_help
    exit 1
  fi
}

_find_java_cmd() {
  [[ "${JAVA_CMD:-}" ]] && return
  detect_os
  _find_java_home

  if [[ "${JAVA_HOME:-}" ]] ; then
    JAVA_CMD="${JAVA_HOME}/bin/java"
  else
    if [ "${DIST_OS}" != "macosx" ] ; then
      # Don't use default java on Darwin because it displays a misleading dialog box
      JAVA_CMD="$(which java || true)"
    fi
  fi

  if [[ ! "${JAVA_CMD:-}" ]]; then
    echo "ERROR: Unable to find Java executable."
    _show_java_help
    exit 1
  fi
}

detect_os() {
  if uname -s | grep -q Darwin; then
    DIST_OS="macosx"
  elif [[ -e /etc/gentoo-release ]]; then
    DIST_OS="gentoo"
  else
    DIST_OS="other"
  fi
}

_find_java_home() {
  [[ "${JAVA_HOME:-}" ]] && return

  case "${DIST_OS}" in
    "macosx")
      JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
      ;;
    "gentoo")
      JAVA_HOME="$(java-config --jre-home)"
      ;;
  esac
}

_show_java_help() {
  echo "* Please use Oracle(R) Java(TM) 8 or OpenJDK(TM) 8."
}

build_classpath() {
  APP_HOME="$(cd "$(dirname "$0")" && pwd)"
  # First try in sub directory
  JARPATH="$(find "${APP_HOME}" -name "geequel-shell.jar" )"

  # Then try installation directory (prefix/bin and prefix/share/geequel-shell/lib)
  if [[ -z "${JARPATH}" ]]; then
    APP_HOME="${APP_HOME}/../share/geequel-shell"
    JARPATH="$(find "${APP_HOME}" -name "geequel-shell.jar" )"
  fi
}

check_java
build_classpath

if [ -z "${JARPATH}" ]; then
  echo "Unable to locate geequel-shell library files" >&2
  exit 1
fi

exec "$JAVA_CMD" ${JAVA_OPTS:-} \
  -jar "$JARPATH" \
  "$@"
