#!/bin/sh
#
# chkconfig: 345 95 5
# description: Starts and stops the HylaFAX server and queue manager \
#              used to provide FAX services
#

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi


start()	{
	echo -n "Starting HylaFAX queue manager: "
	daemon faxq
	RETVAL=$?
	echo
	
	echo -n "Starting HylaFAX server: "
	daemon hfaxd -i hylafax
	RETVAL2=$?
	echo
	
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && \
		touch /var/lock/subsys/hylafax ||  RETVAL=1
        return $RETVAL
}

stop() {
	action "Shutting down HylaFAX queue manager: " /usr/sbin/faxquit
	RETVAL=$?
	
	echo -n "Shutting down HylaFAX server: "
	killproc hfaxd
	RETVAL2=$?
	echo
	
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && \
		rm -f /var/lock/subsys/hylafax || RETVAL=1
	return $RETVAL
}

restart() {
	stop
	start
}

rhstatus() {
	status hfaxd
	status faxq
}

check_config()	{
	SPOOL=/var/spool/fax
	test -f $SPOOL/etc/setup.cache || {
		cat<<-EOF
 
		HylaFAX FATAL ERROR: $SPOOL/etc/setup.cache is missing!
 
		The file $SPOOL/etc/setup.cache is not present. 
		This probably means the machine has not been setup using the 
		faxsetup(1M) command.  Read the documentation on setting up
		HylaFAX before you startup a server system.
 
		EOF
    		
		exit 1	
	}
}

case "$1" in
  start)
	check_config
	[ $? != 0 ] && exit 1
	start
	;;
  stop)
	stop
	;;
  status)
	rhstatus
	;;
  restart)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/hylafax ] && restart
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|condrestart}"
	exit 1
	;;
esac

exit $?

