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

echo 1 > /sys/class/leds/d10/device/leds/d10/brightness
echo 1 > /sys/class/leds/d9/device/leds/d9/brightness

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 " Install 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
	chmod +x *.cgi
	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
	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
    echo "NO INSTALL FILE FOUND!"
fi
