#! /bin/sh
### BEGIN INIT INFO
# Provides:          brltty
# Required-Start:    mountkernfs
# Required-Stop:     
# Should-Start:      udev
# Should-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Braille terminal driver 
# Description: Used to provide access to refreshable braille terminals.
### END INIT INFO

set -e

setvar DAEMON = "/sbin/brltty"
setvar NAME = 'brltty'
setvar DESC = ''Braille terminal driver''

test -f $DAEMON || exit 0

# /etc/brltty.conf may need to be propagated from the initramfs. (This is a
# pretty awful hack.)
if test -e /dev/.initramfs/brltty.conf && test -e /etc/default/brltty {
	mv /dev/.initramfs/brltty.conf /etc/brltty.conf
	sed -i -e 's/^RUN_BRLTTY=.*/RUN_BRLTTY=yes/' /etc/default/brltty
}

test -r /etc/default/brltty && source /etc/default/brltty

# Edit /etc/default/brltty and set RUN_BRLTTY=yes to allow brltty to be
# started.
if test $RUN_BRLTTY != yes {
	exit 0
}

set -e

test -r /etc/default/locale && source /etc/default/locale
test -n $LANG && export LANG

source /lib/lsb/init-functions

case (1) {
  start {
	if test $RUNLEVEL = "S" -a $PREVLEVEL = "N" {
		if test $START_IN_INITRAMFS = "true" -o $START_IN_INITRAMFS = "yes" {
			if $0 status >/dev/null {
				$0 stop
			} else {
				log_warning_msg 'BRLTTY was not running, did you forget to invoke "update-initramfs -u"?'
			}
		}
	}
	log_daemon_msg "Starting $DESC" $NAME
	if start-stop-daemon --start --oknodo --exec $DAEMON -- $ARGUMENTS {
	    log_end_msg 0
	} else {
	    log_end_msg 1
	}
	}
  stop {
	log_daemon_msg "Stopping $DESC" $NAME
	if start-stop-daemon --stop --quiet --oknodo --retry 5 --exec $DAEMON {
	    log_end_msg 0
	} else {
	    log_end_msg 1
	}
	}
  restart|force-reload {
	log_daemon_msg "Restarting $DESC" $NAME
	start-stop-daemon --stop --quiet --retry 5 --exec $DAEMON
	if start-stop-daemon --start --quiet --exec $DAEMON -- $ARGUMENTS {
	    log_end_msg 0
	} else {
	    log_end_msg 1
	}
	}
  status {
        status_of_proc $DAEMON $NAME && exit 0 || exit $?
        }
  * {
	echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
	exit 1
	}
}

exit 0