#! /bin/sh
verbose=off
debug=off
node=on
debugger_port=5885
timeouts=on

# Jail master switch overrides the other jail switches- if
# this is off, all js jails are off.  Valid values are "on" and "off".
jail=on
# Jail palm services.  Valid values are "off", "collective" and "individual"
# Collective means that all palm services share the same jail.
# so one could read data owned by another, but startup of new
# services can be faster.  
# Individual means each service gets its own jail.  It is the default.
# turn palm jails off for now-ericm
palm_jail=off
# palm services that don't run in jail:
palm_jail_pardons="com.palm.service.backup com.palm.service.migration"
# Jail 3rd party services. Same usage as palm_jail.
thirdparty_jail=individual

while [ $# -gt 0 ]
do
	case "$1" in
		-v)  verbose=on;;
		-d)  debug=on;;
		-n)  node=on;;
		-t)  node=off;;
		-p)  debugger_port=$2;shift;debug=on;;
		-k)  timeouts=off;;
		-j)  jail=on;;
		--)	shift; break;;
		-*)
		echo >&2 "usage: $0 [-v] [-d] [-n] [-t] [-j] [-k] [-p debugger-port] [directory]"
		echo >&2 "-v : verbose mode"
		echo >&2 "-d : enable debugger"
		echo >&2 "-n : run service with Node.js engine (default)"
		echo >&2 "-t : run service with triton engine"
		echo >&2 "-p [port] : set port for debugger"
		echo >&2 "-k : disable timeouts (keep running)"
		echo >&2 "-j : enable jailer"
		exit 1;;
		*)  break;;	# terminate while loop
	esac
	shift
done

if [ x$1 != x ]
then
	cd $1
fi

if [ x$BEDLAM_ROOT != x ]; then
    FRAMEWORKS_PATH=$BEDLAM_ROOT/palm/frameworks
    NODE_ADDONS=$BEDLAM_ROOT/palm/nodejs
    JS=$BEDLAM_ROOT/bin/js
    NODE=$BEDLAM_ROOT/bin/node
    SERVICE_LAUNCHER=$BEDLAM_ROOT/palm/services/jsservicelauncher
else
    FRAMEWORKS_PATH=/usr/palm/frameworks
    NODE_ADDONS=/usr/palm/nodejs
    JS=/usr/bin/js
    NODE=/bin/node
    SERVICE_LAUNCHER=/usr/palm/services/jsservicelauncher
fi

SERVICE_PATH=$(pwd)
case "${SERVICE_PATH}" in
	/usr/palm/services/*)
		ROM_SERVICE=on
		ROLES_PATH=/usr/share/ls2
		;;
	/media/cryptofs/apps/*)
		ROM_SERVICE=off
		ROLES_PATH=/var/palm/ls2
		;;
	*)
		echo "Invalid service path ${SERVICE_PATH}.  Exiting."
		exit 1
		;;
esac

SERVICE_NAME=$(basename $SERVICE_PATH)
FULL_SERVICE_NAME=${SERVICE_NAME}
SERVICE_NAME_LENGTH=${#SERVICE_NAME}
if [ $SERVICE_NAME_LENGTH -gt 13 ] ; then
    if [ x`uname` != xDarwin ] ; then
	    SERVICE_NAME_INDEX=$(expr $SERVICE_NAME_LENGTH - 12)
	    SERVICE_NAME=$(expr substr $SERVICE_NAME $SERVICE_NAME_INDEX 13)
    fi
fi

if [ $node = on ]
then
    JS_ENGINE=$NODE
    export NODE_PATH="$FRAMEWORKS_PATH:$NODE_ADDONS"
    COMMON_OPTIONS="--notimeout_script_execution $SERVICE_LAUNCHER/bootstrap-node.js"
else
    JS_ENGINE=$JS
    COMMON_OPTIONS="--syslog --stdout --process-name $SERVICE_NAME.js -I $FRAMEWORKS_PATH -I $FRAMEWORKS_PATH/private $SERVICE_LAUNCHER/bootstrap.js"
fi

COMMON_APPEND=" -- $ROLES_PATH"

# figure out how to jail the app
# is the app a palm app or 3rd party?
# does each app get its own jail or use a common one?
if [ $jail == on ]; then
	case "${FULL_SERVICE_NAME}" in
	com.palm.*)
		appid="com.palm.triton"
		case ${palm_jail} in
		off) jail=off;;
		c*) ;;
		*) appid=${FULL_SERVICE_NAME};;
		esac
		# check for exceptions:
		for i in "${palm_jail_pardons}"
		do
			if [ "${i}" = "${appid}" ]
			then
				jail=off
			fi
		done
		;;
	*)
		# default for collective:
		appid="com.palm.generic.service.thirdparty"
		case ${thirdparty_jail} in
		off) jail=off;;
		c*) ;;
		*) appid=${FULL_SERVICE_NAME};;
		esac
		;;
	esac
	# determine if service is in the rom or was installed, as they
	# use different configs
	jailtype="triton"
	if [ $ROM_SERVICE == off ]; then
		jailtype="triton-inst"
	fi

	# palm or 3rdparty jails might be off
	if [ $jail == on ]; then
		JAIL_STRING="/usr/bin/jailer -t ${jailtype} -i "${appid}" -p $SERVICE_PATH"
	fi
fi

if [ $debug = on ] ; then
	timeouts=off
	if [ $node = on ]
	then
		COMMAND_STRING="$JS_ENGINE --debug=$debugger_port $COMMON_OPTIONS"
	else
		COMMAND_STRING="$JS_ENGINE $COMMON_OPTIONS -d --debugger-port $debugger_port"
	fi
else
	COMMAND_STRING="$JS_ENGINE $COMMON_OPTIONS"
fi
COMMAND_STRING="$COMMAND_STRING $COMMON_APPEND"
if [ $timeouts = off ] ; then
	COMMAND_STRING="$COMMAND_STRING --disable-timeouts"
fi

if [ $verbose = on ] ; then
	echo $JAIL_STRING $COMMAND_STRING
fi

exec $JAIL_STRING $COMMAND_STRING
