#!/bin/bash
echo " Timer Install Script...."

while true 
do 
	if [ -e /var/tmp/*.tar ]; then
		pgrep streamer > /dev/null
		result=$?
		if [ "${result}" -eq "0" ] ; then
			echo "streamer running, killing process"
			killall streamer
		fi
	fi
	
	if [ -f "/var/tmp/b110_update.tar" ]; then
		if [[ `fuser /var/tmp/b110_update.tar 2> /dev/null | wc -w` == 0 ]] ; then
			mv /var/tmp/b110_update.tar /home/root
			echo " New Install File Ready"
			echo 1 > /var/tmp/new_update_file
			sleep 5 
			sync
			cd /home/root
			chmod +x install.sh
			./install.sh
			exit
		else
			echo " Copying New Install File "
		fi
	else
		sleep 5 
	fi

	if [ -f "/var/tmp/b110_license.lic" ]; then
		if [[ `fuser /var/tmp/b110_license.lic 2> /dev/null | wc -w` == 0 ]] ; then
			mv /var/tmp/b110_license.lic /home/root
			echo " New License File Ready"
			cp /home/root/b110_license.lic /home/root/ctrl/b110_license.lic
			rm /home/root/b110_license.lic
			sync
			reboot
		else
			echo " Copying New License File "
		fi
	fi
done
