summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--subr.rtl/rtl.subr1
-rw-r--r--subr.rtl/rtl_complex.subr203
-rw-r--r--subr.rtl/rtl_fetch.subr90
-rw-r--r--subr.rtl/rtl_fetch_git.subr95
-rw-r--r--subr.rtl/rtl_filepath.subr96
-rw-r--r--subr.rtl/rtl_platform.subr124
6 files changed, 315 insertions, 294 deletions
diff --git a/subr.rtl/rtl.subr b/subr.rtl/rtl.subr
index b123555f..532cb514 100644
--- a/subr.rtl/rtl.subr
+++ b/subr.rtl/rtl.subr
@@ -2,7 +2,6 @@
# set +o errexit -o noglob -o nounset is assumed.
#
-rtl_basename() { local _fname="${1##*/}"; printf "%s" "${_fname}"; };
rtl_date() { command date "+${1:-${DEFAULT_TIMESTAMP_FMT:-"%Y/%m/%d %H:%M:%S"}}"; };
rtl_set_IFS_nl() { IFS="
"; };
diff --git a/subr.rtl/rtl_complex.subr b/subr.rtl/rtl_complex.subr
index 34b87837..8866484f 100644
--- a/subr.rtl/rtl_complex.subr
+++ b/subr.rtl/rtl_complex.subr
@@ -2,88 +2,6 @@
# set +o errexit -o noglob -o nounset is assumed.
#
-rtl_check_digest() {
- local _fname="${1}" _digest_check="${2}" _digest=""; RTL_CHECK_DIGEST_DIGEST="";
- if ! [ -e "${_fname}" ]; then
- return 1;
- else set -- $(sha256sum "${_fname}");
- RTL_CHECK_DIGEST_DIGEST="${1}";
- if [ "${RTL_CHECK_DIGEST_DIGEST}" = "${_digest_check}" ]; then
- return 0;
- else
- return 1;
- fi;
- fi;
-};
-
-rtl_check_digest_file() {
- local _fname="${1}" _digest_check="${2}" _digest_fname="${3}" _digest="" RTL_CHECK_DIGEST_DIGEST="";
- if ! [ -e "${_digest_fname}" ]; then
- return 1;
- else _digest="$(cat "${_digest_fname}")";
- if [ "${_digest}" != "${_digest_check}" ]\
- || ! rtl_check_digest "${_fname}" "${_digest_check}"; then
- return 1;
- fi;
- fi;
-};
-
-rtl_check_path_vars() {
- local _vnames="${1}" _rc=0 _vname="" _vname_val=""; _status="";
- for _vname in ${_vnames}; do
- _vname_val="$(rtl_get_var_unsafe "${_vname}")";
- if [ -z "${_vname_val}" ]; then
- _rc=1; _status="Error: variable \`${_vname}' is empty or unset."; break;
- elif [ "${_vname_val#* *}" != "${_vname_val}" ]; then
- _rc=2; _status="Error: variable \`${_vname}' contains one or more whitespace characters."; break;
- fi;
- done;
- return "${_rc}";
-};
-
-rtl_check_prereqs() {
- local _cmd="" _cmds_missing="" _rc=0; _status="";
- for _cmd in "${@}"; do
- if ! which "${_cmd}" >/dev/null 2>&1; then
- _cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
- fi;
- done;
- if [ -n "${_cmds_missing}" ]; then
- _rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
- fi;
- return "${_rc}";
-};
-
-rtl_clean_env() {
- local _env_vars_except="${1}" _env_var="" _env_vars="";
- _env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
- for _env_var in ${_env_vars}; do
- if [ "${_env_var#DEFAULT_}" != "${_env_var}" ]\
- || [ "${_env_var#PKG_}" != "${_env_var}" ]; then
- _env_vars_except="$(rtl_lconcat "${_env_vars_except}" "${_env_var}")";
- fi;
- done;
- rtl_unset_vars $(rtl_lfilter "${_env_vars}" "${_env_vars_except}");
-};
-
-rtl_dirname() {
- local _dname="${1%/*}";
- while rtl_matchr "${_dname}" "*/"; do
- _dname="${_dname%/}";
- done; printf "%s" "${_dname:-/}";
-};
-
-rtl_exists_any() {
- local _subdir="${1}"; shift;
- while [ "${#}" -gt 0 ]; do
- if [ -e "${_subdir}/${1}" ]; then
- return 0;
- else
- shift;
- fi;
- done; return 1;
-};
-
rtl_export_vars() {
local _unsetfl=0; [ "x${1}" = "x-u" ] && { _unsetfl=1; shift; };
while [ "${#}" -ge 2 ]; do
@@ -135,49 +53,6 @@ rtl_filter_vars() {
printf "%s" "${_vars}";
};
-rtl_flock_acquire() {
- local _fd="${1}" _conflict_exit_code="${2:-253}" _wait="${3:-3600}"
- while true; do
- if flock -E "${_conflict_exit_code}" -w "${_wait}" "${_fd}"; then
- break;
- elif [ "${?}" -eq "${_conflict_exit_code}" ]; then
- continue;
- else
- return "${?}";
- fi;
- done;
-};
-
-rtl_get_cpu_count() {
- local _line="" _ncpus=0 _rc=0 _sname="$(uname -s 2>/dev/null)"; _status="";
- case "${_sname}" in
- Linux) if [ ! -e "/proc/cpuinfo" ]; then
- _rc=1; _status="Error: /proc/cpuinfo non-existent.";
- else while read -r _line; do
- if rtl_match "${_line}" "processor*:"; then
- : $((_ncpus+=1));
- fi;
- done < /proc/cpuinfo; printf "%s" "${_ncpus}";
- fi; ;;
- *) _rc=1; _status="Error: unknown platform \`${_sname}'."; ;;
- esac; return "${_rc}";
-};
-
-rtl_get_var_unsafe() {
- local _vname="";
- if [ "x${1}" = "x-u" ]; then
- shift; _vname="$(rtl_toupper "${1}")";
- else
- _vname="${1}";
- fi;
- eval echo \${${_vname}} 2>/dev/null;
-};
-
-rtl_get_vars_fast() {
- local _pattern="${1}";
- set | awk -F= '/'"${_pattern}"'/{print $1}' | sort;
-};
-
rtl_head() {
local _pattern="${1}" _s="${2}";
while true; do
@@ -190,34 +65,6 @@ rtl_head() {
printf "%s" "${_s}";
};
-rtl_is_newer() {
- local _new_fname="${1}" _old_fname="${2}" _new_ts="" _old_ts="";
- if ! [ -e "${_old_fname}" ]; then
- return 0;
- else _new_ts="$(stat -c %Y "${_new_fname}" 2>&1)";
- _old_ts="$(stat -c %Y "${_old_fname}" 2>&1)";
- if [ "${_new_ts:-0}" -gt "${_old_ts:-0}" ]; then
- return 0;
- else
- return 1;
- fi;
- fi;
-};
-
-rtl_kill_tree() {
- local _pid="${1}" _signal="${2:-TERM}" _pid_child="" _pids="";
- if _pids="$(pgrep -P "${_pid}")"\
- && [ -n "${_pids}" ]; then
- for _pid_child in ${_pids}; do
- rtl_kill_tree "${_pid_child}" "${_signal}";
- done;
- fi;
- if [ "${_pid:-0}" -ne "${$}" ]\
- && kill "-${_signal}" "${_pid}" 2>/dev/null; then
- RTL_KILL_TREE_PIDS="$(rtl_lconcat "${RTL_KILL_TREE_PIDS}" "${_pid}")";
- fi;
-};
-
rtl_percentage() {
local _in="${1}" _max="${2}" _perc;
_perc="$((100 * ${_in} + ${_max} / 2))";
@@ -225,54 +72,4 @@ rtl_percentage() {
printf "%d\n" "${_perc}";
};
-rtl_prompt() {
- local _fmt="${1}" _choice=""; shift;
- printf "${_fmt}? (y|N) " "${@}";
- read -r _choice;
- case "${_choice}" in
- [yY]) _choice=1; ;;
- *) _choice=0; ;;
- esac;
- return "${_choice}";
-};
-
-rtl_rc() {
- local _nflag="${1}" _cmd="${2}"; shift 2;
- case "${_nflag}" in
- 1) if [ "${#}" -gt 0 ]; then
- rtl_log_msg notice "Command line: %s %s" "${_cmd}" "${*}";
- else
- rtl_log_msg notice "Command line: %s" "${_cmd}";
- fi; ;;
- *) "${_cmd}" "${@}";
- esac;
-};
-
-rtl_run_cmd_unsplit() {
- local _cmd="${1}" _cmdline="" _rc="" IFS; shift;
- while [ ${#} -gt 0 ]; do
- [ -n "${1}" ] &&\
- _cmdline="${_cmdline:+${_cmdline}:}${1}";
- shift;
- done;
- IFS=:; ${_cmd} ${_cmdline}; _rc=$?;
- return ${_rc};
-};
-
-rtl_set_vars() {
- local _vars_set_vname="${1}" _vname_dst="${2}" _vname_src_tmpls="${3}" \
- _vars_set_old="" _vars_set_tmp="" _vname_src="";
- for _vname_src in $(rtl_toupper "${_vname_src_tmpls}"); do
- _vname_src="${_vname_src}_${_vname_dst}";
- _vval_src="$(rtl_get_var_unsafe "${_vname_src}")";
- if [ -n "${_vval_src}" ]; then
- rtl_set_var_unsafe "PKG_${_vname_dst}" "${_vval_src}";
- _vars_set_tmp="$(rtl_lconcat "${_vars_set_tmp}" "PKG_${_vname_dst}")";
- fi;
- done;
- _vars_set_old="$(rtl_get_var_unsafe "${_vars_set_vname}")";
- rtl_set_var_unsafe "${_vars_set_vname}" \
- "$(rtl_lconcat "${_vars_set_old}" "${_vars_set_tmp}")";
-};
-
# vim:filetype=sh
diff --git a/subr.rtl/rtl_fetch.subr b/subr.rtl/rtl_fetch.subr
index f5410e30..c8e12d1b 100644
--- a/subr.rtl/rtl_fetch.subr
+++ b/subr.rtl/rtl_fetch.subr
@@ -2,96 +2,6 @@
# set +o errexit -o noglob -o nounset is assumed.
#
-rtl_fetch_mirror_urls_git() {
- local _git_args="${1}" _tgtdir="${2}" _rc=0 _repo_dname="" _subdir="" _url="" _url_spec=""; shift 2;
-
- for _url_spec in "${@}"; do
- _subdir="${_url_spec%=*}"; _subdir="${_subdir##*/}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
- _repo_dname="${_url##*/}"; [ "${_repo_dname%.git}" = "${_repo_dname}" ] && _repo_dname="${_repo_dname}.git";
-
- if [ ! -e "$(rtl_dirname "${_tgtdir}")" ]; then
- rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}")";
- fi;
- (set -o errexit -o noglob -o nounset;
- rtl_flock_acquire 4 || exit "${?}";
- trap "rm -f \"${_tgtdir}/.fetching\"" EXIT;
- if [ -e "${_tgtdir}/${_repo_dname}" ]; then
- (rtl_fileop cd "${_tgtdir}/${_repo_dname}" && git fetch ${_git_args} --all) || return 1;
- else (rtl_fileop cd "${_tgtdir}" && git clone ${_git_args} --mirror "${_url}") || return 1;
- fi) 4<>"${_tgtdir}/.fetching";
- if [ "${?}" -ne 0 ]; then
- _rc=1;
- fi;
- done; return "${_rc}";
-};
-
-rtlp_fetch_url_git() {
- local _cache_dname="${1}" _git_args="${2}" _git_branch="${3}" _mirrors="${4}" _pkg_name="${5}"\
- _subdir="${6}" _tgtdir="${7}" _url="${8}" _cache_dname_full="" _clonefl=0 _oldpwd="" _url_base="";
-
- _cache_dname_full="${_cache_dname}/${_subdir##*/}";
- (set -o errexit -o noglob -o nounset;
- rtl_flock_acquire 4 || exit "${?}";
- trap "rm -f \"${_cache_dname_full%%[/]}.fetching\"" EXIT;
- if [ -e "${_cache_dname_full}" ]; then
- (rtl_fileop cd "${_cache_dname_full}" &&\
- git pull ${_git_args} origin "${_git_branch:-main}") || return 1;
- (rtl_fileop cd "${_cache_dname_full}" &&\
- git submodule update) || return 1;
- else if git clone ${_git_args} -b "${_git_branch:-main}" "${_url}" "${_cache_dname_full}"; then
- _clonefl=1;
- elif [ "${_mirrors}" = "skip" ]; then
- return 1;
- else for _url_base in ${_mirrors}; do
- if git clone ${_git_args} -b "${_git_branch:-main}" "${_url_base}/${_pkg_name}/${_url##*/}" "${_cache_dname_full}"; then
- _clonefl=1; break;
- fi;
- done;
- fi;
- if [ "${_clonefl}" -eq 0 ]; then
- return 1;
- else if [ -n "${_git_branch}" ]; then
- (rtl_fileop cd "${_cache_dname_full}" &&\
- git checkout "${_git_branch}") || return 1;
- fi;
- (rtl_fileop cd "${_cache_dname_full}" &&\
- git submodule update --init) || return 1;
- fi;
- fi;
- if [ "${_cache_dname}" != "${_tgtdir}" ]; then
- _oldpwd="${PWD}"; rtl_fileop cd "${_tgtdir}" || return 1;
- rtl_fileop rm "${_tgtdir}/${_subdir}" || return 1;
- if [ ! -e "$(rtl_dirname "${_tgtdir}/${_subdir}")" ]; then
- rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}/${_subdir}")";
- fi;
- rtl_fileop cp "${_cache_dname_full}" "${_tgtdir}/${_subdir}" || return 1;
- rtl_fileop cd "${_oldpwd}" || return 1;
- fi) 4<>"${_cache_dname_full%%[/]}.fetching";
-
- if [ "${?}" -eq 0 ]; then
- cd "$(pwd)";
- else
- return 1;
- fi;
-};
-
-rtl_fetch_urls_git() {
- local _cache_dname="${1}" _git_args="${2}" _tgtdir="${3}" _pkg_name="${4}" _mirrors="${5}"\
- _git_branch="" _subdir="" _url="" _url_spec=""; shift 5;
-
- for _url_spec in "${@}"; do
- _subdir="${_url_spec%=*}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
- if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
- _git_branch=${_url_spec#*@};
- fi;
- if ! rtlp_fetch_url_git \
- "${_cache_dname}" "${_git_args}" "${_git_branch}" "${_mirrors}"\
- "${_pkg_name}" "${_subdir}" "${_tgtdir}" "${_url}"; then
- return 1;
- fi;
- done;
-};
-
# N.B. URLs ($1) may contain `?' or '&' characters.
rtl_fetch_url_wget() {
local _urls="${1}" _sha256sum_src="${2}" _target_dname="${3}" _target_fname="${4}" _target_name="${5}" _mirrors="${6:-}"\
diff --git a/subr.rtl/rtl_fetch_git.subr b/subr.rtl/rtl_fetch_git.subr
new file mode 100644
index 00000000..2d6ce8ab
--- /dev/null
+++ b/subr.rtl/rtl_fetch_git.subr
@@ -0,0 +1,95 @@
+#
+# set +o errexit -o noglob -o nounset is assumed.
+#
+
+rtlp_fetch_url_git() {
+ local _cache_dname="${1}" _git_args="${2}" _git_branch="${3}" _mirrors="${4}" _pkg_name="${5}"\
+ _subdir="${6}" _tgtdir="${7}" _url="${8}" _cache_dname_full="" _clonefl=0 _oldpwd="" _url_base="";
+
+ _cache_dname_full="${_cache_dname}/${_subdir##*/}";
+ (set -o errexit -o noglob -o nounset;
+ rtl_flock_acquire 4 || exit "${?}";
+ trap "rm -f \"${_cache_dname_full%%[/]}.fetching\"" EXIT;
+ if [ -e "${_cache_dname_full}" ]; then
+ (rtl_fileop cd "${_cache_dname_full}" &&\
+ git pull ${_git_args} origin "${_git_branch:-main}") || return 1;
+ (rtl_fileop cd "${_cache_dname_full}" &&\
+ git submodule update) || return 1;
+ else if git clone ${_git_args} -b "${_git_branch:-main}" "${_url}" "${_cache_dname_full}"; then
+ _clonefl=1;
+ elif [ "${_mirrors}" = "skip" ]; then
+ return 1;
+ else for _url_base in ${_mirrors}; do
+ if git clone ${_git_args} -b "${_git_branch:-main}" "${_url_base}/${_pkg_name}/${_url##*/}" "${_cache_dname_full}"; then
+ _clonefl=1; break;
+ fi;
+ done;
+ fi;
+ if [ "${_clonefl}" -eq 0 ]; then
+ return 1;
+ else if [ -n "${_git_branch}" ]; then
+ (rtl_fileop cd "${_cache_dname_full}" &&\
+ git checkout "${_git_branch}") || return 1;
+ fi;
+ (rtl_fileop cd "${_cache_dname_full}" &&\
+ git submodule update --init) || return 1;
+ fi;
+ fi;
+ if [ "${_cache_dname}" != "${_tgtdir}" ]; then
+ _oldpwd="${PWD}"; rtl_fileop cd "${_tgtdir}" || return 1;
+ rtl_fileop rm "${_tgtdir}/${_subdir}" || return 1;
+ if [ ! -e "$(rtl_dirname "${_tgtdir}/${_subdir}")" ]; then
+ rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}/${_subdir}")";
+ fi;
+ rtl_fileop cp "${_cache_dname_full}" "${_tgtdir}/${_subdir}" || return 1;
+ rtl_fileop cd "${_oldpwd}" || return 1;
+ fi) 4<>"${_cache_dname_full%%[/]}.fetching";
+
+ if [ "${?}" -eq 0 ]; then
+ cd "$(pwd)";
+ else
+ return 1;
+ fi;
+};
+
+rtl_fetch_mirror_urls_git() {
+ local _git_args="${1}" _tgtdir="${2}" _rc=0 _repo_dname="" _subdir="" _url="" _url_spec=""; shift 2;
+
+ for _url_spec in "${@}"; do
+ _subdir="${_url_spec%=*}"; _subdir="${_subdir##*/}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
+ _repo_dname="${_url##*/}"; [ "${_repo_dname%.git}" = "${_repo_dname}" ] && _repo_dname="${_repo_dname}.git";
+
+ if [ ! -e "$(rtl_dirname "${_tgtdir}")" ]; then
+ rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}")";
+ fi;
+ (set -o errexit -o noglob -o nounset;
+ rtl_flock_acquire 4 || exit "${?}";
+ trap "rm -f \"${_tgtdir}/.fetching\"" EXIT;
+ if [ -e "${_tgtdir}/${_repo_dname}" ]; then
+ (rtl_fileop cd "${_tgtdir}/${_repo_dname}" && git fetch ${_git_args} --all) || return 1;
+ else (rtl_fileop cd "${_tgtdir}" && git clone ${_git_args} --mirror "${_url}") || return 1;
+ fi) 4<>"${_tgtdir}/.fetching";
+ if [ "${?}" -ne 0 ]; then
+ _rc=1;
+ fi;
+ done; return "${_rc}";
+};
+
+rtl_fetch_urls_git() {
+ local _cache_dname="${1}" _git_args="${2}" _tgtdir="${3}" _pkg_name="${4}" _mirrors="${5}"\
+ _git_branch="" _subdir="" _url="" _url_spec=""; shift 5;
+
+ for _url_spec in "${@}"; do
+ _subdir="${_url_spec%=*}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
+ if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
+ _git_branch=${_url_spec#*@};
+ fi;
+ if ! rtlp_fetch_url_git \
+ "${_cache_dname}" "${_git_args}" "${_git_branch}" "${_mirrors}"\
+ "${_pkg_name}" "${_subdir}" "${_tgtdir}" "${_url}"; then
+ return 1;
+ fi;
+ done;
+};
+
+# vim:filetype=sh
diff --git a/subr.rtl/rtl_filepath.subr b/subr.rtl/rtl_filepath.subr
new file mode 100644
index 00000000..fa1baf57
--- /dev/null
+++ b/subr.rtl/rtl_filepath.subr
@@ -0,0 +1,96 @@
+#
+# set +o errexit -o noglob -o nounset is assumed.
+#
+
+rtl_basename() {
+ local _fname="${1##*/}"; printf "%s" "${_fname}";
+};
+
+rtl_check_digest() {
+ local _fname="${1}" _digest_check="${2}" _digest=""; RTL_CHECK_DIGEST_DIGEST="";
+ if ! [ -e "${_fname}" ]; then
+ return 1;
+ else set -- $(sha256sum "${_fname}");
+ RTL_CHECK_DIGEST_DIGEST="${1}";
+ if [ "${RTL_CHECK_DIGEST_DIGEST}" = "${_digest_check}" ]; then
+ return 0;
+ else
+ return 1;
+ fi;
+ fi;
+};
+
+rtl_check_digest_file() {
+ local _fname="${1}" _digest_check="${2}" _digest_fname="${3}" _digest="" RTL_CHECK_DIGEST_DIGEST="";
+ if ! [ -e "${_digest_fname}" ]; then
+ return 1;
+ else _digest="$(cat "${_digest_fname}")";
+ if [ "${_digest}" != "${_digest_check}" ]\
+ || ! rtl_check_digest "${_fname}" "${_digest_check}"; then
+ return 1;
+ fi;
+ fi;
+};
+
+rtl_check_path_vars() {
+ local _vnames="${1}" _rc=0 _vname="" _vname_val=""; _status="";
+ for _vname in ${_vnames}; do
+ _vname_val="$(rtl_get_var_unsafe "${_vname}")";
+ if [ -z "${_vname_val}" ]; then
+ _rc=1; _status="Error: variable \`${_vname}' is empty or unset."; break;
+ elif [ "${_vname_val#* *}" != "${_vname_val}" ]; then
+ _rc=2; _status="Error: variable \`${_vname}' contains one or more whitespace characters."; break;
+ fi;
+ done;
+ return "${_rc}";
+};
+
+rtl_dirname() {
+ local _dname="${1%/*}";
+ case "${_dname}" in
+ "") _dname="."; ;;
+ *) while rtl_matchr "${_dname}" "*/"; do
+ _dname="${_dname%/}";
+ done; ;;
+ esac; printf "%s" "${_dname:-/}";
+};
+
+rtl_exists_any() {
+ local _subdir="${1}"; shift;
+ while [ "${#}" -gt 0 ]; do
+ if [ -e "${_subdir}/${1}" ]; then
+ return 0;
+ else
+ shift;
+ fi;
+ done; return 1;
+};
+
+rtl_flock_acquire() {
+ local _fd="${1}" _conflict_exit_code="${2:-253}" _wait="${3:-3600}"
+ while true; do
+ if flock -E "${_conflict_exit_code}" -w "${_wait}" "${_fd}"; then
+ break;
+ elif [ "${?}" -eq "${_conflict_exit_code}" ]; then
+ continue;
+ else
+ return "${?}";
+ fi;
+ done;
+};
+
+rtl_is_newer() {
+ local _new_fname="${1}" _old_fname="${2}" _new_ts="" _old_ts="";
+ if ! [ -e "${_old_fname}" ]; then
+ return 0;
+ else _new_ts="$(stat -c %Y "${_new_fname}" 2>&1)";
+ _old_ts="$(stat -c %Y "${_old_fname}" 2>&1)";
+ if [ "${_new_ts:-0}" -gt "${_old_ts:-0}" ]; then
+ return 0;
+ else
+ return 1;
+ fi;
+ fi;
+};
+
+# vim:filetype=sh
diff --git a/subr.rtl/rtl_platform.subr b/subr.rtl/rtl_platform.subr
new file mode 100644
index 00000000..c8137e56
--- /dev/null
+++ b/subr.rtl/rtl_platform.subr
@@ -0,0 +1,124 @@
+#
+# set +o errexit -o noglob -o nounset is assumed.
+#
+
+rtl_check_prereqs() {
+ local _cmd="" _cmds_missing="" _rc=0; _status="";
+ for _cmd in "${@}"; do
+ if ! which "${_cmd}" >/dev/null 2>&1; then
+ _cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
+ fi;
+ done;
+ if [ -n "${_cmds_missing}" ]; then
+ _rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
+ fi;
+ return "${_rc}";
+};
+
+rtl_clean_env() {
+ local _env_vars_except="${1}" _env_var="" _env_vars="";
+ _env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
+ for _env_var in ${_env_vars}; do
+ if [ "${_env_var#DEFAULT_}" != "${_env_var}" ]\
+ || [ "${_env_var#PKG_}" != "${_env_var}" ]; then
+ _env_vars_except="$(rtl_lconcat "${_env_vars_except}" "${_env_var}")";
+ fi;
+ done;
+ rtl_unset_vars $(rtl_lfilter "${_env_vars}" "${_env_vars_except}");
+};
+
+rtl_get_cpu_count() {
+ local _line="" _ncpus=0 _rc=0 _sname="$(uname -s 2>/dev/null)"; _status="";
+ case "${_sname}" in
+ Linux) if [ ! -e "/proc/cpuinfo" ]; then
+ _rc=1; _status="Error: /proc/cpuinfo non-existent.";
+ else while read -r _line; do
+ if rtl_match "${_line}" "processor*:"; then
+ : $((_ncpus+=1));
+ fi;
+ done < /proc/cpuinfo; printf "%s" "${_ncpus}";
+ fi; ;;
+ *) _rc=1; _status="Error: unknown platform \`${_sname}'."; ;;
+ esac; return "${_rc}";
+};
+
+rtl_get_var_unsafe() {
+ local _vname="";
+ if [ "x${1}" = "x-u" ]; then
+ shift; _vname="$(rtl_toupper "${1}")";
+ else
+ _vname="${1}";
+ fi;
+ eval echo \${${_vname}} 2>/dev/null;
+};
+
+rtl_get_vars_fast() {
+ local _pattern="${1}";
+ set | awk -F= '/'"${_pattern}"'/{print $1}' | sort;
+};
+
+rtl_kill_tree() {
+ local _pid="${1}" _signal="${2:-TERM}" _pid_child="" _pids="";
+ if _pids="$(pgrep -P "${_pid}")"\
+ && [ -n "${_pids}" ]; then
+ for _pid_child in ${_pids}; do
+ rtl_kill_tree "${_pid_child}" "${_signal}";
+ done;
+ fi;
+ if [ "${_pid:-0}" -ne "${$}" ]\
+ && kill "-${_signal}" "${_pid}" 2>/dev/null; then
+ RTL_KILL_TREE_PIDS="$(rtl_lconcat "${RTL_KILL_TREE_PIDS}" "${_pid}")";
+ fi;
+};
+
+rtl_prompt() {
+ local _fmt="${1}" _choice=""; shift;
+ printf "${_fmt}? (y|N) " "${@}";
+ read -r _choice;
+ case "${_choice}" in
+ [yY]) _choice=1; ;;
+ *) _choice=0; ;;
+ esac;
+ return "${_choice}";
+};
+
+rtl_rc() {
+ local _nflag="${1}" _cmd="${2}"; shift 2;
+ case "${_nflag}" in
+ 1) if [ "${#}" -gt 0 ]; then
+ rtl_log_msg notice "Command line: %s %s" "${_cmd}" "${*}";
+ else
+ rtl_log_msg notice "Command line: %s" "${_cmd}";
+ fi; ;;
+ *) "${_cmd}" "${@}";
+ esac;
+};
+
+rtl_run_cmd_unsplit() {
+ local _cmd="${1}" _cmdline="" _rc="" IFS; shift;
+ while [ ${#} -gt 0 ]; do
+ [ -n "${1}" ] &&\
+ _cmdline="${_cmdline:+${_cmdline}:}${1}";
+ shift;
+ done;
+ IFS=:; ${_cmd} ${_cmdline}; _rc=$?;
+ return ${_rc};
+};
+
+rtl_set_vars() {
+ local _vars_set_vname="${1}" _vname_dst="${2}" _vname_src_tmpls="${3}" \
+ _vars_set_old="" _vars_set_tmp="" _vname_src="";
+ for _vname_src in $(rtl_toupper "${_vname_src_tmpls}"); do
+ _vname_src="${_vname_src}_${_vname_dst}";
+ _vval_src="$(rtl_get_var_unsafe "${_vname_src}")";
+ if [ -n "${_vval_src}" ]; then
+ rtl_set_var_unsafe "PKG_${_vname_dst}" "${_vval_src}";
+ _vars_set_tmp="$(rtl_lconcat "${_vars_set_tmp}" "PKG_${_vname_dst}")";
+ fi;
+ done;
+ _vars_set_old="$(rtl_get_var_unsafe "${_vars_set_vname}")";
+ rtl_set_var_unsafe "${_vars_set_vname}" \
+ "$(rtl_lconcat "${_vars_set_old}" "${_vars_set_tmp}")";
+};
+
+# vim:filetype=sh