#!/bin/bash
echo " B110 Startup Script...."
if [ -f "/home/root/b110_update.tar" ]; then
	echo " Upgrade File found!"
	chmod 777 /home/root
	cd /home/root
	cp /home/root/usr/share/apache2/htdocs/index_upgrade.html /home/root/usr/share/apache2/htdocs/index.html
	tar -xf b110_update.tar
	sync
	killall b110_main
	killall remote
	killall streamer
	cd /home/root/usr/bin
	chmod +x *
	cp /home/root/usr/bin/* /usr/bin
	cd /home/root/etc
	chmod +x inittab
	cp inittab /etc
	cd /home/root/etc/apache2
	cp /home/root/etc/apache2/* /etc/apache2
	cd /home/root/usr/share/apache2/cgi-bin
	chmod +x *.cgi
	cp /home/root/usr/share/apache2/cgi-bin/* /usr/share/apache2/cgi-bin
	cd /home/root/usr/share/apache2/htdocs
	cp /home/root/usr/share/apache2/htdocs/* /usr/share/apache2/htdocs
	cp /usr/share/apache2/htdocs/index_upgrade.html /usr/share/apache2/htdocs/index.html
	apachectl restart
	cd /home/root/etc/network
	chmod +x interfaces
	cp interfaces /etc/network
	cd /home/root/etc/init.d
	chmod +x mystartup.sh
	cp mystartup.sh /etc/init.d
	/usr/sbin/update-rc.d -f mystartup.sh remove
	/usr/sbin/update-rc.d mystartup.sh defaults 100
	cd /home/root
	chmod +x checkupdate.sh
	chmod +x webup.sh
	rm -fr /home/root/etc
	rm -fr /home/root/usr
	sync
	if [ -f "/home/root/m30_fw" ]; then
		chmod +x m30_upgrade.sh
		./m30_upgrade.sh
		rm /home/root/m30_fw
	fi
	rm /home/root/*.tar
	sync
	reboot 
else
	if [ -f "/home/root/update_boot" ]; then
		exit
	else
		echo "Start PCIe/M30 configuration...."
		killall b110_main
		killall remote
		killall b110_ctrl
		killall streamer
		chmod 777 /home/root
		cd /home/root
		chmod +x checkupdate.sh
		chmod +x webup.sh
		./checkupdate.sh&
		./webup.sh&
		cp /home/root/fpga_load /var/tmp
		cp /home/root/remote /var/tmp
		cp /home/root/b110_main /var/tmp
		cp /home/root/b110_ctrl /var/tmp
		cp /home/root/ctrl/ip_config.sh /var/tmp
		cp /home/root/ctrl/br_config.sh /var/tmp
		cp /usr/share/apache2/htdocs/index_std.html /usr/share/apache2/htdocs/index.html
		sync
		cd /var/tmp
		chmod +x fpga_load
		chmod +x remote
		chmod +x b110_main
		chmod +x b110_ctrl
		chmod +x ip_config.sh
		chmod +x br_config.sh
		cd /var/tmp
		echo Load FPGA...
		sync
		./fpga_load > /dev/null
		echo Start user processes...
		./remote > /dev/null&
		./b110_main > /dev/null&
		./b110_ctrl > /dev/null&
	fi
fi
