libpmodules.bash 2.8 KB
Newer Older
1 2
#!/bin/bash

3 4 5 6 7
if [[  -z ${bindir} ]]; then
        local bindir=$(dirname "${BASH_SOURCE}")
	bindir=$(cd "${bindir}"/.. && pwd)"/bin"
fi

8 9 10 11
log() {
        local -ri fd=$1
        local -r fmt="$2\n"
        shift 2
12
        printf -- "$fmt" "$@" 1>&$fd
13 14 15
}

info() {
16
        log 2 "$1" "${@:2}"
17 18 19
}

error() {
20
        log 2 "$1" "${@:2}"
21 22 23
}

debug() {
gsell's avatar
gsell committed
24
        [[ ${PMODULES_DEBUG} ]] || return 0
25 26 27 28 29 30 31 32 33
        log 2 "$@"
}

die() {
        local -ri ec=$1
        shift
        if [[ -n $@ ]]; then
                local -r fmt=$1
                shift
34
                log 2 "$fmt" "$@"
35 36 37 38 39 40 41 42 43 44 45 46 47 48
        fi
        exit $ec
}

#
# get answer to yes/no question
#
# $1: prompt
#
get_YN_answer() {
	local -r prompt="$1"
	local ans
	read -p "${prompt}" ans
	case ${ans} in
stadler_h's avatar
stadler_h committed
49
		y|Y ) 
50 51 52 53 54 55
			return 0;;
		* )
			return 1;;
	esac
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69
#
# return normalized abolute pathname
# $1: filename
get_abspath() {
	local -r fname=$1
	[[ -r "${fname}" ]] || return 1
	if [[ -d ${fname} ]]; then
		echo $(cd "${fname}" && pwd)
	else
		local -r dname=$(dirname "${fname}")
		echo $(cd "${dname}" && pwd)/$(basename "${fname}")
	fi
}

70 71 72 73
get_options() {
	"${bindir}/getopt" "$@"
}

74
check_pmodules_env_vars() {
gsell's avatar
gsell committed
75 76 77
	[[ -n "${PMODULES_ROOT}" ]] &&
	    [[ -n "${PMODULES_CONFIG_DIR}" ]] &&
	    [[ -n "${PMODULES_TEMPLATES_DIR}" ]] &&
78 79 80 81
	    [[ -n "${PMODULES_HOME}" ]] &&
	    [[ -n "${PMODULES_VERSION}" ]] || die 1 "
Error: the module environment you are going to use as source has not been
initialized properly!"
82
}
83

84 85
check_pmodules_directories() {
	local -r src_prefix="$1"
stadler_h's avatar
stadler_h committed
86

87
	[[ -d "${src_prefix}" ]] &&
gsell's avatar
gsell committed
88 89
	    [[ -d "${src_prefix}/${PMODULES_CONFIG_DIR}" ]] &&
	    [[ -d "${src_prefix}/${PMODULES_TEMPLATES_DIR}" ]] &&
90 91 92
	    [[ -d "${src_prefix}/Tools/Pmodules/${PMODULES_VERSION}" ]] || die 1 "
Error: the module environment '${src_prefix}' has not been initialized properly!"
}
93

94 95
check_pmodules_env() {
	check_pmodules_env_vars
gsell's avatar
gsell committed
96
	check_pmodules_directories "${PMODULES_ROOT}"
97 98
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
append_path () {
        local -r P=$1
        local -r d=$2

        if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then
                if [[ -z ${!P} ]]; then
                        eval $P=${d}
                else
                        eval $P=${!P}:${d}
                fi
        fi
}

prepend_path () {
        local -r P=$1
        local -r d=$2

        if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then
                if [[ -z ${!P} ]]; then
                        eval $P=${d}
                else
                        eval $P=${d}:${!P}
                fi
        fi
}

remove_path() {
        local -r P=$1
        local -r d=$2
	local new_path=''
	local -r _P=( ${!P//:/ } )
	# loop over all entries in path
	for entry in "${_P[@]}"; do
		[[ "${entry}" != "${d}" ]] && new_path+=":${entry}"
	done
	# remove leading ':'
	eval ${P}="${new_path:1}"
}

138 139 140 141 142 143

# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End: