-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions.sh
executable file
·322 lines (282 loc) · 10.2 KB
/
.functions.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bash
# get character from character code
#
# syntax: chr <character_code>
function chr() {
[[ $1 -ge 0 && $1 -le 255 ]] || return 1
printf "\\$(printf '%03o' "$1")"
}
# get character code from character
#
# syntax: ord <character>
function ord() {
LC_TYPE=C printf '%d' "'$1"
}
# back up a list of files
#
# syntax: back_up_files [file_1] ... [file_n]
function back_up_files()
{
# process list of files
for file in $@; do
if [[ -h "${file}" ]]; then
# file is a symlink
rm "${file}"
elif [[ -f "${file}" || -d "${file}" ]]; then
# if file exists, move it
mv "${file}" "${file}.$(date +%Y%m%dT%H%M%S).bak"
fi
done
}
# get the platform of the OS where this function is called
#
# syntax: get_platform
function get_platform()
{
# get the uname
local unamestr=$(uname)
# parse the uname
local shell_nocasematch=$(shopt -p nocasematch)
shopt -s nocasematch
case ${unamestr} in
*bsd) platform='bsd' ;;
cygwin*) platform='windows' ;;
darwin) platform='mac' ;;
linux) platform='linux' ;;
ming*) platform='windows' ;;
minix) platform='minix' ;;
msys*) platform='windows' ;;
sunos) platform='solaris' ;;
windows*) platform='windows' ;;
*) platform='unknown' ;;
esac
${shell_nocasematch}
# return the platform
echo ${platform}
}
# checks to make sure a list of prereqs are met
#
# syntax: check_prereqs [prereq_1] ... [prereq_n]
function check_prereqs()
{
# declare local array to keep track of what needs to be installed
local to_install=()
# loop through and check what's there and what's not
for prereq in $@; do
if ! command -v ${prereq} &>/dev/null; then
to_install+=(${prereq})
fi
done
if [[ ${#to_install[@]} -ne 0 ]]; then
# things need to be installed
echo " └ needs to be installed: ${to_install[@]}"
return 1
else
# things don't need to be installed
return 0
fi
}
# creates multiple links from pairs of inputs
#
# syntax: create_links [(<link_from_1> <link_to_1>) ... (<link_from_n> <link_to_n>)]
function create_links()
{
# run through pairs of links, shifting as needed
while [[ "${1}" != "" && "${2}" != "" ]]; do
ln -s "${1}" "${2}"
shift 2
done
}
# parses a config file and substitutes variables for a given action type
#
# syntax: parse_config_action <config_file> <action_type> [(<var_name_1> <var_value_1>) ... (<var_name_n> <var_value_n>)]
function parse_config_action()
{
# set the variable marker prefix and suffix
local var_prefix='{{'
local var_suffix='}}'
# get the config file and which action type is being parsed for
local config_file="${1:?}"
local action_type="${2:?}"
# read the variables that are going to be used
local -A variables
while [[ "${3}" != "" && "${4}" != "" ]]; do
variables[${3}]=${4}
shift 2
done
# get all options of the specified action type and escape '|' as necessary
options=($(grep '^'${action_type} ${config_file} | cut -d ' ' -f '2-' | sed 's/|/\|/g'))
# go through options and search and replace variables
for variable in ${!variables[@]}; do
options=($(sed 's|'${var_prefix}${variable}${var_suffix}'|'${variables[${variable}]}'|g' <<< ${options[@]}))
done
# check for any unknown variables and exit if any are found
if grep -P '('${var_prefix}'|'${var_suffix}')' <<< ${options[@]} &>/dev/null; then
declare -A unknown_vars
for unknown_var in $(grep -Po '(?<='${var_prefix}')\w+(?='${var_suffix}')' <<< ${options[@]}); do
unknown_vars[${unknown_var}]=1
done
>&2 echo "unrecognized variable(s): ${!unknown_vars[@]}"
>&2 echo "config_file: ${config_file}"
>&2 echo "action_type: ${action_type}"
return 1
fi
# return options
echo ${options[@]}
}
# tuple-ify an associative array
#
# syntax: tuplify <array_name>
function tuplify() {
# note that this uses features found ONLY in bash versions >= 4.3
# get a namedref to associative array
local -n array_name=$1
# set up variable to keep track of tuplified portions of the array
local tuplified=()
# run through the array
for index in ${!array_name[@]}; do
tuplified+=(${index})
tuplified+=(${array_name[${index}]})
done
# return tuplified version of the array
echo ${tuplified[@]}
}
# tuple-ify the 'variables' associative array
#
# syntax: tuplify_variables
function tuplify_variables() {
# set up variable to keep track of tuplified portions of the array
local tuplified=()
# run through the array
for index in ${!variables[@]}; do
tuplified+=(${index})
tuplified+=(${variables[${index}]})
done
# return tuplified version of the array
echo ${tuplified[@]}
}
# run an arbitrary command
#
# syntax: run_command [param_1] ... [param_n]
function run_command() {
# run the command
eval "$@"
return $?
}
# checks whether the minimum version is met
#
# syntax: check_minimum_version <program_to_check> <version_argument> <minimum_version> <version_string_type>
function check_minimum_version() {
# declare local variables
local program_to_check=""
local version_argument=""
local version_string_type=""
local minimum_version_string=""
local minimum_version=()
local current_version_string_raw=""
local current_version_string=""
local current_version=()
# declare local array to keep track of what commands don't meet minimum version reqs
local minimum_version_not_met=()
# loop through and check versions
while [[ -n "$@" ]]; do
# reinitialize version variables
minimum_version=()
current_version=()
# get version string type
# type1: x.y
# x: number
# y: number
# type2: x.y.z
# x: number
# y: number
# z: number
# type3: x.ya
# x: number
# y: number
# a: letter
version_string_type=$4
# get minimum version
minimum_version_string="$3"
case ${version_string_type} in
type1|type2)
readarray -t minimum_version < <(echo "${minimum_version_string}" | sed 's/\./\n/g')
;;
type3)
readarray -t minimum_version < <(echo "${minimum_version_string}" | awk '{print tolower($1)}' | sed -r 's/([0-9]+)([a-zA-Z])/\1.\2/' | sed 's/\./\n/g')
;;
*)
echo " └ error: unknown version string type '${version_string_type}'"
;;
esac
# get current version
program_to_check="$1"
version_argument="$2"
current_version_string_raw="$(${program_to_check} ${version_argument})"
case ${version_string_type} in
type1)
current_version_string="$(echo "${current_version_string_raw}" | egrep -o '[0-9]+\.[0-9]+' | head -n 1)"
readarray -t current_version < <(echo "${current_version_string}" | sed 's/\./\n/g')
;;
type2)
current_version_string="$(echo "${current_version_string_raw}" | egrep -o '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)"
readarray -t current_version < <(echo "${current_version_string}" | sed 's/\./\n/g')
;;
type3)
current_version_string="$(echo "${current_version_string_raw}" | egrep -o '[0-9]+\.[0-9]+[a-zA-Z]' | head -n 1)"
readarray -t current_version < <(echo "${current_version_string}" | awk '{print tolower($1)}' | sed -r 's|([0-9]+)([a-zA-Z])|\1.\2|' | sed 's/\./\n/\g')
;;
*)
;;
esac
# compare versions
case ${version_string_type} in
type1)
if [[ ${current_version[0]} -lt ${minimum_version[0]} || ( ${current_version[0]} -eq ${minimum_version[0]} && ${current_version[1]} -lt ${minimum_version[1]} ) ]]; then
minimum_version_not_met+=( "${program_to_check}=${current_version_string}<${minimum_version_string}" )
fi
;;
type2)
if [[ ${current_version[0]} -lt ${minimum_version[0]} || ( ${current_version[0]} -eq ${minimum_version[0]} && ${current_version[1]} -lt ${minimum_version[1]} ) || ( ${current_version[0]} -eq ${minimum_version[0]} && ${current_version[1]} -eq ${minimum_version[1]} && ${current_version[2]} -lt ${minimum_version[2]} ) ]]; then
minimum_version_not_met+=( "${program_to_check}=${current_version_string}<${minimum_version_string}" )
fi
;;
type3)
if [[ ${current_version[0]} -lt ${minimum_version[0]} || ( ${current_version[0]} -eq ${minimum_version[0]} && ${current_version[1]} -lt ${minimum_version[1]} ) || ( ${current_version[0]} -eq ${minimum_version[0]} && ${current_version[1]} -eq ${minimum_version[1]} && $(ord ${current_version[2]}) -lt $(ord ${minimum_version[2]}) ) ]]; then
minimum_version_not_met+=( "${program_to_check}=${current_version_string}<${minimum_version_string}" )
fi
;;
*)
;;
esac
shift 4
done
if [[ ${#minimum_version_not_met[@]} -ne 0 ]]; then
# minimum versions not met
echo " └ minimum versions not met: ${minimum_version_not_met[@]}"
return 1
else
# minimum versions met
return 0
fi
}
# define actions to be taken
#
# syntax: define_action <action_name> <action_to_be_taken> <true|false>
function define_action() {
# define the action name
actions+=($1)
# define the action to be executed
action_execs[$1]="$2"
# define whether an error in this action is fatal
error_is_fatal[$1]="$3"
}
# download something via curl
#
# syntax download <url_to_grab> <filename_to_save_as>
function download() {
url="$1"
file="$2"
curl -sL "${url}" -o "${file}" --create-dirs
return $?
}