#!/bin/bash
echo " B110 Startup Script...."

echo 0 > /sys/class/leds/d7/device/leds/d7/brightness
echo 0 > /sys/class/leds/d8/device/leds/d8/brightness
echo 0 > /sys/class/leds/d5/device/leds/d5/brightness
echo 0 > /sys/class/leds/d6/device/leds/d6/brightness
echo 0 > /sys/class/leds/d3/device/leds/d3/brightness
echo 0 > /sys/class/leds/d4/device/leds/d4/brightness
echo 0 > /sys/class/leds/d1/device/leds/d1/brightness
echo 0 > /sys/class/leds/d2/device/leds/d2/brightness

echo 1 > /sys/class/leds/d7/device/leds/d7/brightness
echo 1 > /sys/class/leds/d8/device/leds/d8/brightness
echo 1 > /sys/class/leds/d5/device/leds/d5/brightness
echo 1 > /sys/class/leds/d6/device/leds/d6/brightness
echo 1 > /sys/class/leds/d3/device/leds/d3/brightness
echo 1 > /sys/class/leds/d4/device/leds/d4/brightness
echo 1 > /sys/class/leds/d1/device/leds/d1/brightness
echo 1 > /sys/class/leds/d2/device/leds/d2/brightness

if [ -f "/home/root/b110_update.tar" ]; then
	echo " Upgrade File found!"
	chmod 777 /home/root
	cd /home/root
	tar -xf b110_update.tar
	sync
	killall restart.sh > /dev/null
	killall b110_main > /dev/null
	killall remote > /dev/null
	killall b110_ctrl > /dev/null
	killall streamer > /dev/null
	killall receiver > /dev/null
	killall recorder > /dev/null
	killall make_ini > /dev/null
	killall make_json > /dev/null
	killall make_bmp > /dev/null
	killall can > /dev/null
	echo 1 > /sys/class/leds/d7/device/leds/d7/brightness
	echo 0 > /sys/class/leds/d8/device/leds/d8/brightness
	cd /home/root/usr/bin
	chmod +x *
	cp /home/root/usr/bin/* /usr/bin
	cd /home/root/etc
	chmod +x inittab
	cp inittab /etc
	cp hostname /etc
	cd /home/root/etc/apache2
	cp /home/root/etc/apache2/* /etc/apache2
	cd /home/root/usr/share/apache2/cgi-bin
	cp /home/root/usr/share/apache2/cgi-bin/* /usr/share/apache2/cgi-bin
	cd /home/root/usr/share/apache2/htdocs
	cp -R /home/root/usr/share/apache2/htdocs/* /usr/share/apache2/htdocs
	cp /usr/share/apache2/htdocs/index_upgrade.html /usr/share/apache2/htdocs/index.html
	cd /usr/share/apache2/cgi-bin
	chmod 755 *.cgi
	cd /usr/share/apache2/htdocs/
	chmod -R 777 config
	chmod -R 777 preset
	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 checkusb.sh
	chmod +x webup.sh
	rm -fr /home/root/etc
	rm -fr /home/root/usr
	sync
	if [ -f "/home/root/m30_fw" ]; then
		echo 1 > /sys/class/leds/d5/device/leds/d5/brightness
		echo 0 > /sys/class/leds/d6/device/leds/d6/brightness
		chmod +x m30_upgrade.sh
		./m30_upgrade.sh
		rm /home/root/m30_fw
		echo 1 > /sys/class/leds/d3/device/leds/d3/brightness
		echo 0 > /sys/class/leds/d4/device/leds/d4/brightness
	fi
	rm /home/root/*.tar
	echo 1 > /var/tmp/update_boot
	echo 1 > /sys/class/leds/d1/device/leds/d1/brightness
	echo 0 > /sys/class/leds/d2/device/leds/d2/brightness
	sync
	reboot 
else
	if [ -f "/var/tmp/update_boot" ]; then
		exit
	else
		if [ -f "/var/tmp/started" ]; then
			exit
		else
			hostname b110_sapphire
			echo "Start PCIe/M30 configuration...."
			killall restart.sh > /dev/null
			killall b110_main > /dev/null
			killall remote > /dev/null
			killall b110_ctrl > /dev/null
			killall streamer > /dev/null
			killall receiver > /dev/null
			killall recorder > /dev/null
			killall make_ini > /dev/null
			killall make_json > /dev/null
			killall make_bmp > /dev/null
			killall can > /dev/null
			chmod 777 /home/root
			cd /home/root
			chmod +x dtb_check
			./dtb_check
			chmod +x checkupdate.sh
			chmod +x checkusb.sh
			chmod +x webup.sh
			./checkupdate.sh&
			./checkusb.sh&
			./webup.sh&
			cp /home/root/restart.sh /var/tmp
			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 /home/root/make_ini /var/tmp
			cp /home/root/make_json /var/tmp
			cp /home/root/make_bmp /var/tmp
			cp /home/root/can /var/tmp
			cp /home/root/ctrl_test /var/tmp
			cp /home/root/cid_set /var/tmp
			cp /home/root/bdf /var/tmp
			cp /home/root/fp_recovery /var/tmp
			cp /home/root/stream_driver.ko /var/tmp
			cp /home/root/receive_driver.ko /var/tmp
			cp /home/root/record_driver.ko /var/tmp
			cp /home/root/streamer /var/tmp
			cp /home/root/receiver /var/tmp
			cp /home/root/recorder /var/tmp
			cd /usr/share/apache2/cgi-bin
			chmod 755 *.cgi
			cp /usr/share/apache2/htdocs/index_std.html /usr/share/apache2/htdocs/index.html
			mkdir -p /usr/share/apache2/htdocs/config
			mkdir -p /usr/share/apache2/htdocs/preset
			cd /usr/share/apache2/htdocs/
			chmod -R 777 config
			chmod -R 777 preset
			cd /home/root
			mv *.so /usr/lib
			sync
			ldconfig -n -v /usr/lib
			echo 1 > /sys/class/leds/d7/device/leds/d7/brightness
			echo 0 > /sys/class/leds/d8/device/leds/d8/brightness
			sync
			if [ ! -d "/mnt/share" ]; then
				mkdir -p /mnt/share
				cd /mnt/share
				ln -s /usr/share/apache2/htdocs/.htaccess ./.htaccess
			fi
			cd /var/tmp
			ln -s /usr/share/apache2/htdocs/.htaccess ./.htaccess
			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
			chmod +x restart.sh
			chmod +x make_ini
			chmod +x make_json
			chmod +x make_bmp
			chmod +x can
			chmod +x ctrl_test
			chmod +x cid_set
			chmod +x bdf
			chmod +x fp_recovery
			chmod +x stream_driver.ko
			chmod +x receive_driver.ko
			chmod +x record_driver.ko
			chmod +x streamer
			chmod +x receiver
			chmod +x recorder
			./fp_recovery > /var/tmp/fp_sts.log
			echo Load FPGA...
			sync
#			ifconfig eth0 down
#			ifconfig eth1 down
#			ifconfig br0 down
			echo 1 > /sys/class/leds/d5/device/leds/d5/brightness
			echo 0 > /sys/class/leds/d6/device/leds/d6/brightness
			./fpga_load > /dev/null
			echo 1 > /sys/class/leds/d3/device/leds/d3/brightness
			echo 0 > /sys/class/leds/d4/device/leds/d4/brightness
#			ifconfig eth0 up&
#			ifconfig eth1 up&
#			ifconfig br0 up&
			echo Start user processes...
			./ctrl_test > /dev/null
			./remote > /dev/null&
			./b110_ctrl > /dev/null&
			./restart.sh > /dev/null&
			./make_ini > /dev/null&
			./make_json > /dev/null&
			./make_bmp > /dev/null&
			./can > /dev/null&
			./bdf > /dev/null&
			echo 1 > /sys/class/leds/d1/device/leds/d1/brightness
			echo 0 > /sys/class/leds/d2/device/leds/d2/brightness
			echo 1 > /var/tmp/started
		fi
	fi
fi
