#! /bin/sh
### BEGIN INIT INFO
# Provides:          ttyd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO
#
# ttyd	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)ttyd  1.8  03-Mar-1998  miquels@cistron.nl
#
# This file was automatically customized by dh-make on #DATE#

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
DAEMON="/usr/sbin/ttyd"
NAME="ttyd"
DESC="Remote Modem Utility"

# edit /etc/default/ttyd to change this
NO_START="0"
ARGS=""

set -e

if [ -x "$DAEMON" ]; then
    HAVE_TTYD=1
else
    exit 0
fi

. /lib/lsb/init-functions
test -f /etc/default/rcS && . /etc/default/rcS
test -f /etc/default/ttyd && . /etc/default/ttyd

if [ "$NO_START" != "0" -a "$1" != "stop" ]; then
    if [ "$VERBOSE" != "no" ]; then
        log_warning_msg "Not starting ttyd - edit /etc/default/ttyd and change NO_START to be 0."
    fi
    exit 0
fi

if [ -z "$ARGS" -a "$1" != "stop" ]; then
    if [ "$VERBOSE" != "no" ]; then
        log_warning_msg "Not starting ttyd - edit /etc/default/ttyd and configure ARGS."
    fi
    exit 0
fi

ttyd_start() {
    if start-stop-daemon --start --quiet \
           --exec "$DAEMON" -- $ARGS; then
        return 0
    fi
    return 1
}

ttyd_stop() {
    if start-stop-daemon --oknodo --stop --quiet --name "$NAME" \
           --exec "$DAEMON"; then
        return 0
    fi
    return 1
}

case "$1" in
  start)
    log_begin_msg "Starting $DESC..."
    if ttyd_start; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
  ;;
  stop)
    log_begin_msg "Stopping $DESC..."
    if ttyd_stop; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
  ;;
  restart|force-reload|reload)
    log_begin_msg "Forcing reload of $DESC..."
    if ! ttyd_stop; then
        log_end_msg 1
    fi
    sleep 1
    if ttyd_start;
    then
        log_end_msg 0
    else
        log_end_msg 1
    fi
  ;;
  status)
    exit 4
  ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 2
	;;
esac

