#!/bin/sh
#
# JavaFX Packager tool execution script for Linux/Solaris/OS X.
#

# resolve symlinks
PRG=$0
while [ -h "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
    if expr "$link" : '^/' 2> /dev/null >/dev/null; then
	PRG="$link"
    else
	PRG="`dirname "$PRG"`/$link"
    fi
done

# detect Darwin and Cygwin environments
darwin=false;
cygwin=false;
case "`uname`" in
  Darwin*) darwin=true;
           if [ -z "$JAVA_HOME" ] ; then
               BIN_DIR=`dirname $PRG`
               JAVA_HOME=`cd $BIN_DIR > /dev/null; pwd`/..
           fi
           ;;
  CYGWIN*) cygwin=true;;
esac

if [ -n "$JAVA_HOME" -a -x "$JAVA_HOME/bin/javac" ] ; then
    JAVA_CMD="$JAVA_HOME/bin/java"
    export JAVA_HOME
else
  JAVAC_CMD=`which javac 2> /dev/null `
  while [ -h "$JAVAC_CMD" ]; do
    ls=`ls -ld "$JAVAC_CMD"`
    link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
    if expr "$link" : '^/' 2> /dev/null >/dev/null; then
	JAVAC_CMD="$link"
    else
	JAVAC_CMD="`dirname "$JAVAC_CMD"`/$link"
    fi
  done
  BIN_DIR=`dirname "$JAVAC_CMD"`
  JAVA_HOME=`dirname "$BIN_DIR"`
  if [ "m$JAVA_HOME" != "m." ]; then
      JAVA_CMD="$JAVA_HOME/bin/java"
      export JAVA_HOME
  else
    unset JAVA_HOME
    JAVA_CMD=`which java 2> /dev/null `
    if [ -z "$JAVA_CMD" ]; then
      JAVA_CMD="java"
    fi
  fi
fi


if [ ! -x "$JAVA_CMD" ] ; then
  echo 'Error: JAVA_HOME is not defined, cannot find "java" command.'
  exit 1
fi

javafx_home=`dirname "$PRG"`/..

classpath="$javafx_home/lib/ant-javafx.jar"

if [ -e "$javafx_home/jre/lib/jfxrt.jar" ] ; then
  classpath="$classpath:$javafx_home/jre/lib/javafxrt.jar"
elif [ -e "$javafx_home/rt/lib/jfxrt.jar" ] ; then
  classpath="$classpath:$javafx_home/rt/lib/javafxrt.jar"
fi

if $cygwin ; then
    classpath=`cygpath --path --windows "$classpath"`
    javafx_home=`cygpath --windows "$javafx_home"`
    JAVA_CMD=`cygpath --unix "$JAVA_CMD"`
fi

exec "$JAVA_CMD" -Xmx256M "-Djavafx.home=$javafx_home" -classpath "$classpath" com.sun.javafx.tools.packager.Main "$@"
