
erase_cmd="mtd_debug erase /dev/mtd6 0x0 0x01000000"
TARGET_FILE="/data/upgrade.sh"

upgrade_cmd="nandwrite -a /dev/mtd6 /data/OTA/app_squashfs.img"
check_md5="md5sum -c $2"
cd /data/OTA
while true
do
	if [[ -f $1 ]]  &&  [[ -f $2 ]]; 
	then
		check_result=$(eval $check_md5)
		echo "$check_result"
		result=$(echo $check_result | grep "OK")

		if [[ "$result" != "" ]]
		then		
			tar -zxf $1 -C /data/OTA/
			rm $TARGET_FILE -f
			echo "echo 'now eval upgrade cmd by upgrade.sh'" >> $TARGET_FILE 
			echo "mtd_debug erase /dev/mtd6 0x0 0x01000000" >> $TARGET_FILE
			echo "nandwrite -a /dev/mtd6 /data/OTA/app_squashfs.img" >> $TARGET_FILE
			echo "rm -rf /data/OTA" >> $TARGET_FILE
			echo "reboot -f" >> $TARGET_FILE	
			echo "now eval upgrade cmd"
			chmod 777 $TARGET_FILE
			sync
			eval $erase_cmd
			eval $upgrade_cmd
			rm -rf /data/OTA
		fi

	else
		echo "check load fail"
	fi
	echo "where is error ?"
	exit 0

done


