#!/bin/bash
#
# Startup script for the Yaws Web Server
#
# config: /etc/yaws.conf
#
# chkconfig: 2345 65 35
# description: yaws - Erlang enabled http server
# use "/sbin/chkconfig --add yaws" to install
#
#
### BEGIN INIT INFO
# Provides:       yaws
# Required-Start: $local_fs
# Required-Stop:  $local_fs
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    yaws - Erlang enabled http server
### END INIT INFO


# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

PIDFILE=/var/run/yaws.pid
LOCKFILE=/var/lock/subsys/yaws
RETVAL=0

yaws=/usr/bin/yaws
prog=yaws

#
# Default yawsid is "default". If you change this to another ID,
# be sure to also uncomment the yawsid_opts line just below.
#
yawsid=default
yawsid_opts="--id $yawsid"
conf="--conf /etc/yaws/yaws.conf"

YAWS_FLAGS="${yawsid_opts} --daemon --heart ${conf}"


start() {
	action "Starting $prog: " ${yaws} ${YAWS_FLAGS}
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
	    touch $LOCKFILE
	fi
	return $RETVAL
}

stop() {
	action "Stopping $prog: " $yaws ${yawsid_opts} --stop
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
	    rm -f $LOCKFILEs $PIDFILE
	fi
	return $RETVAL
}


reload() {
	action "Reloading $prog: " $yaws ${yawsid_opts} --hup
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  condstop)
	stop
	;;
  status)
        $yaws --status
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if [ -f /tmp/yaws.ctl ] ; then
		stop
		start
	fi
	;;
  reload)
        reload
	;;
  help)
	$yaws -?
	;;
  *)
	echo $"Usage: $prog {start|stop|condstop|restart|condrestart|reload|status|help}"
	exit 1
esac

exit $RETVAL
