#!/bin/sh

#
# Copyright (c) DbVis Software AB. All Rights Reserved.
#

# Script to launch the DbVisualizer tool

# Function: Initializes the java version variables
init_java_version() {
  version_output=$("$JAVA_EXEC" -version 2>&1)
  is_gcj=$(expr "$version_output" : '.*gcj')
  IS_OPENJDK=$(expr "$version_output" : '.*OpenJDK')
  if [ "$is_gcj" = "0" ]; then
    java_version=$(expr "$version_output" : '.*"\(.*\)".*')
    JAVA_MAJOR_VERSION=$(expr "$java_version" : '\([0-9][0-9]*\).*')
    JAVA_MINOR_VERSION=$(expr "$java_version" : '[0-9][0-9]*\.\([0-9][0-9]*\).*')
    JAVA_MICRO_VERSION=$(expr "$java_version" : '[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\).*')
    VER_PATCH=$(expr "$java_version" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*[\._]\([0-9][0-9]*\).*')
  fi

  if [ -z "$java_version" ] ; then
    echo "Unable to extract java version - is a supported JRE installed?"
    exit 1
  fi

  if [  "$JAVA_MAJOR_VERSION" -eq "1" ]; then
    if [  "$JAVA_MINOR_VERSION" -le "7" ]; then
      echo  "Java version $java_version is not supported. At least java 8 is required."
      exit 1
    fi
  fi
}

# If JAVA_EXEC is not set check if we're on macOS and if there is a bundled
# JRE that should be used. Otherwise use java in the PATH.
init_java_exec() {
  if [ -z "$JAVA_EXEC" ] ; then
    if [ "$DBVIS_OSTYPE" = "Darwin" ]; then
      JAVA_PATH="../../PlugIns/jre.bundle/Contents/Home/bin/java"
      if [ -f "$DBVIS_HOME/$JAVA_PATH" ]; then
        JAVA_EXEC="$DBVIS_HOME/$JAVA_PATH"
      fi
    fi
    if [ "$DBVIS_OSTYPE" = "Linux" ]; then
      JAVA_PATH="jre/bin/java"
      if [ -f "$DBVIS_HOME/$JAVA_PATH" ]; then
        JAVA_EXEC="$DBVIS_HOME/$JAVA_PATH"
      fi
    fi
  fi
  # Use java in PATH
  if [ -z "$JAVA_EXEC" ] ; then
    JAVA_EXEC=java
  fi
}

set_vars() {
  if [ -z "$DBVIS_HOME" ] ; then
    DBVIS_HOME=$(dirname "$0")
  fi
  CP="$DBVIS_HOME/resources"
  CP="$CP:$DBVIS_HOME/lib/*"
  DBVIS_OSTYPE=$(uname)
}

set_vars
init_java_exec
init_java_version "$JAVA_EXEC"

# All code up to this row is the same as in dbviscmd.sh

VM_ARGS="-Xmx768M -XX:StringTableSize=1000003 -Dsun.locale.formatasdefault=true"

if [ "$JAVA_MAJOR_VERSION" -ge "9" ]; then
  if [ "$DBVIS_OSTYPE" = "Darwin" ]; then
    # Java 9+ with macOS
    "$JAVA_EXEC" ${VM_ARGS} \
      @"${DBVIS_HOME}"/java9-args @"${DBVIS_HOME}"/java9-args-macos \
      -Ddbvis.home="${DBVIS_HOME}" \
      -splash:"${DBVIS_HOME}/resources/images/ix4/dbvis-splash_dark.png" \
      -Xdock:name="DbVisualizer" \
      -Xdock:icon="${DBVIS_HOME}/resources/images/ix4/dbvis-icon.icns" \
      -cp "${CP}" \
      com.onseven.dbvis.DbVisualizerGUI "$@"
  else
    # Java 9+ with Linux/UNIX
    "$JAVA_EXEC" ${VM_ARGS} \
      @"${DBVIS_HOME}"/java9-args @"${DBVIS_HOME}"/java9-args-linux \
      -Ddbvis.home="${DBVIS_HOME}" \
      -splash:"${DBVIS_HOME}/resources/images/ix4/dbvis-splash_dark.png" \
      -Dsun.java2d.xrender=false \
      -cp "${CP}" \
      com.onseven.dbvis.DbVisualizerGUI "$@"
  fi
elif [ "$JAVA_MAJOR_VERSION" = "1" ] && [ "$JAVA_MINOR_VERSION" = "8" ]; then
  if [ "$DBVIS_OSTYPE" = "Darwin" ]; then
    # Java 8 with macOS
    "$JAVA_EXEC" ${VM_ARGS} \
      -Ddbvis.home="${DBVIS_HOME}" \
      -splash:"${DBVIS_HOME}/resources/images/ix4/dbvis-splash_dark.png" \
      -Xdock:name="DbVisualizer" \
      -Xdock:icon="${DBVIS_HOME}/resources/images/ix4/dbvis-icon.icns" \
      -cp "${CP}" \
      com.onseven.dbvis.DbVisualizerGUI "$@"
    else
      # Java 8 with Linux/UNIX
     "$JAVA_EXEC" ${VM_ARGS} \
        -Ddbvis.home="${DBVIS_HOME}" \
        -splash:"${DBVIS_HOME}/resources/images/ix4/dbvis-splash_dark.png" \
        -Dsun.java2d.xrender=false \
        -cp "${CP}" \
        com.onseven.dbvis.DbVisualizerGUI "$@"
    fi
else
  printf "Error: At least Java 8 is required. Below is the version currently being used:\n"
  "$JAVA_EXEC" -version
  exit 2
fi

