#! /bin/sh

# --------------------------------------------------------------------------
# No user configurable parts below this line.
DIALD=/usr/sbin/diald
FIFO=""

#. /etc/init.d/functions

# Make sure that we don't get started if there is no options file.
# We certainly don't want to get started if diald is missing.
test -x /usr/sbin/diald || exit 0
test -f /etc/diald/diald.options || exit 0

# Look for fifo in config file
NEW_FIFO=`egrep '^[^#]*fifo' /etc/diald/diald.options | sed -e 's/^ *fifo *//'`
if test "X$NEW_FIFO"="X"; then
    # The user location exists, and is a named pipe.
    FIFO="$NEW_FIFO";
fi

case "$1" in
  start)
      if [ "$FIFO" != "" ] ; then
        if test -p $FIFO ; then
          rm -f $FIFO
        fi
        mknod --mode=0660 $FIFO p
        chown root.dialout $FIFO
	if test -p $FIFO ; then
          echo Created diald control fifo: $FIFO
        fi
      fi
      start-stop-daemon --start --verbose \
              --pidfile /var/run/diald.pid --exec /usr/sbin/diald
    ;;
  stop)
      if [ "$FIFO" != "" ] ; then
        if test -p $FIFO ; then
          rm -f $FIFO
          echo Removed diald control fifo: $FIFO
        fi
      fi
      start-stop-daemon --stop --verbose \
              --pidfile /var/run/diald.pid --exec /usr/sbin/diald
    ;;
    *)
    echo "Usage: /etc/init.d/diald {start|stop}"
    exit 1
esac

exit 0

