#!/bin/sh
#
# Start/stop brltty
#
brltty_start() {
  if [ ! "`ps -C brltty --noheaders|wc -l`" = "0" ]; then
    echo "brltty is already started."
    brltty_status
    exit
  fi
  if [ -x /bin/brltty ]; then
    echo "Starting brltty daemon: /bin/brltty "
    /bin/brltty
  fi
}
brltty_stop() {
  NBPROC="`ps -C brltty --noheaders|wc -l`"
  if [  "$NBPROC" = "1" ]; then
    echo "Stopping brltty..."
    PID="`ps -C brltty --noheaders -o pid`"
    kill $PID
  elif [ "$NBPROC" = "0" ]; then    
    echo "brltty is not started."
  else
    echo "I can't stop brltty, several daemons are running:"
    brltty_status
  fi
}
brltty_status () {
  NBPROC="`ps -C brltty --noheaders|wc -l`"
  if [ "$NBPROC" = "0" ]; then
    echo "No active brltty daemon." 
  elif [ "$NBPROC" = "1" ]; then
    echo "A brltty daemon is running, PID: `ps -C brltty --no-headers -o pid`"
  else
    ps -C brltty -o pid,args
fi
}
case "$1" in
'start')
  brltty_start
  ;;
'stop')
  brltty_stop
  ;;
'restart')
  brltty_stop
  sleep 3
  brltty_start
  ;;
'status')
  brltty_status
  ;;
*)
  echo "usage $0 restart|start|status|stop|"
esac 
