#! /bin/sh
#
# tux        This starts and stops the TUX kernel-based http server.
#
# chkconfig: - 50 50
# description: The TUX threaded kernel-based http server
#
# processname: /usr/sbin/tux
# config: /etc/sysconfig/tux
# config: /etc/sysctl.conf


# Source function library.
. /etc/rc.d/init.d/functions

# Set defaults
NRCPUS=$(cat /proc/cpuinfo | grep '^processor[	 ]*:' | wc -l)
TUXTHREADS=$NRCPUS

# Get config.
. /etc/sysconfig/network
. /etc/sysconfig/tux

# Check that networking is up.
if [ ${NETWORKING} = "no" ]
then
	exit 0
fi

if [ $NRCPUS -gt $TUXTHREADS ] ; then
    TUXTHREADS=$NRCPUS
fi

# DOCROOT must have exactly one trailing /; MODULEPATH precisely none
MODULEPATH=$(echo $DOCROOT | sed 's:/$::g')
DOCROOT=$(echo $MODULEPATH | sed 's:$:/:')

[ -f /usr/sbin/tux ] || exit 1
RETVAL=0

start(){
    echo -n "Starting tux: "
    # TUX ships as a module but many installations will build
    # it into the kernel for maximum performance.  Try to modprobe
    # but failures are fine.
    modprobe tux >/dev/null 2>&1
    echo $TUXTHREADS > /proc/sys/net/http/threads
    echo $DOCROOT > /proc/sys/net/http/documentroot
    daemon /usr/sbin/tux $TUXTHREADS $MODULEPATH $TUXMODULES
    RETVAL=$?
    echo
    touch /var/lock/subsys/tux
}

stop(){
    echo -n "Stopping tux: "
    # FIXME: killall is a quick hack here that will be fixed in short order
    killall -9 /usr/sbin/tux
    /usr/sbin/stoptux
    RETVAL=$?
    echo
    rm -f /var/lock/subsys/tux
}

restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/tux ] && restart
}


# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	status /usr/sbin/tux
	;;
    restart|reload)
	restart
	;;
    condrestart)
	condrestart
	;;
    *)
	echo "Usage: tux {start|stop|status|restart|condrestart|reload}"
	RETVAL=1
esac

exit $RETVAL
