#!/bin/bash
echo " Timer Install Script...."
renice -n 5 $$

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
		pgrep receiver > /dev/null
		result=$?
		if [ "${result}" -eq "0" ] ; then
			echo "receiver running, killing process"
			killall receiver
		fi
		pgrep recorder > /dev/null
		result=$?
		if [ "${result}" -eq "0" ] ; then
			echo "recorder running, killing process"
			killall recorder
		fi
	fi
	
	if [ -f "/var/tmp/b110_update.tar" ]; then
		if [[ `fuser /var/tmp/b110_update.tar 2> /dev/null | wc -w` == 0 ]] ; then
			if [ -f "/home/root/ctrl/SoftwareBuild.ini" ]; then
				tar -xf /var/tmp/b110_update.tar -C /var/tmp ctrl/SoftwareBuild.ini
				result=$?
				if [ ! "${result}" -eq "0" ] ; then
					mkdir -p /var/tmp/ctrl
					echo -n B110 > /var/tmp/ctrl/SoftwareBuild.ini
					echo " B110 Downgrade detected"
				fi
				if cmp -s /var/tmp/ctrl/SoftwareBuild.ini /home/root/ctrl/SoftwareBuild.ini ; then
					accept=1
				else
					accept=0
				fi
			else
				accept=1
			fi
			if [ "${accept}" -eq 0 ] ; then
				rm /var/tmp/b110_update.tar
				echo " New Install File Rejected"
			else				
				mv /var/tmp/b110_update.tar /home/root
				echo " New Install File Ready"
				echo 1 > /var/tmp/new_update_file
				sleep 15 
				sync
				cd /home/root
				chmod +x install.sh
				./install.sh
				exit
			fi
		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

	if [ -f "/var/tmp/d1550_update.all" ]; then
		if [[ `fuser /var/tmp/d1550_update.all 2> /dev/null | wc -w` == 0 ]] ; then
			echo " New D1550 Upgrade File Ready"
            mv /var/tmp/d1550_update.all /var/tmp/d1550_install.all
			echo all > /var/tmp/d1550_actions
		else
			echo " Copying New D1550 Upgrade File"
		fi
	fi

	if [ -f "/var/tmp/d1550_license.lic" ]; then
		if [[ `fuser /var/tmp/d1550_license.lic 2> /dev/null | wc -w` == 0 ]] ; then
			echo " New D1550 License File Ready"
            mv /var/tmp/d1550_license.lic /var/tmp/d1550_install.lic
			echo lic > /var/tmp/d1550_actions
		else
			echo " Copying New D1550 License File"
		fi
	fi

	if [ -f "/var/tmp/camrx_update.fmw" ]; then
		if [[ `fuser /var/tmp/camrx_update.fmw 2> /dev/null | wc -w` == 0 ]] ; then
			echo " New CAMRX Upgrade File Ready"
            mv /var/tmp/camrx_update.fmw /var/tmp/camrx_install.fmw
			echo fmw > /var/tmp/camrx_actions
		else
			echo " Copying New CAMRX Upgrade File"
		fi
	fi

	if [ -f "/var/tmp/camrx_license.lic" ]; then
		if [[ `fuser /var/tmp/camrx_license.lic 2> /dev/null | wc -w` == 0 ]] ; then
			echo " New CAMRX License File Ready"
            mv /var/tmp/camrx_license.lic /var/tmp/camrx_install.lic
			echo lic > /var/tmp/camrx_actions
		else
			echo " Copying New CAMRX License File"
		fi
	fi
done
