#!/bin/sh

export PATH=$PATH:.:/var/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.:/var/lib
export LD_PRELOAD=/var/lib/libsyscalllog.so.1

cd /tmp/

start_emu() {
	evocamd &
	echo evocamd > /var/etc/SifTeam/tmp/cam_active.sif
	echo evocamd > /var/tmp/cam_active.sif
	sleep 3
}

auto_restart() {
	while [ 1 ] ; do
		if [ "`ps -aef | grep evocamd | grep -v grep`" = "" ]; then
			killall evocamd && /var/bin/evocamd &
			sleep 2
		fi
		sleep 2
		if [ "`cat /tmp/ecm.info`" = "" ]; then
			killall evocamd && /var/bin/evocamd &
			sleep 2
		fi
	done
}

stop_emu() {
	kill `pidof evocamd`
	sleep 2
	killall -9 evocamd
	rm -rf /tmp/evocamd*
	rm -rf /tmp/*.info
	rm -f /var/tmp/cam_active.sif
	#rm -f /var/etc/SifTeam/tmp/cam_active.sif
	sleep 2
}

case "$1" in
	start)
		start_emu
		#auto_restart
		;;
	stop)
		stop_emu
		;;
	restart)
		$0 stop
		sleep 5
		$0 start
		;;
	*)
		echo "usage: {start|stop|restart}"
		;;
esac

exit 0
