Blame build.subr

Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
#
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# . ./build.vars and set -o errexit -o noglob are assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
# See warning at the top of build.vars.
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
#
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
date() { command date +"${1:-${TIMESTAMP_FMT}}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
get_var_unsafe() { eval echo \${${1}}; };
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
set_var_unsafe() { eval ${1}=\"${2}\"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
push_IFS() { _pI_IFS="${IFS}"; IFS="${1}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
pop_IFS() { IFS="${_pI_IFS}"; unset _pI_IFS; };
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
set_build_dir() { PKG_BUILD_DIR=${1}-${2}-${TARGET}; };
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
split() { push_IFS "${1}"; set -- ${2}; pop_IFS; echo "${*}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
test_cmd() { command -v "${1}" >/dev/null; };
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
unsplit() { push_IFS "${1}"; shift; set -- "${@}"; echo "${*}"; pop_IFS; };
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
# N.B.	URLs ($1) may contain `?' or '&' characters.
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
fetch() {
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	_f_url="${1}"; _f_sha256sum_src="${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	_f_url_dst="${DLCACHEDIR}/$(basename "${_f_url}")";
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	if [ -e ${_f_url_dst}.fetched ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		unset _f_url _f_sha256sum_src _f_url_dst;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		wget ${WGET_ARGS} -c -O ${_f_url_dst} "${_f_url}";
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ -n "${_f_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		set -- $(openssl dgst -sha256 ${_f_url_dst}); shift $((${#}-1));
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		if [ "${_f_sha256sum_dst:=${1}}" != "${_f_sha256sum_src}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
			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) 672107
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	touch ${_f_url_dst}.fetched;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	unset _f_url _f_url_dst _f_sha256sum_src _f_sha256sum_dst;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
};
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
fetch_git() {
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
	_fg_subdir="${1}"; _fg_url="${2}"; _fg_branch="${3}";
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	if [ -e "${DLCACHEDIR}/${_fg_subdir}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
		cd ${DLCACHEDIR}/${_fg_subdir} &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
			git pull origin ${_fg_branch:-main} && cd ${OLDPWD};
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
		git clone ${_fg_url} ${DLCACHEDIR}/${_fg_subdir};
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
		if [ -n "${_fg_branch}" -a		\
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
		     \( -z "${_fg_branch#main}" \) -a	\
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
		     \( -z "${_fg_branch#master}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
			cd ${DLCACHEDIR}/${_fg_subdir} &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
				git checkout -b ${_fg_branch} && cd ${OLDPWD};
Lucio Andrés Illanes Albornoz (arab, vxp) 49b310
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	secure_rm ${_fg_subdir};
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	echo cp -pr ${DLCACHEDIR}/${_fg_subdir} .;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
	cp -pr ${DLCACHEDIR}/${_fg_subdir} .;
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
};
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
get_vars_unsafe() {
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
		_gvu_vval="$(eval echo \${${1}})";
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
		[ -z "${_gvu_vval}" ] || _gvu_vval_="${_gvu_vval}";
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
	done; echo "${_gvu_vval_}";
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
	unset _gvu_vval _gvu_vval_;
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
};
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
disable_build_script_link() {
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	if [ -e ${1%.disabled}.disabled ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		log_msg failexit "Error: build script already disabled.";
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	elif [ ! -e ${1} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		log_msg failexit "Error: invalid or non-existent build script filename/level/name.";
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		echo mv -- ${1} ${1}.disabled;
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		mv -- ${1} ${1}.disabled;
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
}
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
enable_build_script_link() {
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	if [ -e ${1%.disabled} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		log_msg failexit "Error: build script already enabled.";
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	elif [ ! -e ${1%.disabled}.disabled ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		log_msg failexit "Error: invalid or non-existent build script filename/level/name.";
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		echo mv -- ${1%.disabled}.disabled ${1%.disabled};
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
		mv -- ${1%.disabled}.disabled ${1%.disabled};
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 319df8
}
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
insert_build_script_link() {
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	_ibsl_fname=${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	_ibsl_level=${_ibsl_fname%%.*};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	_ibsl_name=${_ibsl_fname#*.}; _ibsl_name=${_ibsl_name%.build};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	if [ -z "${_ibsl_fname}" -o -z "${_ibsl_level}"				\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			-o -z "${_ibsl_name}" ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			|| ! isnumber ${_ibsl_level}				\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			|| [ ${#_ibsl_level} != 3 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		log_msg failexit "Error: invalid or empty build script filename/level/name.";
Lucio Andrés Illanes Albornoz (arab, vxp) cb0470
	elif [ -e ${_ibsl_fname} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) cb0470
		log_msg failexit "Error: build script \`${_ibsl_fname}' already exists.";
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	elif [ -z "$(find -maxdepth 1 -name ${_ibsl_level}.\* -printf '%P\n' -quit)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		echo ln -s -- pkg.build ${_ibsl_fname};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		ln -s -- pkg.build ${_ibsl_fname};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		unset _ibsl_fname _ibsl_level _ibsl_name;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	else
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
		_ibsl_levels="$(find -maxdepth 1 -name	\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			 ${_ibsl_level%[0-9][0-9]}\[0-9\]\[0-9\].\*		\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			-printf '%P\n' | sort -nk1)";
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	for _ibsl_fname_cur in ${_ibsl_levels}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		if [ \( ${_ibsl_changed:=0} -eq 0 \) -a	\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
				\( "${_ibsl_fname_cur%%.*}" -eq ${_ibsl_level} \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			echo ln -s -- pkg.build ${_ibsl_fname};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			ln -s -- pkg.build ${_ibsl_fname};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			_ibsl_changed=1;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		if [ ${_ibsl_changed:=0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			if [ -z "$(find -maxdepth 1				\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
					-name $((${_ibsl_fname_cur%%.*}+1)).\*	\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
					-printf '%P\n' -quit)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
				_ibsl_last=1;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			echo mv -- ${_ibsl_fname_cur}				\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
				$((${_ibsl_fname_cur%%.*}+1)).${_ibsl_fname_cur#*.};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			mv -- ${_ibsl_fname_cur}				\
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
				$((${_ibsl_fname_cur%%.*}+1)).${_ibsl_fname_cur#*.};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			if [ ${_ibsl_last:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
				break;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	if [ ${_ibsl_changed:=0} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		log_msg failexit "Error: build level ${_ibsl_level} not in \`${_ibsl_levels}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		unset _ibsl_levels _ibsl_fname_cur _ibsl_changed _ibsl_last;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		unset _ibsl_fname _ibsl_level _ibsl_name;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
isnumber() {
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	[ -z "${1}" ] && return 1 || _i_num=${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	while [ -n "${_i_num}" ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		if [ "${_i_num#[0-9]}" = "${_i_num}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
			_i_num="${_i_num#[0-9]}";
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	done; unset _i_num; return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
remove_build_script_link() {
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	_rbsl_fname=${1};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	_rbsl_level=${_rbsl_fname%%.*};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	_rbsl_name=${_rbsl_fname#*.}; _rbsl_name=${_rbsl_name%.build};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	if [ -z "${_rbsl_fname}" -o -z "${_rbsl_level}"				\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			-o -z "${_rbsl_name}" ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			|| ! isnumber ${_rbsl_level}				\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			|| [ ${#_rbsl_level} != 3 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
		log_msg failexit "Error: invalid or empty build script filename/level/name.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	elif [ ! -e ${_rbsl_fname} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
		log_msg failexit "Error: build script \`${_rbsl_fname}' doesn't exist.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	else for _rbsl_fname_cur in $(find -maxdepth 1				\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			-name ${_rbsl_level%[0-9][0-9]}\[0-9\]\[0-9\].\*	\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			-printf '%P\n' | sort -nk1); do
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
		_rbsl_fname_cur_level=${_rbsl_fname_cur%%.*};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
		if [ ${_rbsl_fname_cur_level} -gt ${_rbsl_level} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
			echo mv ${_rbsl_fname_cur}			\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
				$(printf "%03u"				\
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
					$((${_rbsl_fname_cur_level#00}-1))).${_rbsl_fname_cur#*.};
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
	done; fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec8132
};
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
lalbornoz 340f4c
is_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
	if [ -n "${ARG_RESTART_SCRIPT_AT}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
		if [ "${1}" = clean ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
		&& [ -z "${ARG_RESTART_SCRIPT_AT#ALL}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
			return 0;	# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) 0724e5
		elif [ "${1}" = finish ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
		|| [ -z "${ARG_RESTART_SCRIPT_AT#ALL}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			return 1;	# Build
Lucio Andrés Illanes Albornoz (arab, vxp) 6e48a0
		elif ! match_list ${ARG_RESTART_SCRIPT_AT} , ${1}; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			return 0;	# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			return 1;	# Build
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	elif [ -f "${WORKDIR}/.${2:-$(basename ${SCRIPT_FNAME%.build})}.${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		return 0;		# Skip
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		return 1;		# Build
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	fi;
lalbornoz 340f4c
};
lalbornoz 340f4c
set_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	_sbsd_script_fname=${SCRIPT_FNAME##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	_sbsd_done_fname=${WORKDIR}/.${_sbsd_script_fname%.build};
lalbornoz 340f4c
	while [ $# -ge 1 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 9ba5d3
		if [ "${1#-}" != "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			secure_rm ${_sbsd_done_fname}.${1#-};
lalbornoz 340f4c
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
			touch ${_sbsd_done_fname}.${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
			log_msg info "Finished build step ${1} of build script \`${_sbsd_script_fname}'.";
lalbornoz 340f4c
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	done; unset _sbsd_script_fname _sbsd_done_fname;
lalbornoz 340f4c
};
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
log_env_vars() {
Lucio Andrés Illanes Albornoz (arab, vxp) c288df
	log_msg info "Variables for this ${1:-build}:"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	while [ ${_lev_nvar:=0} -lt ${#} ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		_lev_arg="$(eval echo \${${_lev_nvar}})";
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		_lev_arg="${_lev_arg%%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		if [ ${#_lev_arg} -gt ${_lev_arg_len_max:=0} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
			_lev_arg_len_max=${#_lev_arg};
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		fi; : $((_lev_nvar+=1));
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	done; unset _lev_nvar _lev_arg;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		log_msg info "$(printf					\
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
			"%${_lev_arg_len_max}.${_lev_arg_len_max}s=%s"	\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
			"${1%%=*}" "$(get_var_unsafe ${1#*=})")";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	done; unset _lev_arg_len_max;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
};
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
lalbornoz 340f4c
log_msg() {
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	_lm_lvl=${1}; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	case ${_lm_lvl} in
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		failexit) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		fail) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		info) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		succ) printf "\033[0m\033[${LOG_MSG_SUCC_COLOUR}m"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 672107
		warn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
lalbornoz 340f4c
	esac;
lalbornoz 340f4c
	if [ $# -gt 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
		printf "==> %s %s %s\033[0m\n" "$(date "${TIMESTAMP_FMT}")" "${1}" "$*";
lalbornoz 340f4c
	else
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
		printf "==> %s %s\033[0m\n" "$(date "${TIMESTAMP_FMT}")" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	fi; [ ${_lm_lvl} = failexit ] && exit 1 || unset _lm_lvl;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
match_list() {
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	_ml_cmp="${3}"; push_IFS "${2}"; set -- ${1}; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 9ba5d3
		if [ "${1}" = "${_ml_cmp}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
			unset _ml_cmp; return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	done; unset _ml_cmp; return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
lalbornoz 340f4c
parse_with_pkg_name() {
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	PKG_LVL=${1}; PKG_NAME=${2}; shift 2;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	_pwpn_pkg_NAME=$(echo ${PKG_NAME} | tr a-z A-Z);
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ -z "$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_URL)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		unset _pwpn_pkg_NAME;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		log_msg failexit "Error: package \`${PKG_NAME}' missing in build.vars.";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		for _pwpn_vname in ${PKG_BUILD_VARS}; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			if [ -n "$(get_var_unsafe DEFAULT_${_pwpn_vname})" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
				export "PKG_${_pwpn_vname}=$(get_var_unsafe DEFAULT_${_pwpn_vname})";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			if [ -n "$(get_var_unsafe PKG_LVL${PKG_LVL}_${_pwpn_vname})" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
				export "PKG_${_pwpn_vname}=$(get_var_unsafe PKG_LVL${PKG_LVL}_${_pwpn_vname})";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
			if [ "${BUILD}" = "debug" ]	\
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
			&& [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_${_pwpn_vname}_DEBUG)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
				export "PKG_${_pwpn_vname}=$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_${_pwpn_vname}_DEBUG)";
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
			elif [ "${BUILD}" = "release" ]	\
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
			&& [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_${_pwpn_vname}_RELEASE)" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
				export "PKG_${_pwpn_vname}=$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_${_pwpn_vname}_RELEASE)";
Lucio Andrés Illanes Albornoz (arab, vxp) f41d1e
			elif [ -n "$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_${_pwpn_vname})" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
				export "PKG_${_pwpn_vname}=$(get_var_unsafe PKG_${_pwpn_pkg_NAME}_${_pwpn_vname})";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		done; unset _pwpn_vname;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	[ -z "${PKG_FNAME}" ] && PKG_FNAME=${PKG_URL##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	[ -z "${PKG_SUBDIR}" ] && PKG_SUBDIR=${PKG_FNAME%%.tar*};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	[ -n "${PKG_ENV_VARS_EXTRA}" ] && set_env_vars_with_sep : "${PKG_ENV_VARS_EXTRA}";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	for _pwpn_env_var in $(export); do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		if [ "${_pwpn_env_var#PKG_}" != "${_pwpn_env_var}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
			_pwpn_env_vars="${_pwpn_env_vars:+${_pwpn_env_vars} }${_pwpn_env_var%%=*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 3243be
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	if [ -n "${_pwpn_env_vars}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		log_env_vars "build script" ${_pwpn_env_vars};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi; unset _pwpn_pkg_NAME _pwpn_env_var _pwpn_env_vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
set_env_vars_with_sep() {
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	_sevws_sep=${1}; shift; push_IFS ${_sevws_sep}; set -- ${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
		export "${1}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	done; unset _sevws_sep; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
};
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
secure_cd() {
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	if [ \( -z "${1}" \) -o \( ! -e "${1}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	else
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		(cd "${1}"; [ "${PWD#${PREFIX_ROOT}}" = "${PWD}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			return 1 || return 0);
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		if [ ${?} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
			[ "${ARG_VERBOSE:-0}" -eq 1 ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
				log_msg warn "Changing working directory to \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			cd -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		else
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			log_msg failexit "secure_cd() called with pathname \`${1}' not below \${PREFIX_ROOT} (${PREFIX_ROOT}). This is a bug.";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 820be1
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
};
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
insecure_mkdir() {
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		if [ -z "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		elif [ ! -e "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
			[ "${ARG_VERBOSE:-0}" -eq 1 ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
				log_msg warn "Making directory \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			mkdir -p -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
};
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
secure_rm() {
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		if [ -z "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		elif [ -e "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			if [ -d "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
				_sr_pname_check="${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			else
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
				_sr_pname_check="$(dirname "${1}")";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			(cd "${_sr_pname_check}"; [ "${PWD#${PREFIX_ROOT}}" = "${PWD}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
				return 1 || return 0);
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			if [ ${?} -eq 0 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
				unset _sr_pname_check;
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
				[ "${ARG_VERBOSE:-0}" -eq 1 ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) f42965
					log_msg warn "Removing directory or file \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
				rm -rf -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			else
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
				log_msg failexit "secure_rm() called with pathname \`${1}' not below \${PREFIX_ROOT} (${PREFIX_ROOT}). This is a bug.";
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
		fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) afe7b9
	done;
lalbornoz 340f4c
};
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
run_cmd_unsplit() {
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	_rcu_cmd=${1}; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		[ -n "${1}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) c288df
			_rcu_cmdline="${_rcu_cmdline:+${_rcu_cmdline}:}${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) c288df
	push_IFS :; ${_rcu_cmd} ${_rcu_cmdline}; _rcu_rc=$?; pop_IFS;
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
	unset _rcu_cmd _rcu_cmdline; return ${_rcu_rc};
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
};
Lucio Andrés Illanes Albornoz (arab, vxp) 392fef
lalbornoz 340f4c
# vim:filetype=sh