#!/bin/sh

DEV_FILE="/dev/ubi0"
LOG_FL_PATH='cms5_Log/BB_LOG.txt'

echo "UPGRADE BB APP"

# check if BB already flashed
if [[  -e $DEV_FILE ]]; then

	APP_FILES="$1/UPGRADE_FILES/BB/APPS/APP_FILES/"

	#/usr/bin/kill_servers 

	echo "UPGRADE BB APPS" >> $1/$LOG_FL_PATH

	cd $1/UPGRADE_FILES/BB/APPS/APP_FILES/
	if [ "$?" = "0" ]; then
	rm reflashmanager
	cp * /usr/bin/
	#Added to Update the APP VERSION
	sh /usr/bin/SYS_VER_INFO 5 $1	
	sync
	chmod 777 /usr/bin/*
	sync
	fi
	

	echo "UPGRADE BB FILES" >> $1/$LOG_FL_PATH

	cd $1/UPGRADE_FILES/BB/APPS/RULES/
	
	if [[ -f usb.rules ]]; then
		cp usb.rules /etc/udev/rules.d/usb.rules
		chmod 777 /etc/udev/rules.d/usb.rules
	fi

	cd $1/UPGRADE_FILES/BB/APPS/INIT_FILES/
	if [[ -f cms5_init ]]; then
		cp cms5_init /etc/rc.d/init.d/
		chmod 777 /etc/rc.d/init.d/*
	fi

	cd $1/UPGRADE_FILES/BB/APPS/RC_FILES/
	if [[ -f rc.conf ]]; then
		cp rc.conf /etc/rc.d/
		chmod 777 /etc/rc.d/*
	fi

	cd $1/UPGRADE_FILES/BB/APPS/LIB_FILES/

	if [ "$?" = "0" ]; then
	cp * /usr/lib/
        fi
	
	cd $1/UPGRADE_FILES/BB/APPS/GSTLIB_FILES/
	if [ "$?" = "0" ]; then
	cp * /usr/lib/gstreamer-0.10/
	fi
	sync

	echo "UPRADING ROOTFS COMPLETED" >> $1/$LOG_FL_PATH 
	echo "UPRADING ROOTFS COMPLETED"
	/usr/bin/vol_cntrl 10
	/usr/bin/gplay /usr/bin/BBSOFTWARE.mp3
	else
	echo "!!!!!!!!!!! BOARD NOT UPGRADED ............ !!!! NEED TO BE FLASHED USING MFG_TOOL" >> $1/$LOG_FL_PATH
	echo "!!!!!!!!!!! BOARD NOT UPGRADED ............ !!!! NEED TO BE FLASHED USING MFG_TOOL"
fi
