summaryrefslogtreecommitdiffhomepage
path: root/subr/ex_rtl_fileop.subr
blob: 3732ab5e3919e21e3929dfb9232a14785f973d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# set -o errexit -o noglob are assumed.
#

ex_build_fileop() {
	local _op="${1}"; shift;
	if [ "${_op}" = cd ]; then
		ex_log_msg varn "Changing working directory to \`${1}'.";
		[ \( -n "${1}" \) -a \( -L "${1}" -o -e "${1}" \) ] && cd -- "${1}";
	elif [ "${_op}" = cp ]; then
		ex_log_msg varn "Copying \`${1}' to \`${2}' w/ -pPR.";
		[ ${#} -ge 2 ] && cp -pPR -- "${@}";
	elif [ "${_op}" = ln_symbolic ]; then
		ex_log_msg varn "Linking \`${1}' to \`${2}' w/ -fs";
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && ln -fs -- "${1}" "${2}";
	elif [ "${_op}" = mv ]; then
		ex_log_msg varn "Moving \`${1}' to \`${2}' w/ -fs";
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && mv -f -- "${1}" "${2}";
	elif [ "${_op}" = mkdir ]\
	||   [ "${_op}" = rm ]; then
		while [ ${#} -gt 0 ]; do
			if [ -z "${1}" ]; then
				return 1;
			elif [ "${_op}" = mkdir ]\
			&&   [ ! -e "${1}" ]; then
				ex_log_msg varn "Making directory \`${1}'.";
				mkdir -p -- "${1}";
			elif [ "${_op}" = rm ]\
			&&   [ \( -L "${1}" \) -o \( -e "${1}" \) ]; then
				ex_log_msg varn "Removing directory or file \`${1}'.";
				rm -rf -- "${1}";
			fi; shift;
		done;
	else
		ex_log_msg failexit "Error: ex_build_fileop() called w/ invalid parameter(s) \`${@}'.";
	fi;
};

# vim:filetype=sh