file.sh 2.81 KB
##
# \file

# To each interface a set of caller functions exist, that take an instance
# of an object and then in turn call the implementation for the class of
# this object. If there is none within the class it looks into its
# parent class and so forth.
#
# This is somewhat similar to late binding in real OOP languages, but
# by far not so elaborated. This is not a real object oriented language
# and will surely never ever provide all features these have.
#
# That said it has proven very usefull for me to orgnize code and prevent
# code duplication.
#
# \author	Georg Hopp
#
# \copyright
# Copyright © 2014 Georg Hopp
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

##
# 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: