#!/bin/sh

### BEGIN INIT INFO
# Provides:          mqtt-to-bluetooth
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Control a Java micro service server
# Description:       Control a Java micro service with the MQTT to Bluetooth application
### END INIT INFO

start() {
    /usr/bin/java -jar Path/MQTT_To_Bluetooth.jar &
    echo "Service started."
}

stop() {
    pid=`ps -ef | grep '[j]ava Path/MQTT_To_Bluetooth.jar' | awk '{ print $2 }'`
    echo $pid
    kill $pid
    sleep 2
    echo "Service killed."
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    status_of_proc "mqtt-to-bluetooth" "/usr/bin/java Path/MQTT_To_Bluetooth.jar" && exit 0 || exit $?
    ;;
  *)
    echo "Usage: /etc/init.d/mqtt-to-bluetooth {start|stop|restart|status}"
    exit 1
esac
exit 0
