#! /bin/sh
#
#

INSTANCE_NAME=ogo-mw
INSTANCE_ROOT=/opt
BASEPORT=21000

INSTANCE_PATH="$INSTANCE_ROOT/$INSTANCE_NAME"

if [ -e "$INSTANCE_PATH/OpenGroupware.org.sh" ]; then
	. $INSTANCE_PATH/OpenGroupware.org.sh
else
	echo "OpenGroupware.org is not installed correctly."
	exit 0
fi



export FORCED_USER_ROOT=$INSTANCE_PATH
source /usr/lib/skyrix/system/Makefiles/GNUstep.sh

HOST=localhost
LOGFILE="/var/log/opengroupware.org/messages_$INSTANCE_NAME"


OPENGROUPWARE="$FORCED_USER_ROOT/WOApps/OpenGroupware.woa/ix86/linux-gnu/gnu-fd-nil/OpenGroupware"
ZIDESTORE="$FORCED_USER_ROOT/WOApps/ZideStore.woa/ix86/linux-gnu/gnu-fd-nil/ZideStore"
XMLRPCD="$FORCED_USER_ROOT/Tools/ix86/linux-gnu/gnu-fd-nil/xmlrpcd"

if [ ! -x "$OPENGROUPWARE" ]; then
	echo "OpenGroupware.org is not installed correctly."
	exit 0
fi

case "$1" in
  start)
	echo -n "Starting groupware server: "
	echo -n "opengroupware.org"
	start-stop-daemon -c opengroupware --start --exec $OPENGROUPWARE \
		-- -WOHttpAllowHost $HOST \
		-OGoMinimumActiveSessionCount 0 \
		-WOPort "localhost:$BASEPORT" \
		>> $LOGFILE 2>&1 &
	if [ -x "$XMLRPCD" ]; then
        XMLRPCPORT=$(($BASEPORT+1))
		echo -n " xmlrpcd"
		start-stop-daemon -b -c opengroupware --start --exec \
			$XMLRPCD -- -WOHttpAllowHost $HOST \
			-WOPort "localhost:$XMLRPCPORT" 
	fi
	if [ -x "$ZIDESTORE" ]; then
        ZIDESTOREPORT=$(($BASEPORT+2))
		echo -n " zidestore"
		start-stop-daemon -b -c opengroupware --start --exec \
			$ZIDESTORE -- -WOHttpAllowHost $HOST \
			-OGoMinimumActiveSessionCount 0 \
			-WOPort "localhost:$ZIDESTOREPORT" 
	fi
	echo "."
	;;
  stop)
	echo -n "Stopping groupware server: "
	echo -n "opengroupware.org"
	start-stop-daemon -c opengroupware --stop --exec $OPENGROUPWARE
	if [ -x "$XMLRPCD" ]; then
		echo -n " xmlrpcd"
		start-stop-daemon -c opengroupware --stop --exec $XMLRPCD
	fi
	if [ -x "$ZIDESTORE" ]; then
		echo -n " zidestore"
		start-stop-daemon -c opengroupware --stop --exec $ZIDESTORE
	fi
	echo "."
	;;
  restart)
	$0 stop
	sleep 1
	$0 start
	;;
  *)
	N=/etc/init.d/ogo-mw
	echo "Usage: $N {start|stop|restart}" >&2
	exit 1
	;;
esac

exit 0
