#/bin/sh
# exitIfHelpers.sh
#
# helper functions:
#  tests: 
#	- If Not Absolute Path
#	- If Not A File
#	- If Not A Directory
#	- If Not Executable
#  else exits with error 1
#
# created: 20-01-2010
# author: p.zumbruch, GSI
# modified: 22-01-2010

#debug - uncomment to debug
#set -xv

isAbsolutePath()
{
	# isAbsolutePath path 
	# checks if path is absolute
	# exit values: 
	# 	0 : is not absolute
	#	1 : is absolute

	[ 3 -le $myVerbosity ] && myLevel=$(($myLevel+1)) && echo [${myLevel}]  entering function \"isAbsolutePath $*\"

	if [ $# -lt 1 ]
	then 
		echo "$scriptname: function \"absolutePath\" called with too few arguments, needs at least \"$0 path [name]\" ... exiting" 1>&2
		unsets
		exit 1;
	fi
	
	if [ ! -z "${1##/*}" ] # doesn't start with '/'
	then 
		if [ ! -z "${1##~*}" ] # nor starts with "~"
		then
			[ 3 -le $myVerbosity ] && echo [${myLevel}]  " leaving" function \"isAbsolutePath\" [return: 1]&& myLevel=$(($myLevel-1))
			return 1;
		fi
	fi

	[ 3 -le $myVerbosity ] && echo [${myLevel}]  " leaving" function \"isAbsolutePath\" [return: 0] && myLevel=$(($myLevel-1))

	return 0;
}

#exitIfNotAbsolutePath function
exitIfNotAbsolutePath()
{
	# exitIfNotAbsolutePath path description
	# checks if path is absolute
	# otherwise exits
	
	[ 3 -le $myVerbosity ] && myLevel=$(($myLevel+1)) && echo [${myLevel}]  entering function \"exitIfNotAbsolutePath $*\"
	
	if [ $# -lt 1 ]
	then 
		echo "$scriptname: function \"exitIfNotAbsolutePath\" called with too few arguments, needs at least \"$0 path [name]\" ... exiting" 1>&2
		unsets
		exit 1;
	fi

	isAbsolutePath $1  
	if [ 0 -ne $? ]
	then 
		echo "$scriptname: $2 \`${1}' is not an absulute path ... exiting" 1>&2
		unsets
		exit 1;
	fi
	[ 3 -le $myVerbosity ] && echo [${myLevel}]  " leaving" function \"exitIfNotAbsolutePath\" && myLevel=$(($myLevel-1))
}

#exitIfNotAFile function
exitIfNotAFile()
{
	# exitIfNotAFile path description
	# checks if path is a file 
	# otherwise exits
	
	[ 3 -le $myVerbosity ] && myLevel=$(($myLevel+1)) && echo [${myLevel}]  entering function \"exitIfNotAFile $*\"
	
	if [ $# -lt 1 ]
	then 
		echo "$scriptname: function \"exitIfNotAFile\" called with too few arguments, needs at least \"$0 path [name]\" ... exiting" 1>&2
		unsets
		exit 1;
	fi

	if [ ! -f "${1}" ] 
	then 
		echo "$scriptname: $2 \`${1}' is not a file ... exiting" 1>&2
		unsets
		exit 1;
	fi

	[ 3 -le $myVerbosity ] && echo [${myLevel}]  " leaving" function \"exitIfNotAFile\" && myLevel=$(($myLevel-1))
}

#exitIfNotADirectory function
exitIfNotADirectory()
{
	# exitIfNotADirectory path description
	# checks if path is a directory
	# otherwise exits

	[ 3 -le $myVerbosity ] && myLevel=$(($myLevel+1)) && echo [${myLevel}]  entering function \"exitIfNotADirectory $*\"
	
	if [ $# -lt 1 ]
	then 
		echo "$scriptname: function \"exitIfNotADirectory\" called with too few arguments, needs at least \"$0 path [name]\" ... exiting" 1>&2
		unsets
		exit 1;
	fi
	
	if [ ! -d "${1}" ] 
	then 
		echo "$scriptname: $2 \`${1}' is not a directory ... exiting" 1>&2
		unsets
		exit 1;
	fi

	[ 3 -le $myVerbosity ] && echo [${myLevel}]  " leaving" function \"exitIfNotADirectory\" && myLevel=$(($myLevel-1))
}

#exitIfNotExecutable function
exitIfNotExecutable()
{
	# exitIfNotAbsolutePath exe description
	# checks if exe is executable
	# otherwise exits

	[ 3 -le $myVerbosity ] && myLevel=$(($myLevel+1)) && echo [${myLevel}]  entering function \"exitIfNotExecutable $*\"

	if [ $# -lt 1 ]
	then 
		echo "$scriptname: function \"exitIfNotAbsolutePath\" called with too few arguments, needs at least \"$0 path [name]\" ... exiting" 1>&2
		unsets
		exit 1;
	fi

	exitIfNotAFile $*

	if [ ! -x "${1}" ] 
	then 
		echo "$scriptname: $2 \`${1}' is not an executable or does not exist ... exiting" 1>&2
		unsets
		exit 1;
	fi

	[ 3 -le $myVerbosity ] && echo [${myLevel}]  " leaving" function \"exitIfNotExecutable\" && myLevel=$(($myLevel-1))
}

