#!/bin/bash
#
# builder_tsp7520: Build TSP7520 Platform
# Ruinan Duan 07/23/2015
#

. ${P82M_ABS_PATH}/script/replace

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

# Copy custom files
copy_custom_files_for_tsp7520_ap()
{
	return 0
}

# Replace AP and ZTE Application code.
replace_code_for_tsp7520_ap()
{
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/tsp7520" ]; 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_tsp7520.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_tsp7520_ap_images()
{
	return 0
}

copy_tsp7520_normal_config()
{
	print_wait "Copy normal system config files"
	cp -vf ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/vendors/ZTE/zx297520/normal/* ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/vendors/ZTE/zx297520/
	cp -vf ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/user/zte_apps/normal/* ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/user/zte_apps/
	cp -vf ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/lib/zte_libs/normal/* ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/lib/zte_libs/
	return 0
}

copy_tsp7520_recovery_config()
{
	print_wait "Copy recovery system config files"
	cp -vf ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/vendors/ZTE/zx297520/recovery/* ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/vendors/ZTE/zx297520/
	cp -vf ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/user/zte_apps/recovery/* ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/user/zte_apps/
	cp -vf ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/lib/zte_libs/recovery/* ${TSP7520_SDK_BASE_DIR}/base/CPU_R7/os/uClinux/lib/zte_libs/
	return 0
}

check_tsp7520_have_recovery()
{
	test_amt_str=`echo ${P82M_PROJECT} | grep "AMT"`
	if [ "${test_amt_str}" != "" ]; then
		print_hint "None Recovery"
		return 1
	fi
	if [ -f ${TSP7520_SDK_BASE_DIR}/base/Makefile.recovery ]; then
		print_hint "Have Recovery"
		return 0
	fi
	print_hint "None Recovery"
	return 1
}

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

	prev_dir=`pwd`

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

	export TARGET_PRODUCT=${P82M_PROJECT}
	export P82M_PROJECT_DIR=$P82M_PROJECT_DIR
	export P82M_ABS_PATH=$P82M_ABS_PATH
	export P82M_PROJECT=$P82M_PROJECT
	export P82M_MACHINE=$P82M_MACHINE
	export P82M_BRANCH=$P82M_BRANCH

	chmod 755 -R $TSP7520_SDK_BASE_DIR/base/CPU_R7/project/zx297520/build/linux
	mv $TSP7520_SDK_BASE_DIR/base/build.log $TSP7520_SDK_BASE_DIR/base/build.log.bak
	mv $TSP7520_SDK_BASE_DIR/base/build_recovery.log $TSP7520_SDK_BASE_DIR/base/build_recovery.log.bak

	if [ "`echo ${P82M_APCLOSECOREDUMP}`" = "1" ]; then
		sed -i 's/^\([ |\t]\+\)\(linux_ramdump_entry();\)/\1\/\/\2/g' $TSP7520_SDK_BASE_DIR/base/CPU_R7/os/uClinux/linux-3.x/fs/coredump.c
	else
		sed -i 's/^\([ |\t]\+\)\/\/\(linux_ramdump_entry();\)/\1\2/g' $TSP7520_SDK_BASE_DIR/base/CPU_R7/os/uClinux/linux-3.x/fs/coredump.c
	fi
	
	have_recovery=1
	if ! check_tsp7520_have_recovery; then
		have_recovery=0
	fi

	if [ "`echo ${P82M_APCLEARALL}`" = "1" ]; then
		print_hint "Clear and rebuild."
		cd "$TSP7520_SDK_BASE_DIR/base"
		copy_tsp7520_normal_config
		make refcleanandall
		if [ "$?" != "0" ]; then
			print_fail "Building system failed."
			cd $prev_dir
			return 1
		fi
		if [ "`echo ${have_recovery}`" = "1" ]; then
			copy_tsp7520_recovery_config
			make -f Makefile.recovery refcleanandall
			if [ "$?" != "0" ]; then
				copy_tsp7520_normal_config
				print_fail "Building recovery failed."
				cd $prev_dir
				return 1
			fi
			copy_tsp7520_normal_config
		fi
		cd $prev_dir
	else
		unset build_list
		if [ "${#P82M_APPLIST[@]}" != "0" ] || [ "${#P82M_APPNOPACKLIST[@]}" != "0" ]; then
			copy_tsp7520_normal_config
			for ((i=0; i < ${#P82M_APPLIST[@]}; i++)); do
				cd "$TSP7520_SDK_BASE_DIR/base"
				if [ "${P82M_APPLIST[i]}" = "kernel" ]; then
					make link uc copybin
				elif [ "${P82M_APPLIST[i]}" = "ref" ]; then
					make ref uc copybin
				elif [ "${P82M_APPLIST[i]}" = "romfs" ]; then
					make romfs
				elif [ "${P82M_APPLIST[i]}" = "jffs2" ]; then
					make jffs2
				elif [ "${P82M_APPLIST[i]}" = "recovery" ]; then
					if [ "`echo ${have_recovery}`" = "1" ]; then
						copy_tsp7520_recovery_config
						make -f Makefile.recovery refcleanandall
					fi
				else
					make ${P82M_APPLIST[i]}
				fi
				if [ "$?" != "0" ]; then
					copy_tsp7520_normal_config
					cd $prev_dir
					print_fail "Build failed."
					return 1
				fi
				copy_tsp7520_normal_config
				cd $prev_dir
			done
		else
			cd "$TSP7520_SDK_BASE_DIR/base"
			# make allbuild
			copy_tsp7520_normal_config
			make refcleanandall
			if [ "$?" != "0" ]; then
				cd $prev_dir
				print_fail "Building system failed."
				return 1
			fi
			if [ "`echo ${have_recovery}`" = "1" ]; then
				copy_tsp7520_recovery_config
				make -f Makefile.recovery refcleanandall
				if [ "$?" != "0" ]; then
					copy_tsp7520_normal_config
					print_fail "Building recovery failed."
					cd $prev_dir
					return 1
				fi
				copy_tsp7520_normal_config
			fi
			cd $prev_dir
		fi
	fi
	
	print_okey "Build uClinux okey."
	# copy images to uSDK/images/$P82M_PROJECT/
	if ! copy_tsp7520_ap_images ; then
		print_fail "copy images aborted."
		return 1
	fi

	return 0
}

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

	if [ -d $1 ];then
		cd $1
		if [ $3 == "jffs2" ]; then
			tar -czf $2/$3.tgz $4 $5 ./
			find ./ -type f -printf '"/mnt/jffs2/%P" Regular 25 _redbend_%06m:   0:   0\n' | grep -v \.svn | grep -v lwresd > $2/$3_attr.txt
			find ./ -type l -printf '"/mnt/jffs2/%P" SymLink 25 _redbend_%06m:   0:   0\n' | grep -v \.svn >> $2/$3_attr.txt
			find ./ -type d -printf '"/mnt/jffs2/%P" Regular 25 _redbend_%06m:   0:   0\n' | grep -v \.svn >> $2/$3_attr.txt
		fi
	fi
	cd $cur_dir
}

pack_tsp7520_fs()
{
	local base_path="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520/code/base/prebuild/rootfs"
	local userdata_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
	userdata_fs_path="${base_path}/jffs2"
	make_tsp7520_fs_tarballs ${userdata_fs_path} ${fs_pack_path} jffs2 --exclude=\.svn --exclude=lwresd
	cd $pre_path
	chmod -R 777 ${image_path}
	print_okey "Package tsp7520 file system complete."
	return 0
}
# end

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

	print_head "Build $P82M_MACHINE AP"

	unset P82M_TSP7520_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 "$TSP7520_SDK_BASE_DIR/base/CPU_R7/os/uClinux/user"
	rm -rf ./zte_sdk
	ln -sf ../../../../../../../../../zte_sdk ./zte_sdk
	# ln ../../../../../../../../../zte_sdk ./zte_sdk
	cd $prev_dir

	# do something else before copy files
	export TSP7520_SDK_BASE_DIR=${TSP7520_SDK_BASE_DIR}
	export P82M_ABS_PATH=${P82M_ABS_PATH}
	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_tsp7520_ap

	# replace code
	replace_code_for_tsp7520_ap

	cd "$TSP7520_SDK_BASE_DIR/base"
	# modify some files
	tsp7520_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_tsp7520_ap_native_build; then
		print_fail "run_tsp7520_ap_native_build failed."
		return 1
	fi
	
	# generate FOTA_FS
	if ! pack_tsp7520_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 ${P82M_MACHINE} AP complete."

	return 0
}


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

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


