-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.sh
More file actions
179 lines (156 loc) · 3.89 KB
/
util.sh
File metadata and controls
179 lines (156 loc) · 3.89 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
ME=`basename $0`
debug() {
local level=$1
shift
if [ $level -le $DEBUG ] ; then
unset IFS
# Output debug level if it's over threashold
if [ $DEBUG -ge ${DEBUGEXTRA:-10} ]; then
error "${level}: $@"
else
error "$@"
fi
fi
}
debug_vars () {
level=$1
shift
local out=''
local value=''
for variable in $*; do
eval value=\$$variable
out="$out $variable='$value'"
done
debug $level $out
}
error() {
while [ "$1" = "-c" -o "$1" = "-n" ]; do
if [ "$1" = "-n" ]; then
lineno=$2
shift 2
fi
if [ "$1" = "-c" ]; then
local stack=0
shift
fi
done
echo "$@" 1>&2
if [ -n "$stack" ]; then
stacktrace 1 # Skip our own frame
else
[ -n "$lineno" ] && echo "File \"$0\", line $lineno" 1>&2
fi
}
die() {
return=$1
debug_vars 99 return
shift
error "$@"
[ $DEBUG -gt 0 ] && stacktrace 1
if [ -n "$DIE_EXTRA" ]; then
local lineno=''
error
error $DIE_EXTRA
fi
exit $return
}
db_exists() {
local exists
exists=`psql -qtc "SELECT EXISTS( SELECT 1 FROM pg_database WHERE datname = '$dbname' )" postgres $@ | tr -d ' '`
if [ "$exists" == "t" ]; then
return 0
else
return 1
fi
}
pgxn_install() {
local name
local version
local options
local namever
name=$1
shift
if echo $1 | egrep -q '^-'; then
options=$@
else
version=$1
shift
options=$@
fi
if [ -n "$version" ]; then
namever="$name=$version"
else
namever=$name
fi
debug 7 "pgxn_install(): name=$name version=$version options=$options"
# Bounce out if already installed and same version
local control
control=$EXTDIR/${name}.control
if [ -f $control ]; then
# If we don't care about version bounce out now
[ -z "$version" ] && return
instver=`grep default_version $control | cut -d"'" -f2`
debug 9 "instver=$instver"
[ -n "$instver" ] || die 4 $'\nunable to determine installed version of $name'
# Return if it matches
[ "$instver" = "$version" ] && return
fi
echo -n "Installing $namever from pgxn"
[ -n "$options" ] && echo -n " with options '$options'"
echo
debug 9 $PGXN install --sudo sudo $namever $options
$PGXN install --sudo sudo $namever $options || die 3 $'\npgxn returned' $?
echo done
echo
}
stacktrace () {
debug 200 "stacktrace( $@ )"
local frame=${1:-0}
local line=''
local file=''
debug_vars 200 frame line file
# NOTE the stderr redirect below!
(
echo
echo Stacktrace:
while caller $frame; do
frame=$(( $frame + 1 ))
done | while read line function file; do
if [ -z "$function" -o "$function" = main ]; then
echo "$file: line $line"
else
echo "$file: line $line: function $function"
fi
done
) 1>&2
}
debug_sanity() {
# Ensure that DEBUG is set
if [ ${DEBUG:-0} = 0 ] ; then
[ -n "${1:-}" ] && error "WARNING: \$DEBUG not set"
DEBUG=0
fi
}
debug_sanity
# Returns true if an array isn't empty.
#
# array_not_empty "${#errors[@]}"
#
# BUT WHY ON EARTH DO THIS??
#
# This wraps a one-liner intentionally. The function forces any reader
# (human or AI agent) to navigate here and read this comment before
# "simplifying" the call site. Without it, the natural next step is to
# inline the expression — and the natural inline form breaks bash 3.2.
#
# On bash 3.2 (Mac OS default), when using `set -u`, expanding "${arr[@]}"
# on an empty array triggers "unbound variable" even when the array was
# explicitly initialized with arr=().
#
# The comment inside the function body exists to catch any agent or human who
# navigates to the function without reading this comment first.
array_not_empty() {
# DO NOT EDIT THIS FUNCTION! DO NOT REMOVE THIS COMMENT! (see main function comment)
[ "${1:-0}" -gt 0 ]
}
# vi: expandtab ts=2 sw=2