#!/bin/sh

PARAM1=$1
TARGET=$2
PARAM_NUM=$#
METHOD=net
PACKAGE_NAME=uImage
FLASH_MTD_KERNEL=p3

kernel_Install()
{
	#Get cpu type
	CPU_TYPE=$(uname -m)
	
	mkdir -p /tmp/img
	
	if [ "$CPU_TYPE" != "x86_64" ]
    then
		echo "========================================================="
		echo "${CPU_TYPE} unknown type."
		echo "========================================================="
		return 0
	fi
	
	mount ${MTD_BLK} /tmp/img
	if [ $? == 0 ];then
		echo "========================================================="
		echo "${MTD_BLK} mount success."
		echo "========================================================="
	else
		echo "========================================================="
		echo "${MTD_BLK} mount fail."
		echo "========================================================="
		umount /tmp/img
		return 0
	fi
	
	echo "install kernel."
    cp /tmp/tftp/${PACKAGE_NAME} /tmp/img
    umount ${MTD_BLK}
		
	rm -rf /tmp/img
}

help()
{
	echo -e "\n###"
	echo "Usage:"
	echo "burn_kernel net ipaddress"
	echo "burn_kernel 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)"
		kernel_Install
		echo "========================================================="
		echo "update kernel ok."
	else
		echo "========================================================="
		echo "update kernel 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
			kernel_Install
			echo "========================================================="
			echo "update kernel ok."
		else
			echo "========================================================="
			echo "update kernel fail!!"	
		fi
	else
		echo "========================================================="
		echo "update kernel fail!!"	
	fi
	
	rm -rf /tmp/tftp
}

Main()
{
	stime=$(date +%s)
	
#	HDNAME=$(head -n 1 /proc/diskstats | awk '{print $3}')
	HDNAME=mmcblk0
	
	MTD_BLK=/dev/${HDNAME}${FLASH_MTD_KERNEL}
	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
