#! /bin/bash
################################################################################
#
#	Startup script for Mbedthis AppWeb
#	Copyright (c) Mbedthis LLC, 2003-2007. All Rights Reserved.
#
#	chkconfig: - 81 15
#	description: Start Mbedthis AppWeb the embedded HTTP web server
#
################################################################################
#
#	Copyright (c) Mbedthis Software LLC, 2003-2007. All Rights Reserved.
#	The latest version of this code is available at http://www.mbedthis.com
#
#	This software is open source; you can redistribute it and/or modify it 
#	under the terms of the GNU General Public License as published by the 
#	Free Software Foundation; either version 2 of the License, or (at your 
#	option) any later version.
#
#	This program is distributed WITHOUT ANY WARRANTY; without even the 
#	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#	See the GNU General Public License for more details at:
#	http://www.mbedthis.com/downloads/gplLicense.html
#	
#	This General Public License does NOT permit incorporating this software 
#	into proprietary programs. If you are unable to comply with the GPL, a 
#	commercial license for this software and support services are available
#	from Mbedthis Software at http://www.mbedthis.com
#
###############################################################################
#
#	Source utility functions
#
if [ -f /etc/init.d/functions ]
then
	#	Red-Hat
	. /etc/init.d/functions
	. /etc/sysconfig/network
elif [ -f /lib/lsb/init-functions ]
then
	#	Debian
	. /lib/lsb/init-functions
	. /etc/default/rcS
fi

APPWEB_NAME="Mbedthis Appweb"
APPWEB=appweb
APPWEB_CMD=/usr/sbin/$APPWEB
APPWEB_DIR=/etc/appweb
INITLOG_ARGS=""
RETVAL=0

###############################################################################

start() {
	local RETVAL=0
	echo -n "Starting ${APPWEB_NAME}: "
	if type daemon >/dev/null 2>&1
	then
		daemon $APPWEB_CMD -b -r ${APPWEB_DIR} -f appweb.conf
		RETVAL=$?
		[ ${RETVAL} = 0 ] && touch /var/lock/subsys/$APPWEB
	elif type start-stop-daemon >/dev/null 2>&1
	then
		start-stop-daemon --start --quiet --exec "${APPWEB_CMD}" -- \
			-b -r ${APPWEB_DIR} -f appweb.conf
		RETVAL=$?
		[ ${RETVAL} = 0 ] && touch /var/lock/$APPWEB
	fi
	if [ ${RETVAL} = 0 ] ; then
		echo "OK"
	else
		echo "FAILED"
	fi
	return ${RETVAL}
}

###############################################################################

stop() {
	if type start-stop-daemon >/dev/null 2>&1
	then
		echo -n $"Stopping ${APPWEB_NAME}: "
		start-stop-daemon --stop --retry=2 --quiet --oknodo \
			--exec "${APPWEB_CMD}"
		RETVAL=$?
		[ ${RETVAL} = 0 ] && rm -f /var/lock/$APPWEB
	elif type killproc >/dev/null 2>&1
	then
		# Note: killproc exists on debian also.
		echo -n $"Stopping ${APPWEB_NAME}: "
		killproc $APPWEB_CMD
		RETVAL=$?
		[ ${RETVAL} = 0 ] && rm -f /var/lock/subsys/$APPWEB
	fi
	if [ ${RETVAL} = 0 ] ; then
		echo "OK"
	else
		echo "FAILED"
	fi
}

###############################################################################
#
#	Main script
#

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status $APPWEB_CMD
		RETVAL=$?
		;;
	restart|reload|force-reload)
		stop
		start
		;;
	condrestart)
		if [ -f /var/lock/subsys/$APPWEB -o -f /var/lock/$APPWEB ]; then
			stop
			start
		fi
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|condrestart|status}"
		exit 1
esac

exit $RETVAL

##
##  Local variables:
##  tab-width: 4
##  c-basic-offset: 4
##  End:
##  vim: sw=4 ts=4
##
