Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
#
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
# set -o errexit is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
#
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
lalbornoz 340f4c
TIMESTAMP_FMT="${TIMESTAMP_FMT:-"%Y/%m/%d %H:%M:%S"}";
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
check_prereqs() {
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	for _cmd in "$@"; do
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		if ! command -v ${_cmd} >/dev/null; then
Lucio Andrés Illanes Albornoz (arab, vxp) bd43d0
			log_msg fail "Error: missing prerequisite command \`${_cmd}";
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
			exit 1;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	done; unset _cmd;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
};
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
lalbornoz 340f4c
date() {
lalbornoz 340f4c
	command date +"${TIMESTAMP_FMT}";
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
fetch() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	wget --no-check-certificate -N ${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	if [ $# -eq 2 ] && ! compare_hash ${1##*/} ${2}; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		log_msg fail "Error: hash mismatch for URL \`${1##*/}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) bd43d0
		exit 1;
Lucio Andrés Illanes Albornoz (arab, vxp) bd43d0
	fi;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
fetch_git() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	if [ -d ${1} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		(cd ${1} && git pull origin main);
lalbornoz 340f4c
	else
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		git clone ${2} ${1};
lalbornoz 340f4c
	fi;
lalbornoz 340f4c
};
lalbornoz 340f4c
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
compare_hash() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_hash_cmp=${2}; set -- $(openssl dgst -sha256 ${1});
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	shift $((${#}-1)); set -- ${1} ${_hash_cmp}; unset _hash_cmp;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	[ "x${1}" = "x${2}" ];
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
};
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
lalbornoz 340f4c
is_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_script_fname=${SCRIPT_FNAME##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	if [ -f ${WORKDIR}/.${_script_fname%.build}.${1} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
		unset script_fname;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
		return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	else
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
		unset script_fname;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
		return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	fi;
lalbornoz 340f4c
};
lalbornoz 340f4c
set_build_script_done() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_script_fname=${SCRIPT_FNAME##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_done_fname=${WORKDIR}/.${_script_fname%.build};
lalbornoz 340f4c
	while [ $# -ge 1 ]; do
lalbornoz 340f4c
		if [ "x${1#-}" != "x${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
			rm -f ${_done_fname}.${1#-};
lalbornoz 340f4c
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
			touch ${_done_fname}.${1};
lalbornoz 340f4c
			log_msg info "Finished build step ${1} of build script \`${_script_fname}'.";
lalbornoz 340f4c
		fi; shift;
lalbornoz 340f4c
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	unset _script_fname _done_fname;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
log_msg() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_lvl=${1}; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	case ${_lvl} in
lalbornoz 340f4c
		fail) printf "\033[91m"; ;;
lalbornoz 340f4c
		info) printf "\033[97m"; ;;
lalbornoz 340f4c
		succ) printf "\033[92m"; ;;
lalbornoz 340f4c
	esac;
lalbornoz 340f4c
	if [ $# -gt 1 ]; then
lalbornoz 340f4c
		printf "==> %s %s %s\033[0m\n" "$(date +"${TIMESTAMP_FMT}")" "${1}" "$*";
lalbornoz 340f4c
	else
lalbornoz 340f4c
		printf "==> %s %s\033[0m\n" "$(date +"${TIMESTAMP_FMT}")" "${1}";
lalbornoz 340f4c
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	unset _lvl;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
parse_with_pkg_name() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_pkg_name=${1}; shift;
lalbornoz 340f4c
	while [ $# -ge 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		_pkg_url=${1}; _envvs="${2}"; _pkg_fname=${_pkg_url##*/};
lalbornoz 340f4c
		if [ "x${_pkg_fname%%-*}" = "x${_pkg_name}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
			_IFS="${IFS}"; IFS=:;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
			for _envv in ${_envvs}; do
lalbornoz 340f4c
				export ${_envv};
Lucio Andrés Illanes Albornoz (arab, vxp) bd43d0
			done; IFS="${_IFS}"; unset _envvs _envv;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
			_pkg_subdir=${_pkg_fname%%.tar*};
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
			eval _pkg_sha256sum=\${PKG_$(echo "${_pkg_name}" |\
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
				tr a-z A-Z)_SHA256SUM};
lalbornoz 340f4c
			return;
lalbornoz 340f4c
		fi; shift 2;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	unset _pkg_name _pkg_url _envvs _pkg_fname _envv _IFS _pkg_subdir;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
rm_if_exists() {
lalbornoz 340f4c
	while [ $# -gt 1 ]; do [ "x${1%[a-z]}" = "x-" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
		eval _${1#-}flag=1; shift; done;
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_dir=${1};
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	if [ -d ${1} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		rm -rf ${1} || return 1;
lalbornoz 340f4c
	fi;
lalbornoz 340f4c
	if [ ${_mflag:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		unset _mflag; mkdir ${1} || return 2;
lalbornoz 340f4c
	fi;
lalbornoz 340f4c
	if [ ${_cflag:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
		unset _cflag; cd ${1} || return 3;
lalbornoz 340f4c
	fi;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
run_configure_with_extra_args() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_IFS="${IFS}"; IFS=","; _script=${1}; _extra_args="${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	shift 2; ${_script} "${@}" ${_extra_args}; IFS="${_IFS}";
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	unset _script _extra_args;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
set_build_dir() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	export _build_dir=${1}-build-${2};
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
set_env_vars() {
Lucio Andrés Illanes Albornoz (arab, vxp) 6e940c
	_val=${1}; shift;
lalbornoz 340f4c
	while [ $# -ge 1 ]; do
lalbornoz 340f4c
		export "${1}=${_val}"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	done; unset _val;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
split() {
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	_IFS="${IFS}"; IFS="${1}"; set -- ${2};
Lucio Andrés Illanes Albornoz (arab, vxp) f5eeee
	IFS="${_IFS}"; echo "${*}"; unset _IFS;
lalbornoz 340f4c
};
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh