#/bin/bash

bool_project=0
bool_machine=0
bool_base=0
bool_type=0
bool_inherit=0
type="model"
project=""
base=""
machine=""

size="0"
if [ -e ./last_create.log ]; then
	size=`ls -l ./last_create.log | awk '{print $5}'`
	if [ "${size}" -ge "2048" ]; then
		echo "" > ./last_create.log
	fi
fi
	
echo "$0 $*" >> ./last_create.log

abs_path=$(cd $(dirname $0); pwd)

usage()
{
	echo -ne "Usage: ./create_project OPTIONS\n"
	echo -ne "\t e.g. ./create_project -m mdm9x25 -p TESTZM8620 -b ZM8630\n"
	echo -ne "\t e.g. ./create_project -m tsp7520m -p BEELINEMF833T -b MF833T -i\n"
	echo -ne "OPTIONS:\n"
	echo -ne "\t-m MACHINE, --machine=MACHINE (something as mdm9x25/tsp7520m)\n"
	echo -ne "\t-p PROJECT_NAME, --project=PROJECT_NAME\n"
	echo -ne "\t-t type, --type=model|custom\n"
	echo -ne "\t[-b base_project_name, --base=base_project_name]\n"
	echo -ne "\t[-i|--inherit] It is not supported for mdm9x25/mdm9x30\n"
	echo -ne "\t[-h, --help]\n"
	exit 1
}

parser=$(getopt -o m:p:b:t:hi --long project:,machine:,base:,type:,help,inherit -n $(basename $0) -- "$@")

if [ "$?" != "0" ]; then
	usage
	exit 1
fi

eval set -- "${parser}"

create_qc_empty_project()
{
	base_project_dir=""
	cur_project_dir=""

	if [ "${bool_base}" = "1" ]; then
		unset base_project_dir
		base_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        	do
		                for p in \`ls ${abs_path}/cust/$device_model\`;
		                do
		                        echo "$device_model/$p";
		                done;
		        done | grep "\b\w\+/${base}\b"`
	fi
	if [ "${type}" = "custom" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	else
		rm -rf cust/${project}/${project}
		mkdir -p cust/${project}/${project}
	fi
	unset cur_project_dir
	cur_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        do
	                for p in \`ls ${abs_path}/cust/$device_model\`;
	                do
	                        echo "$device_model/$p";
	                done;
	        done | grep "\b\w\+/${project}\b"`
	if [ "${bool_base}" = "1" ]; then
		echo "base_project_dir=${base_project_dir}"
	fi
	echo "cur_project_dir=${cur_project_dir}"
	echo "Create empty project."
	mkdir -p cust/${cur_project_dir}/ap_config
	echo "# bb-list for zte application packages." > cust/${cur_project_dir}/ap_config/applications.inc
	echo "# bb-list for zte adapter packages." > cust/${cur_project_dir}/ap_config/adapter.inc
	echo "# This file contains the method of making recovery boot image." > cust/${cur_project_dir}/ap_config/make-recovery-bootimg-additional.inc
	echo "# bb-list for zte recovery-application packages." > cust/${cur_project_dir}/ap_config/recovery-applications.inc
	echo "# bb-list for zte recovery-adapter packages." > cust/${cur_project_dir}/ap_config/recovery-adapter.inc
	echo "#ifndef	CUSTZTE_H" > cust/${cur_project_dir}/ap_config/custzte.h
	echo "#define	CUSTZTE_H" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "#endif" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "<conf>" > cust/${cur_project_dir}/ap_config/options.xml
	echo "</conf>" >> cust/${cur_project_dir}/ap_config/options.xml
	if [ "${machine}" = "mdm9x25" ]; then
		echo "mpss:LAAAANAZ" > cust/${cur_project_dir}/BUILDID_LIST.cfg
		echo "rpm:AAAAANAAR" >> cust/${cur_project_dir}/BUILDID_LIST.cfg
		echo "tz:AAEAANBAA" >> cust/${cur_project_dir}/BUILDID_LIST.cfg
		echo -e "sdi:AAAAANAAA\n" >> cust/${cur_project_dir}/BUILDID_LIST.cfg
	fi
	mkdir -p cust/${cur_project_dir}/code_replaced_with/apps_proc
	touch cust/${cur_project_dir}/code_replaced_with/apps_proc/flag
	mkdir -p cust/${cur_project_dir}/code_replaced_with/zte_sdk
	touch cust/${cur_project_dir}/code_replaced_with/zte_sdk/flag
	mkdir -p cust/${cur_project_dir}/script
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_after_build_apss
	chmod 755 cust/${cur_project_dir}/script/custom_actions_after_build_apss
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_build_apss
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_build_apss
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_copy_apss_files
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_copy_apss_files
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_build_common
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_build_common
	if [ "${machine}" = "mdm9x40" -o "${machine}" = "mdm9x50" -o "${machine}" = "mdm9x07" ]; then
		echo "/* Here is the ${machine} base direcotry */" > cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "./platform_sdk/qualcomm/mdm9xxx.le/code/apps_proc\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "/* Here is directory of project modifications*/\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/apps_proc\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "/*\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne " */\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "/*\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne " * Here is the list which contains the files in original ${machine} those\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne " */\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_mdm.desc
		echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_mdm.desc
	fi
	if [ "${machine}" = "mdm9x40" -o "${machine}" = "mdm9x50" -o "${machine}" = "mdm9x07" ]; then
		echo "/* Here is the zte sdk base directory */" > cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "./zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "/* Here is directory of makefile modifications and project modifications for zte sdk */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne " * Here is the list which contains the files in original zte sdk those\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "build/qualcomm/mdm9xxx.le/applications/zte-apps/zte-topsw-mmi/zte-topsw-mmi_git.bb\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "build/qualcomm/mdm9xxx.le/applications/zte-apps/zte-topsw-mmi-mf98/zte-topsw-mmi-mf98_git.bb\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "build/qualcomm/mdm9xxx.le/applications/zte-apps/zte-topsw-dlna/zte-topsw-dlna_git.bb\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
		echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	fi
	export cur_project_dir=${cur_project_dir}
	if [ "${bool_base}" = "1" ]; then
		export base_project_dir=${base_project_dir}
	fi
	return 0
}

create_qc_legacy_project()
{
	mask=""
	cur_project_dir_masked=""

	if ! create_qc_empty_project; then
		echo "create_qc_empty_project failed."
		return 1
	fi
	if [ "${bool_base}" = "1" ]; then
		if [ "${bool_inherit}" = "1" ]; then
			echo "inherit not implemented."
		else
			cp -rvf cust/${base_project_dir}/* cust/${cur_project_dir}/
			find cust/${cur_project_dir}/ -type d -name .svn | xargs rm -rf
			if [ "${machine}" = "mdm9x30" ]; then
				mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
				cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
				sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/script/custom_actions_before_build_apss
				sed -i -e "s/q6_elf_prefix=\"${base}\"/q6_elf_prefix=\"${project}\"/g" cust/${cur_project_dir}/script/custom_actions_before_build_apss
			fi
		fi
	fi
	return 0
}

create_qc_project()
{
	mask=""
	cur_project_dir_masked=""

	if ! create_qc_empty_project; then
		echo "create_qc_empty_project failed."
		return 1
	fi
	if [ "${bool_base}" = "1" ]; then
		if [ "${bool_inherit}" = "1" ]; then
			if [ "${machine}" = "mdm9x40" -o "${machine}" = "mdm9x50" -o "${machine}" = "mdm9x07" ]; then
				mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
				cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
				sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/apps_proc" cust/${cur_project_dir}/rep_mdm.desc
				sed -i -e "/REP_SRC_LIST:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_mdm.desc
				sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/zte_sdk" cust/${cur_project_dir}/rep_zte_sdk.desc
				sed -i -e "/REP_SRC_LIST:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_zte_sdk.desc
				cp -rvf cust/${base_project_dir}/ap_config/* cust/${cur_project_dir}/ap_config/
				find cust/${cur_project_dir}/ap_config/ -type d -name .svn | xargs rm -rf
				cp -rvf cust/${base_project_dir}/script/* cust/${cur_project_dir}/script/
				find cust/${cur_project_dir}/script/ -type d -name .svn | xargs rm -rf
			else
				echo "inherit not implemented."
			fi
		else
			cp -rvf cust/${base_project_dir}/* cust/${cur_project_dir}/
			find cust/${cur_project_dir}/ -type d -name .svn | xargs rm -rf
			if [ "${machine}" = "mdm9x40" -o "${machine}" = "mdm9x50" -o "${machine}" = "mdm9x07" ]; then
				mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
				cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
				sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/script/custom_actions_before_build_apss
				sed -i -e "s/q6_elf_prefix=\"${base}\"/q6_elf_prefix=\"${project}\"/g" cust/${cur_project_dir}/script/custom_actions_before_build_apss
				sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_zte_sdk.desc
				sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_mdm.desc
			fi
		fi
	fi
	return 0
}

create_tsp7520_empty_project()
{
	base_project_dir=""
	cur_project_dir=""

	if [ "${bool_base}" = "1" ]; then
		unset base_project_dir
		base_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        	do
		                for p in \`ls ${abs_path}/cust/$device_model\`;
		                do
		                        echo "$device_model/$p";
		                done;
		        done | grep "\b\w\+/${base}\b"`
	fi
	if [ "${type}" = "custom" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	elif [ "${type}" = "amt" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	else
		rm -rf cust/${project}/${project}
		mkdir -p cust/${project}/${project}
	fi
	unset cur_project_dir
	cur_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        do
	                for p in \`ls ${abs_path}/cust/$device_model\`;
	                do
	                        echo "$device_model/$p";
	                done;
	        done | grep "\b\w\+/${project}\b"`
	if [ "${bool_base}" = "1" ]; then
		echo "base_project_dir=${base_project_dir}"
	fi
	echo "cur_project_dir=${cur_project_dir}"
	echo "Create empty project."
	mkdir -p cust/${cur_project_dir}/ap_config
	touch cust/${cur_project_dir}/ap_config/applications.inc
	touch cust/${cur_project_dir}/ap_config/adapter.inc
	touch cust/${cur_project_dir}/ap_config/recovery-applications.inc
	touch cust/${cur_project_dir}/ap_config/recovery-adapter.inc
	touch cust/${cur_project_dir}/ap_config/amt-adapter.inc
	touch cust/${cur_project_dir}/ap_config/amt-applications.inc
	touch cust/${cur_project_dir}/ap_config/rwfs_exec_bin_list.inc
	echo "#ifndef	CUSTZTE_H" > cust/${cur_project_dir}/ap_config/custzte.h
	echo "#define	CUSTZTE_H" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "#endif" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "<conf>" > cust/${cur_project_dir}/ap_config/options.xml
	echo "</conf>" >> cust/${cur_project_dir}/ap_config/options.xml
	mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520
	touch cust/${cur_project_dir}/code_replaced_with/tsp7520/flag
	mkdir -p cust/${cur_project_dir}/code_replaced_with/zte_sdk
	touch cust/${cur_project_dir}/code_replaced_with/zte_sdk/flag
	mkdir -p cust/${cur_project_dir}/script
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_after_build_ap
	chmod 755 cust/${cur_project_dir}/script/custom_actions_after_build_ap
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_build_ap
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_build_ap
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files

	echo "/* Here is the ${machine} base direcotry */" > cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "./platform_sdk/tsp/tsp7520/code/base\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "/* Here is directory of project modifications*/\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "COMMON:./platform_sdk/tsp/tsp7520/code/common\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/tsp7520\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_SRC_FILTER:COMMON:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_SRC_FILTER:COMMON:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne " * Here is the list which contains the files in original ${machine} those\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520.desc
	echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520.desc

	echo "/* Here is the zte sdk base directory */" > cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "./zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/* Here is directory of makefile modifications and project modifications for zte sdk */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TSP7520_MK:./zte_sdk/build/tsp/tsp7520/ZTE_Build\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * replacing (for makefile)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:TSP7520_MK:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:TSP7520_MK:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files in original zte sdk those\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc

	export cur_project_dir=${cur_project_dir}
	if [ "${bool_base}" = "1" ]; then
		export base_project_dir=${base_project_dir}
	fi
	return 0
}

create_tsp7520_project()
{
	mask=""
	cur_project_dir_masked=""

	if ! create_tsp7520_empty_project; then
		echo "create_tsp7520_empty_project failed."
		return 1
	fi
	if [ "${bool_base}" = "1" ]; then
		if [ "${bool_inherit}" = "1" ]; then
			mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
			cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
			sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/tsp7520" cust/${cur_project_dir}/rep_tsp7520.desc
			sed -i -e "/REP_SRC_FILTER:COMMON:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_tsp7520.desc
			sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/zte_sdk" cust/${cur_project_dir}/rep_zte_sdk.desc
			sed -i -e "/REP_SRC_FILTER:TSP7520_MK:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_zte_sdk.desc
			cp -rvf cust/${base_project_dir}/ap_config/* cust/${cur_project_dir}/ap_config/
			find cust/${cur_project_dir}/ap_config/ -type d -name .svn | xargs rm -rf
			cp -rvf cust/${base_project_dir}/script/* cust/${cur_project_dir}/script/
			find cust/${cur_project_dir}/script/ -type d -name .svn | xargs rm -rf
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			sed -i -e "/echo remove ok/d" cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "rm -rfv \${P82M_ABS_PATH}/cust/${base_project_dir}/code_replaced_with/tsp7520/prebuild/rootfs/jffs2/usr/zte_web/web" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "ls -al \${P82M_ABS_PATH}/cust/${base_project_dir}/code_replaced_with/tsp7520/prebuild/rootfs/jffs2/usr/zte_web" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "echo remove ok" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			if [ "${type}" = "amt" ]; then
				echo -ne "# application source code path for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n" > cust/${cur_project_dir}/ap_config/applications.inc
				echo -ne "OEM_SPESIFIC_LIBDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/applications.inc
				echo -ne "OEM_SPESIFIC_APPDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/applications.inc
				echo -ne "# adapter source code path for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n" > cust/${cur_project_dir}/ap_config/adapter.inc
				echo -ne "OEM_SPESIFIC_ADAPTLIBDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/adapter.inc
				echo -ne "OEM_SPESIFIC_ADAPTAPPDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/adapter.inc
				echo -ne "# adapter source code path (amt mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/amt-adapter.inc
				echo -ne "# application source code path (amt mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/amt-applications.inc
				echo -ne "#ifndef ${project}_H\n#define ${project}_H\n\n\n#endif\n\n\n" > cust/${cur_project_dir}/ap_config/custzte.h
				echo -ne "# application source code path (recovery mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/recovery-applications.inc
				echo -ne "zte_topsw_dua\nupd_util" > cust/${cur_project_dir}/ap_config/rwfs_exec_bin_list.inc
				echo -ne "echo custom_actions_before_copy_apss_files\npwd" > cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
				cp -rvf amt_files/legacy/code_replaced_with/* cust/${cur_project_dir}/code_replaced_with/
				is903=""
				is903=`echo ${project} | grep "MF903"`
				if [ "${is903}" != "" ]; then
					sed -i -e "s/CA_MF79S_LM/CA_YN_MF910W/g" cust/${cur_project_dir}/code_replaced_with/tsp7520/Makefile
					sed -i -e "s/\t@cp -v \$(BOOTLOADER_DIR)\/cpe_tloader.bin.*/#\t@cp -v \$(BOOTLOADER_DIR)\/ufi_tloader.bin                            \$(P82M_ABS_PATH)\/images\/\$(P82M_PROJECT)\/amtallbins\//g" cust/${cur_project_dir}/code_replaced_with/tsp7520/Makefile
					sed -i -e "s/\t@cp -v \$(BOOTLOADER_DIR)\/cpe_tboot.bin.*/#\t@cp -v \$(BOOTLOADER_DIR)\/ufi_tloader.bin                              \$(P82M_ABS_PATH)\/images\/\$(P82M_PROJECT)\/amtallbins\//g" cust/${cur_project_dir}/code_replaced_with/tsp7520/Makefile
				fi
			fi
		else
			cp -rvf cust/${base_project_dir}/* cust/${cur_project_dir}/
			find cust/${cur_project_dir}/ -type d -name .svn | xargs rm -rf
			mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
			cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_zte_sdk.desc
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_tsp7520.desc
			mv cust/${cur_project_dir}/code_replaced_with/tsp7520/CPU_R7/amt/${base} cust/${cur_project_dir}/code_replaced_with/tsp7520/CPU_R7/amt/${project}
			mv cust/${cur_project_dir}/code_replaced_with/tsp7520/CPU_R7/project/zx297520/ref/${base} cust/${cur_project_dir}/code_replaced_with/tsp7520/CPU_R7/project/zx297520/ref/${project}
			mv cust/${cur_project_dir}/code_replaced_with/tsp7520/CPU_R7/project/zx297520/config/${base} cust/${cur_project_dir}/code_replaced_with/tsp7520/CPU_R7/project/zx297520/config/${project}
		fi
	fi
	return 0
}

create_tsp7520m_empty_project()
{
	base_project_dir=""
	cur_project_dir=""

	if [ "${bool_base}" = "1" ]; then
		unset base_project_dir
		base_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        	do
		                for p in \`ls ${abs_path}/cust/$device_model\`;
		                do
		                        echo "$device_model/$p";
		                done;
		        done | grep "\b\w\+/${base}\b"`
	fi
	if [ "${type}" = "custom" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	elif [ "${type}" = "amt" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	else
		rm -rf cust/${project}/${project}
		mkdir -p cust/${project}/${project}
	fi
	unset cur_project_dir
	cur_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        do
	                for p in \`ls ${abs_path}/cust/$device_model\`;
	                do
	                        echo "$device_model/$p";
	                done;
	        done | grep "\b\w\+/${project}\b"`
	if [ "${bool_base}" = "1" ]; then
		echo "base_project_dir=${base_project_dir}"
	fi
	echo "cur_project_dir=${cur_project_dir}"
	echo "Create empty project."
	mkdir -p cust/${cur_project_dir}/ap_config
	touch cust/${cur_project_dir}/ap_config/applications.inc
	touch cust/${cur_project_dir}/ap_config/adapter.inc
	touch cust/${cur_project_dir}/ap_config/recovery-applications.inc
	touch cust/${cur_project_dir}/ap_config/recovery-adapter.inc
	touch cust/${cur_project_dir}/ap_config/amt-adapter.inc
	touch cust/${cur_project_dir}/ap_config/amt-applications.inc
	touch cust/${cur_project_dir}/ap_config/rwfs_exec_bin_list.inc
	echo "#ifndef	CUSTZTE_H" > cust/${cur_project_dir}/ap_config/custzte.h
	echo "#define	CUSTZTE_H" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "#endif" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "<conf>" > cust/${cur_project_dir}/ap_config/options.xml
	echo "</conf>" >> cust/${cur_project_dir}/ap_config/options.xml
	mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520m
	touch cust/${cur_project_dir}/code_replaced_with/tsp7520m/flag
	mkdir -p cust/${cur_project_dir}/code_replaced_with/zte_sdk
	touch cust/${cur_project_dir}/code_replaced_with/zte_sdk/flag
	mkdir -p cust/${cur_project_dir}/script
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_after_build_ap
	chmod 755 cust/${cur_project_dir}/script/custom_actions_after_build_ap
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_build_ap
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_build_ap
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files

	echo "/* Here is the ${machine} base direcotry */" > cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "./platform_sdk/tsp/tsp7520m/code/base\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "/* Here is directory of project modifications*/\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/tsp7520m\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne " * Here is the list which contains the files in original ${machine} those\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc
	echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520m.desc

	echo "/* Here is the zte sdk base directory */" > cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "./zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/* Here is directory of makefile modifications and project modifications for zte sdk */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TSP7520M_MK:./zte_sdk/build/tsp/tsp7520m/ZTE_Build\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * replacing (for makefile)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:TSP7520M_MK:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:TSP7520M_MK:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files in original zte sdk those\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc

	export cur_project_dir=${cur_project_dir}
	if [ "${bool_base}" = "1" ]; then
		export base_project_dir=${base_project_dir}
	fi
	return 0
}

create_tsp7520m_project()
{
	mask=""
	cur_project_dir_masked=""

	if ! create_tsp7520m_empty_project; then
		echo "create_tsp7520m_empty_project failed."
		return 1
	fi
	if [ "${bool_base}" = "1" ]; then
		if [ "${bool_inherit}" = "1" ]; then
			mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
			cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
			sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/tsp7520m" cust/${cur_project_dir}/rep_tsp7520m.desc
			sed -i -e "/REP_SRC_LIST:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_tsp7520m.desc
			sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/zte_sdk" cust/${cur_project_dir}/rep_zte_sdk.desc
			sed -i -e "/REP_SRC_FILTER:TSP7520M_MK:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_zte_sdk.desc
			cp -rvf cust/${base_project_dir}/ap_config/* cust/${cur_project_dir}/ap_config/
			find cust/${cur_project_dir}/ap_config/ -type d -name .svn | xargs rm -rf
			cp -rvf cust/${base_project_dir}/script/* cust/${cur_project_dir}/script/
			find cust/${cur_project_dir}/script/ -type d -name .svn | xargs rm -rf
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			sed -i -e "/echo remove ok/d" cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "rm -rfv \${P82M_ABS_PATH}/cust/${base_project_dir}/code_replaced_with/tsp7520m/prebuild/rootfs/normal/jffs2/usr/zte_web/web" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "ls -al \${P82M_ABS_PATH}/cust/${base_project_dir}/code_replaced_with/tsp7520m/prebuild/rootfs/normal/jffs2/usr/zte_web" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "echo remove ok" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			if [ "${type}" = "amt" ]; then
				echo -ne "# application source code path for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n" > cust/${cur_project_dir}/ap_config/applications.inc
				echo -ne "OEM_SPESIFIC_LIBDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/applications.inc
				echo -ne "OEM_SPESIFIC_APPDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/applications.inc
				echo -ne "# adapter source code path for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n" > cust/${cur_project_dir}/ap_config/adapter.inc
				echo -ne "OEM_SPESIFIC_ADAPTLIBDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/adapter.inc
				echo -ne "OEM_SPESIFIC_ADAPTAPPDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/adapter.inc
				echo -ne "# adapter source code path (amt mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/amt-adapter.inc
				echo -ne "# application source code path (amt mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/amt-applications.inc
				echo -ne "#ifndef ${project}_H\n#define ${project}_H\n\n\n#endif\n\n\n" > cust/${cur_project_dir}/ap_config/custzte.h
				echo -ne "# application source code path (recovery mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/recovery-applications.inc
				echo -ne "zte_topsw_dua\nupd_util" > cust/${cur_project_dir}/ap_config/rwfs_exec_bin_list.inc
				echo -ne "echo custom_actions_before_copy_apss_files\npwd" > cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
				cp -rvf amt_files/sdk/code_replaced_with/* cust/${cur_project_dir}/code_replaced_with/
			else
				mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520m/prebuild/nvbins
				touch cust/${cur_project_dir}/code_replaced_with/tsp7520m/prebuild/nvbins/flag
				mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520m/prebuild/pcdriver
				touch cust/${cur_project_dir}/code_replaced_with/tsp7520m/prebuild/pcdriver/flag
				mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520m/prebuild/rootfs/normal/jffs2/usr/zte_web
				touch cust/${cur_project_dir}/code_replaced_with/tsp7520m/prebuild/rootfs/normal/jffs2/usr/zte_web/flag
				mkdir -p cust/${cur_project_dir}/code_replaced_with/zte_sdk/applications/zte_apps/zte_conf/config
				touch cust/${cur_project_dir}/code_replaced_with/zte_sdk/applications/zte_apps/zte_conf/config/flag
			fi
		else
			cp -rvf cust/${base_project_dir}/* cust/${cur_project_dir}/
			find cust/${cur_project_dir}/ -type d -name .svn | xargs rm -rf
			mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
			cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_zte_sdk.desc
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_tsp7520m.desc
		fi
	fi
	return 0
}
create_tsp7520v3_empty_project()
{
	base_project_dir=""
	cur_project_dir=""

	if [ "${bool_base}" = "1" ]; then
		unset base_project_dir
		base_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        	do
		                for p in \`ls ${abs_path}/cust/$device_model\`;
		                do
		                        echo "$device_model/$p";
		                done;
		        done | grep "\b\w\+/${base}\b"`
	fi
	if [ "${type}" = "custom" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	elif [ "${type}" = "amt" ]; then
		rm -rf cust/${base_project_dir%/*}/${project}
		mkdir -p cust/${base_project_dir%/*}/${project}
	else
		rm -rf cust/${project}/${project}
		mkdir -p cust/${project}/${project}
	fi
	unset cur_project_dir
	cur_project_dir=`for device_model in \`ls ${abs_path}/cust\`;
	        do
	                for p in \`ls ${abs_path}/cust/$device_model\`;
	                do
	                        echo "$device_model/$p";
	                done;
	        done | grep "\b\w\+/${project}\b"`
	if [ "${bool_base}" = "1" ]; then
		echo "base_project_dir=${base_project_dir}"
	fi
	echo "cur_project_dir=${cur_project_dir}"
	echo "Create empty project."
	mkdir -p cust/${cur_project_dir}/ap_config
	touch cust/${cur_project_dir}/ap_config/applications.inc
	touch cust/${cur_project_dir}/ap_config/adapter.inc
	touch cust/${cur_project_dir}/ap_config/recovery-applications.inc
	touch cust/${cur_project_dir}/ap_config/recovery-adapter.inc
	echo "#ifndef	CUSTZTE_H" > cust/${cur_project_dir}/ap_config/custzte.h
	echo "#define	CUSTZTE_H" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "#endif" >> cust/${cur_project_dir}/ap_config/custzte.h
	echo "<conf>" > cust/${cur_project_dir}/ap_config/options.xml
	echo "</conf>" >> cust/${cur_project_dir}/ap_config/options.xml
	mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520v3
	touch cust/${cur_project_dir}/code_replaced_with/tsp7520v3/flag
	mkdir -p cust/${cur_project_dir}/code_replaced_with/zte_sdk
	touch cust/${cur_project_dir}/code_replaced_with/zte_sdk/flag
	mkdir -p cust/${cur_project_dir}/script
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_after_build_ap
	chmod 755 cust/${cur_project_dir}/script/custom_actions_after_build_ap
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_build_ap
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_build_ap
	echo -e "#/bin/bash\n" > cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
	chmod 755 cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files

	echo "/* Here is the ${machine} base direcotry */" > cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "./platform_sdk/tsp/tsp7520v3/code\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "/* Here is directory of project modifications*/\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "BASETSPCODE:./platform_sdk/tsp/tsp7520v3/replace_code\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/tsp7520v3\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne " * Here is the list which contains the files in original ${machine} those\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc
	echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_tsp7520v3.desc

	echo "/* Here is the zte sdk base directory */" > cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_DST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "./zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_DST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/* Here is directory of makefile modifications and project modifications for zte sdk */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_LIST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TSP7520V3_MK:./zte_sdk/build/tsp/tsp7520v3/ZTE_Build\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "CUST:./cust/${cur_project_dir}/code_replaced_with/zte_sdk\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_LIST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * replacing (for makefile)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:TSP7520M_MK:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:TSP7520M_MK:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files that should not be used for\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * replacing (for project)\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:CUST:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "REP_SRC_FILTER:CUST:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "/*\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * Here is the list which contains the files in original zte sdk those\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " * should be removed when building.\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne " */\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TARGET_REMOVE:BEGIN\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc
	echo -ne "TARGET_REMOVE:END\n\n" >> cust/${cur_project_dir}/rep_zte_sdk.desc

	export cur_project_dir=${cur_project_dir}
	if [ "${bool_base}" = "1" ]; then
		export base_project_dir=${base_project_dir}
	fi
	return 0
}

create_tsp7520v3_project()
{
	mask=""
	cur_project_dir_masked=""

	if ! create_tsp7520v3_empty_project; then
		echo "create_tsp7520v3_empty_project failed."
		return 1
	fi
	if [ "${bool_base}" = "1" ]; then
		if [ "${bool_inherit}" = "1" ]; then
			mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
			cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
			sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/tsp7520v3" cust/${cur_project_dir}/rep_tsp7520v3.desc
			sed -i -e "/REP_SRC_LIST:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_tsp7520v3.desc
			sed -i -e "/CUST:\.\/cust\/${cur_project_dir_masked}/iBASECUST:\.\/cust\/${mask}\/code_replaced_with\/zte_sdk" cust/${cur_project_dir}/rep_zte_sdk.desc
			sed -i -e "/REP_SRC_FILTER:TSP7520M_MK:END/a\/\*\n \* Here is the list which contains the files that should not be used for\n \* replacing \(for base project\)\n \*\/\nREP_SRC_FILTER:BASECUST:BEGIN\nREP_SRC_FILTER:BASECUST:END" cust/${cur_project_dir}/rep_zte_sdk.desc
			cp -rvf cust/${base_project_dir}/ap_config/* cust/${cur_project_dir}/ap_config/
			find cust/${cur_project_dir}/ap_config/ -type d -name .svn | xargs rm -rf
			cp -rvf cust/${base_project_dir}/script/* cust/${cur_project_dir}/script/
			find cust/${cur_project_dir}/script/ -type d -name .svn | xargs rm -rf
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			sed -i -e "/echo remove ok/d" cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "rm -rfv \${P82M_ABS_PATH}/cust/${base_project_dir}/code_replaced_with/tsp7520v3/project/zx297520v3/prj_mifi/rootfs/normal/usr/zte_web/web" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "ls -al \${P82M_ABS_PATH}/cust/${base_project_dir}/code_replaced_with/tsp7520v3/project/zx297520v3/prj_mifi/rootfs/normal/usr/zte_web" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			echo "echo remove ok" >> cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			##if [ "${type}" = "amt" ]; then
			##	echo -ne "# application source code path for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n" > cust/${cur_project_dir}/ap_config/applications.inc
			##	echo -ne "OEM_SPESIFIC_LIBDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/applications.inc
			##	echo -ne "OEM_SPESIFIC_APPDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/applications.inc
			##	echo -ne "# adapter source code path for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n" > cust/${cur_project_dir}/ap_config/adapter.inc
			##	echo -ne "OEM_SPESIFIC_ADAPTLIBDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/adapter.inc
			##	echo -ne "OEM_SPESIFIC_ADAPTAPPDIRS += \n\n" >> cust/${cur_project_dir}/ap_config/adapter.inc
			##	echo -ne "# adapter source code path (amt mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/amt-adapter.inc
			##	echo -ne "# application source code path (amt mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/amt-applications.inc
			##	echo -ne "#ifndef ${project}_H\n#define ${project}_H\n\n\n#endif\n\n\n" > cust/${cur_project_dir}/ap_config/custzte.h
			##	echo -ne "# application source code path (recovery mode) for zte.\n# Created by Ruinan Duan  12-24-2014\n#\n\n\n\n\n\n\n\n" > cust/${cur_project_dir}/ap_config/recovery-applications.inc
			##	echo -ne "zte_topsw_dua\nupd_util" > cust/${cur_project_dir}/ap_config/rwfs_exec_bin_list.inc
			##	echo -ne "echo custom_actions_before_copy_apss_files\npwd" > cust/${cur_project_dir}/script/custom_actions_before_copy_ap_files
			##	cp -rvf amt_files/sdk/code_replaced_with/* cust/${cur_project_dir}/code_replaced_with/
			##else
				mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520v3/copyfiles/nvbins
				touch cust/${cur_project_dir}/code_replaced_with/tsp7520v3/copyfiles/nvbins/flag
				mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520v3/linux_plat/mod/PC_Driver
				touch cust/${cur_project_dir}/code_replaced_with/tsp7520v3/linux_plat/mod/PC_Driver/flag
				mkdir -p cust/${cur_project_dir}/code_replaced_with/tsp7520v3/project/zx297520v3/prj_mifi/rootfs/normal/usr/zte_web
				touch cust/${cur_project_dir}/code_replaced_with/tsp7520v3/project/zx297520v3/prj_mifi/rootfs/normal/usr/zte_web/flag
				mkdir -p cust/${cur_project_dir}/code_replaced_with/zte_sdk/applications/zte_apps/zte_conf/config
				touch cust/${cur_project_dir}/code_replaced_with/zte_sdk/applications/zte_apps/zte_conf/config/flag
			##fi
		else
			cp -rvf cust/${base_project_dir}/* cust/${cur_project_dir}/
			find cust/${cur_project_dir}/ -type d -name .svn | xargs rm -rf
			mask=$(echo "${base_project_dir}" | sed -e 's#\/#\\\/#g')
			cur_project_dir_masked=$(echo "${cur_project_dir}" | sed -e 's#\/#\\\/#g')
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_zte_sdk.desc
			sed -i -e "s/${mask}/${cur_project_dir_masked}/g" cust/${cur_project_dir}/rep_tsp7520v3.desc
		fi
	fi
	return 0
}
while true; do
	case "$1" in
		-m|--machine)
			echo -ne "MACHINE: $2\n"
			bool_machine=1
			machine="$2"
			shift 2
			;;
		-p|--project)
			echo -ne "PROJECT: $2\n"
			bool_project=1
			project="$2"
			shift 2
			;;
		-t|--type)
			if [ "$2" != "model" -a "$2" != "custom" -a "$2" != "amt" ]; then
				echo "Invalid \"type\", \"type\" can be set as \"model\", \"custom\" or \"amt\"."
				shift 2
				exit 1
			fi
			echo -ne "TYPE: $2\n"
			bool_type=1
			type="$2"
			shift 2
			;;
		-b|--base)
			echo -ne "BASE PROJECT: $2\n"
			bool_base=1
			base="$2"
			shift 2
			;;
		-i|--inherit)
			echo -ne "Inherited from base project\n"
			bool_inherit=1
			shift 1
			;;
		-h|--help)
			usage
			shift
			;;
		--)
			shift
			break
			;;
		*)
			echo -ne "Internal error.\n"
			exit 1
	esac
done

if [ "${bool_project}" = "0" ]; then
	echo "You must set project!"
	usage
	exit 1
fi

if [ "${bool_machine}" = "0" ]; then
	echo "You must set machine!"
	usage
	exit 1
fi

if [ "${bool_type}" = "0" ]; then
	echo "You must set type!"
	usage
	exit 1
fi

if [ "${type}" = "custom" ]; then
	if [ "${bool_base}" = "0" ]; then
		echo "You must set a base project."
		exit 1
	fi
fi

if [ "${type}" = "amt" ]; then
	if [ "${bool_base}" = "0" ]; then
		echo "You must set a base project for amt project."
		exit 1
	fi
fi

if [ "${bool_inherit}" = "1" ]; then
	if [ "${bool_base}" = "0" ]; then
		echo "You must specify a base project, use \"-b|--base\"."
		exit 1
	fi
	if [ "${machine}" = "mdm9x25" ]; then
		echo "\"-i|--inherit\" is not supported for mdm9x25."
		exit 1
	fi
	if [ "${machine}" = "mdm9x30" ]; then
		echo "\"-i|--inherit\" is not supported for mdm9x30."
		exit 1
	fi
fi

if [ "${machine}" = "mdm9x25" -o "${machine}" = "mdm9x30" ]; then
	if ! create_qc_legacy_project; then
		echo "create_qc_legacy_project failed."
		exit 1
	fi
	exit 0
fi
if [ "${machine}" = "mdm9x07" -o "${machine}" = "mdm9x40" -o "${machine}" = "mdm9x50" ]; then
	if ! create_qc_project; then
		echo "create_qc_project failed."
		exit 1
	fi
	exit 0
fi
if [ "${machine}" = "tsp7520" ]; then
	if ! create_tsp7520_project; then
		echo "create_tsp7520_project failed."
		exit 1
	fi
	exit 0
fi
if [ "${machine}" = "tsp7520m" ]; then
	if ! create_tsp7520m_project; then
		echo "create_tsp7520m_project failed."
		exit 1
	fi
	exit 0
fi
if [ "${machine}" = "tsp7520v3" ]; then
	if ! create_tsp7520v3_project; then
		echo "create_tsp7520v3_project failed."
		exit 1
	fi
	exit 0
fi

