#!/bin/sh
# vi:set ts=2 sw=2 sts=2 et:
# Start or stop HylaFAX
#
# This script start and stop hylafax daemons.
# It is driver by a configuration file sourced by this shell
# and called /etc/default/hylafax.

# 28-Dec-2003 Ross Boylan
# Add "init" option to USE_FAXGETTY
# This will cause this script to assume that faxgetty is already
# running from the inittab file


PATH=/sbin:/bin:/usr/sbin:/usr/bin
HYLAFAX_HOME=/var/spool/hylafax
FAXGETTY=/usr/sbin/faxgetty
FAXMODEM=/usr/sbin/faxmodem

RUN_HYLAFAX=0

if [ -f /etc/hylafax/setup.cache ];
then
  #
  # Check if old protocol server should be started
  grep "^HFAXD_OLD_PROTOCOL='yes'" /etc/hylafax/setup.cache >/dev/null
  if [ $? -eq 0 ];
  then
    OLDPROT="-o 4557"
  else
    OLDPROT=
  fi

  #
  # Check if new protocol server should be started
  grep "^HFAXD_SERVER='yes'" /etc/hylafax/setup.cache >/dev/null
  if [ $? -eq 0 ];
  then
    NEWPROT="-i 4559"
  else
    NEWPROT=
  fi

  #
  # Check if SNPP server should be started
  grep "^HFAXD_SNPP_SERVER='yes" /etc/hylafax/setup.cache >/dev/null
  if [ $? -eq 0 ];
  then
    SNPP="-s 444"
  else
    SNPP=
  fi
else
  echo "ERROR: You must run faxsetup before starting hylafax." 1>&2
  exit 1
fi

if [ -r /etc/default/hylafax ]; then
  . /etc/default/hylafax
fi

test -x /usr/sbin/faxq || exit 0
test -x /usr/sbin/hfaxd || exit 0

if [ "$RUN_HYLAFAX" -ne 1 ]; then
  echo "hylafax is disabled, see /etc/default/hylafax"
  exit 0
fi


echo_fax_devices()
{
  for device in config.tty[MS][0-9] config.faxCAPI; do
    if [ -f "$device" ]; then
      echo "$device"
    fi
  done
}


x()
{
	echo + "$@" 1>&2
	eval "$@"
}


update_dir()
{
  # This function won't copy dot-files.
  for src in "$1/"*
  do
    dst="$2/"$(basename $src)
    if [ ! -e "$dst" ] ;
    then
      x /bin/cp -a "\"$src\"" "\"$dst\""
    else
      if [ -d "$dst" ];
      then
        update_dir "$src" "$dst"
      else
        if [ -L "$dst" -a \
           '(' ! -L "$src" -o x$(readlink "$dst") != x$(readlink "$src") ')' ];
        then
          x /bin/rm -f "\"$dst\""
          x /bin/cp -a "\"$src\"" "\"$dst\""
        else
          if [ "$src" -nt "$dst" ];
          then
            # The configuration file has been changed in $src
            x /bin/cp -a "\"$src\"" "\"$dst\""
          else
            if [ "$dst" -nt "$src" ];
            then
              # The configuration file has been changed in $dst
              echo "ERROR: $dst is newer than $src" 1>&2
              echo "Please send a bug report on the hylafax-server package" 1>&2
              exit 1
            fi
          fi
        fi
      fi
    fi
  done
}


# This function in called when starting the hylafax server. 
# It clone the /etc directory into the chroot() directory
copy_slash_etc()
{
  SPOOL=$(grep SPOOL= /etc/hylafax/setup.cache | awk -F= '{print $2}' | tr -d \'\")

  if [ -z "$SPOOL" ];
  then
    echo "ERROR: You must run faxsetup before starting hylafax" 1>&2
    echo "and be sure to check that the SPOOL variable is assigned." 1>&2
    exit 1
  fi

  if [ -L "$SPOOL/etc" ];
  then
    # Upgrading from old system where $SPOOL/etc was a link to /etc
    x /bin/rm "\"$SPOOL/etc\""
  else
    if [ -e "$SPOOL/etc" -a ! -d "$SPOOL/etc" ];
    then
      echo "ERROR: $SPOOL/etc isn't the old link or new directory." 1>&2
      echo "Please send a bug report on the hylafax-server package." 1>&2
      exit 1
    fi
  fi

  if [ ! -d "$SPOOL/etc" ];
  then
    x /bin/mkdir "\"$SPOOL/etc\""
  fi

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

  if [ ! -e "$SPOOL/bin" \
       -o '(' -L "$SPOOL/bin" -a x$(readlink "$SPOOL/bin") != "xetc/bin" ')' ];
  then
    x /bin/rm -f "\"$SPOOL/bin\""
    x /bin/ln -s etc/bin "\"$SPOOL/bin\""
  fi
}


case "$1" in
  start)
    count=$(ps aux | grep -E '/usr/sbin/(hfaxd|fax[q])' | wc -l)
    if [ $count -eq 0 ];
    then
      copy_slash_etc
      echo -n "Starting HylaFAX daemons:"
      echo -n " faxq"
      start-stop-daemon --start --exec /usr/sbin/faxq
      echo -n " hfaxd"
      start-stop-daemon --start --exec /usr/sbin/hfaxd -- $BINDTO $NEWPROT $OLDPROT $SNPP
      cd ${HYLAFAX_HOME}/etc
      devices="`echo_fax_devices`"
      if [ ${USE_FAXGETTY} = "yes" ]; then
        echo -n " faxgetty"
        for device in $devices none; do
          [ "$device" = none ] && continue
          ${FAXGETTY} `echo $device | cut -d . -f 2` &
        done
      elif [ ${USE_FAXGETTY} != "init" ]; then
        echo -n " faxmodem"
        for device in $devices none; do
          [ "$device" = none ] && continue
          ${FAXMODEM} `echo $device | cut -d . -f 2` &
        done
      fi
      # do nothing for "init"
    else
      echo "Not starting HylaFAX daemons since they are already running." 1>&2
      exit 1
    fi  
    echo "."
    ;;
  stop)
    echo -n "Stopping HylaFAX daemons:"
    echo -n " faxq"
    start-stop-daemon --stop --exec /usr/sbin/faxq
    echo -n " hfaxd"
    start-stop-daemon --stop --exec /usr/sbin/hfaxd -- $BINDTO $NEWPROT $OLDPROT $SNPP
    if [ ${USE_FAXGETTY} = "yes" ]; then
        echo -n " faxgetty"
        killall faxgetty 2> /dev/null || true
    fi
    echo "."
    ;;
  restart|force-reload)
    copy_slash_etc
    echo -n "Restarting HylaFAX daemons:"
    echo -n " faxq"
    start-stop-daemon --stop --exec /usr/sbin/faxq
    sleep 1
    start-stop-daemon --start --exec /usr/sbin/faxq
    echo -n " hfaxd"
    start-stop-daemon --stop --exec /usr/sbin/hfaxd -- $BINDTO $NEWPROT $OLDPROT $SNPP
    sleep 1
    start-stop-daemon --start --exec /usr/sbin/hfaxd -- $BINDTO $NEWPROT $OLDPROT $SNPP
    cd ${HYLAFAX_HOME}/etc
    devices="`echo_fax_devices`"
    if [ ${USE_FAXGETTY} = "yes" ]; then
      echo -n " faxgetty"
      killall faxgetty 2> /dev/null || true
      for device in $devices none; do
        [ "$device" = none ] && continue
        ${FAXGETTY} `echo $device | cut -d . -f 2` &
      done
    elif [ ${USE_FAXGETTY} != "init" ]; then
      echo -n " faxmodem"
      killall faxmodem 2> /dev/null || true
      for device in $devices none; do
        [ "$device" = none ] && continue
        ${FAXMODEM} `echo $device | cut -d . -f 2` &
      done
    fi
    # do nothing for "init"
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/hylafax {start|stop|restart|force-reload}"
    exit 1
    ;;
esac

exit 0
