#!/bin/bash
#
# platform_sdk_dl_qc_mdm9xxx: Qualcomm MDM9xxx Platform SDK download url parser
# Ruinan Duan 10/28/2014
#

QC_9XXX_SDK_BASE_DIR="platform_sdk/qualcomm/mdm9xxx.le/code"

# Make MDM9x15/MDM9x25/MDM9x30 platform SDK URL
mdm9xxx_url_parser()
{
	local cfg_file="conf/${P82M_MACHINE}_cfg.xml"
	local i
	local svn_auth=""

	script/dos2unix "$cfg_file"

	unset P82M_APSS_URL
	unset P82M_MPSS_URL
	unset P82M_COMMON_URL
	unset P82M_ADSP_URL
	unset P82M_CNSS_URL
	unset P82M_RPM_URL

	if [ "$P82M_SVN_USERNAME" != "" ]; then
		svn_auth=" --username $P82M_SVN_USERNAME --password $P82M_SVN_PASSWORD --no-auth-cache "
	else
		svn_auth=""
	fi

	if [ "$P82M_MACHINE" = "mdm9x25" -o "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_SDI_URL
	fi

	if [ "$P82M_MACHINE" = "mdm9x25" -o "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_TZ_URL
	fi

	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_ADSP_URL
	fi

	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_CNSS_URL
	fi

	unset P82M_BOOT_URL

	if [ "$P82M_MACHINE" = "mdm9x15" ]; then
		unset P82M_LPASS_URL
	fi

	unset P82M_CONTENTS_URL

	unset P82M_BRANCH_LIST
	P82M_BRANCH_LIST=(`sed -n 's/.*>\(.*\)<\/branch>/\1/p'\
				$cfg_file`)
	unset P82M_APSS_LIST
	P82M_APSS_LIST=(`sed -n 's/.*>\(.*\)<\/apss_addr>/\1/p'\
				$cfg_file`)
	unset P82M_APSS_DL_MODE_LIST
	P82M_APSS_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/apss_dl_mode>/\1/p'\
				$cfg_file`)
	unset P82M_APSS_REVISION_LIST
	P82M_APSS_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/apss_revision_num>/\1/p'\
				$cfg_file`)
	unset P82M_MPSS_LIST
	P82M_MPSS_LIST=(`sed -n 's/.*>\(.*\)<\/mpss_addr>/\1/p'\
				$cfg_file`)
	unset P82M_MPSS_DL_MODE_LIST
	P82M_MPSS_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/mpss_dl_mode>/\1/p'\
				$cfg_file`)
	unset P82M_MPSS_REVISION_LIST
	P82M_MPSS_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/mpss_revision_num>/\1/p'\
				$cfg_file`)
	unset P82M_COMMON_LIST
	P82M_COMMON_LIST=(`sed -n 's/.*>\(.*\)<\/common_addr>/\1/p'\
				$cfg_file`)
	unset P82M_COMMON_DL_MODE_LIST
	P82M_COMMON_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/common_dl_mode>/\1/p'\
				$cfg_file`)
	unset P82M_COMMON_REVISION_LIST
	P82M_COMMON_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/common_revision_num>/\1/p'\
				$cfg_file`)
	if [ "$P82M_MACHINE" = "mdm9x25" -o "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_SDI_LIST
		P82M_SDI_LIST=(`sed -n 's/.*>\(.*\)<\/sdi_addr>/\1/p'\
					$cfg_file`)
		unset P82M_SDI_DL_MODE_LIST
		P82M_SDI_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/sdi_dl_mode>/\1/p'\
					$cfg_file`)
		unset P82M_SDI_REVISION_LIST
		P82M_SDI_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/sdi_revision_num>/\1/p'\
					$cfg_file`)
	fi
	unset P82M_BOOT_LIST
	P82M_BOOT_LIST=(`sed -n 's/.*>\(.*\)<\/boot_addr>/\1/p'\
				$cfg_file`)
	unset P82M_BOOT_DL_MODE_LIST
	P82M_BOOT_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/boot_dl_mode>/\1/p'\
				$cfg_file`)
	unset P82M_BOOT_REVISION_LIST
	P82M_BOOT_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/boot_revision_num>/\1/p'\
				$cfg_file`)
	if [ "$P82M_MACHINE" = "mdm9x25" -o "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_TZ_LIST
		P82M_TZ_LIST=(`sed -n 's/.*>\(.*\)<\/tz_addr>/\1/p'\
					$cfg_file`)
		unset P82M_TZ_DL_MODE_LIST
		P82M_TZ_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/tz_dl_mode>/\1/p'\
					$cfg_file`)
		unset P82M_TZ_REVISION_LIST
		P82M_TZ_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/tz_revision_num>/\1/p'\
					$cfg_file`)
	fi
	unset P82M_RPM_LIST
	P82M_RPM_LIST=(`sed -n 's/.*>\(.*\)<\/rpm_addr>/\1/p'\
				$cfg_file`)
	unset P82M_RPM_DL_MODE_LIST
	P82M_RPM_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/rpm_dl_mode>/\1/p'\
				$cfg_file`)
	unset P82M_RPM_REVISION_LIST
	P82M_RPM_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/rpm_revision_num>/\1/p'\
				$cfg_file`)
	if [ "$P82M_MACHINE" = "mdm9x15" ]; then
		unset P82M_LPASS_LIST
		P82M_LPASS_LIST=(`sed -n 's/.*>\(.*\)<\/lpass_addr>/\1/p'\
					$cfg_file`)
		unset P82M_LPASS_DL_MODE_LIST
		P82M_LPASS_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/lpass_dl_mode>/\1/p'\
					$cfg_file`)
		unset P82M_LPASS_REVISION_LIST
		P82M_LPASS_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/lpass_revision_num>/\1/p'\
					$cfg_file`)
	fi

	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_ADSP_LIST
		P82M_ADSP_LIST=(`sed -n 's/.*>\(.*\)<\/adsp_addr>/\1/p'\
					$cfg_file`)
		unset P82M_ADSP_DL_MODE_LIST
		P82M_ADSP_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/adsp_dl_mode>/\1/p'\
					$cfg_file`)
		unset P82M_ADSP_REVISION_LIST
		P82M_ADSP_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/adsp_revision_num>/\1/p'\
					$cfg_file`)
	fi
	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		unset P82M_CNSS_LIST
		P82M_CNSS_LIST=(`sed -n 's/.*>\(.*\)<\/cnss_addr>/\1/p'\
					$cfg_file`)
		unset P82M_CNSS_DL_MODE_LIST
		P82M_CNSS_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/cnss_dl_mode>/\1/p'\
					$cfg_file`)
		unset P82M_CNSS_REVISION_LIST
		P82M_CNSS_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/cnss_revision_num>/\1/p'\
					$cfg_file`)
	fi

	unset P82M_CONTENTS_LIST
	P82M_CONTENTS_LIST=(`sed -n 's/.*>\(.*\)<\/contents_addr>/\1/p'\
				$cfg_file`)
	unset P82M_CONTENTS_DL_MODE_LIST
	P82M_CONTENTS_DL_MODE_LIST=(`sed -n 's/.*>\(.*\)<\/contents_dl_mode>/\1/p'\
				$cfg_file`)
	unset P82M_CONTENTS_REVISION_LIST
	P82M_CONTENTS_REVISION_LIST=(`sed -n 's/.*>\(.*\)<\/contents_revision_num>/\1/p'\
				$cfg_file`)

	for ((i=0; i < ${#P82M_BRANCH_LIST[@]}; i++)); do
		if [ "${P82M_BRANCH_LIST[i]}" = "$P82M_BRANCH" ]; then
			break
		else
			continue
		fi
	done

	if [ $i = ${#P82M_BRANCH_LIST[@]} ]; then
		print_fail "Cannot find branch $P82M_BRANCH"
		return 1
	fi

	if [ "${P82M_APSS_REVISION_LIST[i]}" = "null" ]; then
		P82M_APSS_URL="svn $svn_auth ${P82M_APSS_DL_MODE_LIST[i]} ${P82M_APSS_LIST[i]} --quiet"
	else
		P82M_APSS_URL="svn $svn_auth --revision ${P82M_APSS_REVISION_LIST[i]} ${P82M_APSS_DL_MODE_LIST[i]} ${P82M_APSS_LIST[i]} --quiet"
	fi

	if [ "${P82M_MPSS_REVISION_LIST[i]}" = "null" ]; then
		P82M_MPSS_URL="svn $svn_auth ${P82M_MPSS_DL_MODE_LIST[i]} ${P82M_MPSS_LIST[i]} --quiet"
	else
		P82M_MPSS_URL="svn $svn_auth --revision ${P82M_MPSS_REVISION_LIST[i]} ${P82M_MPSS_DL_MODE_LIST[i]} ${P82M_MPSS_LIST[i]} --quiet"
	fi

	if [ "${P82M_COMMON_REVISION_LIST[i]}" = "null" ]; then
		P82M_COMMON_URL="svn $svn_auth ${P82M_COMMON_DL_MODE_LIST[i]} ${P82M_COMMON_LIST[i]} --quiet"
	else
		P82M_COMMON_URL="svn $svn_auth --revision ${P82M_COMMON_REVISION_LIST[i]} ${P82M_COMMON_DL_MODE_LIST[i]} ${P82M_COMMON_LIST[i]} --quiet"
	fi

	if [ "$P82M_MACHINE" = "mdm9x25" -o "$P82M_MACHINE" = "mdm9x30" ]; then
		if [ "${P82M_SDI_REVISION_LIST[i]}" = "null" ]; then
			P82M_SDI_URL="svn $svn_auth ${P82M_SDI_DL_MODE_LIST[i]} ${P82M_SDI_LIST[i]} --quiet"
		else
			P82M_SDI_URL="svn $svn_auth --revision ${P82M_SDI_REVISION_LIST[i]} ${P82M_SDI_DL_MODE_LIST[i]} ${P82M_SDI_LIST[i]} --quiet"
		fi
	fi

	if [ "${P82M_RPM_REVISION_LIST[i]}" = "null" ]; then
		P82M_RPM_URL="svn $svn_auth ${P82M_RPM_DL_MODE_LIST[i]} ${P82M_RPM_LIST[i]} --quiet"
	else
		P82M_RPM_URL="svn $svn_auth --revision ${P82M_RPM_REVISION_LIST[i]} ${P82M_RPM_DL_MODE_LIST[i]} ${P82M_RPM_LIST[i]} --quiet"
	fi

	if [ "${P82M_BOOT_REVISION_LIST[i]}" = "null" ]; then
		P82M_BOOT_URL="svn $svn_auth ${P82M_BOOT_DL_MODE_LIST[i]} ${P82M_BOOT_LIST[i]} --quiet"
	else
		P82M_BOOT_URL="svn $svn_auth --revision ${P82M_BOOT_REVISION_LIST[i]} ${P82M_BOOT_DL_MODE_LIST[i]} ${P82M_BOOT_LIST[i]} --quiet"
	fi

	if [ "$P82M_MACHINE" = "mdm9x25" -o "$P82M_MACHINE" = "mdm9x30" ]; then
		if [ "${P82M_TZ_REVISION_LIST[i]}" = "null" ]; then
			P82M_TZ_URL="svn $svn_auth ${P82M_TZ_DL_MODE_LIST[i]} ${P82M_TZ_LIST[i]} --quiet"
		else
			P82M_TZ_URL="svn $svn_auth --revision ${P82M_TZ_REVISION_LIST[i]} ${P82M_TZ_DL_MODE_LIST[i]} ${P82M_TZ_LIST[i]} --quiet"
		fi
	fi

	if [ "$P82M_MACHINE" = "mdm9x15" ]; then
		if [ "${P82M_LPASS_REVISION_LIST[i]}" = "null" ]; then
			P82M_LPASS_URL="svn $svn_auth ${P82M_LPASS_DL_MODE_LIST[i]} ${P82M_LPASS_LIST[i]} --quiet"
		else
			P82M_LPASS_URL="svn $svn_auth --revision ${P82M_LPASS_REVISION_LIST[i]} ${P82M_LPASS_DL_MODE_LIST[i]} ${P82M_LPASS_LIST[i]} --quiet"
		fi
	fi

	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		if [ "${P82M_ADSP_REVISION_LIST[i]}" = "null" ]; then
			P82M_ADSP_URL="svn $svn_auth ${P82M_ADSP_DL_MODE_LIST[i]} ${P82M_ADSP_LIST[i]} --quiet"
		else
			P82M_ADSP_URL="svn $svn_auth --revision ${P82M_ADSP_REVISION_LIST[i]} ${P82M_ADSP_DL_MODE_LIST[i]} ${P82M_ADSP_LIST[i]} --quiet"
		fi
	fi

	if [ "$P82M_MACHINE" = "mdm9x30" ]; then
		if [ "${P82M_CNSS_REVISION_LIST[i]}" = "null" ]; then
			P82M_CNSS_URL="svn $svn_auth ${P82M_CNSS_DL_MODE_LIST[i]} ${P82M_CNSS_LIST[i]} --quiet"
		else
			P82M_CNSS_URL="svn $svn_auth --revision ${P82M_CNSS_REVISION_LIST[i]} ${P82M_CNSS_DL_MODE_LIST[i]} ${P82M_CNSS_LIST[i]} --quiet"
		fi
	fi

	if [ "${P82M_CONTENTS_REVISION_LIST[i]}" = "null" ]; then
		P82M_CONTENTS_URL="svn $svn_auth ${P82M_CONTENTS_DL_MODE_LIST[i]} ${P82M_CONTENTS_LIST[i]} --quiet"
	else
		P82M_CONTENTS_URL="svn $svn_auth --revision ${P82M_CONTENTS_REVISION_LIST[i]} ${P82M_CONTENTS_DL_MODE_LIST[i]} ${P82M_CONTENTS_LIST[i]} --quiet"
	fi
	print_hint "CONTENTS_URL: ${P82M_CONTENTS_URL}"

	unset i
	for ((i=0; i < ${#P82M_METABUILD[@]}; i++)); do
		if [ "${P82M_METABUILD[i]}" != "" ]; then
			case "${P82M_METABUILD[i]}" in
				"apss")
					print_hint "APSS_URL: $P82M_APSS_URL"
					;;
				"mpss")
					print_hint "MPSS_URL: $P82M_MPSS_URL"
					;;
				"common")
					if [ "$P82M_MACHINE" = "mdm9x30" ]; then
						print_hint "ADSP_URL: $P82M_ADSP_URL"
					fi
					if [ "$P82M_MACHINE" = "mdm9x30" ]; then
						print_hint "CNSS_URL: $P82M_CNSS_URL"
					fi
					print_hint "COMMON_URL: $P82M_COMMON_URL"
					;;
				"rpm")
					print_hint "RPM_URL: $P82M_RPM_URL"
					;;
				"sdi")
					print_hint "SDI_URL: $P82M_SDI_URL"
					;;
				"tz")
					print_hint "TZ_URL: $P82M_TZ_URL"
					;;
				"boot")
					print_hint "BOOT_URL: $P82M_BOOT_URL"
					;;
				"lpass")
					print_hint "LPASS_URL: $P82M_LPASS_URL"
					;;
				*)
					print_fail "Unknown METABUILD: ${P82M_METABUILD[i]}"
					;;
			esac
		fi
	done

	return 0
}

# Download mdm9xxx platform SDK code.
mdm9xxx_platform_sdk_dl()
{
	local i
	local prev_dir=""

	prev_dir=`pwd`
	if [ "${P82M_BUILDOPTION}" = "build_only" ]; then
		print_okey "BUILD_OPTION=${P82M_BUILDOPTION}, skip downloading"
		return 0
	fi

	cd "${QC_9XXX_SDK_BASE_DIR}"
	rm -f contents.xml
	print_wait "Downloading contents.xml: $P82M_CONTENTS_URL"
	$P82M_CONTENTS_URL
	if [ "$?" != "0" ]; then
		print_fail "Downloading contents.xml failed."
		return 1
	fi
	unset i
	for ((i=0; i < ${#P82M_METABUILD[@]}; i++)); do
		if [ "${P82M_METABUILD[i]}" != "" ]; then
			case "${P82M_METABUILD[i]}" in
				"apss")
					print_wait "Downloading APSS: $P82M_APSS_URL"
					if [ -d apps_proc ]; then
						print_hint "apps_proc already exist, skip download."
					else
						$P82M_APSS_URL
					fi
					;;
				"mpss")
					print_wait "Downloading MPSS: $P82M_MPSS_URL"
					if [ -d modem_proc ]; then
						print_hint "modem_proc already exist, skip download."
					else
						$P82M_MPSS_URL
					fi
					;;
				"common")
					if [ "$P82M_MACHINE" = "mdm9x30" ]; then
						if [ ! -d boot_images ]; then
							print_wait "Downloading BOOT: $P82M_BOOT_URL"
							$P82M_BOOT_URL
						else
							print_wait "boot_images already exist, skip download."
						fi
					fi
					if [ "$P82M_MACHINE" = "mdm9x30" ]; then
						print_wait "Downloading ADSP: $P82M_ADSP_URL"
						if [ -d adsp_proc ]; then
							print_hint "adsp_proc already exist, skip download."
						else
							$P82M_ADSP_URL
						fi
					fi
					if [ "$P82M_MACHINE" = "mdm9x30" ]; then
						print_wait "Downloading CNSS: $P82M_CNSS_URL"
						if [ -d cnss_proc ]; then
							print_hint "cnss_proc already exist, skip download."
						else
							$P82M_CNSS_URL
						fi
					fi
					print_wait "Downloading COMMON: $P82M_COMMON_URL"
					if [ -d common ]; then
						print_hint "common already exist, skip download."
					else
						$P82M_COMMON_URL
					fi
					;;
				"rpm")
					print_wait "Downloading RPM: $P82M_RPM_URL"
					if [ -d rpm_proc ]; then
						print_hint "rpm_proc already exist, skip download."
					else
						$P82M_RPM_URL
					fi
					;;
				"sdi")
					print_wait "Downloading SDI: $P82M_SDI_URL"
					if [ -d debug_image ]; then
						print_hint "debug_image already exist, skip download."
					else
						$P82M_SDI_URL
					fi
					;;
				"tz")
					print_wait "Downloading TZ: $P82M_TZ_URL"
					if [ -d trustzone_images ]; then
						print_hint "trustzone_images already exist, skip download."
					else
						$P82M_TZ_URL
					fi
					;;
				"boot")
					print_wait "Downloading BOOT: $P82M_BOOT_URL"
					if [ -d boot_images ]; then
						print_hint "boot_images already exist, skip download."
					else
						$P82M_BOOT_URL
					fi
					;;
				"lpass")
					print_wait "Downloading LPASS: $P82M_LPASS_URL"
					if [ -d lpass_proc ]; then
						print_hint "lpass_proc already exist, skip download."
					else
						$P82M_LPASS_URL
					fi
					;;
				*)
					print_fail "Unknown METABUILD: ${P82M_METABUILD[i]}"
					;;
			esac
		fi
	done

	if [ "$?" != 0 ]; then
		cd "$prev_dir"
		return 1
	fi
	cd "$prev_dir"

	return 0
}





