#!/usr/bin/env bash
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [http://neo4j.com]
#
# This file is part of Neo4j.
#
# Neo4j 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 -o errexit -o nounset -o pipefail
[[ "${TRACE:-}" ]] && set -o xtrace

PROGRAM="$(basename "$0")"
readonly PROGRAM

# Sets up the standard environment for running Neo4j shell scripts.
#
# Provides these environment variables:
#   NEO4J_HOME
#   NEO4J_CONF
#   NEO4J_DATA
#   NEO4J_LIB
#   NEO4J_LOGS
#   NEO4J_PIDFILE
#   NEO4J_PLUGINS
#   one per config setting, with dots converted to underscores
#
setup_environment() {
  _setup_calculated_paths
  _read_config
  _setup_configurable_paths
}

setup_heap() {
  if [[ -n "${HEAP_SIZE:-}" ]]; then
    JAVA_MEMORY_OPTS_XMX="-Xmx${HEAP_SIZE}"
  fi
}

build_classpath() {
  CLASSPATH="${NEO4J_PLUGINS}:${NEO4J_CONF}:${NEO4J_LIB}/*:${NEO4J_PLUGINS}/*"
}

detect_os() {
  if uname -s | grep -q Darwin; then
    DIST_OS="macosx"
  else
    DIST_OS="other"
  fi
}

check_limits() {
  detect_os
  MIN_ALLOWED_OPEN_FILES=40000
  if [ "${DIST_OS}" != "macosx" ] ; then
    ALLOWED_OPEN_FILES="$(ulimit -n)"

    if [ "${ALLOWED_OPEN_FILES}" -lt "${MIN_ALLOWED_OPEN_FILES}" ]; then
        echo "WARNING: Max ${ALLOWED_OPEN_FILES} open files allowed, minimum of ${MIN_ALLOWED_OPEN_FILES} recommended. See the Neo4j manual."
    fi
  fi
}

setup_memory_opts() {
  # In some cases the heap size may have already been set before we get here, from e.g. HEAP_SIZE env.variable, if so then skip
  if [[ -n "${dbms_memory_heap_max_size:-}" && -z "${JAVA_MEMORY_OPTS_XMX-}" ]]; then
    if ! [[ "${dbms_memory_heap_max_size}" =~ .*[gGmMkK] ]]; then
      cat >&2 <<EOF
ERROR: dbms.memory.heap.max_size require a unit suffix. Use
       'k' for kilobytes, 'm' for megabytes and 'g' for gigabytes.
       Example:

       dbms.memory.heap.max_size=512m
                                    ^
EOF
      exit 1
    fi
    JAVA_MEMORY_OPTS_XMX="-Xmx${dbms_memory_heap_max_size}"
  fi
}

check_java() {
  _find_java_cmd
  setup_memory_opts

  version_command=("${JAVA_CMD}" "-version")

  JAVA_VERSION=$("${version_command[@]}" 2>&1 | awk -F '"' '/version/ {print $2}')
  if [[ $JAVA_VERSION = "1."* ]] || [[ $JAVA_VERSION = "9"* ]] || [[ $JAVA_VERSION = "10"* ]]; then
      echo "ERROR! Neo4j cannot be started using java version ${JAVA_VERSION}. "
      _show_java_help
      exit 1
  elif [[ $JAVA_VERSION != "11"* ]] ; then
    unsupported_runtime_warning
  else
    if ! ("${version_command[@]}" 2>&1 | grep -Eq "(Java HotSpot\\(TM\\)|OpenJDK) (64-Bit Server|Server|Client) VM"); then
      unsupported_runtime_warning
    fi
  fi
}

unsupported_runtime_warning() {
    echo "WARNING! You are using an unsupported Java runtime. "
    _show_java_help
}

# Resolve a path relative to $NEO4J_HOME.  Don't resolve if
# the path is absolute.
resolve_path() {
    orig_filename=$1
    if [[ ${orig_filename} == /* ]]; then
        filename="${orig_filename}"
    else
        filename="${NEO4J_HOME}/${orig_filename}"
    fi
    echo "${filename}"
}

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

  if [[ "${JAVA_HOME:-}" ]] ; then
    JAVA_CMD="${JAVA_HOME}/bin/java"
    if [[ ! -f "${JAVA_CMD}" ]]; then
      echo "ERROR: JAVA_HOME is incorrectly defined as ${JAVA_HOME} (the executable ${JAVA_CMD} does not exist)"
      exit 1
    fi
  else
    if [ "${DIST_OS}" != "macosx" ] ; then
      # Don't use default java on Darwin because it displays a misleading dialog box
      JAVA_CMD="$(command -v java || true)"
    fi
  fi

  if [[ ! "${JAVA_CMD:-}" ]]; then
    echo "ERROR: Unable to find Java executable. Make sure the java executable is on the PATH or define JAVA_HOME."
    _show_java_help
    exit 1
  fi
}

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

  case "${DIST_OS}" in
    "macosx")
      JAVA_HOME="$(/usr/libexec/java_home -v 11)"
      ;;
  esac
}

_show_java_help() {
  echo "* Please use Oracle(R) Java(TM) 11, OpenJDK(TM) 11 to run Neo4j."
  echo "* Please see https://neo4j.com/docs/ for Neo4j installation instructions."
}

_setup_calculated_paths() {
  if [[ -z "${NEO4J_HOME:-}" ]]; then
    NEO4J_HOME="$(cd "$(dirname "$0")"/.. && pwd)"
  fi
  : "${NEO4J_CONF:="${NEO4J_HOME}/conf"}"
  readonly NEO4J_HOME NEO4J_CONF
}

_read_config() {
  # - plain key-value pairs become environment variables
  # - keys have '.' chars changed to '_'
  # - keys of the form KEY.# (where # is a number) are concatenated into a single environment variable named KEY
  parse_line() {
    line="$1"
    if [[ "${line}" =~ ^([^#\s][^=]+)=(.+)$ ]]; then
      key="${BASH_REMATCH[1]//./_}"
      value="${BASH_REMATCH[2]}"
      if [[ "${key}" =~ ^(.*)_([0-9]+)$ ]]; then
        key="${BASH_REMATCH[1]}"
      fi
      # Ignore keys that start with a number because export ${key}= will fail - it is not valid for a bash env var to start with a digit
      if [[ ! "${key}" =~ ^[0-9]+.*$ ]]; then
        if [[ "${key}" == "dbms_jvm_additional" && "${!key:-}" ]]; then
          export "${key}=${!key} ${value}"
        else
          export "${key}=${value}"
        fi
      else
        echo >&2 "WARNING: Ignoring key ${key}, environment variables cannot start with a number."
      fi
    fi
  }

  path="${NEO4J_CONF}/neo4j.conf"
  if [ -e "${path}" ]; then

    # we want to get multi lines as a single value
    # shellcheck disable=SC2162
    while read line || [[ -n "$line" ]]; do
      parse_line "${line}"
    done <"${path}"
  fi
}

_setup_configurable_paths() {
  NEO4J_DATA=$(resolve_path "${dbms_directories_data:-data}")
  NEO4J_LIB=$(resolve_path "${dbms_directories_lib:-lib}")
  NEO4J_LOGS=$(resolve_path "${dbms_directories_logs:-logs}")
  NEO4J_PLUGINS=$(resolve_path "${dbms_directories_plugins:-plugins}")
  NEO4J_RUN=$(resolve_path "${dbms_directories_run:-run}")
  NEO4J_CERTS=$(resolve_path "${dbms_directories_certificates:-certificates}")

  if [ -z "${dbms_directories_import:-}" ]; then
    NEO4J_IMPORT="NOT SET"
  else
    NEO4J_IMPORT=$(resolve_path "${dbms_directories_import:-}")
  fi

  readonly NEO4J_DATA NEO4J_LIB NEO4J_LOGS NEO4J_PLUGINS NEO4J_RUN NEO4J_IMPORT NEO4J_CERTS
}


call_main_class() {
  setup_environment
  check_limits
  check_java
  build_classpath
  class_name=$1
  shift

  export NEO4J_HOME NEO4J_CONF

  # shellcheck disable=SC2086
  exec "${JAVA_CMD}" ${JAVA_OPTS:--XX:+UseParallelGC} ${JAVA_MEMORY_OPTS_XMX-} \
    -classpath "${CLASSPATH}" \
    "-Dfile.encoding=UTF-8" \
    "$class_name" "$@"
}

setup_heap
call_main_class "org.neo4j.cli.AdminTool" "$@"
