:
#
#	Script to start/stop the DigiFAX UNIX facsimile system
#	
#	The master copy is in /etc/init.d/digifax
#	It is linked to /etc/rc0.d/K88digifax (shutdown)
#	And to /etc/rc2.d/S88digifax (startup)
#
#	Except on XENIX, where it is in /etc/rc.d/8/digifax,
#	and there is no shutdown procedure.
#
#	Note:	the su root crap is to get around SCO's 'security',
#		mere mortal UNIXes realize we are already root!
#

#
#	Some versions of Unix (such as UnixWare) use "implementation
#	specific" values for the TZ (timezone) variable, which DigiFAX
#	cannot understand.  Change this variable to be the traditional
#	value, as specified in the environ(5) man page.  For example,
#	TZ=EST5EDT or TZ=CST6CDT, etc.
#
TZ="$TZ"

#
#	When faxsrv has to spawn a command that needs an LUID, this
#	is the LUID that is used.
#
LUIDNAME=-Lroot
UMASK=-U0

#
#	The spool directory
#
SPOOLDIR=/usr/spool/fax
CONF=/etc/conf

#
#	Check to make sure spool directory is here
#
if [ ! -d $SPOOLDIR ]
then
	echo "No spooling directory $SPOOLDIR"
	exit 1;
fi

state="$1"		# Save start/stop request

if [ -f /xenix ]
then
	# XENIX must be faked out
	if [ "Q$state" = Q ]; then state=start; fi
	set 1 2 3 4 5 6 7 8 9
else
	#
	#	Fail-safe: make device nodes just in case idmkenv forgot to
	#
	for d in dfax bfax jfax hpsj
	do
		if grep "	Y" $CONF/sdevice.d/$d >/dev/null 2>&1
		then
			if [ -f $CONF/mdevice.d/$d ]; then
				exec 3<&0 0<$CONF/mdevice.d/$d
			else
				exec 3<&0 0<$CONF/cf.d/mdevice
			fi
			while read name x x x x major x
			do
				if [ "$d" = "$name" ]; then break; fi
			done
			exec 0<&3 3<&-

			exec 3<&0 0<$CONF/node.d/$d
			while read dev name bc minor x
			do
				if [ ! -c /dev/$name ]
				then
					echo "Making device node /dev/$name"
					/etc/mknod /dev/$name c $major $minor
					chmod 600 /dev/$name
				fi
				case "$name" in
				hpsj*)	chmod 666 /dev/$name;;
				dfax*lp) chown lp /dev/$name;;
				esac
			done
			exec 0<&3 3<&-
		fi
	done
fi

#
#	Start or stop the server depending on whether $1 is 'start' or 'stop'
#
case $state in
start)
	pid=`/bin/su root -c "/bin/ps -e" | grep faxsrv | sed -e 's/^  *//' -e 's/ .*//'`
	if [ "${pid}" != "" ]
	then
		echo "FAX services already running."
	else
		cd $SPOOLDIR
		if [ -x /etc/faxsrv ]
		then
			PATH=/etc:/bin:/usr/bin; export PATH
			SHELL=/bin/sh; export SHELL
			/etc/faxsrv $LUIDNAME $UMASK &
			echo "FAX services started."
		fi
	fi
	if [ -c /dev/hpsj0 ]
	then
		# Allow users access to scanner
		chmod 666 /dev/hpsj0
	fi
	;;
stop)
	pid=`/bin/su root -c "/bin/ps -e" | grep faxrun | sed -e 's/^  *//' -e 's/ .*//'`
	if [ "${pid}" != "" ]
	then
		/bin/kill ${pid}
	fi
	pid=`/bin/su root -c "/bin/ps -e" | grep faxsrv | sed -e 's/^  *//' -e 's/ .*//'`
	if [ "${pid}" != "" ]
	then
		/bin/kill ${pid}
	fi
	echo "FAX services stopped."
	;;
*)
	echo "usage: /etc/init.d/digifax {start|stop}"
	;;
esac
