file.sh 1.58 KB
##
# some functions for file handling...this includes a kind of
# include_once for shell scripts. This one also includes the
# utils/program.sh file as it is needed in all scripts.
#
command -v filename2symbol >/dev/null
if [ $? -ne 0 ]
then
	filename2symbol() {
		local SYM="${1}"

		if [ 1 -ne $# ]
		then
			${LOGGER} -p syslog.err 'filename2sybol: missing filename'
			exit 1
		fi

		while [ "${SYM}" != "${SYM%/*}" -o "${SYM}" != "${SYM%.*}" ]
		do
			if [ "${SYM}" != "${SYM%.*}" ]
			then
				SYM="${SYM%%.*}_${SYM#*.}"
			else
				SYM="${SYM%%/*}_${SYM#*/}"
			fi
		done

		echo "${SYM}"
	}

	##
	# create an absolute filename from a given relative one.
	# This deals only with leading ./ and ../ it does not
	# handle then when they are in the middle of the name.
	#
	abs_filename() {
		local FILE="${1}"
		local DIR="${PWD}"

		# if FILE starts with a / its already absolute
		if [ "${FILE}" != ${FILE#/} ]
		then
			echo "${FILE}"
			return 0
		fi

		# simply remove ./ and make a ../ remove a part from pwd
		while [ "${FILE}" != ${FILE#./} -o "${FILE}" != ${FILE#../} ]
		do
			if [ "${FILE}" != ${FILE#./} ]
			then
				FILE="${FILE#./}"
			else
				FILE="${FILE#../}"
				DIR="${DIR%/*}"
			fi
		done

		echo "${DIR}/${FILE}"
	}

	include_once() {
		local FILE="$(abs_filename "${1}")"
		local SYM="$(filename2symbol "${FILE}")"

		if [ 1 -ne $# ]
		then
			${LOGGER} -p syslog.err 'include_once: missing filename'
			exit 1
		fi

		if eval [ -z \"\${SOURCED_${SYM}}\" ]
		then
			eval export SOURCED_${SYM}=\"\${FILE}\"
			. ${FILE}
		fi
	}

	include_once utils/programs.sh
fi

# vim: set ts=4 sw=4: