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_install() - install files
# @[-v]:	increase verbosity
# @_prefix:	installation prefix pathname
# @...:		install operation (one of: -* (remove,) !*=* (move/rename,) /=* (create directory/ies,) @*=* (create/update symbolic link,) m[0-7][0-7][0-7][0-7]=* (set mode bits,) o*=* (set user/group owner,) *=* (copy))
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_install() {
	local	_ri_verbose="" _ri_prefix=""							\
		_ri_chmod_mode="" _ri_dname="" _ri_file_fname_dst="" _ri_file_fname_src=""	\
		_ri_fname="" _ri_ln_fname="" _ri_ln_target="" _ri_owner_spec="" _ri_pname="";

	if [ "${1}" = "-v" ]; then
	       _ri_verbose=1; shift;
	fi;
	_ri_prefix="${1}"; shift;

	while [ ${#} -gt 0 ]; do
	case "${1}" in

	-*)
		_ri_pname="${1#-}";
		if [ "${_ri_pname#/}" = "${_ri_pname}" ]; then
			_ri_pname="${_ri_prefix:+${_ri_prefix}/}${_ri_pname}";
		fi;
		if ! rtl_fileop rm "${_ri_pname}"; then
			return 1;
		fi; ;;

	!*=*)
		_ri_file_fname_src="${1#!}"; _ri_file_fname_src="${_ri_file_fname_src%=*}";
	       	_ri_file_fname_dst="${1#!}"; _ri_file_fname_dst="${_ri_file_fname_dst#*=}";
		if [ "${_ri_file_fname_src#/}" = "${_ri_file_fname_src}" ]; then
			_ri_file_fname_src="${_ri_prefix:+${_ri_prefix}/}${_ri_file_fname_src}";
		fi;
		if [ "${_ri_file_fname_dst#/}" = "${_ri_file_fname_dst}" ]; then
			_ri_file_fname_dst="${_ri_prefix:+${_ri_prefix}/}${_ri_file_fname_dst}";
		fi;
		if ! rtl_fileop mv "${_ri_file_fname_src}" "${_ri_file_fname_dst}"; then
			return 1;
		fi; ;;

	/=*)
		_ri_dname="${1#/=}";
		if [ "${_ri_dname#/}" = "${_ri_dname}" ]; then
			_ri_dname="${_ri_prefix:+${_ri_prefix}/}${_ri_dname}";
		fi;
		if ! rtl_fileop mkdir "${_ri_dname}"; then
			return 1;
		fi; ;;

	@*=*)
		_ri_ln_target="${1%=*}"; _ri_ln_target="${_ri_ln_target#@}"; _ri_ln_fname="${1#*=}";
		if [ "${_ri_ln_fname#/}" = "${_ri_ln_fname}" ]; then
			_ri_ln_fname="${_ri_prefix:+${_ri_prefix}/}${_ri_ln_fname}";
		fi;
		if [ -e "${_ri_ln_fname}" ]; then
			rtl_fileop rm "${_ri_ln_fname}";
		fi;
		if ! rtl_fileop ln_symbolic "${_ri_ln_target}" "${_ri_ln_fname}"; then
			return 1;
		fi; ;;

	m[0-7][0-7][0-7][0-7]=*)
		_ri_fname="${1#m[0-7][0-7][0-7][0-7]=}"; _ri_chmod_mode="${1%%=*}"; _ri_chmod_mode="${_ri_chmod_mode#m}";
		if ! rtl_fileop chmod "${_ri_chmod_mode}" "${_ri_fname}"; then
			return 1;
		fi; ;;

	o*=*)
		_ri_fname="${1#o*=}"; _ri_owner_spec="${1%%=*}"; _ri_owner_spec="${_ri_owner_spec#o}";
		if ! rtl_fileop chown "${_ri_owner_spec}" "${_ri_fname}"; then
			return 1;
		fi; ;;

	*=*)
		_ri_file_fname_src="${1%=*}"; _ri_file_fname_dst="${1#*=}";
		if [ "${_ri_file_fname_src#/}" = "${_ri_file_fname_src}" ]; then
			_ri_file_fname_src="${_ri_prefix:+${_ri_prefix}/}${_ri_file_fname_src}";
		fi;
		if [ "${_ri_file_fname_dst#/}" = "${_ri_file_fname_dst}" ]; then
			_ri_file_fname_dst="${_ri_prefix:+${_ri_prefix}/}${_ri_file_fname_dst}";
		fi;
		if ! rtl_fileop cp "${_ri_file_fname_src}" "${_ri_file_fname_dst}"; then
			return 1;
		fi; ;;

	esac; shift;
	done;

	return 0;
};

# vim:filetype=sh textwidth=0