Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
exp_rtl_fetch_url_git() {
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	local _tgtdir="${1}" _subdir="${2}" _url="${3}" _branch="${4}"	\
Lucio Andrés Illanes Albornoz 907bd3
		_oldpwd="";
Lucio Andrés Illanes Albornoz e1d469
	(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz 847bee
	rtl_flock_acquire 4 || exit "${?}";
Lucio Andrés Illanes Albornoz e1d469
	trap "rm -f \"${BUILD_DLCACHEDIR}/${_subdir%%[/]}.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	if [ -e "${BUILD_DLCACHEDIR}/${_subdir}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
Lucio Andrés Illanes Albornoz d279a9
			git pull ${DEFAULT_GIT_ARGS} origin "${_branch:-main}");
Lucio Andrés Illanes Albornoz c6d6e0
		(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
Lucio Andrés Illanes Albornoz adfcc4
			git submodule update);
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	else
Lucio Andrés Illanes Albornoz d279a9
		git clone ${DEFAULT_GIT_ARGS} "${_url}" "${BUILD_DLCACHEDIR}/${_subdir}";
Lucio Andrés Illanes Albornoz (arab, vxp) 76f7b7
		if [ -n "${_branch}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 76f7b7
				git checkout "${_branch}");
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
Lucio Andrés Illanes Albornoz adfcc4
			git submodule update --init);
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	fi;
Lucio Andrés Illanes Albornoz c6d6e0
	_oldpwd="${PWD}"; rtl_fileop cd "${PKG_BASE_DIR}";
Lucio Andrés Illanes Albornoz c6d6e0
	rtl_fileop rm "${_tgtdir}/${_subdir}";
Lucio Andrés Illanes Albornoz c6d6e0
	rtl_fileop cp "${BUILD_DLCACHEDIR}/${_subdir}" "${_tgtdir}";
Lucio Andrés Illanes Albornoz c6d6e0
	rtl_fileop cd "${_oldpwd}";) 4<>"${BUILD_DLCACHEDIR}/${_subdir%%[/]}.fetching";
Lucio Andrés Illanes Albornoz 689d3c
	if [ "${?}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 689d3c
		cd "$(pwd)";
Lucio Andrés Illanes Albornoz 689d3c
	else
Lucio Andrés Illanes Albornoz bf9edf
		return 1;
Lucio Andrés Illanes Albornoz 689d3c
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fetch_urls_git() {
Lucio Andrés Illanes Albornoz 907bd3
	local _tgtdir="" _url_spec="" _subdir="" _url="" _git_branch="";
Lucio Andrés Illanes Albornoz (arab, vxp) fce0a9
	_tgtdir="${1}"; shift;
Lucio Andrés Illanes Albornoz d279a9
	if [ "${ARG_FETCH_FORCE}" = "offline" ]; then
Lucio Andrés Illanes Albornoz d279a9
		return 0;
Lucio Andrés Illanes Albornoz d279a9
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		_subdir="${_url_spec%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		_url="${_url_spec#*=}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		_url="${_url%@*}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			_git_branch=${_url_spec#*@};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		exp_rtl_fetch_url_git "${_tgtdir}" "${_subdir}"	\
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			"${_url}" "${_git_branch}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
# N.B.	URLs ($1) may contain `?' or '&' characters.
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fetch_url_wget() {
Lucio Andrés Illanes Albornoz 689d3c
	local _url="${1}" _sha256sum_src="${2}" _target_fname="${3}" _sha256sum_dst="";
Lucio Andrés Illanes Albornoz d279a9
	if [ "${ARG_FETCH_FORCE}" = "offline" ]; then
Lucio Andrés Illanes Albornoz d279a9
		return 0;
Lucio Andrés Illanes Albornoz 689d3c
	elif [ -z "${_target_fname}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		_target_fname="$(rtl_basename "${_url}")";
Lucio Andrés Illanes Albornoz d279a9
	fi;
Lucio Andrés Illanes Albornoz 689d3c
	_url_dst="${BUILD_DLCACHEDIR}/${_target_fname}";
Lucio Andrés Illanes Albornoz e1d469
	(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz 847bee
	rtl_flock_acquire 4 || exit "${?}";
Lucio Andrés Illanes Albornoz e1d469
	trap "_rc=\"\${?}\" ;rm -f \"${_url_dst}.fetching\"; exit \"\${_rc}\";" EXIT;
Lucio Andrés Illanes Albornoz 689d3c
	if [ ! -e "${_url_dst}.fetched" ]; then
Lucio Andrés Illanes Albornoz 689d3c
		wget ${DEFAULT_WGET_ARGS} -O "${_url_dst}" "${_url}";
Lucio Andrés Illanes Albornoz 689d3c
		if [ -n "${_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz 06576a
			set -- $(sha256sum "${_url_dst}");
Lucio Andrés Illanes Albornoz 689d3c
			if [ "${_sha256sum_dst:=${1}}" != "${_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
				rtl_log_msg fatalexit "Error: hash mismatch for URL \`%s' (downloaded file: %s, from build variables: %s.)"\
Lucio Andrés Illanes Albornoz b6a9a1
						"${_url}" "${_sha256sum_dst}" "${_sha256sum_src}";
Lucio Andrés Illanes Albornoz 689d3c
			fi;
Lucio Andrés Illanes Albornoz 689d3c
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		rtl_fileop touch "${_url_dst}.fetched";
Lucio Andrés Illanes Albornoz 689d3c
	fi;) 4<>"${_url_dst}.fetching";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
# vim:filetype=sh