#!/bin/sh 
#
# chkconfig: 345 80 16
# description: ASP.NET Web server
#

. /etc/init.d/functions
. /etc/xsp.conf
DESC="XSP WebServer"
CFGDIR=/etc/xsp
PIDFILE=/var/run/xsp/xsp.pid
RUNXSP=/usr/bin/xsp
RETVAL=0

mesg_retval()
{
	if [ "$RETVAL" == 0 ];then
	    echo_success
	else
	    echo_failure
	fi
	echo
}

start()
{
	msg_starting "$DESC"
	if [ ! -f $PIDFILE ]; then
	    start-stop-daemon --start --background --make-pidfile \
		    --quiet --pidfile $PIDFILE \
		    --chuid $user \
		    --exec $RUNXSP -- \
		    --nonstop \
	    	    --applications $applications \
		    --root $rootdir \
		    --port $port \
		    --address $bindaddr \
		    --appconfigdir $cfgdir 

	    RETVAL=$?
	else
	    RETVAL=1
	fi

	mesg_retval
        return $RETVAL
}

stop()
{
	msg_stopping "$DESC"
	if [ -f $PIDFILE ]; then
    	    start-stop-daemon --stop --quiet --signal 6 --pidfile $PIDFILE
	    RETVAL=$?
	    if [ "$RETVAL" != 0 ];then 
		LN=`ps -p \`cat $PIDFILE\` | wc -l`
		if [ $LN = 1 ]; then 
		    RETVAL=0
		fi
	    fi

	    if [ "$RETVAL" = 0 ];then
		rm -f $PIDFILE
	    fi
	    mesg_retval
	else
	    echo_passed; echo
	fi


        return $RETVAL
}

restart()
{
        stop
        start
}

case "$1" in
	start)
		start
		;;
	reload|sreload|restart)
		restart
		;;
	condstop|stop)
		stop
		;;
	status)
		if [ -f $PIDFILE ]; then
		    LN=`ps -p \`cat $PIDFILE\` | wc -l`
		    if [ $LN = 2 ];then 
			echo "$DESC exists"
		    else
			echo "pidfile exists but server does not"
		    fi
		else
		    echo "$DESC is not loaded"
		fi
		;;
	*)
		msg_usage "${0##*/} {start|stop|status|reload|restart|condstop}"
		RETVAL=1
esac

exit $RETVAL



