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

### BEGIN INIT INFO
# Provides: hylafax+
# Required-Start: $local_fs $network $syslog $remote_fs
# Required-Stop:  $local_fs $network $syslog $remote_fs
# Should-Start: hfaxd faxd
# Should-Stop:    hfaxd faxd
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts and stops the HylaFAX+
# Description: Starts and stops the HylaFAX+ server and queue manager \
#              used to provide FAX services
### END INIT INFO

return_success=
return_failure=

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
  alias START_DAEMON=daemon
  alias STATUS=status
  alias ACTION=action
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
  alias START_DAEMON=daemon
  alias STATUS=status
  alias ACTION=action
elif [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  alias START_DAEMON=start_daemon
  alias STATUS=suse_status
  alias ACTION=suse_action
  return_success=$rc_done
  return_failure=$rc_failed
else
  exit 0
fi

NAME=hylafax+

SPOOL=/var/spool/$NAME
[ -d $SPOOL ] || SPOOL=/var/spool/fax
LKF=/var/lock/subsys/$NAME

HFAXD_ARGS=
FAXQ_ARGS=

# Source init script configuration
[ -f /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME

# Put Extra args first as some may be needed before ports parameters
[ "$HFAXD_EXTRA_ARGS" ] && HFAXD_ARGS=$HFAXD_EXTRA_ARGS

# Bind to a specific address
[ "$HFAXD_LISTEN" ] && HFAXD_ARGS="$HFAXD_ARGS -l $HFAXD_LISTEN"

[ "$HFAXD_OLD_PROTOCOL" = "yes" ] && HFAXD_ARGS="$HFAXD_ARGS -o 4557"

[ -f "$SPOOL/etc/pagermap" ] && HFAXD_ARGS="$HFAXD_ARGS -s snpp"

HFAXD_ARGS="$HFAXD_ARGS -i hylafax"

HFAXD_ARGS="$HFAXD_ARGS -q $SPOOL"
FAXQ_ARGS="$FAXQ_ARGS -q $SPOOL"

start()	{
	check_config || exit 1

	echo -n "Starting HylaFAX+ queue manager (faxq): "
	START_DAEMON /usr/sbin/faxq
	RETVAL=$?
	[ $RETVAL -eq 0 ] && echo -e $return_success || echo -e $return_failure

	echo -n "Starting HylaFAX+ server (hfaxd): "
	START_DAEMON /usr/sbin/hfaxd $HFAXD_ARGS
	RETVAL2=$?
	[ $RETVAL2 -eq 0 ] && echo -e $return_success || echo -e $return_failure
	
	reset_faxgetty
	
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch $LKF || RETVAL=1
        return $RETVAL
}

stop() {
	ACTION "Shutting down HylaFAX+ queue manager (faxq): " /usr/sbin/faxquit
	RETVAL=$?
	
	echo -n "Shutting down HylaFAX+ server (hfaxd): "
	killproc hfaxd
	RETVAL2=$?
	[ $RETVAL2 -eq 0 ] && echo -e $return_success || echo -e $return_failure
	
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f $LKF || RETVAL=1
	return $RETVAL
}

restart() {
	stop
	start
}

suse_action() {
	echo -n $1
	$2
	RETVAL=$?
	[ $RETVAL -eq 0 ] && echo -e $return_success || echo -e $return_failure
	return $RETVAL
}

suse_status() {
	checkproc $1
	rc_status -v
}

hfstatus() {
	echo -n "HylaFAX+ client-server protocol server: "
	STATUS hfaxd
	echo -n "HylaFAX+ queue manager process: "
	STATUS faxq
}

check_config()	{
	[ -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(8C) command.  Read the documentation on setting up
		HylaFAX+ before you startup a server system.
 
		EOF
    		
		exit 1	
	}
}

reset_faxgetty () {
	pids=`pidofproc faxgetty`
	[ -n "$pids" ] && ps h $pids >/dev/null 2>&1 && {
		echo -n "Restarting HylaFAX+ modem manager (faxgetty):"
		# will be respawned from /etc/inittab
		killproc faxgetty
		[ $? -eq 0 ] && echo -e $return_success || echo -e $return_failure
	}
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	hfstatus
	;;
  restart|reload)
	restart
	;;
  condrestart)
	[ -f $LKF ] && restart
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|reload|condrestart}"
	exit 1
	;;
esac

exit $?