#!/bin/sh

PARAM1=$1
TARGET=$2
PARAM_NUM=$#
METHOD=net
PACKAGE_NAME=u-boot.tar.bz2
FLASH_MTD_UBOOT=p1

grub_Install()
{
	#Get cpu type
	CPU_TYPE=$(uname -m)
  
	mount ${MTD_BLK} /boot
	
	if [ $? == 0 ];then
	echo "========================================================="
  	echo "${MTD_BLK} mount success."
  	echo "========================================================="
	else
	echo "========================================================="
  	echo "${MTD_BLK} mount fail."
  	echo "========================================================="
	umount /boot
  	return 0
	fi
	#Save the previous grub image
 	mkdir -p /tmp/img
 	mkdir -p /tmp/grub
  	
	cp -rf /tmp/tftp/${PACKAGE_NAME} /tmp/img
	
	if [ -e /boot/EFI ];then
 	cp -rf /boot/* /tmp/grub
	fi
	
 	#Empty everything under the grub partition
 	rm -rf /boot/*

 	#Unzip the grub installation package to the root directory
	echo "unpack u-boot."
 	bunzip2 /tmp/img/${PACKAGE_NAME}
 	tar xf /tmp/img/u-boot.tar -C /tmp/img
	echo "========================================================="
 	bunzip2 /tmp/img/grub_install.tar.bz2
	tar xf /tmp/img/grub_install.tar -C /
	
	echo "install u-boot."
	echo "========================================================="
	if [ "$CPU_TYPE" == "x86_64" ]
  	then
  		/grub_install/sbin/grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=grub --boot-directory=/boot/EFI --recheck
  	fi

  	cp -rf /grub_install/config/*  /boot/EFI/grub/
  	rm -rf /grub_install

  	#Verify that the installation is successful
  	if [ -e /boot/EFI/grub/grub.cfg ];then
  		umount /boot
  		rm -rf /tmp/img
  		rm -rf /tmp/grub
  		return 0
  	fi

	#Verification fails, restore the old image before
  	echo "========================================================="
  	echo "update u-boot failed, recover the old u-boot image"
  	echo "========================================================="
  	cp -rf /tmp/grub/* /boot/
  	umount /boot
  	rm -rf /tmp/img
  	rm -rf /tmp/grub
  	return 1
}

help()
{
	echo -e "\n###"
	echo "Usage:"
	echo "burn_uboot net ipaddress"
	echo "burn_uboot local file_path"
}

#$1 parameter analysis function
parse_ARG1()
{	
    case $PARAM1 in
    "--help" | "-h")
    	help
    	exit 0
    	;;
    *)
    	METHOD=$PARAM1
    	;;
    esac
   
	if [ $PARAM_NUM != 2 ];then
		help
		exit 0
	fi
}

net_Process()
{
	if [ "${TARGET}" == "" ]
	then
		echo "TARGET=${TARGET}"
		help
		exit 0
	fi
	
	rm -rf /tmp/tftp
	mkdir -p /tmp/tftp
	cd /tmp/tftp

	echo "========================================================="
	echo "tftp download ${TARGET} -gr braswell/${PACKAGE_NAME}"
	echo "========================================================="
	
	stftp_time=$(date +%s)
	tftp ${TARGET} -gr braswell/${PACKAGE_NAME} -b 512
	etftp_time=$(date +%s)
	
	tftp_elapsed=$(( etftp_time - stftp_time ))
	
	if [ -e /tmp/tftp/${PACKAGE_NAME} ];then
		FILESIZE=$(ls -alh /tmp/tftp/${PACKAGE_NAME} | awk '{print $5}')
		echo "tftp download ok.(${FILESIZE} ${tftp_elapsed}sec)"
		grub_Install
		echo "========================================================="
		echo "update u-boot ok."
	else
		echo "========================================================="
		echo "update u-boot fail!!"
	fi
	echo "========================================================="
	rm -rf /tmp/tftp
}

local_Process()
{
	rm -rf /tmp/tftp
	mkdir -p /tmp/tftp
	cd /tmp/tftp
	
	if [ -e ${TARGET}/${PACKAGE_NAME} ];then
		cp -a ${TARGET}/${PACKAGE_NAME} /tmp/tftp
		if [ -e /tmp/tftp/${PACKAGE_NAME} ];then
			grub_Install
			echo "========================================================="
			echo "update u-boot ok."
		else
			echo "========================================================="
			echo "update u-boot fail!!"	
		fi
	else
		echo "========================================================="
		echo "update u-boot fail!!"	
	fi
}

Main()
{
	stime=$(date +%s)
	
	#HDNAME=$(head -n 1 /proc/diskstats | awk '{print $3}')
	HDNAME=mmcblk0
	
	MTD_BLK=/dev/${HDNAME}${FLASH_MTD_UBOOT}
	parse_ARG1
	
	case $METHOD in
     "net")
 	    net_Process
 	    ;;
     "local")
 	    local_Process
 	    ;;
      *)
        echo -e "input paramment is invalid ..."
        help
        exit 0
		;;
    esac
	  
	etime=$(date +%s)
	elapsed=$(( etime - stime ))
	echo "elapsed time:${elapsed}sec"
	
	sync
}

#Execute the main function
Main
