-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply
executable file
·361 lines (292 loc) · 7.17 KB
/
apply
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
#!/usr/bin/env bash
set -euo pipefail
# -*- mode: shell-script -*-
#set -eio pipefail
APPLY_FORCE=0
APPLY_DRYRUN=0
export APPLY_DEBUG=${APPLY_DEBUG:-0}
APPLY_ARCH=$(dpkg-architecture -qDEB_BUILD_ARCH 2>&1 || echo amd64)
# Add directory of this "apply" script to the path if not yet there.
APPLY_DIR=${BASH_SOURCE[0]}
#echo "1: APPLY_DIR $APPLY_DIR"
APPLY_DIR=$(realpath "$APPLY_DIR")
#echo "2: APPLY_DIR $APPLY_DIR"
APPLY_DIR=$(dirname "$APPLY_DIR")
#echo "3: APPLY_DIR $APPLY_DIR"
cd "$APPLY_DIR" || true
echo "$PATH" | grep -q ":$APPLY_DIR" || PATH="$PATH:$APPLY_DIR"
#echo "PATH $PATH"
#############################################################################
#
# Emit colorful messages
#
COLOR_BLUE="\033[1;36;40m"
COLOR_RED="\033[1;31;40m"
COLOR_GREEN="\033[1;32;40m"
COLOR_NONE="\033[0m"
# Usage: debug <text>
debug()
{
test "$APPLY_DEBUG" = 0 && return
if [ -z "$APPLY_UNIT" ]; then
echo "Debug: $*"
else
echo "$APPLY_UNIT: $*"
fi
}
# Usage: info <text>
info()
{
if [ -z "$APPLY_UNIT" ]; then
echo -e "\n${COLOR_GREEN}Info${COLOR_NONE}: $*"
else
echo -e "\n${COLOR_GREEN}$APPLY_UNIT:${COLOR_NONE} $*"
fi
}
# Usage: warning <text>
warning()
{
if [ -z "$APPLY_UNIT" ]; then
echo -e "\n${COLOR_BLUE}Warning:${COLOR_NONE} $*"
else
echo -e "\n${COLOR_BLUE}$APPLY_UNIT:${COLOR_NONE} $*"
fi
}
# Usage: error <text>
error()
{
if [ -z "$APPLY_UNIT" ]; then
echo -e "\n${COLOR_RED}Error:${COLOR_NONE} $*"
else
echo -e "\n${COLOR_RED}$APPLY_UNIT:${COLOR_NONE} $*"
fi
exit 1
}
# info "info text"
# warning "warning text"
# error "error text"
#############################################################################
#
# Functions for apply units
#
install_one_deb()
{
local NUM
# shellcheck disable=SC2086
NUM=$(find /var/lib/dpkg/info/$1.* /var/lib/dpkg/info/$1:$APPLY_ARCH.* 2>/dev/null | wc --lines)
if [ "$NUM" = 0 ]; then
debug "not installed: $1"
APPLY_DEBS+=("$1")
else
debug "already installed: $1"
fi
}
# Usage:
# apply_deb DEB [DEB ...]
apply_deb()
{
APPLY_DEBS=()
# shellcheck disable=SC2048
for i in $*; do
install_one_deb "$i"
done
test ${#APPLY_DEBS[@]} = 0 && {
debug "no .deb to install"
return
}
local CMD="apt-get -y install ${APPLY_DEBS[*]}"
# speed up installation with eatmydata
test -x /usr/bin/eatmydata && CMD="eatmydata $CMD"
# become root if necessary
test $UID = 0 || {
if [ -x /usr/bin/sudo ]; then
CMD="sudo $CMD"
else
info "Enter root password for 'su':"
# preserve env for the DEBIAN_FRONTEND
CMD="su --preserve-environment root -c \"$CMD\""
fi
}
debug "$CMD"
export DEBIAN_FRONTEND=noninteractive
eval "$CMD"
unset DEBIAN_FRONTEND
}
# Usage:
# apply_stow [STOW-DIRECTORY]
apply_stow()
{
local STOW_DIR="${1:-$APPLY_UNIT.stow}"
test -d "$STOW_DIR" || error "$STOW_DIR is not a directory"
local VERBOSE=""
test "$APPLY_DEBUG" = "1" && VERBOSE=-v
# Let stow operate either on the / directory or the user's home directory:
if [ "$UID" = 0 ]; then
APPLY_ROOT="/"
else
APPLY_ROOT="$HOME"
fi
# Remove all files that stow would install
(
# shellcheck disable=SC2164
cd "$APPLY_UNIT.stow"
find . -type f
) | (
cd "$APPLY_ROOT" || true
xargs rm -f "$VERBOSE"
)
# shellcheck disable=SC2086
stow -t "$APPLY_ROOT" $VERBOSE $APPLY_STOWFLAGS "$STOW_DIR"
}
# Usage:
# apply_compile [BUILD-DIRECTORY] [BUILD-TARGET]
# shellcheck disable=SC2120
apply_compile()
{
local DIRECTORY="${1:-$APPLY_UNIT.build}"
local TARGET="${2:-all}"
test -d "$DIRECTORY" || error "directory $DIRECTORY does not exist"
test -f "$DIRECTORY/Makefile" || error "directory $DIRECTORY has no Makefile"
make -C "$DIRECTORY" --no-print-directory "$TARGET"
}
#############################################################################
#
# Commands
#
# Usage:
# ./apply rename_stow OLD_NAME_OF.stow NEW_NAME_OF.stow
rename_stow()
{
# echo "*: $*"
# echo "1: $1"
# echo "2: $2"
# echo "3: $3"
test "$APPLY_DRYRUN" = 1 && {
info "rename '$1' '$2'"
exit 1
}
test -z "$1" && error "specify source directory"
test -z "$2" && error "specify target directory"
test -d "$1" || error "'$1' is not a directory"
test -e "$2" && error "'$2' already exists"
stow -D "$1"
git mv "$1" "$2"
stow "$2"
}
#############################################################################
#
# Option parsing
#
usage()
{
echo "TODO: $1"
exit 1
}
while getopts ":nfd" i; do
case "${i}" in
d)
APPLY_DEBUG=1
;;
n)
APPLY_DRYRUN=1
;;
f)
APPLY_FORCE=1
;;
*)
usage "unknown option"
;;
esac
done
shift $((OPTIND-1))
test "$#" = 0 && usage "no command / apply script"
# echo "*: $*"
# echo "#: $#"
# echo "APPLY_FORCE: $APPLY_FORCE"
# echo "APPLY_DRYRUN: $APPLY_DRYRUN"
test "$1" = "rename_stow" && rename_stow "$2" "$3"
# The logic here is:
# - if a $1 shell script exist, it is *sourced* (so it doesn't need to be executable)
# - The sourced script can define shell functions apply_check() and apply_run()
# - apply_check() is run. If it returns, further processing is abandoned
# - if apply_run() isn't defined, a default one is executed, that will run
# apply_stow() for the current $APPLY_UNIT
run()
{
APPLY_UNIT="$1"
APPLY_STOWFLAGS=""
# if one uses command-completion in bash then this function might be invoked with
# "foo.stow/" or "foo.stow" instead of just "foo". Fix that.
APPLY_UNIT="${APPLY_UNIT%%/}"
APPLY_UNIT="${APPLY_UNIT%%.stow}"
# Make sure the "root-*" apply units are run as root, and vica verca
if [ "${APPLY_UNIT#root}" = "$APPLY_UNIT" ]; then
#debug "is a user script"
test $UID = 0 && error "don't run me as root"
else
#debug "is a root script"
test $UID = 0 || error "please run me as root"
fi
# define 2 default functions that the sourced script can override
apply_check()
{
true
}
apply_run()
{
true
}
# If a stamp file for this apply unit exist, we do nothing. Use "-f" flag to
# override this.
mkdir -p "$APPLY_DIR/.stamps"
chmod a+rwX "$APPLY_DIR/.stamps"
test -e "$APPLY_DIR/.stamps/$APPLY_UNIT.stamp" && {
test "$APPLY_FORCE" == 1 || {
debug "done (stamp existed)"
return
}
}
# Run script if it exists. Exit afterwards, because 'te script is 'de boss!
if [ -f "$APPLY_UNIT" ]; then
# source the script
# shellcheck disable=SC1090
source "$APPLY_UNIT"
# check if the script applies to us
apply_check || {
info "disabled"
return
}
info "start"
# switch into the .data directory if it exists
cd "$APPLY_UNIT.data" 2>/dev/null || true
# should have been overridded/defined in the sourced script
apply_run || warning "script failed"
touch "$APPLY_DIR/.stamps/$APPLY_UNIT.stamp"
# Not that we don't run the stow code below. If something should be
# stowed, then the script can invoce "apply_stow" by itself.
debug "done"
return
fi
# Maybe we have something to stow?
local DONE=0
if [ -f "$APPLY_UNIT.build/Makefile" ]; then
apply_compile
DONE=1
fi
# Maybe we have something to stow?
if [ -d "$APPLY_UNIT.stow" ]; then
info "stowing"
apply_stow "$APPLY_UNIT"
DONE=1
fi
if [ "$DONE" = "1" ]; then
touch "$APPLY_DIR/.stamps/$APPLY_UNIT.stamp"
debug "done"
return
fi
test "$DONE" = "0" && error "bbo script, stow or build Makefile found"
}
# shellcheck disable=SC2048
for f in $*; do
run "$f"
done