Lucio Andrés Illanes Albornoz aeeaa0
#
Lucio Andrés Illanes Albornoz aeeaa0
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz aeeaa0
#
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtlp_fetch_url_git() {
Lucio Andrés Illanes Albornoz aeeaa0
	local	_cache_dname="${1}" _git_args="${2}" _git_branch="${3}" _mirrors="${4}" _pkg_name="${5}"\
Lucio Andrés Illanes Albornoz aeeaa0
		_subdir="${6}" _tgtdir="${7}" _url="${8}" _cache_dname_full="" _clonefl=0 _oldpwd="" _url_base="";
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	_cache_dname_full="${_cache_dname}/${_subdir##*/}";
Lucio Andrés Illanes Albornoz aeeaa0
	(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz aeeaa0
	rtl_flock_acquire 4 || exit "${?}";
Lucio Andrés Illanes Albornoz aeeaa0
	trap "rm -f \"${_cache_dname_full%%[/]}.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz aeeaa0
	if [ -e "${_cache_dname_full}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
			git pull ${_git_args} origin "${_git_branch:-main}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
			git submodule update) || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
	else	if git clone ${_git_args} -b "${_git_branch:-main}" "${_url}" "${_cache_dname_full}"; then
Lucio Andrés Illanes Albornoz aeeaa0
			_clonefl=1;
Lucio Andrés Illanes Albornoz aeeaa0
		elif [ "${_mirrors}" = "skip" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		else	for _url_base in ${_mirrors}; do
Lucio Andrés Illanes Albornoz aeeaa0
				if git clone ${_git_args} -b "${_git_branch:-main}" "${_url_base}/${_pkg_name}/${_url##*/}" "${_cache_dname_full}"; then
Lucio Andrés Illanes Albornoz aeeaa0
					_clonefl=1; break;
Lucio Andrés Illanes Albornoz aeeaa0
				fi;
Lucio Andrés Illanes Albornoz aeeaa0
			done;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${_clonefl}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		else	if [ -n "${_git_branch}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
				(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
					git checkout "${_git_branch}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
			fi;
Lucio Andrés Illanes Albornoz aeeaa0
			(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz aeeaa0
				git submodule update --init) || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
	if [ "${_cache_dname}" != "${_tgtdir}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		_oldpwd="${PWD}"; rtl_fileop cd "${_tgtdir}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_fileop rm "${_tgtdir}/${_subdir}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		if [ ! -e "$(rtl_dirname "${_tgtdir}/${_subdir}")" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}/${_subdir}")";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_fileop cp "${_cache_dname_full}" "${_tgtdir}/${_subdir}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_fileop cd "${_oldpwd}" || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
	fi) 4<>"${_cache_dname_full%%[/]}.fetching";
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	if [ "${?}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		cd "$(pwd)";
Lucio Andrés Illanes Albornoz aeeaa0
	else
Lucio Andrés Illanes Albornoz aeeaa0
		return 1;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_mirror_urls_git() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _git_args="${1}" _tgtdir="${2}" _rc=0 _repo_dname="" _subdir="" _url="" _url_spec=""; shift 2;
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz aeeaa0
		_subdir="${_url_spec%=*}"; _subdir="${_subdir##*/}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
Lucio Andrés Illanes Albornoz aeeaa0
		_repo_dname="${_url##*/}"; [ "${_repo_dname%.git}" = "${_repo_dname}" ] && _repo_dname="${_repo_dname}.git";
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
		if [ ! -e "$(rtl_dirname "${_tgtdir}")" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}")";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz aeeaa0
		rtl_flock_acquire 4 || exit "${?}";
Lucio Andrés Illanes Albornoz aeeaa0
		trap "rm -f \"${_tgtdir}/.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz aeeaa0
		if [ -e "${_tgtdir}/${_repo_dname}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			(rtl_fileop cd "${_tgtdir}/${_repo_dname}" && git fetch ${_git_args} --all) || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		else	(rtl_fileop cd "${_tgtdir}" && git clone ${_git_args} --mirror "${_url}") || return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi) 4<>"${_tgtdir}/.fetching";
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${?}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			_rc=1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done; return "${_rc}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_fetch_urls_git() {
Lucio Andrés Illanes Albornoz aeeaa0
	local	_cache_dname="${1}" _git_args="${2}" _tgtdir="${3}" _pkg_name="${4}" _mirrors="${5}"\
Lucio Andrés Illanes Albornoz aeeaa0
		_git_branch="" _subdir="" _url="" _url_spec=""; shift 5;
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz aeeaa0
		_subdir="${_url_spec%=*}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			_git_branch=${_url_spec#*@};
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
		if ! rtlp_fetch_url_git	\
Lucio Andrés Illanes Albornoz aeeaa0
				"${_cache_dname}" "${_git_args}" "${_git_branch}" "${_mirrors}"\
Lucio Andrés Illanes Albornoz aeeaa0
				"${_pkg_name}" "${_subdir}" "${_tgtdir}" "${_url}"; then
Lucio Andrés Illanes Albornoz aeeaa0
			return 1;
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
# vim:filetype=sh