#!/bin/sh
#
#  $Id$
#
# /etc/init.d/fancyhttpd
#
#
# hints for update-rc.d on Debian like distros and chkconfig on SuSE like distros
#
### BEGIN INIT INFO
# Provides:       fancyhttpd
# Required-Start: $network $remote_fs
# Required-Stop:  $network $remote_fs
# Default-Start:  2 3 5
# Default-Stop:   0 1 2 4 6
# Description:    The Fancy::Sock::HTTPd demo application fancyhttpd.
### END INIT INFO
#
#
# hints for chkconfig on RedHat like distros
#
# chkconfig: 345 90 10
# description: Starts/stops the Fancy::Sock::HTTPd demo application fancyhttpd.
# processname: fancyhttpd
#


# defaults
export FANCYHTTPD_OPTIONS=""
export FANCYHTTPD_USER="www-data"
export FANCYHTTPD_GROUP=""

# read in options
test -f /etc/default/fancyhttpd && . /etc/default/fancyhttpd 

test -z "$FANCYHTTPD_GROUP" && export FANCYHTTPD_GROUP=$(getent group $(getent passwd ${FANCYHTTPD_USER} |cut -f4 -d':') |cut -f1 -d':')


case "$1" in
    start)
        if [ "$FANCYHTTPD_START" != "yes" ] ; then
          echo "FANCYHTTPD_START is not set to \"yes\" in /etc/default/fancyhttpd, aborting."
          exit 1 
        fi
    
        echo "Starting fancyhttpd ..."
        chown -R $FANCYHTTPD_USER:$FANCYHTTPD_GROUP /etc/fancyhttpd /var/run/fancyhttpd /var/fancyhttpd
        sudo -u $FANCYHTTPD_USER /usr/bin/fancyhttpd start ${FANCYHTTPD_OPTIONS}
        ;;
    stop)
        echo "Stopping (killing) fancyhttpd ..."
        sudo -u $FANCYHTTPD_USER /usr/bin/fancyhttpd kill ${FANCYHTTPD_OPTIONS}
        ;;
    restart)
        $0 status >/dev/null 2>/dev/null && ( $0 stop || exit 1 ) 
        $0 start
        ;;
    status)
        echo "Statusing fancyhttpd ..."
        sudo -u $FANCYHTTPD_USER /usr/bin/fancyhttpd status ${FANCYHTTPD_OPTIONS}
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
esac

