#!/bin/bash
#
# builder_marvell_pxa: build Marvell PXA platform.
# 
# Ruinan Duan 12/18/2014
#
# Modified by Ruinan Duan 12/26/2014 - Add abs path.
#

# Modify some marvell code to add zte code to build system
marvell_pxa_ap_do_modify()
{
	# modify git_ptk_android_make.sh
	cd vendor/marvell/generic/telephony/tools
	if [ -f git_ptk_android_make.sh.bak ]; then
		mv git_ptk_android_make.sh.bak git_ptk_android_make.sh
	fi
	cp git_ptk_android_make.sh git_ptk_android_make.sh.bak
	sed -e 's/^\tget_yesno/\tCOMMAND="yes"/g' git_ptk_android_make.sh > git_ptk_android_make.sh.tmp
	mv git_ptk_android_make.sh.tmp git_ptk_android_make.sh
	chmod 755 git_ptk_android_make.sh
	cd ../../../../../

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

	# modify platform_projects.mk
	cd device/marvell/${P82M_MACHINE}${P82M_MARVELL_BASE_PRJ_NAME}
	if [ -f platform_projects.mk.bak ]; then
		mv platform_projects.mk.bak platform_projects.mk
	fi
	cp platform_projects.mk platform_projects.mk.bak

	echo "# export P82M_MACHINE and P82M_PROJECT" >> platform_projects.mk
	echo "P82M_MACHINE=${P82M_MACHINE}" >> platform_projects.mk
	echo "P82M_PROJECT=${P82M_PROJECT}" >> platform_projects.mk

	echo "# export options in options.xml" >> platform_projects.mk
	for ((i=0; i < ${#P82M_CONF_OPT_NAME[@]}; i++)); do
		echo "${P82M_CONF_OPT_NAME[i]}=${P82M_CONF_OPT_VALUE[i]}" >> platform_projects.mk
	done

	echo "# include application directories." >> platform_projects.mk
	echo "include ../../../../../cust/$P82M_PROJECT_DIR/ap_config/applications.inc" >> platform_projects.mk
	echo "include ../../../../../cust/$P82M_PROJECT_DIR/ap_config/adapter.inc" >> platform_projects.mk
	echo "include ../../../../../cust/$P82M_PROJECT_DIR/ap_config/recovery-applications.inc" >> platform_projects.mk
	echo "include ../../../../../cust/$P82M_PROJECT_DIR/ap_config/recovery-adapter.inc" >> platform_projects.mk
	cd ../../../
	return 0
}

# Copy custom files
copy_custom_files_for_marvell_pxa_ap()
{
	return 0
}

# copy marvell android.mk
copy_marvell_android_mk()
{
	rm -rf ./ZTE_Build_tmp
	cp -r zte_sdk/build/marvell/pxa1801/ZTE_Build ./ZTE_Build_tmp
	find ./ZTE_Build_tmp -name .svn | xargs rm -rf
	cp -rf ./ZTE_Build_tmp/* zte_sdk/
	rm -rf ./ZTE_Build_tmp
	return 0
}

# Replace AP and ZTE Application code.
replace_code_for_marvell_pxa_ap()
{
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/Android_Dir" ]; then
		rm -rf ./Android_Dir_tmp
		cp -r ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/Android_Dir ./Android_Dir_tmp
		find ./Android_Dir_tmp -name .svn | xargs rm -rf
		cp -rf ./Android_Dir_tmp/* ${P82M_ABS_PATH}/platform_sdk/marvell/pxa/code/Android_Dir/
		rm -rf ./Android_Dir_tmp
	fi
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/zte_sdk" ]; then
		rm -rf ./zte_sdk_tmp
		cp -r ${P82M_ABS_PATH}/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/* ${P82M_ABS_PATH}/zte_sdk/
		rm -rf ./zte_sdk_tmp
	fi
	return 0
}

# copy ap image files.
copy_marvell_pxa_ap_images()
{
	if [ -d "${P82M_ABS_PATH}/images/$P82M_PROJECT" ]; then
		print_wait "Removing existed images."
		rm -rf ${P82M_ABS_PATH}/images/$P82M_PROJECT/
	fi
	print_wait "Saving ap images."
	mkdir -p ${P82M_ABS_PATH}/images/$P82M_PROJECT/

	# $MARVELL_PXA_SDK_BASE_DIR is abs path
	cp -r $MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/prebuilt_bin/output/* ${P82M_ABS_PATH}/images/$P82M_PROJECT
	rm -f ${P82M_ABS_PATH}/images/${P82M_PROJECT}/apps/ztedata.image.nand
	rm -f ${P82M_ABS_PATH}/images/${P82M_PROJECT}/*C0.blf
	cp -f ${P82M_ABS_PATH}/cust/${P82M_PROJECT_DIR}/ztedata.image.nand ${P82M_ABS_PATH}/images/${P82M_PROJECT}/apps
	chmod -R 777 ${P82M_ABS_PATH}/images/$P82M_PROJECT
	print_okey "Saving images complete."
	return 0
}

# run Android make build environment.
run_marvell_pxa_ap_native_build()
{
	local prev_dir=""
	local find_dir=""
	local build_list=""

	prev_dir=`pwd`

	while (( "$#" )); do
		shift
	done

	cd "$MARVELL_PXA_SDK_BASE_DIR/Android_Dir"
	cd ../../../../../zte_sdk/build/marvell/pxa1801/platform_sdk_oem_patch
	./install_oem_packages.sh
	cd "$MARVELL_PXA_SDK_BASE_DIR/Android_Dir"
	. $MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_env.bash
	set_base_git
	set_${P82M_MARVELL_BASE_PRJ_NAME}_git
	cd $prev_dir

	chmod 755 $MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh

	if [ "`echo ${P82M_APCLEARALL}`" = "1" ]; then
		print_hint "Clear and rebuild."
		$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 7
		if [ "$?" != "0" ]; then
			print_fail "Build Android failed."
			return 1
		fi
	else
		if [ "${#P82M_APPLIST[@]}" != "0" ] || [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
			if [ "${#P82M_APPLIST[@]}" != "0" ]; then
				for ((i=0; i < ${#P82M_APPLIST[@]}; i++)); do
					if [ "${P82M_APPLIST[i]}" = "kernel" ]; then
						cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
						$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 2
						cd $prev_dir
					elif [ "${P82M_APPLIST[i]}" = "uboot" ]; then
						cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
						$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 4
						cd $prev_dir
					elif [ "${P82M_APPLIST[i]}" = "obm" ]; then
						cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
						$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 5
						cd $prev_dir
					else
						build_list=${build_list}" ${P82M_APPLIST[i]}"
					fi
					if [ "$?" != "0" ]; then
						cd $prev_dir
						print_fail "Build failed."
						return 1
					fi
				done
			fi

			if [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
				for ((i=0; i < ${#P82M_APPNOPACKLIST[@]}; i++)); do
					if [ "${P82M_APPNOPACKLIST[i]}" = "kernel" ]; then
						cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
						$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 2
						cd $prev_dir
					elif [ "${P82M_APPNOPACKLIST[i]}" = "uboot" ]; then
						cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
						$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 4
						cd $prev_dir
					elif [ "${P82M_APPNOPACKLIST[i]}" = "obm" ]; then
						cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
						$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 5
						cd $prev_dir
					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
				echo "make $build_list"
				cd $MARVELL_PXA_SDK_BASE_DIR/Android_Dir
				make $build_list
				if [ "$?" != "0" ]; then
					cd $prev_dir
					print_fail "Build failed."
					return 1
				fi
				if [ "${#P82M_APPNOPACKLIST[@]}" = "0" ]; then
					make snod
					if [ "$?" != "0" ]; then
						cd $prev_dir
						print_fail "make snod failed."
						return 1
					fi
				fi
				cd $prev_dir
			fi
		else
			$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 8
			if [ "$?" != "0" ]; then
				print_fail "Build Android failed."
				return 1
			fi
			if [ -f ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/IS_MODULE.FLG ]; then
				$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor/marvell/generic/telephony/tools/git_ptk_android_make.sh 19
				if [ "$?" != "0" ]; then
					print_fail "Build Android mk 19 failed."
					return 1
				fi
			fi
		fi
	fi
	
	print_okey "Build Android okey."
	# copy images to uSDK/images/$P82M_PROJECT/
	if ! copy_marvell_pxa_ap_images ; then
		print_fail "copy images aborted."
		return 1
	fi

	return 0
}

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

	if [ -d $1 ];then
		cd $1
		if [ $3 == "system" ]; then
			find -name .svn | xargs rm -rf
			tar -czf $2/$3.tgz $4 $5 $6 $7 ./
			find ./ -type f -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
		if [ $3 == "userdata" ]; then
			find -name .svn | xargs rm -rf
			tar -czf $2/$3.tgz $4 $5 $6 $7 ./
			find ./ -type f -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
		if [ $3 == "mass_storage" ]; then
			find -name .svn | xargs rm -rf
			tar -czf $2/$3.tgz $4 $5 $6 $7 ./
			find ./ -type f -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
		if [ $3 == "NVMFS" ]; then
			find -name .svn | xargs rm -rf
			tar -czf $2/$3.tgz $4 $5 $6 $7 ./
			find ./ -type f -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' > $2/$3_attr.txt
			find ./ -type l -printf '"/%P" SymLink 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
			find ./ -type d -printf '"/%P" Regular 25 _redbend_%06m:   0:   0\n' >> $2/$3_attr.txt
		fi
	fi
	cd $cur_dir
}

pack_marvell_pxa_ap_fs()
{
	local base_path="${P82M_ABS_PATH}/platform_sdk/marvell/pxa/code/Android_Dir"
	local root_fs_path=""
	local userdata_fs_path=""
	local nvm_fs_path=""
	local mass_storage_fs_path=""
	local fs_pack_path="${P82M_ABS_PATH}/images/$P82M_PROJECT/FOTA_FS"
	local image_path="${P82M_ABS_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}/out/target/product/${P82M_MACHINE}${P82M_MARVELL_BASE_PRJ_NAME}/system"
	userdata_fs_path="${base_path}/out/target/product/${P82M_MACHINE}${P82M_MARVELL_BASE_PRJ_NAME}/data"
	nvm_fs_path="${base_path}/out/target/product/${P82M_MACHINE}${P82M_MARVELL_BASE_PRJ_NAME}/NVM"
	mass_storage_fs_path="${base_path}/out/target/product/${P82M_MACHINE}${P82M_MARVELL_BASE_PRJ_NAME}/obj/mifi_artifacts/mass"
	make_marvell_pxa_ap_fs_tarballs ${root_fs_path} ${fs_pack_path} system
	make_marvell_pxa_ap_fs_tarballs ${userdata_fs_path} ${fs_pack_path} userdata
	make_marvell_pxa_ap_fs_tarballs ${nvm_fs_path} ${fs_pack_path} NVMFS
	make_marvell_pxa_ap_fs_tarballs ${mass_storage_fs_path} ${fs_pack_path} mass_storage
	cd $pre_path
	chmod -R 777 ${image_path}
	print_okey "Package ap file system complete."
	return 0
}
# end

# build pxa ap code
build_marvell_pxa_ap()
{
	local prev_dir=""
	local tmp_str=""

	print_head "Build Marvell $P82M_MACHINE AP"

	unset P82M_MARVELL_BASE_PRJ_NAME

	sed -i 's/\r//g' ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/BASE_PRJ.cfg
	sed -i 's/\n//g' ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/BASE_PRJ.cfg
	P82M_MARVELL_BASE_PRJ_NAME=`cat ${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/BASE_PRJ.cfg`
	print_hint "P82M_MARVELL_BASE_PROJ_NAME=$P82M_MARVELL_BASE_PRJ_NAME"

	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}"

	unset tmp_str

	prev_dir=`pwd`
	cd "$MARVELL_PXA_SDK_BASE_DIR/Android_Dir/vendor"
	rm -rf ./zte_sdk
	ln -sf ../../../../../../zte_sdk ./zte_sdk
	cd $prev_dir

	# 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

	# copy custom files
	copy_custom_files_for_marvell_pxa_ap

	# copy Android.mk
	copy_marvell_android_mk

	# replace code
	replace_code_for_marvell_pxa_ap

	cd "$MARVELL_PXA_SDK_BASE_DIR/Android_Dir"
	# modify some files
	marvell_pxa_ap_do_modify

	cd $prev_dir

	# 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_marvell_pxa_ap_native_build; then
		print_fail "run_marvell_pxa_ap_native_build failed."
		return 1
	fi

	# 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

	print_tail "Build Marvell PXA AP complete."

	return 0
}


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

	mkdir -p "${P82M_ABS_PATH}/images/$P82M_PROJECT"
	#ap
	if [ "`echo "${P82M_METABUILD[*]}" | grep "ap"`" != "" ]; then
		if ! build_marvell_pxa_ap; then
			print_fail "Build ap 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
}


