#
# set -o errexit is assumed.
#
TIMESTAMP_FMT="${TIMESTAMP_FMT:-"%Y/%m/%d %H:%M:%S"}";
check_prereqs() {
for _cmd in "$@"; do
if ! command -v ${_cmd} >/dev/null; then
log_msg fail "Error: missing prerequisite command \`${_cmd}";
exit 1;
fi;
done; unset _cmd;
};
date() {
command date +"${TIMESTAMP_FMT}";
};
fetch() {
wget --no-check-certificate -N ${1};
if [ $# -eq 2 ] && ! compare_hash ${1##*/} ${2}; then
log_msg fail "Error: hash mismatch for URL \`${1##*/}'.";
exit 1;
fi;
};
fetch_git() {
if [ -d ${1} ]; then
(cd ${1} && git pull origin main);
else
git clone ${2} ${1};
fi;
};
compare_hash() {
_hash_cmp=${2}; set -- $(openssl dgst -sha256 ${1});
shift $((${#}-1)); set -- ${1} ${_hash_cmp}; unset _hash_cmp;
[ "x${1}" = "x${2}" ];
};
is_build_script_done() {
_script_fname=${SCRIPT_FNAME##*/};
if [ -f ${WORKDIR}/.${_script_fname%.build}.${1} ]; then
unset script_fname;
return 0;
else
unset script_fname;
return 1;
fi;
};
set_build_script_done() {
_script_fname=${SCRIPT_FNAME##*/};
_done_fname=${WORKDIR}/.${_script_fname%.build};
while [ $# -ge 1 ]; do
if [ "x${1#-}" != "x${1}" ]; then
rm -f ${_done_fname}.${1#-};
else
touch ${_done_fname}.${1};
log_msg info "Finished build step ${1} of build script \`${_script_fname}'.";
fi; shift;
done;
unset _script_fname _done_fname;
};
log_msg() {
_lvl=${1}; shift;
case ${_lvl} in
fail) printf "\033[91m"; ;;
info) printf "\033[97m"; ;;
succ) printf "\033[92m"; ;;
esac;
if [ $# -gt 1 ]; then
printf "==> %s %s %s\033[0m\n" "$(date +"${TIMESTAMP_FMT}")" "${1}" "$*";
else
printf "==> %s %s\033[0m\n" "$(date +"${TIMESTAMP_FMT}")" "${1}";
fi;
unset _lvl;
};
parse_with_pkg_name() {
_pkg_name=${1}; shift;
while [ $# -ge 0 ]; do
if [ "x${_pkg_name}" = "x${1}" ]; then
_pkg_NAME=$(echo "${_pkg_name}" | tr a-z A-Z);
eval _pkg_configure_extra_args=\"\${PKG_${_pkg_NAME}_CONFIGURE_EXTRA_ARGS}\"\
_pkg_no_libtool_midipix=\${PKG_${_pkg_NAME}_NO_LIBTOOL_MIDIPIX}\
_pkg_prefix=\${PKG_${_pkg_NAME}_PREFIX}\
_pkg_prefix_extra=\${PKG_${_pkg_NAME}_PREFIX_EXTRA}\
_pkg_sha256sum=\${PKG_${_pkg_NAME}_SHA256SUM}\
_pkg_url=\${PKG_${_pkg_NAME}_URL}\
_pkg_version=\${PKG_${_pkg_NAME}_VERSION}\
_pkg_CC=\${PKG_${_pkg_NAME}_CC}\
_pkg_CFLAGS=\${PKG_${_pkg_NAME}_CFLAGS}\
_pkg_DESTDIR=\${PKG_${_pkg_NAME}_DESTDIR};
_pkg_fname=${_pkg_url##*/};
_pkg_subdir=${_pkg_fname%%.tar*};
return 0;
fi; shift;
done; return 1;
};
rm_if_exists() {
while [ $# -gt 1 ]; do [ "x${1%[a-z]}" = "x-" ] &&\
eval _${1#-}flag=1; shift; done;
_dir=${1};
if [ -d ${1} ]; then
rm -rf ${1} || return 1;
fi;
if [ ${_mflag:-0} -eq 1 ]; then
unset _mflag; mkdir ${1} || return 2;
fi;
if [ ${_cflag:-0} -eq 1 ]; then
unset _cflag; cd ${1} || return 3;
fi;
};
set_build_dir() {
_build_dir=${1}-${2}-${TARGET};
};
set_env_vars() {
_val=${1}; shift;
while [ $# -ge 1 ]; do
export "${1}=${_val}"; shift;
done; unset _val;
};
split() {
_IFS="${IFS}"; IFS="${1}"; set -- ${2};
IFS="${_IFS}"; echo "${*}"; unset _IFS;
};
# vim:filetype=sh