Blob Blame History Raw
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

#
# rtl_export_vars() - 
# @_unsetfl:	
# 
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_export_vars() {
	local _rev_unsetfl=0;

	if [ "x${1}" = "x-u" ]; then
		_rev_unsetfl=1; shift;
	fi;

	while [ "${#}" -ge 2 ]; do
		if [ "${2:+1}" = 1 ]\
		|| [ "${_rev_unsetfl}" -eq 1 ]; then
			case "${_rev_unsetfl}" in
			0)	rtl_set_var_unsafe "${1}" "${2}";
				export "${1}"; ;;
			1)	unset "${1}"; ;;
			esac;
		fi; shift 2;
	done;

	return 0;
};

rtl_head() {
	local _rh_pattern="${1}" _rh_s="${2}" _rh_rs_out="${3#\$}";

	while true; do
		if [ "${_rh_s%%${_rh_pattern}}" = "${_rh_s}" ]; then
			break;
		else
			_rh_s="${_rh_s%%${_rh_pattern}}";
		fi;
	done;

	eval ${_rh_rs_out}='${_rh_s}';
	return 0;
};

rtl_percentage() {
	local	_rp_in="${1}" _rp_max="${2}" _rp_rs_out="${3#\$}"	\
		_rp_perc;

	_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
	_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));

	eval ${_rp_rs_out}='${_rp_perc}';
	return 0;
};

rtl_percentage2() {
	local	_rp_rin="${1#\$}" _rp_rmax="${2#\$}" _rp_rs_out="${3#\$}"	\
		_rp_in=0 _rp_max=0 _rp_perc;

	eval _rp_in=\"\${${_rp_rin}}\";
	eval _rp_max=\"\${${_rp_rmax}}\";

	_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
	_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));

	eval ${_rp_rs_out}='${_rp_perc}';
	return 0;
};

rtl_sunset() {
	local _rs_rset="${1#\$}" _rs_kname="" IFS=" 	";

	eval set -- "\${${_rs_rset}}";
	while [ "${#}" -gt 0 ]; do
		unset "${_rs_rset}${_rs_kname}"; shift;
	done; unset "${_rs_rset}";

	return 0;
};

# vim:filetype=sh textwidth=0