#!/bin/bash
#
# code_manager: Restore files replaced during building progress and exceucute svn update.
# Ruinan Duan 05/20/2015
#
#


# Include tool script.
prev_dir=`pwd`
P82M_ABS_PATH=$(cd `dirname $0`; pwd)
cd $prev_dir
. ${P82M_ABS_PATH}/script/msg
. ${P82M_ABS_PATH}/script/cmd_parser
. ${P82M_ABS_PATH}/script/platform_sdk_dl
. ${P82M_ABS_PATH}/script/builder
. ${P82M_ABS_PATH}/script/replace


# Unset global variables.
unset P82M_CHIPSET
unset P82M_MACHINE
unset P82M_BRANCH
unset P82M_PROJECT
unset P82M_PROJECT_DIR
unset P82M_BUILDOPTION
unset P82M_METABUILD
unset P82M_APOPTION
unset P82M_APCLEARALL
unset P82M_APPLIST
unset P82M_APPNOPACKLIST

# print basic usage information.
usage_basic_hint()
{
	echo ""
	echo "Usage (basic): ./code_manager platform_branch project opname"
	echo "    e.g. ./code_manager mdm9x30_1031 MF970 op_update"
	echo "        opname: op_restore | op_update"
	echo "            op_restore: restore files that have been replaced during build progress."
	echo "            op_update: do restore then update uSDK and platform sdk."
	echo ""
}

restore_mt6735m()
{
	P82M_ANDROID_DIR="${P82M_ABS_PATH}/platform_sdk/mediatek/mt6735/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_ANDROID_DIR}/vendor/zte_sdk" 
	mediatek_mt_undo_modify
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/Android_Dir" ]; then
		print_wait "Begin to restore 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
		restore_replace_files
		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 restore 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
		restore_replace_files
		kill $progress_pid
		echo
		print_okey "Replace project related files in zte sdk complete."
	fi
}

restore_tsp7510()
{
	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7510/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_BASE_DIR}/zte_sdk" 
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/tsp7510" ]; then
		print_wait "Begin to restore 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
		restore_replace_files
		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 restore 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
		restore_replace_files
		kill $progress_pid
		echo
		print_okey "Replace project related files in zte sdk complete."
	fi
}

restore_tsp7520m()
{
	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520m/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_BASE_DIR}/zte_sdk" 
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/tsp7520m" ]; then
		print_wait "Begin to restore 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_tsp7520m.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
		restore_replace_files
		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 restore 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
		restore_replace_files
		kill $progress_pid
		echo
		print_okey "Replace project related files in zte sdk complete."
	fi
}

restore_tsp7520()
{
	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_BASE_DIR}/zte_sdk" 
	if [ -d "${P82M_ABS_PATH}/cust/$P82M_PROJECT_DIR/code_replaced_with/tsp7520" ]; then
		print_wait "Begin to restore 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
		restore_replace_files
		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 restore 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
		restore_replace_files
		kill $progress_pid
		echo
		print_okey "Replace project related files in zte sdk complete."
	fi
}

update_mt6735m()
{
	local cur_path=`pwd`

	P82M_ANDROID_DIR="${P82M_ABS_PATH}/platform_sdk/mediatek/mt6735/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_ANDROID_DIR}/vendor/zte_sdk" 
	print_wait "Begin to update platform sdk."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_ANDROID_DIR}"
	svn update 
	cd "$cur_path"
	print_wait "Begin to update uSDK."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_ABS_PATH}"
	svn update 
	cd "$cur_path"
}

update_tsp7510()
{
	local cur_path=`pwd`

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7510/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_BASE_DIR}/zte_sdk" 
	print_wait "Begin to update platform sdk."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_BASE_DIR}"
	svn update 
	cd "$cur_path"

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7510/code/common/app"
	cd "${P82M_BASE_DIR}"
	svn update 
	cd "$cur_path"

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7510/code/common/drivers"
	cd "${P82M_BASE_DIR}"
	svn update
	cd "$cur_path"

	print_wait "Begin to update uSDK."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_ABS_PATH}"
	svn update 
	cd "$cur_path"
}

update_tsp7520m()
{
	local cur_path=`pwd`

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520m/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_BASE_DIR}/CPU_R7/os/uClinux/user/zte_sdk" 
	print_wait "Begin to update platform sdk."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_BASE_DIR}"
	svn update 
	cd "$cur_path"

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520m/code/mod/common"
	cd "${P82M_BASE_DIR}"
	svn update 
	cd "$cur_path"

	print_wait "Begin to update uSDK."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_ABS_PATH}"
	svn update 
	cd "$cur_path"
}

update_tsp7520()
{
	local cur_path=`pwd`

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520/code/base"
	P82M_REALPROJECT=${P82M_PROJECT##PV}
	rm -rf "${P82M_BASE_DIR}/CPU_R7/os/uClinux/user/zte_sdk" 
	print_wait "Begin to update platform sdk."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_BASE_DIR}"
	svn update 
	cd "$cur_path"

	P82M_BASE_DIR="${P82M_ABS_PATH}/platform_sdk/tsp/tsp7520/code/common"
	cd "${P82M_BASE_DIR}"
	svn update 
	cd "$cur_path"

	print_wait "Begin to update uSDK."
	echo -e "\e[31;49m\e[1mDo not cancel manually in this stage ! \e[0m"
	cd "${P82M_ABS_PATH}"
	svn update 
	cd "$cur_path"
}

# ./build branch_name project_name
if [ $# -lt 2 ]; then
	usage_basic_hint
	exit 1
fi


unset cmd_line
cmd_line="$@"
cmd_line=($cmd_line)

# Get svn password
unset P82M_SVN_USERNAME
unset P82M_SVN_PASSWORD

n=${#cmd_line[@]}
for ((i=0, j=0; j < $n; i++)); do
	if [ $i -ge $n ]; then
		break
	fi
	if [ "${cmd_line[i]}" = "" ]; then
		continue
	else
		if [ "${cmd_line[i]}" = "--username" ]; then
			echo username=${cmd_line[i+1]}
			P82M_SVN_USERNAME=${cmd_line[i+1]}
			unset cmd_line[i]
			unset cmd_line[i+1]
			let i=$i+1
			let j=$j+1
		elif [ "${cmd_line[i]}" = "--password" ]; then
			echo password=${cmd_line[i+1]}
			P82M_SVN_PASSWORD=${cmd_line[i+1]}
			unset cmd_line[i]
			unset cmd_line[i+1]
			let i=$i+1
			let j=$j+1
		fi
		let j=$j+1
	fi
done

# Get code operation name
unset P82M_CODE_OPT

n=${#cmd_line[@]}
for ((i=0; i < $n; i++)); do
	if [ $i -ge $n ]; then
		break
	fi
	if [ "${cmd_line[i]}" = "" ]; then
		continue
	else
		if [ "${cmd_line[i]}" = "op_restore" ]; then
			print_hint "OP_Code:           op_restore"
			P82M_CODE_OPT=${cmd_line[i]}
			unset cmd_line[i]
			let i=$i+1
			break
		elif [ "${cmd_line[i]}" = "op_update" ]; then
			print_hint "OP_Code:           op_update"
			P82M_CODE_OPT=${cmd_line[i]}
			unset cmd_line[i]
			let i=$i+1
			break
		fi
	fi
done

# Parsing command line parameter, such as platform, branch, project name,
# and AP-build-parameters.

if ! parse_cmd_line; then
	exit 1
fi

if [ "${P82M_CODE_OPT}" = "op_restore" ]; then
	print_wait "Restore code"
	if [ "$P82M_MACHINE" = "mt6735m" ]; then
		restore_mt6735m
	fi
	if [ "$P82M_MACHINE" = "tsp7510" ]; then
		restore_tsp7510
	fi
	if [ "$P82M_MACHINE" = "tsp7520m" ]; then
		restore_tsp7520m
	fi
	if [ "$P82M_MACHINE" = "tsp7520" ]; then
		restore_tsp7520
	fi
elif [ "${P82M_CODE_OPT}" = "op_update" ]; then
	print_wait "Update code"
	if [ "$P82M_MACHINE" = "mt6735m" ]; then
		update_mt6735m
	fi
	if [ "$P82M_MACHINE" = "tsp7510" ]; then
		update_tsp7510
	fi
	if [ "$P82M_MACHINE" = "tsp7520m" ]; then
		update_tsp7520m
	fi
	if [ "$P82M_MACHINE" = "tsp7520" ]; then
		update_tsp7520
	fi
fi

exit 0



