#! /bin/sh

### BEGIN INIT INFO
# Provides:          remote-tty
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the remote-tty serial console manager
# Description:       This starts remote-tty listening and logging
#                    on all of its configured serial ports.
### END INIT INFO

set -e

test -x /usr/sbin/ttysrv || exit 0

case "$1" in
  start)
	echo "Starting remote-tty console manager:"
	cd /etc/remote-tty/dev
	if [ ! "x`ls`" = x ]; then
		su rttymgr -c "/usr/sbin/startsrv *"
	else
		echo "  no consoles configured"
	fi
	;;
  stop)
        echo -n "Stopping remote-tty console manager:"
	cd /var/run/remote-tty/pid
	for i in *; do
		if [ -f $i ]; then
			echo -n " $i"
			if start-stop-daemon --stop --quiet --oknodo \
					--pidfile /var/run/remote-tty/pid/$i;
					then
				rm -f $i
				rm -f /var/run/remote-tty/sock/$i
			fi
		else
			echo -n " nothing running"
		fi
	done
	echo "."
	;;
  restart|force-reload)
  	sh $0 stop
	sleep 5
	sh $0 start
	;;
  *)
	echo "Usage: /etc/init.d/ssh {start|stop}"
	exit 1
esac

exit 0
