#!/bin/bash
# Run DomTerm using Java's HttpServer.
# This uses AJAX instead of WebSockets.
# This script is not installed by default.
thisfile=`type -p $0`
case "$thisfile" in
  "") echo "installation error - can't find path to $0"; exit -1 ;;
  /*) ;;
  *) thisfile="$PWD/$thisfile"  ;;
esac         
while test -L "$thisfile"; do thisfile=$(readlink -f "$thisfile"); done
# This script might be as xxx/bin/jdomterm or xxx/share/odmterm/jdomterm.
DOMTERM_DIR=`echo $(dirname $thisfile) | sed -e 's|/bin$||' -e 's|/share/domterm$||'`
if test -n "$JAVA_HOME"; then
    JAVA="${JAVA_HOME}/bin/java"
else
    JAVA=${JAVA-java}
fi
EXTRA_ARG=""
case "$1" in
  "--help"|"-help")
  echo 'DomTerm terminal emulator.'
  echo 'With no arguments runs a shell (bash) using Firefox/XUL in a PTY.'
  echo 'Usage: domterm [foreground-arguments] [-e|--pty|--pipe] [command arg...]'
  echo 'Run [command arg...] (by default bash) in a PTY.'
  echo 'The foregound-arguments control the UI to use:'
  echo '--browser - create a window in your preferred browser'
  echo '--browser[=COMMAND] - same, but used specified browser COMMAND'
  echo '--firefox - use FireFox/XUL [this is currently the default]'
  echo '--chrome - use the Google Chrome browser in --app mode'
  echo '--javafx - use JavaFX WebView'
  echo '--port portnum - Only start a server which you can browse'
  echo 'The background-arguments control what program to run:'
  echo '--pty - use a PTY (pseudo-teletype) [default on Unix-like systems]'
  echo '--pipe - connect using a pipe'
  echo 'Set JAVA or JAVA_HOME in environment if java is not in the PATH.'
  exit 0
  ;;
  "--firefox" | "--chrome" | "--qtwebengine" | "--port" | "--browser"*)
  ;;
  "--javafx")
  shift
  exec $JAVA -Djava.library.path="$DOMTERM_DIR"/lib -cp $DOMTERM_DIR/share/domterm.jar:$DOMTERM_DIR/share/wsdomterm.jar:$CLASSPATH org.domterm.javafx.Main "$@"
  ;;
  *)
  EXTRA_ARG="--firefox"
  ;;
esac
#exec $JAVA -cp "$DOMTERM_DIR/share/domterm/domterm.jar:$DOMTERM_DIR/share/domterm/wsdomterm.jar:$CLASSPATH" -Djava.library.path="$DOMTERM_DIR"/lib org.domterm.websocket.DomServer $EXTRA_ARG "$@"
exec $JAVA -cp "$DOMTERM_DIR/share/domterm/domterm.jar:$CLASSPATH" -Djava.library.path="$DOMTERM_DIR"/lib org.domterm.DomHttpServer $EXTRA_ARG "$@"
