Lucio Andrés Illanes Albornoz (arab, vxp) 570129
#
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
# set -o errexit -o noglob are assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
#
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
# N.B.	URLs ($1) may contain `?' or '&' characters.
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
pkgp_fetch() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _url="${1}" _sha256sum_src="${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) 608811
	if [ -z "${3}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_url_dst="${DLCACHEDIR}/$(basename "${_url}")";
Lucio Andrés Illanes Albornoz (arab, vxp) 608811
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_url_dst="${DLCACHEDIR}/${3}";
Lucio Andrés Illanes Albornoz (arab, vxp) 608811
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	if [ ${ARG_OFFLINE:-0} -eq 1 ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	|| [ -e ${_url_dst}.fetched ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		wget ${WGET_ARGS} -c -O ${_url_dst} "${_url}";
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	if [ -n "${_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		set -- $(openssl dgst -sha256 ${_url_dst}); shift $((${#}-1));
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		if [ "${_sha256sum_dst:=${1}}" != "${_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			if [ ${ARG_IGNORE_SHA256SUMS:-0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
				log_msg failexit "Error: hash mismatch for URL \`${_url}' (is: ${_sha256sum_dst}, should be: ${_sha256sum_src}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			else
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
				log_msg warn "Warning: hash mismatch for URL \`${_url}' (is: ${_sha256sum_dst}, should be: ${_sha256sum_src}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	touch ${_url_dst}.fetched;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
pkgp_fetch_git() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _subdir="${1}" _url="${2}" _branch="${3}";
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	if [ ${ARG_OFFLINE:-0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		if [ -e "${DLCACHEDIR}/${_subdir}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			build_fileop cd ${DLCACHEDIR}/${_subdir} &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
				git pull origin ${_branch:-main} &&\
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
					build_fileop cd ${OLDPWD};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			git clone ${_url} ${DLCACHEDIR}/${_subdir};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			if [ -n "${_branch}" -a		\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			     \( -z "${_branch#main}" \) -a	\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			     \( -z "${_branch#master}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
				build_fileop cd ${DLCACHEDIR}/${_subdir}	&&\
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
					git checkout -b ${_branch}		&&\	
Lucio Andrés Illanes Albornoz (arab, vxp) bd97b6
						build_fileop cd ${OLDPWD};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	build_fileop rm ${_subdir};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	build_fileop cp ${DLCACHEDIR}/${_subdir} .;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
pkgp_fetch_urls_git() {
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	local _url_spec _subdir _url _git_branch;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_subdir=${_url_spec%=*};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_url=${_url_spec#*=};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		_url=${_url%@*};
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
			_git_branch=${_url_spec#*@};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 802215
		pkgp_fetch_git ${_subdir} ${_url} ${_git_branch};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
pkg_fetch() {
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	if [ -n "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		if [ "${1}" = "-git" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			shift; pkgp_fetch_urls_git ${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			pkgp_fetch ${1} ${2};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		if [ -n "${PKG_URL}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 608811
			pkgp_fetch ${PKG_URL} ${PKG_SHA256SUM} ${PKG_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		if [ -n "${PKG_URLS_GIT}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
			pkgp_fetch_urls_git ${PKG_URLS_GIT};
Lucio Andrés Illanes Albornoz (arab, vxp) 03a637
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
};
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
Lucio Andrés Illanes Albornoz (arab, vxp) 570129
# vim:filetype=sh