#!/bin/sh

case $1 in
	start)
		echo $(date) "Starting VirtualHere..." >> /tmp/vhusbd.txt
		"${SYNOPKG_PKGDEST}"/vhusbd -b -r /tmp/vhusbd.txt -c "${SYNOPKG_PKGDEST}"/config.ini >> /tmp/vhusbd.txt 2>&1
		rc=$?
		if [[ $rc -eq 0 ]]; then
			echo $(date) "Started successfully" >> /tmp/vhusbd.txt
		else
			echo $(date) "Failed to start" >> /tmp/vhusbd.txt
		fi
		exit $rc
	;;
	stop)
		echo $(date) "Stopping VirtualHere..." >> /tmp/vhusbd.txt
		killall -q vhusbd
		sleep 6 # give enough time for vhusbd to shutdowwn
		PID=`pidof vhusbd`
		if [ "$PID" -ne "" ]; then
			echo $(date) "Problems stopping VirtualHere...kill -SIGKILL $PID..." >> /tmp/vhusbd.txt
			echo $(date) "If you want to restart VirtualHere you may need to reboot your NAS beforehand" >> /tmp/vhusbd.txt
			kill -SIGKILL $PID
		fi
		echo $(date) "Stopped successfully" >> /tmp/vhusbd.txt
		exit 0
	;;
	status)
		PID=`pidof vhusbd`
	 	if [ "$PID" -eq "" ]; then
	 		exit 3
	 	else
	 		exit 0
	 	fi
	;;
	log)
		echo "/tmp/vhusbd.txt"
		exit 0
	;;
esac
