#!/bin/bash
#
# builder_mediatek_mt: build mediatek mt6735 platform.
# 
# Ruinan Duan 03/20/2015
#
# Modified by Ruinan Duan 04/10/2015 - Add support for mt6735m
#

. ${P82M_ABS_PATH}/script/replace
. ${P82M_ABS_PATH}/script/builder_mt6735_prepare

P82M_SW_TYPE="eng"
P82M_PROCESSOR=1
unset P82M_CCACHE
P82M_UPDATE_UPI="true"
unset MT6735_MODEM_MK

# Get the number of host machine's processor
mediatek_mt_get_processor()
{
	P82M_PROCESSOR=$(cat /proc/cpuinfo | grep processor | wc -l)
}

# Modify some marvell code to add zte code to build system
mediatek_mt_do_modify()
{
	prev_dir=`pwd`

	if [ "$P82M_MACHINE" = "mt6735" ] || [ "$P82M_MACHINE" = "mt6735m" ]; then
		cd "$P82M_ANDROID_DIR"
		# modify main.mk
		if [ -e build/core/main.mk.bak ]; then
			mv build/core/main.mk.bak build/core/main.mk
		fi
		cp build/core/main.mk build/core/main.mk.bak
		sed -i "s#prune=.repo#prune=.repo --prune=.svn --prune=zte_sdk#g" build/core/main.mk
		sed -i -r "s#prune=\.git \\$\(subdirs\) Android\.mk\)#prune=\.git \\$\(subdirs\) Android\.mk\)\nifdef OEM_SPESIFIC_DIRS\n\tsubdir_makefiles += \\$\(shell build\/tools\/findleaves\.py --prune=\.svn \\$\(OEM_SPESIFIC_DIRS\) Android\.mk)\nendif#g" build/core/main.mk

		# modify project.mk
		if [ -e device/ztexambb/${P82M_REALPROJECT}/${P82M_REALPROJECT}.mk.bak ]; then
			mv device/ztexambb/${P82M_REALPROJECT}/${P82M_REALPROJECT}.mk.bak \
				device/ztexambb/${P82M_REALPROJECT}/${P82M_REALPROJECT}.mk
		fi
		cp device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk \
			device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk.bak
		echo "include ../../../../../cust/${P82M_PROJECT_DIR}/ap_config/applications.inc" >> device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk
		echo "include ../../../../../cust/${P82M_PROJECT_DIR}/ap_config/adapter.inc" >> device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk
		echo "include ../../../../../cust/${P82M_PROJECT_DIR}/ap_config/recovery-applications.inc" >> device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk
		echo "include ../../../../../cust/${P82M_PROJECT_DIR}/ap_config/recovery-adapter.inc" >> device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk

		#modify build/core/Makefile
		if [ -e build/core/Makefile.bak ]; then
			mv build/core/Makefile.bak build/core/Makefile
		fi
		cp build/core/Makefile build/core/Makefile.bak
		sed -i -r "s#\\$\(hide\) cp -rf \\$\(recovery_resources_common\)\/\* \\$\(TARGET_RECOVERY_ROOT_OUT\)\/res#\\$\(hide\) cp -rf \\$\(recovery_resources_common)\/\* \\$\(TARGET_RECOVERY_ROOT_OUT\)\/res\n\t\\$\(hide\) find \\$\(TARGET_RECOVERY_ROOT_OUT\) -type d -name \.svn | xargs rm -rf#g" build/core/Makefile

		# modify build/envsetup.sh
		if [ -e build/envsetup.sh.bak ]; then
			mv build/envsetup.sh.bak build/envsetup.sh
		fi
		cp build/envsetup.sh build/envsetup.sh.bak
		sed -i "s/START=\`PWD= \/bin\/pwd\`/START=\`pwd\`/g" build/envsetup.sh
	fi

	cd $prev_dir
	return 0
}

mediatek_mt_undo_modify()
{
	prev_dir=`pwd`

	if [ "$P82M_MACHINE" = "mt6735" ] || [ "$P82M_MACHINE" = "mt6735m" ]; then
		cd "$P82M_ANDROID_DIR"
		# restore main.mk
		if [ -e build/core/main.mk.bak ]; then
			mv build/core/main.mk.bak build/core/main.mk
		fi

		# restore project.mk
		if [ -e device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk.bak ]; then
			mv device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk.bak \
				device/ztexambb/${P82M_REALPROJECT}/full_${P82M_REALPROJECT}.mk
		fi

		#modify build/core/Makefile
		if [ -e build/core/Makefile.bak ]; then
			mv build/core/Makefile.bak build/core/Makefile
		fi

		# restore envsetup.sh
		if [ -e build/envsetup.sh.bak ]; then
			mv build/envsetup.sh.bak build/envsetup.sh
		fi
	fi

	cd $prev_dir
	return 0
}

# Replace Android and ZTE sdk code.
replace_code_for_mediatek_mt()
{
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/Android_Dir" ]; then
		print_wait "Begin to replace project related files in android tree."
		echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
		REP_DST_BASE_PATH="${P82M_ABS_PATH}"
		REP_SRC_BASE_PATH="${P82M_ABS_PATH}"
		REQ_PATH="${P82M_ABS_PATH}/cust/${P82M_PROJECT_DIR}/rep_android.desc"
		REP_FILTER=`get_dir_abs_path`
		get_replace_dst_path
		get_replace_src_path
		progress &
		progress_pid=$!
		trap "kill $progress_pid;echo;exit" 1 2 3 15
		replace_files_ex
		kill $progress_pid
		echo
		print_okey "Replace project related files in android tree complete."
	fi

	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/zte_sdk" ]; then
		print_wait "Begin to replace project related files in zte sdk."
		echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
		REP_DST_BASE_PATH="${P82M_ABS_PATH}"
		REP_SRC_BASE_PATH="${P82M_ABS_PATH}"
		REQ_PATH="${P82M_ABS_PATH}/cust/${P82M_PROJECT_DIR}/rep_zte_sdk.desc"
		REP_FILTER=`get_dir_abs_path`
		get_replace_dst_path
		get_replace_src_path
		progress &
		progress_pid=$!
		trap "kill $progress_pid;echo;exit" 1 2 3 15
		replace_files_ex
		kill $progress_pid
		echo
		print_okey "Replace project related files in zte sdk complete."
	fi
	return 0
}

# copy ap image files.
copy_mediatek_mt_ap_images()
{
	print_wait "Copying android images"
	if [ "${P82M_MACHINE}" = "mt6735" ] || [ "${P82M_MACHINE}" = "mt6735m" ]; then
		rm -rf ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		mkdir -p ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/appsboot.mbn ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/appsboot.raw ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/boot.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/cache.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/custom_build_verno ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/kernel ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/lk.bin ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/logo.bin ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/MT6735_Android_scatter.txt ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/ota_scatter.txt ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/preloader_${P82M_REALPROJECT}.bin ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/ramdisk.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/ramdisk-recovery.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/recovery.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/secro.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/system.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/trustzone.bin ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
		cp ${P82M_ANDROID_DIR}/out/target/product/${P82M_REALPROJECT}/userdata.img ${P82M_ABS_PATH}/images/$P82M_PROJECT/android
	fi
}

mt6735_clean()
{
	local prev_dir=`pwd`

	print_wait "Removing out dir"
	cd "${P82M_ANDROID_DIR}"
	make clobber -j "$P82M_PROCESSOR"
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_android()
{
	local prev_dir=`pwd`

	print_wait "Build Android tree"
	cd "${P82M_ANDROID_DIR}"
	make -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_bootimg()
{
	local prev_dir=`pwd`

	print_wait "Build bootimage"
	cd "${P82M_ANDROID_DIR}"
	make kernel bootimage -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_sysimg()
{
	local prev_dir=`pwd`

	print_wait "Build system image"
	cd "${P82M_ANDROID_DIR}"
	make systemimage -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_usrimg()
{
	local prev_dir=`pwd`

	print_wait "Build userdata image"
	cd "${P82M_ANDROID_DIR}"
	make userdataimage -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_module()
{
	local prev_dir=`pwd`

	print_wait "Build $P82M_MODULE"
	cd "${P82M_ANDROID_DIR}"
	make $P82M_MODULE -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_project()
{
	local prev_dir=`pwd`

	print_wait "Build project $P82M_REALPROJECT"
	cd "${P82M_ANDROID_DIR}"
	mmm $P82M_REALPROJECT 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_update_api()
{
	local prev_dir=`pwd`

	print_wait "Update API"
	cd "${P82M_ANDROID_DIR}"
	find "${P82M_ANDROID_DIR}/frameworks/base/docs" -type d -name ".svn" \
		| xargs rm -rf
	find "${P82M_ANDROID_DIR}/build/tools/droiddoc" -type d -name ".svn" \
		| xargs rm -rf
	make update-api -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_preloader()
{
	local rt=0
	local prev_dir=`pwd`

	print_wait "Build preloader"
	cd "${P82M_ANDROID_DIR}"
	make pl -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_build_lk()
{
	local prev_dir=`pwd`

	print_wait "Build lk"
	cd "${P82M_ANDROID_DIR}"
	make lk -j "$P82M_PROCESSOR" 2>&1 | tee -a $P82M_LOG_FILE
	if [ -n "$(tail -n 2 $P82M_LOG_FILE | grep 'make failed')" ]; then
		cd $prev_dir
		return 1
	fi
	cd $prev_dir
}

mt6735_setup_ccache()
{
	export CCACHE_DIR=${P82M_ANDROID_DIR}/.ccache
	export USE_CCACHE=1
}

mt6735_delete_ccache()
{
	${P82M_ANDROID_DIR}/prebuilts/misc/linux-x86/ccache/ccache -C
	rm -rf $CCACHE_DIR
}

mt6735_create_ccache()
{
	print_hint "Setting CCACHE with 10 GB"
	mt6735_setup_ccache
	mt6735_delete_ccache
	${P82M_ANDROID_DIR}/prebuilts/misc/linux-x86/ccache/ccache -M 10G
}

mt6735_set_java_env()
{
	if [ "${P82M_MACHINE}" = "mt6735" ] || [ "${P82M_MACHINE}" = "mt6735" ]; then
		export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
		export JRE_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64/jre
		export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
		export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
	fi
}

# run Android make build environment.
run_mediatek_mt_ap_native_build()
{
	local prev_dir=""
	print_head "Build Marvell $P82M_MACHINE AP"
	
	prev_dir=`pwd`

	cd "${P82M_ANDROID_DIR}"
	source build/envsetup.sh
	lunch full_${P82M_REALPROJECT}-${P82M_SW_TYPE}
	
	if [ "`echo ${P82M_APCLEARALL}`" = "1" ]; then
		if [ "$P82M_MACHINE" = "mt6735" ] || [ "$P82M_MACHINE" = "mt6735m" ]; then
			print_wait "Wait to clean last build"
			mt6735_clean
			if [ "$?" != "0" ]; then
				print_fail "Build Android failed."
				cd $prev_dir
				return 1
			fi
		fi
	fi

	if [ "${#P82M_APPLIST[@]}" = "0" ] && [ "${#P82M_APPNOPACKLIST[@]}" = "0" ]; then
		mt6735_create_ccache
	fi

	unset build_list
	if [ "${#P82M_APPLIST[@]}" != "0" ] || [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
		for ((i=0; i < ${#P82M_APPLIST[@]}; i++)); do
			if [ "${P82M_APPLIST[i]}" = "bootimg" ]; then
				mt6735_build_bootimg
			elif [ "${P82M_APPLIST[i]}" = "usrimg" ]; then
				mt6735_build_usrimg
			elif [ "${P82M_APPLIST[i]}" = "sysimg" ]; then
				mt6735_build_sysimg
			elif [ "${P82M_APPLIST[i]}" = "preloader" ]; then
				mt6735_build_preloader
			elif [ "${P82M_APPLIST[i]}" = "lk" ]; then
				mt6735_build_lk
			else
				build_list=${build_list}" ${P82M_APPLIST[i]}"
			fi
			if [ "$?" != "0" ]; then
				cd $prev_dir
				print_fail "Build failed."
				return 1
			fi
		done
		for ((i=0; i < ${#P82M_APPNOPACKLIST[@]}; i++)); do
			if [ "${P82M_APPNOPACKLIST[i]}" = "bootimg" ]; then
				mt6735_build_bootimg
			elif [ "${P82M_APPNOPACKLIST[i]}" = "usrimg" ]; then
				mt6735_build_usrimg
			elif [ "${P82M_APPNOPACKLIST[i]}" = "sysimg" ]; then
				mt6735_build_sysimg
			elif [ "${P82M_APPNOPACKLIST[i]}" = "preloader" ]; then
				mt6735_build_preloader
			elif [ "${P82M_APPNOPACKLIST[i]}" = "lk" ]; then
				mt6735_build_lk
			else
				build_list=${build_list}" ${P82M_APPNOPACKLIST[i]}"
			fi
			if [ "$?" != "0" ]; then
				cd $prev_dir
				print_fail "Build failed."
				return 1
			fi
		done
	fi
	if [ -n "$build_list" ]; then
		P82M_MODULE="${build_list}"
		mt6735_build_module
		if [ "$?" != "0" ]; then
			cd $prev_dir
			print_fail "Build failed."
			return 1
		fi
		if [ "${#P82M_APPNOPACKLIST[@]}" = "0" ]; then
			cd "${P82M_ANDROID_DIR}"
			make snod
			if [ "$?" != "0" ]; then
				cd $prev_dir
				print_fail "make snod failed."
				return 1
			fi
			cd $prev_dir
		fi
	else
#		mt6735_update_api
#		if [ "$?" != "0" ]; then
#			print_fail "Update API failed."
#			cd $prev_dir
#			return 1
#		fi
		mt6735_build_android
		if [ "$?" != "0" ]; then
			print_fail "Build Android failed."
			cd $prev_dir
			return 1
		fi
	fi
	cd $prev_dir
}

# generate file system package for FOTA
make_mediatek_mt_ap_fs_tarballs()
{
	return 0
}

pack_mediatek_mt_ap_fs()
{
	return 0
}

copy_mt_modem_images()
{
	print_wait "Copying modem bin"
	if [ "${P82M_MACHINE}" = "mt6735" ]; then
		rm -rf ${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod
		mkdir -p ${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/dhl/database/catcher_filter.bin \
			${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/catcher_filter_1_lwg_n.bin
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/dhl/database/BPLGUInfo* \
			${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/BPLGUInfoCustomApp_ZTE_1_lwg_n
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/bin/DbgInfo* \
			${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/DbgInfo_ZTE_1_lwg_n
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/bin/*_PCB01_*.elf \
			${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/ZTE_PCB01_1_lwg_n.elf
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/bin/*_PCB01_*.bin \
			${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/modem_1_lwg_n.img
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/bin/*DSP*.bin \
			${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/dsp_1_lwg_n.bin
		cp ${P82M_MODEM_DIR}/build/MT6735_SP/LWG_DSDS_C2KIRAT_CUSTOM/bin/~$MT6735_MODEM_MK ${P82M_ABS_PATH}/images/$P82M_PROJECT/modem/mt6735_sp_lwg_dsds_c2kirat_prod/modem_1_lwg_n.mak
	fi
	if [ "${P82M_MACHINE}" = "mt6735m" ]; then
		echo ""
	fi
}

# Build mediatek modem
build_mediatek_mt_modem()
{
	local cur_dir=""

	print_head "Build Mediatek $P82M_REALPROJECT Modem"
	cur_dir=`pwd`
	if [ "${P82M_MACHINE}" = "mt6735" ]; then
		cd "${P82M_MODEM_DIR}"
		rm -rf build
		./make.sh ${MT6735_MODEM_MK} new 2>&1 | tee -a $P82M_LOG_FILE
		if [ "$?" != "0" ];then
			print_fail "Build modem abort."
			cd "$cur_dir"
			return 1
		else
			print_okey "Build modem complete."
		fi
	fi
	cd "$cur_dir"
	copy_mt_modem_images
	return 0
}

# Build mediatek ap and modem code
build_mediatek_mt_ap_modem()
{
	local have_err="0"

	unset P82M_REALPROJECT
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	print_hint "P82M_REALPROJECT=${P82M_REALPROJECT}"
	if [ "$P82M_MACHINE" = "mt6735" ]; then
		P82M_ANDROID_DIR="${P82M_ABS_PATH}/platform_sdk/mediatek/mt6735/code/base"
		P82M_MODEM_DIR="${P82M_ANDROID_DIR}/modem"
	fi
	if [ "$P82M_MACHINE" = "mt6735m" ]; then
		P82M_ANDROID_DIR="${P82M_ABS_PATH}/platform_sdk/mediatek/mt6735/code/base"
		P82M_MODEM_DIR="${P82M_ANDROID_DIR}/modem3"
	fi
	P82M_LOG_FILE="${P82M_ABS_PATH}/images/$P82M_PROJECT/fulllog.txt"
	mkdir -p $(dirname $P82M_LOG_FILE)

	if [ -e "${P82M_LOG_FILE}" ]; then
		mv "${P82M_LOG_FILE}" "${P82M_LOG_FILE}.bak"
	fi

	mediatek_mt_get_processor
	if [ "$P82M_MACHINE" = "mt6735" ]; then
		mt6735_set_java_env
		MT6735_MODEM_MK="MT6735_SP(LWG_DSDS_C2KIRAT_CUSTOM).mak"
	fi
	if [ "$P82M_MACHINE" = "mt6735" ]; then
		MT6735_MODEM_MK="MT6735_SP(LWG_DSDS_C2KIRAT_CUSTOM).mak"
	fi

	unset P82M_CONF_OPT_NAME_LIST
	unset P82M_CONF_OPT_NAME
	unset P82M_CONF_OPT_VALUE
	P82M_CONF_OPT_NAME=(`sed -n 's/.*name>\(.*\)<\/name>/\1/p' ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/ap_config/options.xml`)
	P82M_CONF_OPT_VALUE=(`sed -n 's/.*value>\(.*\)<\/value>/\1/p' ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/ap_config/options.xml`)
	for ((i=0; i < ${#P82M_CONF_OPT_NAME[@]}; i++)); do
		print_hint "export ${P82M_CONF_OPT_NAME[i]}=${P82M_CONF_OPT_VALUE[i]}"
		export "${P82M_CONF_OPT_NAME[i]}=${P82M_CONF_OPT_VALUE[i]}"
		P82M_CONF_OPT_NAME_LIST="${P82M_CONF_OPT_NAME[i]} ${P82M_CONF_OPT_NAME_LIST}"
	done
	# add P82M_MACHINE
	P82M_CONF_OPT_NAME_LIST="P82M_MACHINE ${P82M_CONF_OPT_NAME_LIST}"
	print_hint "export P82M_MACHINE=${P82M_MACHINE}"
	print_hint "export P82M_PROJECT=${P82M_PROJECT}"
	export "P82M_MACHINE=${P82M_MACHINE}"
	export "P82M_PROJECT=${P82M_PROJECT}"

	prev_dir=`pwd`
	if [ "$P82M_MACHINE" = "mt6735" ] || [ "$P82M_MACHINE" = "mt6735m" ]; then
		cd "${MEDIATEK_MT6735_SDK_BASE_DIR}/base/vendor"
		rm -rf ./zte_sdk
		ln -sf ../../../../../../zte_sdk ./zte_sdk
		cd $prev_dir
	fi

	# do something else before copy files
	sh ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/script/custom_actions_before_copy_ap_files
	if [ "$?" != "0" ]; then
		print_fail "run custom_actions_before_copy_ap_files failed."
		return 1
	fi

	if [ "${P82M_MACHINE}" = "mt6735m" ]; then
		mt6735m_prepare
	fi

	if [ "${#P82M_APPLIST[@]}" = "0" ] && [ "${#P82M_APPNOPACKLIST[@]}" = "0" ]; then
		# undo the modify
		mediatek_mt_undo_modify
		# replace code
		replace_code_for_mediatek_mt
		# modify some files
		mediatek_mt_do_modify
	fi

	if [ "$P82M_MACHINE" = "mt6735" ] || [ "$P82M_MACHINE" = "mt6735m" ]; then
		if [ "${#P82M_APPLIST[@]}" = "0" ] && [ "${#P82M_APPNOPACKLIST[@]}" = "0" ]; then
			mt6735_setup_ccache
		fi
	fi

	#modem
	if [ "`echo "${P82M_METABUILD[*]}" | grep "modem"`" != "" ]; then
		if ! build_mediatek_mt_modem; then
			print_fail "Build modem failed."
			return 1
		fi
		print_okey "Build mediatek mt modem complete."
	fi

	#check modem firmware
	print_hint "Checking for modem firmware images."
	if [ "$P82M_MACHINE" = "mt6735" ]; then
		echo stub
	fi
	if [ "$P82M_MACHINE" = "mt6735m" ]; then
		if [ ! -d $P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem/${P82M_REALPROJECT}_c2k_svlte ]; then
			print_wait "${P82M_REALPROJECT}_c2k_svlte does not exist, copy default fw images."
			cp -r $P82M_ANDROID_DIR/modem3/zte_fw/ztembb6735m_65t_l_c2k_svlte \
				$P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem
			mv $P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem/ztembb6735m_65t_l_c2k_svlte \
				$P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem/${P82M_REALPROJECT}_c2k_svlte
		fi
		if [ ! -d $P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem/${P82M_REALPROJECT}_lwg_c2kirat ]; then
			print_wait "${P82M_REALPROJECT}_lwg_c2kirat does not exist, copy default fw images."
			cp -r $P82M_ANDROID_DIR/modem/zte_fw/ztembb6735m_65t_l_lwg_c2kirat \
				$P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem
			mv $P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem/ztembb6735m_65t_l_lwg_c2kirat \
				$P82M_ANDROID_DIR/vendor/mediatek/proprietary/custom/${P82M_REALPROJECT}/modem/${P82M_REALPROJECT}_lwg_c2kirat
		fi
	fi

	if [ "`echo "${P82M_METABUILD[*]}" | grep "ap"`" != "" ]; then
		# do something else before build
		sh ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/script/custom_actions_before_build_ap
		if [ "$?" != "0" ]; then
			print_fail "run custom_actions_before_build_ap failed."
			return 1
		fi

		# build android
		if ! run_mediatek_mt_ap_native_build; then
			print_fail "run_mediatek_mt_ap_native_build failed."
			return 1
		else
			print_okey "Build android complete."
		fi

		# copy android images
		copy_mediatek_mt_ap_images

#		# generate FOTA_FS
#		if ! pack_marvell_pxa_ap_fs; then
#			print_fail "generate FOTA_FS imcomplete."
#			return 1
#		fi
		# do something else after build
		sh ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/script/custom_actions_after_build_ap
		if [ "$?" != "0" ]; then
			print_fail "run custom_actions_after_build_ap failed."
			return 1
		fi
	fi

	return 0
}



# build all metabuild
build_mediatek_system()
{
	local have_err="0"

	mkdir -p "${P82M_ABS_PATH}/images/$P82M_PROJECT"

	if ! build_mediatek_mt_ap_modem; then
		print_fail "Build ap failed."
		have_err="1"
	fi

	if [ "$have_err" = "1" ]; then
		print_fail "There are some errors while building progress."
		return 1
	fi

	return 0
}


