#!/bin/bash
#
# builder_qc_mdm9xxx.le: build qualcomm MDM9x15/MDM9x25/MDM9x30
#     platform_sdk + zte code
# 
# Ruinan Duan 10/29/2014
#
# Modified by Ruinan Duan 11/05/2014 - Copy custom files and replace-files.
# Modified by Ruinan Duan 11/05/2014 - Add custom script.
# Modified by Ruinan Duan 11/06/2014 - Add APSS file system package action.
# Modified by Ruinan Duan 11/06/2014 - Add Qualcomm native build command.
# Modified by Ruinan Duan 11/07/2014 - Add copy image action.
# Modified by Ruinan Duan 11/19/2014 - Add copy System.map action.
# Modified by Ruinan Duan 11/28/2014 - export P82M_PROJECT and configure options to bitbake.
# Modified by Ruinan Duan 12/17/2014 - Exclude .svn when replace the code "apps_proc & zte_sdk"
# Modified by Ruinan Duan 12/18/2014 - add P82M_MACHINE
#

# Modify some qc code to add zte code to build system
qc_mdm9xxx_apss_do_modify()
{
	# modify local.conf (change local build parameters)
	local cpu_number=8
	cpu_number=`cat /proc/cpuinfo | grep 'processor' | wc -l`
	cd oe-core/build/conf
	if [ -f local.conf.bak ]; then
		mv local.conf.bak local.conf
	fi
	cp local.conf local.conf.bak
	sed -e 's/^BB_NUMBER_THREADS ?= \"8\"/BB_NUMBER_THREADS ?= \"'"${cpu_number}"'\"/g' local.conf > local.conf1
	sed -e 's/^PARALLEL_MAKE ?= \"-j 8\"/PARALLEL_MAKE ?= \"-j '"${cpu_number}"'\"/g' local.conf1 > local.conf2
	sed -e 's/^PACKAGE_CLASSES ?= \"package_ipk package_deb\"/PACKAGE_CLASSES ?= \"package_ipk\"/g' local.conf2 > local.conf3
	mv local.conf3 local.conf
	rm local.conf1 local.conf2
	cd ../../../

	# modify site.conf
	cd oe-core/meta-msm/conf
	if [ -f site.conf.bak ]; then
		mv site.conf.bak site.conf
	fi
	cp site.conf site.conf.bak
	sed -e 's/YP_MIRROR =/# YP_MIRROR =/g' site.conf > site.conf.tmp
	sed -e 's/CAF_MIRROR =/# CAF_MIRROR =/g' site.conf.tmp > site.conf.tmp1
	sed -e 's/PREMIRRORS_append/# PREMIRRORS_append/g' site.conf.tmp1 > site.conf.tmp
	sed -e 's/https?$:/# https?$:/g' site.conf.tmp > site.conf.tmp1
	sed -e 's/svn$:/# svn$:/g' site.conf.tmp1 > site.conf.tmp
	sed -e 's/file:/# file:/g' site.conf.tmp > site.conf.tmp1
	sed -e 's/git?$:/# git?$:/g' site.conf.tmp1 > site.conf.tmp
	sed -e 's/ftps?$:/# ftps?$:/g' site.conf.tmp > site.conf.tmp1
	mv site.conf.tmp1 site.conf
	rm -f site.conf.tmp
	cd ../../../

	# modify mdm-image.bb (add zte bb-list in normal mode)
	cd oe-core/meta-qcom/recipes/images
	if [ -f mdm-image.bb.bak ]; then
		mv mdm-image.bb.bak mdm-image.bb
	fi
	cp mdm-image.bb mdm-image.bb.bak
	sed -e 's/require \${MACHINE}-qcom-image\.inc/require \${MACHINE}-qcom-image\.inc\n\n# Added for ZTE Development by Ruinan Duan\nrequire \.\.\/zte\/applications\.inc\nrequire \.\.\/zte\/adapter\.inc/g' mdm-image.bb > mdm-image.bb.tmp
	mv mdm-image.bb.tmp mdm-image.bb
	cd ../../../../

	# modify ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc (remove inopath and wap-fota)
	cd oe-core/meta-qcom/recipes/images
	if [ -f ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.bak ]; then
		mv ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.bak ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc
	fi
	cp ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.bak
	sed -e 's/IMAGE_INSTALL += "ipthdme"/# Deleted by Ruinan Duan\n# IMAGE_INSTALL += "ipthdme"/g' ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc > ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.tmp
	mv ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.tmp ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc
	sed -e 's/IMAGE_INSTALL += "wap-fota-daemon"/# Deleted by Ruinan Duan\n# IMAGE_INSTALL += "wap-fota-daemon"/g' ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc > ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.tmp
	mv ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc.tmp ${P82M_QC_9XXX_TARGET_NAME}-qcom-image.inc
	cd ../../../../

	# modify ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc (add zte bb-list in recovery mode, remove "recovery" in build list)
	cd oe-core/meta-msm/recipes/images
	if [ -f ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.bak ]; then
		mv ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.bak ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc
	fi
	cp ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.bak
	sed -e 's/require mdm-recovery-bootimg\.inc/require mdm-recovery-bootimg\.inc\n\n# Added for ZTE Development in recovery mode by Ruinan Duan\nrequire \.\.\/\.\.\/\.\.\/meta-qcom\/recipes\/zte\/recovery-applications\.inc\nrequire \.\.\/\.\.\/\.\.\/meta-qcom\/recipes\/zte\/recovery-adapter\.inc/g' ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc > ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.tmp
	mv ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.tmp ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc
	sed -e 's/IMAGE_INSTALL += "recovery"/# Deleted by Ruinan Duan for not building inopath recovery. \n# IMAGE_INSTALL += "recovery"/g' ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc > ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.tmp
	mv ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc.tmp ${P82M_QC_9XXX_TARGET_NAME}-recovery-image.inc
	cd ../../../../

	# modify $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc (generating recovery boot image)
	cd oe-core/meta-msm/recipes/images
	if [ -f $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc.bak ]; then
		mv $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc.bak $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc
	fi
	cp $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc.bak
	echo "# Added by Ruinan Duan for make boot image for recovery" >> $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc
	echo "require ../../../meta-qcom/recipes/zte/make-recovery-bootimg-additional.inc" >> $P82M_QC_9XXX_TARGET_PREFIX-recovery-bootimg.inc
	cd ../../../../

	# modify $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb  (do not generate innopath)
	cd oe-core/meta-msm/recipes/images
	if [ -f $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb.bak ]; then
		mv $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb.bak $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb
	fi
	cp $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb.bak
	sed -e 's/require '"${P82M_QC_9XXX_TARGET_PREFIX}"'-ota-target-image\.inc/# Deleted by Ruinan Duan for forbiding enerating Innopath updater\n# require '"${P82M_QC_9XXX_TARGET_PREFIX}"'-ota-target-image\.inc /g' $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb > $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb.tmp
	mv $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb.tmp $P82M_QC_9XXX_TARGET_PREFIX-recovery-image.bb
	cd ../../../../

	# modify $P82M_QC_9XXX_TARGET_NAME-image.inc
	cd oe-core/meta-msm/recipes/images
	if [ -f $P82M_QC_9XXX_TARGET_NAME-image.inc.bak ]; then
		mv $P82M_QC_9XXX_TARGET_NAME-image.inc.bak $P82M_QC_9XXX_TARGET_NAME-image.inc
	fi
	cp $P82M_QC_9XXX_TARGET_NAME-image.inc $P82M_QC_9XXX_TARGET_NAME-image.inc.bak
	sed -e 's/IMAGE_INSTALL += "rtsp-alg"/# Deleted by Ruinan Duan \n# IMAGE_INSTALL += "rtsp-alg"/g' $P82M_QC_9XXX_TARGET_NAME-image.inc > $P82M_QC_9XXX_TARGET_NAME-image.inc.tmp
	mv $P82M_QC_9XXX_TARGET_NAME-image.inc.tmp $P82M_QC_9XXX_TARGET_NAME-image.inc
	cd ../../../../

	# modify layer.conf   (add zte code bb direcoties)
	cd oe-core/meta-qcom/conf
	if [ -f layer.conf.bak ]; then
		mv layer.conf.bak layer.conf
	fi
	cp layer.conf layer.conf.bak
	sed -e 's/BBFILES += \"${LAYERDIR}\/recipes\/\*\/\*\.bb \${LAYERDIR}\/recipes\/\*\/\*\.bbappend"/BBFILES += \"${LAYERDIR}\/recipes\/\*\/\*\.bb \${LAYERDIR}\/recipes\/\*\/\*\.bbappend"\nBBFILES += "\${LAYERDIR}\/recipes\/\*\/\*\/\*\.bb \${LAYERDIR}\/recipes\/\*\/\*\/\*\.bbappend"\nBBFILES += "\${LAYERDIR}\/recipes\/\*\/\*\/\*\/\*\.bb \${LAYERDIR}\/recipes\/\*\/\*\/\*\/\*\.bbappend"\nBBFILES += "\${LAYERDIR}\/recipes\/\*\/\*\/\*\/\*\/\*\.bb \${LAYERDIR}\/recipes\/\*\/\*\/\*\/\*\/\*\.bbappend"\nBBFILES += "\${LAYERDIR}\/recipes\/\*\/\*\/\*\/\*\/\*\/\*\.bb \${LAYERDIR}\/recipes\/\*\/\*\/\*\/\*\/\*\/\*\.bbappend"/g' layer.conf > layer.conf.tmp
	mv layer.conf.tmp layer.conf
	cd ../../../
}

# Copy custom files
copy_custom_files_for_qc_mdm9xxx_apss()
{
	cp -f cust/$P82M_PROJECT_DIR/ap_config/*.inc platform_sdk/qualcomm/mdm9xxx.le/code/apps_proc/oe-core/meta-qcom/recipes/zte
}

# Replace APSS and ZTE Application code.
replace_code_for_qc_mdm9xxx_apss()
{
	if [ -d "cust/$P82M_PROJECT_DIR/code_replaced_with/apps_proc" ]; then
		rm -rf ./apps_proc_tmp
		cp -r cust/$P82M_PROJECT_DIR/code_replaced_with/apps_proc ./apps_proc_tmp
		find ./apps_proc_tmp -name .svn | xargs rm -rf
		cp -rf ./apps_proc_tmp/* platform_sdk/qualcomm/mdm9xxx.le/code/apps_proc/
		rm -rf ./apps_proc_tmp
	fi
	if [ -d "cust/$P82M_PROJECT_DIR/code_replaced_with/zte_sdk" ]; then
		rm -rf ./zte_sdk_tmp
		cp -r cust/$P82M_PROJECT_DIR/code_replaced_with/zte_sdk ./zte_sdk_tmp
		find ./zte_sdk_tmp -name .svn | xargs rm -rf
		cp -rf ./zte_sdk_tmp/* ./zte_sdk/
		rm -rf ./zte_sdk_tmp
	fi
}

# copy apss image files.
copy_qc_mdm9xxx_apss_images()
{
	print_wait "Saving apss images."
	mkdir -p images/$P82M_PROJECT/
	# copy log
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/log.txt images/$P82M_PROJECT/
	# copy aboot
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/*.mbn images/$P82M_PROJECT/
	if [ "$?" != "0" ]; then
		print_fail "copy aboot failed."
		return 1
	fi
	# copy bootimg and recovery-bootimg
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/*.img images/$P82M_PROJECT/
	if [ "$?" != "0" ]; then
		print_fail "copy kernel failed."
		return 1
	fi
	# copy usrfs
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/*usrfs.yaffs2 images/$P82M_PROJECT/
	if [ "$?" != "0" ]; then
		print_fail "copy usrfs failed."
		return 1
	fi
	# copy rootfs
	if [ "`find $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/ -maxdepth 1 -name *sysfs.yaffs2`" != "" ]; then
		cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/*sysfs.yaffs2 images/$P82M_PROJECT/
		if [ "$?" != "0" ]; then
			print_fail "copy sysfs failed."
			return 1
		fi
	else
		cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/$P82M_QC_9XXX_TARGET_PREFIX-image*.yaffs2 images/$P82M_PROJECT/
		if [ "$?" != "0" ]; then
			print_fail "copy sysfs failed."
			return 1
		fi
	fi
	# copy recovery rootfs
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/deploy/images/$P82M_QC_9XXX_TARGET_NAME/$P82M_QC_9XXX_TARGET_PREFIX-recovery-image*.yaffs2 images/$P82M_PROJECT/
		if [ "$?" != "0" ]; then
			print_fail "copy recovery fs failed."
			return 1
		fi
	# copy vmlinux
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/sysroots/$P82M_QC_9XXX_TARGET_NAME/kernel/vmlinux images/$P82M_PROJECT/
	if [ "$?" != "0" ]; then
		print_fail "copy vmlinux failed."
		return 1
	fi
	# copy System.map
	cp -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/build/tmp-eglibc/sysroots/$P82M_QC_9XXX_TARGET_NAME/kernel/System.map images/$P82M_PROJECT/
	if [ "$?" != "0" ]; then
		print_fail "copy System.map failed."
		return 1
	fi

	# copy contents.xml
	cp -f $QC_9XXX_SDK_BASE_DIR/contents.xml images/$P82M_PROJECT/
	
	# added by ye.yy for modifing 3013 AP files' name on 3013 based line 
	if [ "${P82M_BRANCH}" = "3013" ] && [ "$P82M_MACHINE" = "mdm9x25" ]; then
		mv images/$P82M_PROJECT/mdm9625-boot.img  images/$P82M_PROJECT/mdm-image-boot-mdm9625.img
		if [ "$?" != "0" ]; then
		print_fail "modify boot failed."
		return 1
		fi
		mv images/$P82M_PROJECT/mdm9625-sysfs.yaffs2 images/$P82M_PROJECT/mdm-image-mdm9625.yaffs2
		if [ "$?" != "0" ]; then
		print_fail "modify system failed."
		return 1
		fi
		mv images/$P82M_PROJECT/mdm9625-usrfs.yaffs2 images/$P82M_PROJECT/mdm9625-usr-image.usrfs.yaffs2
		if [ "$?" != "0" ]; then
		print_fail "modify usr fs failed."
		return 1
		fi
	fi
	# added by ye.yy for modifing AP files' name on 3013 based line 

	# added by Ruinan Duan for rename recovery boot image.
	if [ "$P82M_MACHINE" = "mdm9x25" ]; then
		if [ -f images/$P82M_PROJECT/mdm-recovery-image-boot-mdm9625.img ]; then
			mv images/$P82M_PROJECT/mdm-recovery-image-boot-mdm9625.img images/$P82M_PROJECT/mdm-image-boot-recovery-mdm9625.img
		fi
	fi
	
	chmod -R 777 images/$P82M_PROJECT
	print_okey "Saving apss images complete."
	return 0
}

# run bitbake build environment.
run_qc_mdm9xxx_apss_native_build()
{
	local prev_dir=""
	local build_all_cmd=""
	
	which python
	prev_dir=`pwd`
	cd "$QC_9XXX_SDK_BASE_DIR/apps_proc"

	if [ -e log.txt ] ;then
		mv log.txt log.txt.bak
	fi

	# Empty all the input arguments, this makes oe build fail otherwise.
	while (( "$#" )); do
		shift
	done

	if [ -f oe-core/build/conf/set_bb_env.sh ]; then
		cd oe-core
		. build/conf/set_bb_env.sh
	fi

	if [ "`echo ${P82M_APCLEARALL}`" = "1" ]; then
		buildclean
		find tmp-eglibc -name .svn | xargs rm -rf
	fi

	if [ "${#P82M_APPLIST[@]}" != "0" ]; then
		for ((i=0; i < ${#P82M_APPLIST[@]}; i++)); do
			print_hint "Clear APP: "${P82M_APPLIST[i]}
			bitbake -fc clean ${P82M_APPLIST[i]} | tee -a ../../log.txt 2>&1
			if [ "`tail -3 ../../log.txt | grep "ERROR"`" != "" ]; then
				print_fail "bitbake -fc clean failed."
				cd $prev_dir
				return 1
			fi
			if [ "${P82M_APPLIST[i]}" = "virtual/kernel" ]; then
				print_wait "Clear sstate-cache/sstate-linux-*"
				rm -rf sstate-cache/sstate-linux-*
			elif [ "${P82M_APPLIST[i]}" = "virtual/bootloader" ]; then
				print_wait "Clear sstate-cache/sstate-lk-*"
				rm -rf sstate-cache/sstate-lk-*
			else
				print_wait "Clear sstate-cache/*"${P82M_APPLIST[i]}"*"
				eval rm -rf sstate-cache/*\${P82M_APPLIST[i]}*
			fi
		done
	fi

	if [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
		for ((i=0; i < ${#P82M_APPNOPACKLIST[@]}; i++)); do
			print_hint "Clear APP: "${P82M_APPNOPACKLIST[i]}
			bitbake -fc clean ${P82M_APPNOPACKLIST[i]} | tee -a ../../log.txt 2>&1
			if [ "`tail -3 ../../log.txt | grep "ERROR"`" != "" ]; then
				print_fail "bitbake -fc clean failed."
				cd $prev_dir
				return 1
			fi
			if [ "${P82M_APPNOPACKLIST[i]}" = "virtual/kernel" ]; then
				print_wait "Clear sstate-cache/sstate-linux-*"
				rm -rf sstate-cache/sstate-linux-*
			elif [ "${P82M_APPNOPACKLIST[i]}" = "virtual/bootloader" ]; then
				print_wait "Clear sstate-cache/sstate-lk-*"
				rm -rf sstate-cache/sstate-lk-*
			else
				print_wait "Clear sstate-cache/*"${P82M_APPNOPACKLIST[i]}"*"
				eval rm -rf sstate-cache/*\${P82M_APPNOPACKLIST[i]}*
			fi
		done
	fi

	if [ "$P82M_MACHINE" = "mdm9x15" ]; then
		build_all_cmd=build9615
	elif [ "$P82M_MACHINE" = "mdm9x25" ]; then
		build_all_cmd=build9625
	elif [ "$P82M_MACHINE" = "mdm9x30" ]; then
		build_all_cmd=build9635
	fi

	if [ "${#P82M_APPNOPACKLIST[@]}" = "0" ]; then
		print_wait "Build AP ALL $build_all_cmd"
		$build_all_cmd | tee -a ../../log.txt 2>&1
		if [ "`tail -3 ../../log.txt | grep "ERROR"`" != "" ]; then
			print_fail "$build_all_cmd failed."
			cd $prev_dir
			return 1
		else
			cd $prev_dir
			# copy images to uSDK/images/$P82M_PROJECT/
			if ! copy_qc_mdm9xxx_apss_images ; then
				print_fail "copy images aborted."
				return 1
			fi
			return 0
		fi
	fi

	if [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
		for ((i=0; i < ${#P82M_APPNOPACKLIST[@]}; i++)); do
			print_wait "Build ${P82M_APPNOPACKLIST[i]}"
			bitbake ${P82M_APPNOPACKLIST[i]} | tee -a ../../log.txt 2>&1
			if [ "`tail -3 ../../log.txt | grep "ERROR"`" != "" ]; then
				print_fail "Build ${P82M_APPNOPACKLIST[i]} failed."
				cd $prev_dir
				return 1
			else
				cd $prev_dir
				return 0
			fi
		done
	fi

	return 1
}

# generate file system package for FOTA
# begin
make_qc_mdm9xxx_apss_fs_tarballs()
{
	local cur_dir=`pwd`

	if [ -d $1 ];then
		cd $1
		tar -czf $2/$3.tgz $4 $5 $6 $7 ./
		if [ $3 == "root_fs" ]; then
			find ./ -type f -printf '"/mnt/system/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/mnt/system/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/mnt/system/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
		if [ $3 == "recovery_root_fs" ]; then
			find ./ -type f -printf '"/mnt/recoveryfs/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/mnt/recoveryfs/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/mnt/recoveryfs/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
		if [ $3 == "usr" ]; then
			find ./ -type f -printf '"/mnt/usr/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/mnt/usr/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/mnt/usr/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
	fi
	cd $cur_dir
}

pack_qc_mdm9xxx_apss_fs()
{
	local base_path="platform_sdk/qualcomm/mdm9xxx.le/code/apps_proc"
	local root_fs_path=""
	local usr_fs_path=""
	local recovery_root_fs_path=""
	local fs_pack_path="images/$P82M_PROJECT/FOTA_FS"
	local image_path="images/$P82M_PROJECT"
	local pre_path=""

	pre_path=`pwd`
	print_wait "Begin to package apss file system."
	rm -rf "${fs_pack_path}"
	mkdir -p "${fs_pack_path}"
	cd ${fs_pack_path}
	rm -f *
	fs_pack_path=`pwd`
	cd $pre_path
	root_fs_path="${base_path}/oe-core/build/tmp-eglibc/work/$P82M_QC_9XXX_TARGET_NAME-oe-linux-gnueabi/$P82M_QC_9XXX_TARGET_PREFIX-image-1.0-r0/rootfs"
	if [ -d "${base_path}/oe-core/build/tmp-eglibc/rootfs/$P82M_QC_9XXX_TARGET_NAME-usr-image" ]; then
		usr_fs_path="${base_path}/oe-core/build/tmp-eglibc/rootfs/$P82M_QC_9XXX_TARGET_NAME-usr-image"
	elif [ -d "${base_path}/oe-core/build/tmp-eglibc/rootfs/$P82M_QC_9XXX_TARGET_NAME-usrfs" ]; then
		usr_fs_path="${base_path}/oe-core/build/tmp-eglibc/rootfs/$P82M_QC_9XXX_TARGET_NAME-usrfs"
	else
		print_fail "pack_qc_mdm9xxx_apss_fs failed. (unknown usr_fs_path)"
		return 1
	fi
	recovery_root_fs_path="${base_path}/oe-core/build/tmp-eglibc/work/$P82M_QC_9XXX_TARGET_NAME-oe-linux-gnueabi/$P82M_QC_9XXX_TARGET_PREFIX-recovery-image-1.0-r0/rootfs"
	make_qc_mdm9xxx_apss_fs_tarballs ${root_fs_path} ${fs_pack_path} root_fs --exclude=./sbin/sys_* --exclude=./sbin/powerapp --exclude=./etc/terminfo/v/vt220 --exclude=./etc/terminfo/v/vt200
	make_qc_mdm9xxx_apss_fs_tarballs ${recovery_root_fs_path} ${fs_pack_path} recovery_root_fs --exclude=./sbin/sys_* --exclude=./sbin/powerapp --exclude=./etc/terminfo/v/vt220 --exclude=./etc/terminfo/v/vt200
	make_qc_mdm9xxx_apss_fs_tarballs ${usr_fs_path} ${fs_pack_path} usr
	cd $pre_path
	chmod -R 777 ${image_path}
	print_okey "Package apss file system complete."
	return 0
}
# end

# build mdm9xxx apss
build_qc_mdm9xxx_apss()
{
	local prev_dir=""
	local tmp_str=""

	print_head "Build Qualcomm MDM9xxx APSS"

	# export P82M_PROJECT and configure options to bitbake var: BB_ENV_EXTRAWHITE
	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' cust/$P82M_PROJECT_DIR/ap_config/options.xml`)
	P82M_CONF_OPT_VALUE=(`sed -n 's/.*value>\(.*\)<\/value>/\1/p' 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]} to BB_ENV_EXTRAWHITELIST"
		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}"
	export "P82M_MACHINE=${P82M_MACHINE}"

	# add P82M_BRANCH
	P82M_CONF_OPT_NAME_LIST="P82M_BRANCH ${P82M_CONF_OPT_NAME_LIST}"
	export "P82M_BRANCH=${P82M_BRANCH}"

	if [ -f $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal.bak ]; then
		mv $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal.bak $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal
	fi
	cp $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal.bak
	tmp_str=`sed -n '/^export\s\+BB_ENV_EXTRAWHITE\s*=\s*"\(\w\+\s*\)\+"$/p' $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal | sed -e 's/"//2'`
	tmp_str="${tmp_str} P82M_PROJECT ${P82M_CONF_OPT_NAME_LIST}\""
	sed -e 's/^export\s\+BB_ENV_EXTRAWHITE\s*=\s*"\(\w\+\s*\)\+"$/'"$tmp_str"'/g' $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal > tmpsed
	mv tmpsed $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal
	chmod 755 $QC_9XXX_SDK_BASE_DIR/apps_proc/oe-core/scripts/oe-buildenv-internal
	print_hint "export P82M_PROJECT=$P82M_PROJECT to BB_ENV_EXTRAWHITELIST"
	export P82M_PROJECT=$P82M_PROJECT
	
	unset tmp_str

	prev_dir=`pwd`
	cd "$QC_9XXX_SDK_BASE_DIR/apps_proc"
	rm -rf ./zte_sdk
	ln -sf ../../../../../zte_sdk ./zte_sdk
	cd oe-core/meta-qcom/recipes
	rm -rf ./zte
	ln -sf ../../../../../../../../zte_sdk/build/qualcomm/mdm9xxx.le ./zte
	cd ../../../
	cd $prev_dir

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

	# copy custom files
	copy_custom_files_for_qc_mdm9xxx_apss

	# replace code
	replace_code_for_qc_mdm9xxx_apss

	cd "$QC_9XXX_SDK_BASE_DIR/apps_proc"
	# modify some files
	qc_mdm9xxx_apss_do_modify

	cd $prev_dir

	# do something else before build
	sh cust/$P82M_PROJECT_DIR/script/custom_actions_before_build_apss
	if [ "$?" != "0" ]; then
		print_fail "run custom_actions_before_build_apss failed."
		return 1
	fi
	
	# run qualcomm oe-core build system
	if ! run_qc_mdm9xxx_apss_native_build; then
		print_fail "run_qc_mdm9xxx_apss_native_build failed."
		return 1
	fi

	# generate FOTA_FS
	if ! pack_qc_mdm9xxx_apss_fs; then
		print_fail "generate FOTA_FS imcomplete."
		return 1
	fi

	# do something else after build
	sh cust/$P82M_PROJECT_DIR/script/custom_actions_after_build_apss
	if [ "$?" != "0" ]; then
		print_fail "run custom_actions_after_build_apss failed."
		return 1
	fi

	print_tail "Build Qualcomm MDM9xxx APSS complete."

	return 0
}

# build mpss
build_qc_mdm9xxx_mpss()
{
	local pre_dir=""
	mpss_alt_build_cmd=""

	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/modem_proc
	chmod 777 -R .
	cd $pre_dir
	if [ -f mpss_build_cmd.cfg ]; then
		mpss_alt_build_cmd=`cat mpss_build_cmd.cfg`
		cd platform_sdk/qualcomm/mdm9xxx.le/code/modem_proc/build/ms
		print_hint "mpss mpss_alt_build_cmd: ${mpss_alt_build_cmd}.sh"
		sh "${mpss_alt_build_cmd}.sh"
	else
		cd platform_sdk/qualcomm/mdm9xxx.le/code/modem_proc/build/ms
		mpss_alt_build_cmd="${P82M_PROJECT}"
		print_hint "mpss mpss_alt_build_cmd: ${mpss_alt_build_cmd}.sh"
		sh "${mpss_alt_build_cmd}.sh"
	fi
	if [ "$?" != 0 ]; then
		print_fail "Build mpss aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build mpss complete."
		if [ "$P82M_MACHINE" = "mdm9x30" ]; then
			cp M9x35${mpss_alt_build_cmd}Q0000.elf ../../../../../../../images/$P82M_PROJECT/
		fi
		if [ "$P82M_MACHINE" = "mdm9x25" ]; then
			cp M9x25${mpss_alt_build_cmd}Q*.elf ../../../../../../../images/$P82M_PROJECT/
			cp bin/${mpss_alt_build_cmd}/*.mbn ../../../../../../../images/$P82M_PROJECT/
		fi
		cd $pre_dir
		return 0
	fi
}

# build lpass
build_qc_mdm9xxx_lpss()
{
	local pre_dir=""

	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/lpass_proc
	echo "run build_lpass.sh $P82M_PROJECT"
	if [ "$?" != 0 ]; then
		print_fail "Build lpass aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build lpass complete."
		cd $pre_dir
	fi
	return 0
}

# build common
build_qc_mdm9xxx_common()
{
	local pre_dir=""
	local modem_buildid=""
	local cmd_project=""
	local cmd_common_project=""
	local cmd_common=""

	print_wait "Check script before build_qc_mdm9xxx_common"
	if [ -e cust/$P82M_PROJECT_DIR/script/custom_actions_before_build_common ]; then
		print_hint "cust/${P82M_PROJECT_DIR}/script/custom_actions_before_build_common exist, excecute ..."
		bash cust/$P82M_PROJECT_DIR/script/custom_actions_before_build_common
	fi
	modem_buildid=`cat cust/$P82M_PROJECT_DIR/BUILDID_LIST.cfg | grep "\bmpss:\w\+\b" | sed 's/mpss://g'`
	print_hint "MPSS BUILDID = $modem_buildid"
	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/common
	chmod 777 -R .
	cd $pre_dir
	cd platform_sdk/qualcomm/mdm9xxx.le/code/common/build
	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		rm -rf ../../modem_proc/build/ms/bin/$modem_buildid
		print_hint "copy bin/$P82M_PROJECT $modem_buildid"
		cp -r ../../modem_proc/build/ms/bin/$mpss_alt_build_cmd ../../modem_proc/build/ms/bin/$modem_buildid
	fi
	cmd_common="update_common_info.py"
	cmd_common_project="update_common_info_${P82M_COMMON_PROJECT}.py"
	cmd_project="update_common_info_${P82M_PROJECT}.py"
	cmd_common_project=`echo $cmd_common_project | tr '[A-Z]' '[a-z]'`
	cmd_project=`echo $cmd_project | tr '[A-Z]' '[a-z]'`
	print_hint "${cmd_common}"
	print_hint "${cmd_project}"
	print_hint "${cmd_common_project}"
	if [ -e "$cmd_project" ]; then
		print_hint "Run ${cmd_project}"
		python ./${cmd_project}
	elif [ -e "$cmd_common_project" ]; then
		print_hint "Run ${cmd_common_project}"
		python ./${cmd_common_project}
	else
		print_hint "Run ${cmd_common}"
		python ./${cmd_common}
	fi

	if [ "$?" != 0 ]; then
		print_fail "Build common aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build common complete."
		if [ "$P82M_MACHINE" = "mdm9x30" ]; then
			cp NON-HLOS.yaffs2 ../../../../../../images/$P82M_PROJECT/
			chmod -R 777 ../../../../../../images/$P82M_PROJECT/
		fi
		cp partition.mbn ../../../../../../images/$P82M_PROJECT/
		cp partition_nand.xml ../../../../../../images/$P82M_PROJECT/
		cd $pre_dir
		cd platform_sdk/qualcomm/mdm9xxx.le/code
		chmod -R 777 common
		cd $pre_dir
		return 0
	fi
}

# build rpm
build_qc_mdm9xxx_rpm()
{
	local pre_dir=""
	local rpm_buildid=""

	rpm_buildid=`cat cust/$P82M_PROJECT_DIR/BUILDID_LIST.cfg | grep "\brpm:\w\+\b" | sed 's/rpm://g'`
	print_hint "RPM BUILDID = $rpm_buildid"
	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/rpm_proc/
	chmod 777 -R .
	cd $pre_dir
	cd platform_sdk/qualcomm/mdm9xxx.le/code
	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		print_hint "run sh build_9x35.sh"
		source ../../../../script/9x35env
		sh rpm_proc/build/build_9x35.sh
	fi
	if [ "$?" != 0 ]; then
		print_fail "Build rpm aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build rpm complete."
		print_hint "copy rpm_proc/build/ms/bin/${rpm_buildid}/rpm.mbn"
		ls ../../../../
		cp rpm_proc/build/ms/bin/${rpm_buildid}/rpm.mbn ../../../../images/$P82M_PROJECT/
		cp rpm_proc/core/bsp/rpm/build/RPM_${rpm_buildid}.elf ../../../../images/$P82M_PROJECT/
		cd $pre_dir
	fi
	return 0
}

# build tz
build_qc_mdm9xxx_tz()
{
	local pre_dir=""
	local tz_buildid=""

	tz_buildid=`cat ./cust/$P82M_PROJECT_DIR/BUILDID_LIST.cfg | grep "\btz:\w\+\b" |sed 's/tz://g'`
	print_hint "TZ BUILDID = $tz_buildid"
	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/trustzone_images
	chmod 777 -R .
	cd $pre_dir
	cd platform_sdk/qualcomm/mdm9xxx.le/code/trustzone_images/build/ms
	print_hint "run $P82M_PROJECT.sh tz"
	sh $P82M_PROJECT.sh tz
	if [ "$?" != 0 ]; then
		print_fail "Build tz aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build tz complete."
		print_hint "copy ./bin/${tz_buildid}/tz.mbn"
		cp ./bin/${tz_buildid}/tz.mbn ../../../../../../../images/$P82M_PROJECT/
		cp ../../core/bsp/tzbsp/build/${tz_buildid}/tz.elf ../../../../../../../images/$P82M_PROJECT/
		cd $pre_dir
	fi
	return 0
}

# build sdi
build_qc_mdm9xxx_sdi()
{
	local pre_dir=""
	local sdi_buildid=""

	sdi_buildid=`cat ./cust/$P82M_PROJECT_DIR/BUILDID_LIST.cfg | grep "\bsdi:\w\+\b" | sed 's/sdi://g'`
	print_hint "SDI BUILDID = $sdi_buildid"
	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/debug_image
	chmod 777 -R .
	cd $pre_dir
	cd platform_sdk/qualcomm/mdm9xxx.le/code/debug_image/build/ms
	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		# print_hint "run build.sh TARGET_FAMILY=9x35 sdi"
		# sh ./build.sh TARGET_FAMILY=9x35 sdi
		print_hint "skip build debug image for mdm9x30"
	fi
	if [ "$?" != 0 ]; then
		print_fail "Build sdi aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build sdi complete."
		cp ./bin/${sdi_buildid}/sdi.mbn ../../../../../../../images/$P82M_PROJECT/
		cd $pre_dir
		return 0
	fi
}

# build boot
build_qc_mdm9xxx_boot()
{
	local pre_dir=""

	pre_dir=`pwd`
	cd platform_sdk/qualcomm/mdm9xxx.le/code/boot_images/build/ms
	echo "run $P82M_PROJECT.sh"
	sh "$P82M_PROJECT.sh"
	if [ "$?" != 0 ]; then
		print_fail "Build boot aborted."
		cd $pre_dir
		return 1
	else
		print_okey "Build boot complete."
		if [ "$P82M_MACHINE" = "mdm9x30" ]; then
			cp ./bin/9x35/nand ../../../../../../../images/$P82M_PROJECT/
		fi
		cd $pre_dir
		return 0
	fi
}


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

	script/dos2unix "./cust/$P82M_PROJECT_DIR/BUILDID_LIST.cfg"
	chmod +x -R "./cust/${P82M_PROJECT_DIR}/script"
	if [ -d "images/$P82M_PROJECT" ]; then
		print_wait "Removing existed images."
		rm -rf images/$P82M_PROJECT/
	fi
	mkdir -p "images/$P82M_PROJECT"
	
	#rpm
	if [ "`echo "${P82M_METABUILD[*]}" | grep "rpm"`" != "" ]; then
		if ! build_qc_mdm9xxx_rpm; then
			print_fail "Build rpm failed."
			have_err="1"
		fi
	fi
	#mpss
	if [ "`echo "${P82M_METABUILD[*]}" | grep "mpss"`" != "" ]; then
		if ! build_qc_mdm9xxx_mpss; then
			print_fail "Build mpss failed."
			have_err="1"
		fi
	fi
	#sdi
	if [ "`echo "${P82M_METABUILD[*]}" | grep "sdi"`" != "" ]; then
		if ! build_qc_mdm9xxx_sdi; then
			print_fail "Build sdi failed."
			have_err="1"
		fi
	fi
	#tz
	if [ "`echo "${P82M_METABUILD[*]}" | grep "tz"`" != "" ]; then
		if ! build_qc_mdm9xxx_tz; then
			print_fail "Build tz failed."
			have_err="1"
		fi
	fi
	#boot
	if [ "`echo "${P82M_METABUILD[*]}" | grep "boot"`" != "" ]; then
		if ! build_qc_mdm9xxx_boot; then
			print_fail "Build boot failed."
			have_err="1"
		fi
	fi
	#lpass
	if [ "`echo "${P82M_METABUILD[*]}" | grep "lpass"`" != "" ]; then
		if ! build_qc_mdm9xxx_lpass; then
			print_fail "Build lpass failed."
			have_err="1"
		fi
	fi
	#common
	if [ "`echo "${P82M_METABUILD[*]}" | grep "common"`" != "" ]; then
		if ! build_qc_mdm9xxx_common; then
			print_fail "Build common failed."
			have_err="1"
		fi
	fi
	#apss
	if [ "`echo "${P82M_METABUILD[*]}" | grep "apss"`" != "" ]; then
		if ! build_qc_mdm9xxx_apss; then
			print_fail "Build apss failed."
			have_err="1"
		fi
	fi

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

	return 0
}


