#! /bin/sh
#
#  hylafax, v 1.2 2001/01/10 Christian Zoffoli <czoffoli@linux-mandrake.com>
#
# chkconfig: 12345 97 05
# description: HylaFAX(tm) is a sophisticated enterprise-strength fax \
#              package for class 1 and 2 fax modems on unix systems. \
#              This init script is the preferred way to start HylaFAX,\
#              but does not, by default, activate any modems. Consult \
#              the faxgetty(8) and faxmodem(8) man pages for more info.
#

SPOOL=/var/spool/fax

# Source networking configuration.
. /etc/sysconfig/network

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

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

checkvar() {
	if [ ! "$1" ]; then
		echo -n -e "HylaFAX ERROR (old setup.cache) please run faxsetup -server\n"
		exit 1
	fi

	if [ $1 = "yes" -o  $1 = "1" ]; then
		return 0
	fi
	return 1
}

check_config()	{
	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
 
		return 1
	}

	# Loading Config
	PATH_SAVE=$PATH
	. $SPOOL/etc/setup.cache
	PATH=$PATH_SAVE:$PATH

	return 0
}

start()	{
	local RETVAL2=0

	OPTS="-i hylafax"
	if checkvar "$HFAXD_OLD_PROTOCOL"; then
		OPTS="$OPTS -o 4557"
		OUT=" OLD Protocol"
	fi

	if checkvar "$HFAXD_SNPP_SERVER"; then
		OPTS="$OPTS -s snpp"
		if [ "$OUT" ]; then
			OUT="$OUT - "
		fi
		OUT="$OUT SNPP"
	fi

	if [ "$OUT" ]; then
		OUT="( $OUT )"
	fi

	if checkvar "$FAXQ_SERVER"; then
		msg_starting "HylaFAX queue manager"
		daemon faxq
		RETVAL=$?
	fi

	if checkvar "$HFAXD_SERVER"; then
		msg="HylaFAX server"
		[ -n "$OUT" ] && msg="$msg $OUT"
		msg_starting "HylaFAX server"
		daemon hfaxd $OPTS
		RETVAL2=$?
	fi

	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/hylafax || RETVAL=1
}

stop() {
	# stop can't return failure by LSB. so make these vars local
	local RETVAL=0
	local RETVAL2=0

	msg_stopping "HylaFAX queue manager"
	daemon /usr/sbin/faxquit
	RETVAL=$?

	msg_stopping "HylaFAX server"
	killproc hfaxd
	RETVAL2=$?

	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f /var/lock/subsys/hylafax
}

faxgettyreset() {
	if pidofproc faxgetty > /dev/null; then
		msg_starting "FaxGetty restart process"
		killproc faxgetty
		msg_starting "FaxGetty restart process (init reload)"
		daemon init q
	fi

	return 0
}

restart() {
	stop
	start
	faxgettyreset
}

hstatus() {
	status hfaxd
	RETVAL=$?
	status faxq
	return $(($RETVAL + $?))
}

check_config || exit 1

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

exit 0
