#!/bin/sh
# -----------------------------------------------------------------------
# Main script for firing up SME client
# Usage: startup_client [options]
#
# options:
#
#	-d 	debug
#	-p	port
#	
# -----------------------------------------------------------------------

# This is written by configure: do not modify
MODEL=null
MORE_ARGS="yes"
DEBUG="0"
JAVA_RUN_ARGS=""
PORT="17736"
APP_ARGS=""
HOST="localhost"
USE_SNI=""
PORTAL_ARGS=""
PORTAL_DIR=${PORTAL_DIR-undefined}
WS_DIR=${WS_DIR-undefined}
WB="SME"
CLASSPATH=${CLASSPATH:-"/"}

if [ $# -gt 0 ] ; then

  if [ $1 = -h ] ; then
	echo "Usage: startup_wsm [options]"
	echo "  options:"
	echo 
	echo "    -h              print usage help"
	echo "    -d <debugMode>  debug mode: ( 1:run debugger, 2:attach debugger manually )"
	echo "    -java <args>    additional args to java command"
	echo "    -wsd <path>     Use Workspace directory <path>"
	echo	
	exit 0
  fi

  while [ $MORE_ARGS = "yes" ] ; do
    MORE_ARGS="no"

    if [ $1 = "-d" ] ; then
      MORE_ARGS="yes"
      shift
      DEBUG="$1"
      shift
      echo "In debug mode:"
    fi

    if [ $1 = "-wsd" ] ; then
      MORE_ARGS="yes"
      shift
      WS_DIR="$1"
      shift
      echo "Using Workspace directiory: $WS_DIR"
    fi

    if [ $1 = "-p" ] ; then
      MORE_ARGS="yes"
      shift
      PORT="$1"
      shift
      echo "Using port: $PORT"
    fi


    if [ $1 = "-java" ] ; then
      MORE_ARGS="yes"
      shift
      JAVA_RUN_ARGS="$JAVA_RUN_ARGS $1"
      shift
    fi

    if [ $1 = "-u" ] ; then
      MORE_ARGS="yes"
      shift
      APP_ARGS="$APP_ARGS -u $1"
      shift
    fi

    if [ $1 = "-portal" ] ; then
      MORE_ARGS="yes"
      shift
      PORTAL_ARGS="$PORTAL_ARGS -portal"
    fi

    if [ $1 = "-portal_dir" ] ; then
      MORE_ARGS="yes"
      shift
      PORTAL_DIR="$1"
      shift
      echo "Setting portal directory to $1"
    fi

    if [ $1 = "-wb" ] ; then
      MORE_ARGS="yes"
      shift
      WB="$1"
      JAVA_RUN_ARGS="$JAVA_RUN_ARGS -wb $WB"
      echo "Setting Workbench name to $WB"
      shift
    fi

    if [ $1 = "-admin" ] ; then
      MORE_ARGS="yes"
      shift
      APP_ARGS="$APP_ARGS -admin"
      echo "Setting Workbench to usr-admin mode"
    fi


    if [ $1 = "-v" ] ; then
      MORE_ARGS="yes"
      shift
      JAVA_RUN_ARGS="$JAVA_RUN_ARGS -verbose"
    fi


  done

fi

if test "$WS_DIR" = "undefined"; then
    WS_DIR=$HOME/.sme/usr/
fi

JAVA_RUN_ARGS="$JAVA_RUN_ARGS -Dws.dir=$WS_DIR"
CLASSPATH="$CLASSPATH:$WS_DIR/$WB.jar"
		
if test "$PORTAL_DIR" != "undefined"; then
    JAVA_RUN_ARGS="$JAVA_RUN_ARGS -Dportal.dir=$PORTAL_DIR"
fi
		
APP_ARGS="$APP_ARGS -p $PORT -h $HOST -m $MODEL  $USE_SNI $PORTAL_ARGS -wbm"
export APP_ARGS
export JAVA_RUN_ARGS
export CLASSPATH

sme_execute_class miiee.mml.ProjectHandler $DEBUG 

exit 0	