Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz 2b85d0
# set +o errexit -o noglob 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 689d3c
	(set -o errexit -o noglob; trap "rm -f \"${BUILD_DLCACHEDIR}/${_subdir%%[/]}.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz 689d3c
	while true; do
Lucio Andrés Illanes Albornoz 689d3c
		if flock -E 622 -w 3600 4; then
Lucio Andrés Illanes Albornoz 689d3c
			break;
Lucio Andrés Illanes Albornoz 689d3c
		elif [ "${?}" -eq 622 ]; then
Lucio Andrés Illanes Albornoz 689d3c
			continue;
Lucio Andrés Illanes Albornoz 689d3c
		else
Lucio Andrés Illanes Albornoz 689d3c
			exit "${?}";
Lucio Andrés Illanes Albornoz 689d3c
		fi;
Lucio Andrés Illanes Albornoz 689d3c
	done;
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 689d3c
		return "${?}";
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 689d3c
	(set -o errexit -o noglob; trap "_rc=\"\${?}\" ;rm -f \"${_url_dst}.fetching\"; exit \"\${_rc}\";" EXIT;
Lucio Andrés Illanes Albornoz 689d3c
	while true; do
Lucio Andrés Illanes Albornoz 689d3c
		if flock -E 622 -w 3600 4; then
Lucio Andrés Illanes Albornoz 689d3c
			break;
Lucio Andrés Illanes Albornoz 689d3c
		elif [ "${?}" -eq 622 ]; then
Lucio Andrés Illanes Albornoz 689d3c
			continue;
Lucio Andrés Illanes Albornoz 689d3c
		else
Lucio Andrés Illanes Albornoz 689d3c
			exit "${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi;
Lucio Andrés Illanes Albornoz 689d3c
	done;
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 c6d6e0
				rtl_log_msg failexit "Error: hash mismatch for URL \`${_url}' (downloaded file: ${_sha256sum_dst}, from build variables: ${_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