aboutsummaryrefslogtreecommitdiff
blob: 14dab084544912f35d95a5fbb981955ada48feee (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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/sh
# -*- coding: utf-8 -*-
# R overlay -- shell functions
# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
# Distributed under the terms of the GNU General Public License;
# either version 2 of the License, or (at your option) any later version.
#
#
# Notes:
# * no bashisms here
#
#
# --- functions provided by this file ---
#
# message:
# void veinfo ( message )
# void einfo  ( message )
# void ewarn  ( message )
# void eerror ( message )
#
# core:
# @noreturn die ( [message], [exit_code] ), raises exit()
# @noreturn OUT_OF_BOUNDS(), raises die()
# int  run_command        ( *cmdv )
# int  run_command_logged ( *cmdv )
# void autodie            ( *cmdv ), raises die()
#
# void load_functions ( *filenames, **SHLIB ), raises die()
# void dont_run_as_root(), raises die()
# int  list_has ( word, *list_items )
# int  qwhich ( *command )
#
# fs util:
# int dodir ( *dir )
#
# str util:
# int  yesno     ( word, **YESNO_YES=0, **YESNO_NO=1, **YESNO_EMPTY=2 )
# ~int str_strim ( *args )
# ~int str_upper ( *args )
# ~int str_lower ( *args )
# ~int str_field ( fieldspec, *args, **FIELD_SEPARATOR=' ' )
#
# int util:
# @intcheck is_int()
# @intcheck is_natural()
# @intcheck is_positive()
# @intcheck is_negative()
#
#
# --- variables provided by this file ---
#
# IFS_DEFAULT
# IFS_NEWLINE
#
# DEVNULL
#
# EX_ERR
# EX_ARG_ERR
#
# EX_GIT_ERR
# EX_GIT_ADD_ERR
# EX_GIT_COMMIT_ERR
# EX_GIT_PUSH_ERR
#
# SCRIPT_FILENAME
# SCRIPT_NAME
#
# lf
#  "reference" to load_functions()
#
# @private __HAVE_CORE_FUNCTIONS__
#
# --- END HEADER ---

if [ -z "${__HAVE_CORE_FUNCTIONS__-}" ]; then
readonly __HAVE_CORE_FUNCTIONS__=y

## make some env vars readonly

readonly FUNCTIONS
[ -z "${SHLIB-}"           ] || readonly SHLIB
[ -z "${DATADIR-}"         ] || readonly DATADIR
[ -z "${ROVERLAY_HOOKRC-}" ] || readonly ROVERLAY_HOOKRC

readonly DEBUG VERBOSE QUIET NO_COLOR

readonly \
   ROVERLAY_PHASE \
   EBUILD ROVERLAY_EXE ROVERLAY_HELPER_EXE \
   OVERLAY S OVERLAY_NAME \
   DISTROOT \
   TMPDIR T \
   ADDITIONS_DIR FILESDIR WORKDIR \
   NOSYNC HAS_CHANGES


## vars / constants

readonly IFS_DEFAULT="${IFS}"
readonly IFS_NEWLINE='
'

: ${DEVNULL:=/dev/null}
readonly DEVNULL

readonly EX_ERR=2
readonly EX_ARG_ERR=5

readonly EX_GIT_ERR=30
readonly EX_GIT_ADD_ERR=35
readonly EX_GIT_COMMIT_ERR=36
readonly EX_GIT_PUSH_ERR=37

readonly SCRIPT_FILENAME="${0##*/}"
readonly SCRIPT_NAME="${SCRIPT_FILENAME%.*}"

readonly lf=load_functions

## message functions

# void veinfo ( message ) [**DEBUG]
#
if [ "${DEBUG:?}" = "y" ]; then
   veinfo() { echo "$*"; }
else
   veinfo() { return 0; }
fi

# void einfo ( message ) [**VERBOSE]
#
if [ "${VERBOSE:?}" = "y" ]; then
   einfo() { echo "$*"; }
else
   einfo() { return 0; }
fi

# void ewarn ( message ) [**QUIET]
#
if [ "${QUIET:?}" != "y" ]; then
   ewarn() { echo "$*" 1>&2; }
else
   ewarn() { return 0; }
fi

# void eerror ( message )
#
eerror() { echo "$*" 1>&2; }


## core functions

# @noreturn die ( [message], [exit_code=**EX_ERR] ), raises exit (exit_code)
#
#  Prints a message to stderr and exits afterwards.
#
die() {
   if [ -n "${1-}" ]; then
      eerror "died: ${1}"
   else
      eerror "died."
   fi
   exit "${2:-${EX_ERR?}}"
}

# @noreturn OUT_OF_BOUNDS(), raises die (**EX_ARG_ERR)
#
#  Catches non-zero shift return and calls die().
#
OUT_OF_BOUNDS() { die "shift returned non-zero." ${EX_ARG_ERR?}; }

# int run_command ( *cmdv )
#
#  Runs a command and passes its return value. Also logs the command.
#
run_command() {
   veinfo "running command: $*"
   "$@"
}

# int run_command_logged ( *cmdv )
#
#  Runs a command and passes its return value. Also logs the command + result.
#
run_command_logged() {
   local rc=0
   veinfo "running command: $*"
   "$@" || rc=${?}
   if [ ${rc} -eq 0 ]; then
      veinfo "command succeeded."
      return 0
   else
      einfo "command '$*' returned ${rc}."
      return ${rc}
   fi
}

# void autodie ( *cmdv ), raises die()
#
#  Executes a command. Dies on non-zero return code.
#
autodie() {
   local rc=0
   veinfo "running command: $*"
   "$@" || rc=$?
   if [ ${rc} -eq 0 ]; then
      return 0
   else
      die "command '$*' returned ${rc}" ${rc}
   fi
}

# void load_functions ( *filenames, **SHLIB ), raises die()
#
#  Loads zero or more additional shell function files from $SHLIB.
#  Dies if a file cannot be sourced.
#
load_functions() {
   [ -n "${SHLIB-}" ] || die "\$SHLIB is not set."
   local f
   local sdir
   local IFS="${IFS_DEFAULT}"
   while [ $# -gt 0 ]; do
      f=
      IFS=":"
      for sdir in ${SHLIB}; do
         IFS="${IFS_DEFAULT}"
         f="${sdir}/${1%.sh}.sh"
         if [ -f "${f}" ]; then
            veinfo "Trying to load functions file ${f} ... "
            . "${f}" || die "failed to load functions file ${f}."
            break 1
         else
            f=
         fi
      done
      [ -n "${f}" ] || die "failed to locate functions file '${1}'."
      shift
   done
   return 0
}

# void dont_run_as_root(), raises die()
#
#  Dies if this process is run as root.
#
dont_run_as_root() {
   local uid=$(id -ru)
   if [ -z "${uid}" ]; then
      die "cannot get \$uid."
   elif [ ${uid} -ne 0 2>>${DEVNULL} ]; then
      return 0
   else
      die "bad \$uid ${uid}."
   fi
}

# int list_has ( word, *list_items )
#
#  Returns true if word is in list_items, else false.
#
list_has() {
   local kw="${1}"
   shift || OUT_OF_BOUNDS

   while [ $# -gt 0 ]; do
      [ "x${kw}" != "x${1}" ] || return 0
      shift
   done
   return 1
}

# int qwhich ( *command )
#
#  Returns true if 'which' finds all listed commands, else false.
#
qwhich() {
   while [ $# -gt 0 ]; do
      which "${1}" 1>>${DEVNULL} 2>>${DEVNULL} || return 1
      shift
   done
   return 0
}

## fs util functions

# int dodir ( *dir )
#
#  Ensures that the given directories exist by creating them if necessary.
#
#  Returns the number of directories that could not be created.
#
dodir() {
   local fail=0
   while [ $# -gt 0 ]; do
      [ -d "${1}" ] || mkdir -p -- "${1}" || fail=$(( ${fail} + 1 ))
      shift
   done
   return ${fail}
}


## str util functions

# int yesno ( word, **YESNO_YES=0, **YESNO_NO=1, **YESNO_EMPTY=2 )
#
#  Returns:
#  * YESNO_YES   (0) if word means yes
#  * YESNO_EMPTY (2) if word is empty
#  * YESNO_NO    (1) otherwise (word is not empty and does not mean yes)
#
yesno() {
   case "${1-}" in
      '')
         return ${YESNO_EMPTY:-2}
      ;;
      # yes | y | true | 1 | enable(d) | on
      [yY][eE][sS]|\
      [yY]|\
      [tT][rR][uU][eE]|\
      1|\
      [eE][nN][aA][bB][lL][eE]?|\
      [oO][nN]\
      )
         return ${YESNO_YES:-0}
      ;;
      *)
         return ${YESNO_NO:-1}
      ;;
   esac
}

# ~int str_trim ( *args )
#
#  Removes whitespace at the beginning + end of a string
#  and replaces any whitespace sequence within the string
#  with a single space char.
#
str_trim() { sed -r -e 's,^\s+,,' -e 's,\s+$,,' -e 's,\s+, ,g' "$@"; }

# ~int str_upper ( *args )
str_upper() { tr [:lower:] [:upper:] "$@"; }

# ~int str_lower ( *args )
str_lower() { tr [:upper:] [:lower:] "$@"; }

# ~int str_field ( fieldspec, *args, **FIELD_SEPARATOR=' ' )
#
str_field() { cut -d "${FIELD_SEPARATOR:- }" -f "$@"; }


## int util functions

# @funcdef shbool @intcheck [<condition>:=true] <function name> ( word )
#
#   Returns true if word is a number and condition(word) evaluates to true.
#

# @intcheck is_int()
is_int() {
   [ -n "${1-}" ] || return 1
   [ "${1}" -ge 0 2>>${DEVNULL} ] || [ "${1}" -lt 0 2>>${DEVNULL} ]
}

# @intcheck >=0 is_natural()
is_natural()  { [ -n "${1-}" ] && [ "${1}" -ge 0 2>>${DEVNULL} ]; }

# @intcheck >0 is_positive()
is_positive() { [ -n "${1-}" ] && [ "${1}" -gt 0 2>>${DEVNULL} ]; }

# @intcheck <0 is_negative()
is_negative() { [ -n "${1-}" ] && [ "${1}" -lt 0 2>>${DEVNULL} ]; }


fi # __HAVE_CORE_FUNCTIONS__