lib.bash 2.27 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7
#!/bin/bash

#
# We need GNU versions of the following utilities. This code works
# well on Linux and Mac OS X with MacPorts.
# :FIXME: implement a smarter, portable solution.
#
gsell's avatar
gsell committed
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
shopt -s expand_aliases
unalias -a

__path=$(which gdate 2>/dev/null)
if [[ $__path ]]; then
	alias date=$__path
else
	alias date=$(which date 2>/dev/null)
fi

__path=$(which ginstall 2>/dev/null)
if [[ $__path ]]; then
	alias install=$__path
else
	alias install=$(which install 2>/dev/null)
fi

__path=$(which greadlink 2>/dev/null)
if [[ $__path ]]; then
	alias readlink=$__path
else
	alias readlink=$(which readlink 2>/dev/null)
fi

__path=$(which gsed 2>/dev/null)
if [[ $__path ]]; then
	alias sed=$__path
else
	alias sed=$(which sed 2>/dev/null)
fi

gsell's avatar
gsell committed
39 40 41 42 43 44
#
# set an error handler. If a function _exit() exists, it will be called
# with the passed exit code.
#
# $1	exit code
#
gsell's avatar
gsell committed
45 46
set -o errexit

gsell's avatar
gsell committed
47
trap "std::error_handler" ERR
gsell's avatar
gsell committed
48

gsell's avatar
gsell committed
49
std::error_handler() {
gsell's avatar
gsell committed
50 51
	local -i ec=$?

gsell's avatar
gsell committed
52
	typeset -F _exit 1>/dev/null 2>&1 && _exit "${ec}"
gsell's avatar
gsell committed
53 54 55
	exit ${ec}
}

gsell's avatar
gsell committed
56 57 58 59
#
# logging/message functions
#
std::log() {
gsell's avatar
gsell committed
60 61 62
	local -ri fd=$1
	local -r fmt="$2\n"
	shift 2
gsell's avatar
gsell committed
63 64 65
	printf -- "$fmt" "$@" >> /dev/fd/$fd
}

gsell's avatar
gsell committed
66 67
std::info() {
	std::log 2 "$1\n" "${@:2}"
gsell's avatar
gsell committed
68 69
}

gsell's avatar
gsell committed
70 71
std::error() {
	std::log 2 "$1\n" "${@:2}"
gsell's avatar
gsell committed
72 73
}

gsell's avatar
gsell committed
74
std::debug() {
gsell's avatar
gsell committed
75
	[[ ${PMODULES_DEBUG} ]] || return 0
gsell's avatar
gsell committed
76
	std::log 2 "$@"
gsell's avatar
gsell committed
77 78
}

gsell's avatar
gsell committed
79
std::die() {
gsell's avatar
gsell committed
80 81 82 83 84 85 86 87 88 89 90
	local -ri ec=$1
	shift
	local cout
	if (( ec == 0)); then
		cout='1'
	else
		cout='2'
	fi	
	if [[ -n $@ ]]; then
		local -r fmt=$1
		shift
gsell's avatar
gsell committed
91
		std::log $cout "$fmt" "$@"
gsell's avatar
gsell committed
92 93 94 95
	fi
	exit $ec
}

gsell's avatar
gsell committed
96
std::abspath () {
gsell's avatar
gsell committed
97
	readlink -f "$1"
gsell's avatar
gsell committed
98 99
}

gsell's avatar
gsell committed
100
std::append_path () {
gsell's avatar
gsell committed
101 102 103
	local -r P=$1
	local -r d=$2

gsell's avatar
gsell committed
104
        if ! egrep -q "(^|:)${d}($|:)" <<<${!P} ; then
gsell's avatar
gsell committed
105 106 107 108 109 110 111
		if [[ -z ${!P} ]]; then
			eval $P=${d}
		else
			eval $P=${!P}:${d}
        	fi
	fi
}
gsell's avatar
gsell committed
112

gsell's avatar
gsell committed
113 114 115 116 117 118 119 120 121 122 123 124 125
std::prepend_path () {
	local -r P=$1
	local -r d=$2

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

gsell's avatar
gsell committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
read_versions() {
	local -r fname="$1"
	local varname=''
	while read _name _version; do
		[[ -z ${_name} ]] && continue
		[[ -z ${_version} ]] && continue
		[[ "${_name:0:1}" == '#' ]] && continue
		var_name=$(echo ${_name} | tr [:lower:] [:upper:])_VERSION
		# don't set version, if already set
		if [[ -z ${!var_name} ]]; then
			eval ${var_name}="${_version}"
		fi
	done < "${fname}"
}