Blob Blame History Raw
#
# set +o errexit -o noglob -o nounset is assumed.
#

rtl_basename() {
	rtl_basename2 "${1}" "${1}";
};

rtl_basename2() {
	local	_rb2_rfname="${1#\$}" _rb2_rfname_out="${2#\$}"	\
		_rb2_fname="";

	eval _rb2_fname="\${${_rb2_rfname}}";
	eval ${_rb2_rfname_out}='${_rb2_fname##*/}';
	return 0;
};

rtl_check_digest() {
	local	_rcd_rdigest="${1#\$}" _rcd_fname="${2}" _rcd_digest_check="${3}"	\
		_rcd_digest="";

	if ! [ -e "${_rcd_fname}" ]; then
		return 1;
	else	set -- $(sha256sum "${_rcd_fname}");
		_rcd_digest="${1}";
		eval ${_rcd_rdigest}='${_rcd_digest}';
		if [ "${_rcd_digest}" = "${_rcd_digest_check}" ]; then
			return 0;
		else
			return 1;
		fi;
	fi;
};

rtl_check_digest_file() {
	local	_rcdf_fname="${1}" _rcdf_digest_check="${2}" _rcdf_digest_fname="${3}"	\
		_rcdf_digest="" _rcdf_digest_file="";

	if !  [ -e "${_rcdf_digest_fname}" ]; then
		return 1;
	else	_rcdf_digest_file="$(cat "${_rcdf_digest_fname}")";
		if [ "${_rcdf_digest_file}" != "${_rcdf_digest_check}" ]\
		|| ! rtl_check_digest \$_rcdf_digest "${_rcdf_fname}"	\
				"${_rcdf_digest_check}";
		then
			return 1;
		else
			return 0;
		fi;
	fi;
};

rtl_check_path_vars() {
	local	_rcpv_rstatus="${1#\$}" _rcpv_vnames="${2}"	\
		_rcpv_rc=0 _rcpv_vname="" _rcpv_vname_val="";

	for _rcpv_vname in ${_rcpv_vnames}; do
		rtl_get_var_unsafe \$_rcpv_vname_val "${_rcpv_vname}";
		if [ "${_rcpv_vname_val:+1}" != 1 ]; then
			_rcpv_rc=1;
			rtl_setrstatus "${_rcpv_rstatus}" 'Error: variable \`'"${_rcpv_vname}'"' is empty or unset.';
			break;
		elif [ "${_rcpv_vname_val#* *}" != "${_rcpv_vname_val}" ]; then
			_rcpv_rc=2;
			rtl_setrstatus "${_rcpv_rstatus}" 'Error: variable \`'"${_rcpv_vname}'"' contains one or more whitespace characters.';
			break;
		fi;
	done;
	return "${_rcpv_rc}";
};

rtl_dirname() {
	rtl_dirname2 "${1}" "${1}";
};

rtl_dirname2() {
	local	_rdname="${1#\$}" _rdname_out="${2#\$}"	\
		_rd2_dname="";

	eval _rd2_dname="\${${_rdname}}";
	_rd2_dname="${_rd2_dname%/*}";

	case "${_rd2_dname}" in
	"")	_rd2_dname="."; ;;
	*)	while rtl_matchr "${_rd2_dname}" "*/"; do
			_rd2_dname="${_rd2_dname%/}";
		done; ;;
	esac;

	eval ${_rdname_out}='${_rd2_dname}';
	return 0;
};

rtl_exists_any() {
	local _rea_subdir="${1}"; shift;

	while [ "${#}" -gt 0 ]; do
		if [ -e "${_rea_subdir}/${1}" ]; then
			return 0;
		else
			shift;
		fi;
	done;
	return 1;
};

rtl_flock_acquire() {
	local _rfa_fd="${1}" _rfa_conflict_exit_code="${2:-253}" _rfa_wait="${3:-3600}";

	while true; do
		if flock				\
			-E "${_rfa_conflict_exit_code}"	\
			-w "${_rfa_wait}"		\
			"${_rfa_fd}";
		then
			return 0;
		elif [ "${?}" -eq "${_rfa_conflict_exit_code}" ]; then
			continue;
		else
			return "${?}";
		fi;
	done;
};

rtl_is_newer() {
	local	_ris_new_fname="${1}" _ris_old_fname="${2}"	\
		_ris_new_ts="" _ris_old_ts="";

	if ! [ -e "${_ris_old_fname}" ]; then
		return 0;
	else
		_ris_new_ts="$(stat -c %Y "${_ris_new_fname}" 2>/dev/null)";
		_ris_old_ts="$(stat -c %Y "${_ris_old_fname}" 2>/dev/null)";

		if [ "${_ris_new_ts:-0}" -gt "${_ris_old_ts:-0}" ]; then
			return 0;
		else
			return 1;
		fi;
	fi;
};

# vim:filetype=sh textwidth=0