aboutsummaryrefslogtreecommitdiff
path: root/sh/functions.sh
blob: 90d207550dbe25f396a87ef3c7162f9ec258da82 (plain)
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# Please keep this useable by every shell in portage

RC_GOT_FUNCTIONS="yes"

eindent() {
	RC_EINDENT=$((${RC_EINDENT:-0} + 2))
	[ "${RC_EINDENT}" -gt 40 ] && RC_EINDENT=40
	export RC_EINDENT
}

eoutdent() {
	RC_EINDENT=$((${RC_EINDENT:-0} - 2))
	[ "${RC_EINDENT}" -lt 0 ] && RC_EINDENT=0
	return 0
}

# Safer way to list the contents of a directory,
# as it do not have the "empty dir bug".
#
# char *dolisting(param)
#
#    print a list of the directory contents
#
#    NOTE: quote the params if they contain globs.
#          also, error checking is not that extensive ...
#
dolisting() {
	local x= y= mylist= mypath="$*"

	# Here we use file globbing instead of ls to save on forking
	for x in ${mypath} ; do
		[ ! -e "${x}" ] && continue

		if [ -L "${x}" -o -f "${x}" ] ; then
			mylist="${mylist} "${x}
		elif [ -d "${x}" ] ; then
			[ "${x%/}" != "${x}" ] && x=${x%/}
			
			for y in "${x}"/* ; do
				[ -e "${y}" ] && mylist="${mylist} ${y}"
			done
		fi
	done

	echo "${mylist# *}"
}

# bool is_older_than(reference, files/dirs to check)
#
#   return 0 if any of the files/dirs are newer than
#   the reference file
#
#   EXAMPLE: if is_older_than a.out *.o ; then ...
is_older_than() {
	local x= ref="$1"
	shift

	for x in "$@" ; do
		[ -e "${x}" ] || continue
		# We need to check the mtime if it's a directory too as the
		# contents may have changed.
		[ "${x}" -nt "${ref}" ] && return 0
		[ -d "${x}" ] && is_older_than "${ref}" "${x}"/* && return 0
	done

	return 1 
}

uniqify() {
    local result=
    while [ -n "$1" ] ; do
		case " ${result} " in
			*" $1 "*) ;;
			*) result="${result} $1" ;;
		esac
		shift
	done
    echo "${result# *}"
}

KV_to_int() {
	[ -z $1 ] && return 1

	local x=${1%%-*}
	local KV_MAJOR=${x%%.*}
	x=${x#*.}
	local KV_MINOR=${x%%.*}
	x=${x#*.}
	local KV_MICRO=${x%%.*}
	local KV_int=$((${KV_MAJOR} * 65536 + ${KV_MINOR} * 256 + ${KV_MICRO} ))

	# We make version 2.2.0 the minimum version we will handle as
	# a sanity check ... if its less, we fail ...
	[ "${KV_int}" -lt 131584 ] && return 1
	
	echo "${KV_int}"
}

# Allow our scripts to support zsh
if [ -n "${ZSH_VERSION}" ] ; then
  emulate sh
  NULLCMD=:
  # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
  # is contrary to our usage.  Disable this feature.
  alias -g '${1+"$@"}'='"$@"'
  setopt NO_GLOB_SUBST
fi

# Setup a basic $PATH.  Just add system default to existing.
# This should solve both /sbin and /usr/sbin not present when
# doing 'su -c foo', or for something like:  PATH= rcscript start
case "${PATH}" in
	/lib/rc/bin:/bin:/sbin:/usr/bin:/usr/sbin) ;;
	/lib/rc/bin:/bin:/sbin:/usr/bin:/usr/sbin:*) ;;
	*) export PATH="/lib/rc/bin:/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" ;;
esac

for arg in "$@" ; do
	case "${arg}" in
		--nocolor|--nocolour|-C)
			export RC_NOCOLOR="yes"
			;;
	esac
done

if [ "${RC_NOCOLOR}" != "yes" -a -z "${GOOD}" ] ; then
	eval $(eval_ecolors)
fi

# vim: set ts=4 :