Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
#
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# . ./build.vars and set -o errexit -o noglob are assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# See warning at the top of build.vars.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
#
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# N.B.	URLs ($1) may contain `?' or '&' characters.
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
fetch() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_f_url="${1}"; _f_sha256sum_src="${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_f_url_dst="${DLCACHEDIR}/$(basename "${_f_url}")";
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
	if [ ${ARG_OFFLINE:-0} -eq 1 ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
	|| [ -e ${_f_url_dst}.fetched ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		unset _f_url _f_sha256sum_src _f_url_dst;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		wget ${WGET_ARGS} -c -O ${_f_url_dst} "${_f_url}";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	if [ -n "${_f_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		set -- $(openssl dgst -sha256 ${_f_url_dst}); shift $((${#}-1));
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		if [ "${_f_sha256sum_dst:=${1}}" != "${_f_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			log_msg failexit "Error: hash mismatch for URL \`${_f_url}' (is: ${_f_sha256sum_dst}, should be: ${_f_sha256sum_src}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	touch ${_f_url_dst}.fetched;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	unset _f_url _f_url_dst _f_sha256sum_src _f_sha256sum_dst;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
fetch_git() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_fg_subdir="${1}"; _fg_url="${2}"; _fg_branch="${3}";
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
	if [ ${ARG_OFFLINE:-0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
		if [ -e "${DLCACHEDIR}/${_fg_subdir}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			cd ${DLCACHEDIR}/${_fg_subdir} &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
				git pull origin ${_fg_branch:-main} && cd ${OLDPWD};
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
			git clone ${_fg_url} ${DLCACHEDIR}/${_fg_subdir};
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
			if [ -n "${_fg_branch}" -a		\
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
			     \( -z "${_fg_branch#main}" \) -a	\
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
			     \( -z "${_fg_branch#master}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
				cd ${DLCACHEDIR}/${_fg_subdir} &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
					git checkout -b ${_fg_branch} && cd ${OLDPWD};
Lucio Andrés Illanes Albornoz (arab, vxp) 35633f
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	secure_rm ${_fg_subdir};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	cp -pr ${DLCACHEDIR}/${_fg_subdir} .;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
is_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	if match_list "${ARG_RESTART}" , ${BUILD_PACKAGE_LC}; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		if [ -n "${ARG_RESTART_AT}" ]			\
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		&& [ "${ARG_RESTART_AT}" != "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			return 0;	# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			return 1;	# Build
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	elif [ -f ${WORKDIR}/.${2:-${BUILD_PACKAGE_LC}}.${1} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		return 0;		# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		return 1;		# Build
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
log_env_vars() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	log_msg info "Variables for this ${1:-build}:"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	while [ ${_lev_nvar:=0} -lt ${#} ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		_lev_arg="$(eval echo \${${_lev_nvar}})";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		_lev_arg="${_lev_arg%%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		if [ ${#_lev_arg} -gt ${_lev_arg_len_max:=0} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			_lev_arg_len_max=${#_lev_arg};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi; : $((_lev_nvar+=1));
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	done; unset _lev_nvar _lev_arg;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		log_msg info "$(printf					\
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			"%${_lev_arg_len_max}.${_lev_arg_len_max}s=%s"	\
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			"${1%%=*}" "$(get_var_unsafe ${1#*=})")";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	done; unset _lev_arg_len_max;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
parse_with_pkg_name() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	PKG_NAME=${1}; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_pwpn_pkg_name_uc=$(echo ${PKG_NAME} | tr a-z A-Z);
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	if [ -z "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_URL)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		unset _pwpn_pkg_name_uc;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		log_msg failexit "Error: package \`${PKG_NAME}' missing in build.vars.";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		for _pwpn_vname in ${PKG_BUILD_VARS}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			if [ -n "$(get_var_unsafe DEFAULT_${_pwpn_vname})" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe DEFAULT_${_pwpn_vname})";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			if [ -n "$(get_var_unsafe ${BUILD_TARGET}_${_pwpn_vname})" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe ${BUILD_TARGET}_${_pwpn_vname})";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			if [ "${BUILD}" = "debug" ]	\
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			&& [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_DEBUG)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_DEBUG)";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			elif [ "${BUILD}" = "release" ]	\
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			&& [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_RELEASE)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname}_RELEASE)";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			elif [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname})" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
				set_var_unsafe PKG_${_pwpn_vname} "$(get_var_unsafe PKG_${_pwpn_pkg_name_uc}_${_pwpn_vname})";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		done; unset _pwpn_vname;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	[ -z "${PKG_FNAME}" ] && PKG_FNAME=${PKG_URL##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	[ -z "${PKG_SUBDIR}" ] && PKG_SUBDIR=${PKG_FNAME%%.tar*};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	[ -n "${PKG_ENV_VARS_EXTRA}" ] && set_env_vars_with_sep : "${PKG_ENV_VARS_EXTRA}";
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
	for _pwpn_env_var in $(set); do
Lucio Andrés Illanes Albornoz (arab, vxp) 952e33
		if [ "${_pwpn_env_var#PKG_${_pwpn_pkg_name_uc}}" != "${_pwpn_env_var}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			_pwpn_env_vars="${_pwpn_env_vars:+${_pwpn_env_vars} }${_pwpn_env_var%%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	if [ -n "${_pwpn_env_vars}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		log_env_vars "build" ${_pwpn_env_vars};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	fi; unset _pwpn_pkg_name_uc _pwpn_env_var _pwpn_env_vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
run_cmd_unsplit() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_rcu_cmd=${1}; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		[ -n "${1}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			_rcu_cmdline="${_rcu_cmdline:+${_rcu_cmdline}:}${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	push_IFS :; ${_rcu_cmd} ${_rcu_cmdline}; _rcu_rc=$?; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	unset _rcu_cmd _rcu_cmdline; return ${_rcu_rc};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
set_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_sbsd_script_fname=${BUILD_PACKAGE_LC};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_sbsd_done_fname=${WORKDIR}/.${_sbsd_script_fname};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	while [ $# -ge 1 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		if [ "${1#-}" != "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			secure_rm ${_sbsd_done_fname}.${1#-};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			touch ${_sbsd_done_fname}.${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
			log_msg info "Finished build step ${1} of \`${_sbsd_script_fname}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	done; unset _sbsd_script_fname _sbsd_done_fname;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
set_env_vars_with_sep() {
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	_sevws_sep=${1}; shift; push_IFS ${_sevws_sep}; set -- ${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
		export "${1}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
	done; unset _sevws_sep; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
};
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
Lucio Andrés Illanes Albornoz (arab, vxp) 59765f
# vim:filetype=sh