#!/bin/bash
#
# builder_tsp7510: Build TSP7510 Platform
# Ruinan Duan 06/30/2015
#

. ${P82M_ABS_PATH}/script/replace

# Modify some marvell code to add zte code to build system
tsp7510_ap_do_modify()
{
	return 0
}

# Copy custom files
copy_custom_files_for_tsp7510_ap()
{
	return 0
}

# Replace AP and ZTE Application code.
replace_code_for_tsp7510_ap()
{
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/tsp7510" ]; then
		print_wait "Begin to replace project related platform sdk files."
		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_tsp7510.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 platform sdk files 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_tsp7510_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/

	# $TSP7510_SDK_BASE_DIR is abs path
	cp -r $TSP7510_SDK_BASE_DIR/base/images ${P82M_ABS_PATH}/images/$P82M_PROJECT
	chmod -R 777 ${P82M_ABS_PATH}/images/$P82M_PROJECT
	print_okey "Saving images complete."
	return 0
}

run_tsp7510_ap_native_build()
{
	local prev_dir=""
	local find_dir=""
	local build_list=""

	prev_dir=`pwd`

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

	export KCFG=${P82M_PROJECT}_defconfig
	export TARGET_PRODUCT=${P82M_PROJECT}
	export COMPILE_LEVEL=0
	export P82M_PROJECT_DIR=$P82M_PROJECT_DIR

	if [ "`echo ${P82M_APCLEARALL}`" = "1" ]; then
		print_hint "Clear and rebuild."
		cd "$TSP7510_SDK_BASE_DIR/base"
		make -f Makefile.7510_ZTE clean
		if [ "$?" != "0" ]; then
			print_fail "Build Android failed."
			cd $prev_dir
			return 1
		fi
		cd $prev_dir
	else
		unset build_list
		if [ "${#P82M_APPLIST[@]}" != "0" ] || [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
			for ((i=0; i < ${#P82M_APPLIST[@]}; i++)); do
				cd "$TSP7510_SDK_BASE_DIR/base"
				make -f Makefile.7510_ZTE ${P82M_APPLIST[i]}
				if [ "$?" != "0" ]; then
					cd $prev_dir
					print_fail "Build failed."
					return 1
				fi
				cd $prev_dir
			done
		else
			cd "$TSP7510_SDK_BASE_DIR/base"
			make -f Makefile.7510_ZTE
			if [ "$?" != "0" ]; then
				cd $prev_dir
				print_fail "Build ${P82M_MACHINE} failed."
				return 1
			fi
			cd $prev_dir
		fi
	fi
	
	print_okey "Build Android okey."
	# copy images to uSDK/images/$P82M_PROJECT/
	if ! copy_tsp7510_ap_images ; then
		print_fail "copy images aborted."
		return 1
	fi

	return 0
}

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

	print_head "Build $P82M_MACHINE AP"

	unset P82M_TSP7510_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}"
	print_hint "export P82M_BRANCH=${P82M_BRANCH}"
	export "P82M_MACHINE=${P82M_MACHINE}"
	export "P82M_PROJECT=${P82M_PROJECT}"
	export "P82M_BRANCH=${P82M_BRANCH}"

	unset tmp_str

	prev_dir=`pwd`
	cd "$TSP7510_SDK_BASE_DIR/base"
	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_tsp7510_ap

	# replace code
	replace_code_for_tsp7510_ap

	cd "$TSP7510_SDK_BASE_DIR/base"
	# modify some files
	tsp7510_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
	if ! run_tsp7510_ap_native_build; then
		print_fail "run_tsp7510_ap_native_build failed."
		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 ${P82M_MACHINE} AP complete."

	return 0
}


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

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


