#!/bin/sh
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

moz_pis_run_script()
{
	[ -x "$pis" ] ||
		return 0
	case "$pis" in
		*.sh) . "$pis"      ;;
		*)      "$pis" "$1" ;;
	esac

}

moz_pis_startstop_scripts()
{
	MOZ_USER_DIR=".mozilla/xulrunner"
	[ -z "$APP_NAME" ] ||
		MOZ_USER_DIR=".mozilla/$APP_NAME"

	# MOZ_PIS_ is the name space for "Mozilla Plugable Init Scripts"
	# These variables and there meaning are specified in
	# mozilla/xpfe/bootstrap/init.d/README
	MOZ_PIS_API=2
	MOZ_PIS_MOZBINDIR="${APP_PREFIX:-$dist_bin}"
	MOZ_PIS_SESSION_PID="$$"
	MOZ_PIS_USER_DIR="${MOZ_USER_DIR}"
	export MOZ_PIS_API MOZ_PIS_MOZBINDIR MOZ_PIS_SESSION_PID MOZ_PIS_USER_DIR

	case "$1" in
		start)
			for pis in "$MOZ_PIS_MOZBINDIR/init.d"/S* "$HOME/$MOZ_USER_DIR/init.d"/S* ; do
				moz_pis_run_script "$1"
			done
			;;
		stop)
			for pis in "$HOME/$MOZ_USER_DIR/init.d"/K* "$MOZ_PIS_MOZBINDIR/init.d"/K* ; do
				moz_pis_run_script "$1"
			done
			;;
	esac
}

moz_libdir=/usr/lib/xulrunner
progname="$0"
progbase="${progname##*/}"
profile=""
dist_bin="$moz_libdir"

MOZILLA_BIN="xulrunner-bin"
MOZ_CLIENT_PROGRAM="$dist_bin/mozilla-xremote-client"

run_xremote_client() {
    "$MOZ_CLIENT_PROGRAM" -a "${progbase}" ${profile:+-p "$profile"} "$1" ||
	return $?
}

# The following is to check for a currently running instance.
is_running() {
    run_xremote_client 'ping()' 2>/dev/null >/dev/null ||
	return $?
}

################################################################ Parse Arguments
pass_arg_count=0
_optOne=
_optLast=
_optOthers=
while [ $# -gt $pass_arg_count ]; do
	[ -n "${_optOne}" ] || _optOne="$1"
	_optLast="$1"
	case "$1" in
		-p|--pure|-pure|-g|--debug)
			echo "Argument '$1' ignored"
			shift
			;;
		-d|--debugger)
			echo "Argument '$1' ignored"
			shift 2
			;;
		-P)
			# check for -P argument used to select a user profile
			profile="$2"
			;;
		*)
			# Move the unrecognized argument to the end of the list.
			arg="$1"
			_optOthers="${_optOthers} $1"
			shift
			set -- "$@" "$arg"
			pass_arg_count=$(($pass_arg_count + 1))
			;;
	esac
done
_optOthers="${_optOthers% $_optLast}"

################################################################ Parse Arguments
# If there's a command line argument but it doesn't begin with a -
# it's probably a url.  Try to send it to a running instance.
_USE_EXIST=0
_NEW_WINDOW=
case "${_optOne}" in
	-*) 
		;;
	*)
		_USE_EXIST=1
		;;
esac

#???: needs check if othersopt begin with -* ?
if [ `expr "${_optLast}" : '.*:/.*'` -eq 0 -a \( -f "${_optLast}" -o -d "${_optLast}" \) ]; then
	# Last argument seems to be a local file/directory
	# Check, if it is absolutely specified (ie. /home/foo/file vs. ./file)
	# If it is just "relatively" (./file) specified, make it absolutely
	[ `expr "${_optLast}" : '/.*'` -eq 0 ] && _optLast="file://`pwd`/${_optLast}"
elif [ `expr "${_optLast}" : '.*:/.*'` -gt 0 -o -n "${_optOthers}" ]; then #???? like before...
	_NEW_WINDOW=1
fi

########################################################################### Main
if is_running; then
	# There's an instance already running. Use it.
	# Any command line args passed in?
	if [ $# -gt 0 ]; then
		# There were "some" command line args.
		if [ ${_USE_EXIST} -eq 1 ]; then
			# We should use an existing instance, as _USE_EXIST=$_USE_EXIST=-1
			_open_type="window"
			#_open_type="tab"
			_remote_cmd="openURL(${_optLast} , new-${_open_type})"
			run_xremote_client "${_remote_cmd}"
			unset _remote_cmd _open_type
			exit $?
		fi
	else
		# No command line args. Open new window/tab
		run_xremote_client "xfeDoCommand(openBrowser)"
		exit $?
	fi
fi
# Default action - no running instance or _USE_EXIST (${_USE_EXIST}) ! -eq 1
########################################################################### Main
unset MOZILLA_FIVE_HOME

## Start addon scripts
moz_pis_startstop_scripts "start"

"$dist_bin/$MOZILLA_BIN" "$@"
exitcode=$?

## Stop addon scripts
moz_pis_startstop_scripts "stop"

exit $exitcode
