#!/bin/bash

#######################################################################################
#./build/map_calcsize.sh -c [0, 1]. control whether to generate a param table.
#./build/map_calcsize.sh -p percent, percent=non-negative integer.
#######################################################################################

PWDDIR=`pwd`
MAPPATH="${PWDDIR}/project/image/output/map"
DESTPATH="${PWDDIR}/build"
SEG_LIST=".text .rodata .data"
MAP_SUBDIR="ko so"
PARAMSWITCH=1

MAPPARSER=map_parsesize.awk
PARAMTABLE=map_param.thd
PERCENT=0
isGenerateParam=0

#Determine the MAX_THD by PERCENT.
while getopts "c:p:" opt; do
	case $opt in
		c)
			if [ ${OPTARG} -eq 1 ]; then isGenerateParam=1; else isGenerateParam=0; fi
			#echo "isGenerateParam=${isGenerateParam}"
			;;
		p)
			if [ ${OPTARG} -lt 0 ]; then PERCENT=0; else PERCENT=${OPTARG}; fi
			#echo "PERCENT=${PERCENT}"
			;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			;;
	esac
done

generate_param_table()
{
	cd ${PWDDIR};
	if [ ${PARAMSWITCH} == 1 ];
	then
		if [ -f ${DESTPATH}/${PARAMTABLE} ]
		then
			rm -rf ${DESTPATH}/${PARAMTABLE}
		fi
		PARAMSWITCH=0
	fi

	awk -f ${DESTPATH}/${MAPPARSER} m=0 o=0 d=0 f=$2 ${MAPPATH}/$1/$2 >> ${DESTPATH}/${PARAMTABLE}
}

#######################################################################################
#               calc .text, .rodata and .data size between testmap and THD            #
#######################################################################################
calc_size_for_each_map()
{
	#-------------------------------------------------------------------------------------------
	for mapsubdir in ${MAP_SUBDIR}
	do
		if test -d ${MAPPATH}/${mapsubdir}
		then
			for file in `ls ${MAPPATH}/${mapsubdir}/`;
			do
				if test -f ${MAPPATH}/${mapsubdir}/${file}
				then
					#-------------------------------------------------------------------------------------------
					#generate .text .rodata and .data totalsize to ${PARAMTABLE}
					if [[ ${isGenerateParam} -eq 1 ]]; then generate_param_table ${mapsubdir} ${file}; fi

					#-------------------------------------------------------------------------------------------
					for segtype in ${SEG_LIST}
					do
						#-------------------------------------------------------------------------------------------
						#echo "segtype=${segtype}"
						if [ ${segtype} == ".text" ]; then column=3; elif [ ${segtype} == ".rodata" ]; then column=4; elif [ ${segtype} == ".data" ]; then column=5; fi

						#-------------------------------------------------------------------------------------------
						#calc .text .rodata .data size in the mapfile
						size=`awk -f ${DESTPATH}/${MAPPARSER} m=0 o=0 d=0 f=${file} t=${segtype} ${MAPPATH}/${mapsubdir}/${file}`
						size_dec=`printf %d ${size}`
						#echo "size=${size} size_dec=${size_dec}"

						#-------------------------------------------------------------------------------------------
						#calc .text .rodata .data max_thdsize in the ${PARAMTABLE}
						sizeThd=`awk '{ if ($1 ~ "'$file'") {print ""$"'$column'"}}' ${DESTPATH}/${PARAMTABLE}`

						size_decThd=`printf %d ${sizeThd}`
						size_modThd=`expr ${size_decThd} + ${size_decThd} \* ${PERCENT} / 100`
						size_hexThd=`printf 0x%x ${size_modThd}`
						#echo "PERCENT=${PERCENT} size=${size} size_dec=${size_dec} | sizeThd=${sizeThd} size_decThd=${size_decThd} size_modThd=${size_modThd} size_hexThd=${size_hexThd}"

						#-------------------------------------------------------------------------------------------
						#campare seg size int the mapfile and seg max_thdsize in the ${PARAMTABLE}
						if [[ ${size_dec} -gt ${size_modThd} ]]; then echo -e "\033[1;34;47m Warning: check ${file} fail! ${segtype}size:[${size}] > THD:[${size_hexThd}] Percent:[${PERCENT}] \033[0m\n"; fi

						#-------------------------------------------------------------------------------------------
					done
				fi
			done
		else
			echo -e "\033[1;32m ${MAPPATH}/${mapsubdir} is not exist!\033[0m"
		fi
	done
}

calc_size_for_each_map






