#! /bin/sh
#
# Start or stop c2faxrecv daemon
#

test -x /usr/bin/c2faxrecv || exit 0

# This function clones the /etc directory into the chroot() directory
# Usually hylafax does that for us, but if it isn't running,
# we have to do it ourselves, even if we don't run c2faxrecv (for c2faxsend)
copy_slash_etc()
{
  SPOOL=/var/spool/hylafax/

  if [ -d "$SPOOL/etc" ];
  then
    cp -a "/etc/hylafax/config.faxCAPI" "$SPOOL/etc"
  else
    echo "Can't create directory $SPOOL/etc" 1>&2
    exit 1
  fi

}

copy_slash_etc

run_capi4hylafax=0
if [ -f /etc/default/capi4hylafax ]; then
    . /etc/default/capi4hylafax
fi

if [ "$run_capi4hylafax" = 0 ]; then
    cat <<EOF

Please edit the file /etc/hylafax/config.faxCAPI according to your needs.
The current /etc/hylafax/config.faxCAPI is just the sample file that is
provided with the upstream package, so it will not be useful at all for
you. (Please read /usr/share/doc/capi4hylafax/README.Debian)

After you are done you will have to edit /etc/default/capi4hylafax as
well. There you will have to set the variable run_capi4hylafax to 1,
and then type "/etc/init.d/capi4hylafax start" to start the c2faxrecv
daemon.

EOF
	exit 0
fi

C2FAXRECVPID=/var/run/c2faxrecv.pid

case "$1" in
	start)
		echo -n "Starting capi4hylafax: c2faxrecv"
		start-stop-daemon --start --quiet --background \
			--make-pidfile --pidfile $C2FAXRECVPID \
			--exec /usr/bin/c2faxrecv
		echo "."
		;;
	stop)
		echo -n "Stopping capi4hylafax: c2faxrecv"
		start-stop-daemon --stop --quiet --pidfile $C2FAXRECVPID
		rm -f $C2FAXRECVPID
		echo "."
		;;
	restart | force-reload)
		echo -n "Restarting capi4hylafax: c2faxrecv"
		start-stop-daemon --stop --quiet --pidfile $C2FAXRECVPID
		sleep 2
		start-stop-daemon --start --quiet --background \
			--make-pidfile --pidfile $C2FAXRECVPID \
			--exec /usr/bin/c2faxrecv
		echo "."
		;;
	*)
		echo "Usage: /etc/init.d/capi4hylafax {start|stop|restart|force-reload}"
		exit 1
esac
exit 0
