#!/bin/sh

# this script is used to switch firmware from Yun to IoT

# Check if we have the correct middleware
Firmware_Time='Build Mon Jul  4 18:46:24 CST 2016'

if [ -z "`cat /etc/banner | grep 'Jul  4 18:46:24'`" ]; then
	echo "please upgrade to the IoT to Yun middleware first"
	echo "Download Link: http://www.dragino.com/downloads/index.php?dir=motherboards/ms14/Firmware/IoT/IoT_to_Yun_middleWare/&file=IoT-to-Yun_MiddleWare_Sysupgrade.bin"
	exit 0
fi

cd /var

if [ ! -f "common-2.0.7.u-boot" ] || [ ! -f "common-2.0.7.firmware" ] || [ ! -f "common-2.0.7.config" ];then
	echo "#################"
	echo "please make sure you have these files in /var directory"
	echo "	common-2.0.7.u-boot"
	echo "	common-2.0.7.firmware"
	echo "	common-2.0.7.config"
	echo "they can be downloaded from http://www.dragino.com/downloads/index.php?dir=motherboards/ms14/Firmware/IoT/IoT_to_Yun_middleWare/"
	echo "#################"
fi 

echo "Start Flashing, Don't power off the device until it finish !!!"

mtd write common-2.0.7.u-boot u-boot
mtd write common-2.0.7.firmware firmware
mtd write common-2.0.7.config config

sleep 5
echo "Flashing finish. Device will be reboot to Yun firmware."

reboot