#!/bin/bash

# find matching executable jar file
JAR_FILE_EXP='iot-setup-manager-[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.*.jar'
for file in "$(dirname "$0")"/*; do
  if [[ "$(basename $file)" =~ $JAR_FILE_EXP ]]; then
    JAR_FILE="$(dirname "$0")"/"$(basename $file)"
    echo "Executable jar file : "$JAR_FILE
    break
  fi
done;

if [ $JAR_FILE ]; then
  EMBEDDED_JDK_DIR="$(dirname "$0")"/../../jdk

  if [ -f "$EMBEDDED_JDK_DIR/bin/java" ]; then
    # first, check embedded jdk in tizen studio
    echo "On Tizen Studio jdk environement"
    $EMBEDDED_JDK_DIR/bin/java -jar "$JAR_FILE" "$@"
  elif [ -n "$JAVA_HOME" ]; then
    # second, check JAVA_HOME env.
    echo "On installed JAVA_HOME environment"
    $JAVA_HOME/bin/java -jar "$JAR_FILE" "$@"
  else
    # last, check java executable.
    echo "Execute java directly"
    java -jar "$JAR_FILE" "$@"
  fi
fi
