#!/bin/bash
### BEGIN INIT INFO
# Provides:			 qr_scanner
# Required-Start:	 $network
# Required-Stop:	 $network
# Default-Start:	 2 3 4 5
# Default-Stop:		 0 1 6
# Short-Description: Start/stop qr daemon
### END INIT INFO

#Ustawienia zmiennych

USER=root # Uruchom program jako
QR_SCANNER=/etc/qr_reader/qr_scanner.pl # Lokalizacja programu
SCREEN=/usr/bin/screen # Lokalizacja binarki programu screen
SCREEN_NAME=qr # Nazwa sesji screen
PIDFILE=/var/run/qr_scanner.pid # pidfile

	start() {
#		export /usr/lib/arm-linux-gnueabihf/libv4l/v4l1compat.so && sleep 2 &&
		start-stop-daemon --start --background --oknodo \
			--pidfile "$PIDFILE" --make-pidfile \
			--chuid $USER \
			--exec $SCREEN -- -DmUS $SCREEN_NAME $QR_SCANNER
		if [[ $? -ne 0 ]]; then
			echo -e "QR Scanner has failed to start \t\t [\e[0;31m FAIL \e[0m] \n"
			exit 1
		fi
		echo -e "QR Scanner started successfully \t [\e[0;32m  OK  \e[0m] \n"
		}

	stop() {
		start-stop-daemon --stop --pidfile "$PIDFILE"
		if [[ $? -ne 0 ]]; then
			echo -e "QR Scanner has failed to stop \t\t [\e[0;31m FAIL \e[0m] \n"
			exit 1
		fi
		echo -e "QR Scanner stopped successfully \t [\e[0;32m  OK  \e[0m] \n"
		}


case "$1" in
	start)
		start
	;;
	stop)
		stop
	;;
	status)
		status XXXXXXXXXXXX
	;;
	restart)
		stop;
	sleep 2;
	start
	;;
	*)
		echo "Usage: $0 {start|stop|status|restart}"
	;;
esac

exit 0

