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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
#!/bin/sh
# This file was automatically created - don't modify it!
#%%SOURCEINFO-START%%
# source version: $Id: $
# installed by port/package: %%SOURCEINFO-PACKAGE%%
# installed from file: %%SOURCEINFO-FILE%%
#%%SOURCEINFO-END%%
if [ -n "$MULTIEXEC_WRAPPER_VERBOSE" ]
then
QUIET=0
else
QUIET=1
fi
#%%PKG-INSTALL-START%%
# ===== IMPORTANT =====
# If you modify any byte of this script outside of the
# %%SOURCEINFO-*%% and #%%PKG-INSTALL-*%% sections, then
# don't forget to bump the MINOR version variable below!
#
case "$2" in
# no-op cases if called as port/package de/installation script
PRE-INSTALL|POST-DEINSTALL)
exit 0
;;
esac
#%%PKG-INSTALL-END%%
ME=multiexec-wrapper
CALLED_NAME="`basename $0`"
PREFIX=%%PREFIX%%
CONFIG="$PREFIX/etc/$ME.conf"
lookup_executable() {
local calledname pkgname name executable rest \
portname version envvarname envvarversion result
# use global variable for the result to avoid subshell
lookup_executable_result=
[ -r "$CONFIG" ] || return 1
calledname="$1"
result=
while read pkgname name executable rest
do
if [ "$name" = "$calledname" -a -n "$executable" ]
then
result="$executable"
portname=`expr "$pkgname" : "\(.*\)-"`
envvarname="`echo -n "$portname" | \
tr "[:lower:]" "[:upper:]" | \
tr -cs "[:alnum:]" "_"`"
# <PORTNAME> variable selects package if name matches
eval envvarvalue='$'"$envvarname"
if [ -n "$envvarvalue" ]
then
break
fi
# <PORTNAME>_VERSION variable selects package version
version=`expr "$pkgname" : ".*-\([^-_,]*\)"`
eval envvarversion='$'"$envvarname"_VERSION
if [ "$envvarversion" = "$version" ]
then
break
fi
fi
done < "$CONFIG"
lookup_executable_result="$result"
}
warn () {
echo "${ME}: $1" 1>&2
}
die () {
warn "$1"
exit 1
}
# Normal usage: if called through a symlink,
# lookup and exec a registered executable.
if [ -L "$0" -a "$CALLED_NAME" != "$ME" ]
then
lookup_executable "$CALLED_NAME"
executable="$lookup_executable_result"
if [ -x "$executable" ]
then
exec "$executable" "$@"
elif [ -z "$executable" ]
then
die "no executable registered for '$CALLED_NAME'."
elif [ ! -e "$executable" ]
then
die "$executable does not exist."
else
die "$executable is not executable."
fi
fi
EPOCH=1
CONFIG_FORMAT=1
MAJOR=1
MINOR=1
VERSION=$EPOCH.$CONFIG_FORMAT.$MAJOR.$MINOR
usage() {
echo "usage: $ME <cmd> <arg> ..."
echo " with one of the following <cmd>s:"
#%%PKG-INSTALL-START%%
echo " install-self-to <prefix> <pkgname>"
echo " deinstall-self-from <prefix>"
#%%PKG-INSTALL-END%%
echo " deinstall-pkg <pkgname>"
echo " register <pkgname> <name> <executable>"
echo " unregister <pkgname> <name> <executable>"
echo " version"
echo " which <name>"
echo " list-all-packages"
echo " list-packages-for-name <name>"
exit 1
}
say () {
[ $QUIET -gt 0 ] || echo "${ME}: $1"
}
version() {
echo "$VERSION"
exit 0
}
which() {
local name executable
name="$1"
lookup_executable "$name"
executable="$lookup_executable_result"
[ -z "$executable" ] && return 1
echo "$executable"
}
#%%PKG-INSTALL-START%%
install_self_to() {
local pkgname myself target oldversion tmpfile tmpfile_gz
PREFIX="$1"
pkgname="$2"
if [ "$0" = "/bin/sh" ]
then # We're stdin of a shell (installation from binary package).
# Recover original script from its b64encoded compressed source.
tmpfile=`mktemp "/tmp/$ME.XXXXXX"`
tmpfile_gz=`mktemp "/tmp/$ME.gz.XXXXXX"`
trap "rm -f $tmpfile $tmpfile_gz" EXIT
b64decode -p > $tmpfile_gz << '~EOF~'
~EOF~
gunzip -cf $tmpfile_gz > $tmpfile || die "decompression of script source for $ME failed."
myself=$tmpfile
else
myself=`realpath "$0"`
fi
target="$PREFIX/bin/$ME"
MYSELF_INSTALLED="$target"
if [ -e "$target" ]
then
if [ ! -x "$target" ]
then
die "$target already exists but is not executable."
else
oldversion=`$target version 2>/dev/null`
if [ $? -ne 0 ]
then
die "bogus $target already exists."
elif [ "$oldversion" = "$VERSION" ]
then
oldmd5=`sed -e '/^#%%PKG-INSTALL-START%%$/,/^#%%PKG-INSTALL-END%%$/d' \
-e '/^#%%SOURCEINFO-START%%$/,/^#%%SOURCEINFO-END%%$/d' \
-e 's?\%\%PREFIX\%\%?'"$PREFIX"'?g' "$target" | \
md5 -q`
newmd5=`sed -e '/^#%%PKG-INSTALL-START%%$/,/^#%%PKG-INSTALL-END%%$/d' \
-e '/^#%%SOURCEINFO-START%%$/,/^#%%SOURCEINFO-END%%$/d' \
-e 's?\%\%PREFIX\%\%?'"$PREFIX"'?g' "$myself" | \
md5 -q`
if [ "$newmd5" = "$oldmd5" ]
then
say "$myself (version $VERSION) already installed as $target."
return
else
die "$myself and $target have same version ($VERSION) but differ."
fi
elif [ `pkg_version -t "$oldversion" "$VERSION"` != "<" ]
then
say "$myself (version $VERSION) is not newer than $target (version $oldversion)."
fi
fi
elif [ ! -d `dirname $target` ]
then
if [ -e `dirname $target` ]
then
die "`dirname $target` is not a directory."
else
mkdir -p `dirname $target` || die "could not create `dirname $target`"
fi
fi
if [ ! -d `dirname $CONFIG` ]
then
if [ -e `dirname $CONFIG` ]
then
die "`dirname $CONFIG` is not a directory."
else
mkdir -p `dirname $CONFIG` || die "could not create `dirname $CONFIG`"
fi
fi
if sed -e '/^#%%PKG-INSTALL-START%%$/,/^#%%PKG-INSTALL-END%%$/d' \
-e 's?%%SOURCEINFO-PACKAGE%%?'"$pkgname"'?g' \
-e 's?%%SOURCEINFO-FILE%%?'"$myself"'?g' \
-e 's?\%\%PREFIX\%\%?'"$PREFIX"'?g' "$myself" > "$target" && \
chmod +x "$target"
then
say "$myself (version $VERSION) installed as $target."
return
else
die "failed to install $ME (version $VERSION) as $target."
fi
}
deinstall_self_from() {
local config_empty=1
PREFIX="$1"
MYSELF_INSTALLED="$PREFIX/bin/$ME"
CONFIG="$PREFIX/etc/$ME.conf"
if [ ! -e "$CONFIG" ]
then
say "$CONFIG does not exist."
elif grep -v -q "^/SELF/" "$CONFIG"
then
say "$CONFIG is not empty."
config_empty=0
elif rm -f "$CONFIG"
then
say "$CONFIG removed."
if rmdir `dirname $CONFIG`
then
say "`dirname $CONFIG` removed"
fi
else
warn "failed to remove $CONFIG."
fi
if [ "$config_empty" -eq 1 ]
then
if [ ! -e "$MYSELF_INSTALLED" ]
then
warn "$MYSELF_INSTALLED does not exist."
elif rm -f "$MYSELF_INSTALLED"
then
say "$MYSELF_INSTALLED removed."
if rmdir `dirname $MYSELF_INSTALLED`
then
say "`dirname $MYSELF_INSTALLED` removed"
fi
else
warn "failed to remove $MYSELF_INSTALLED."
fi
fi
}
#%%PKG-INSTALL-END%%
config_init() {
local format oldformat pkgname name executable rest
format="$EPOCH.$CONFIG_FORMAT"
if [ -r "$CONFIG" -a -s "$CONFIG" ]
then
oldformat=`cat "$CONFIG" | \
while read pkgname name executable rest
do
if [ "$pkgname" = "/SELF/" -a "$name" = "/FORMAT/" ]
then
echo "$executable"
break
fi
done`
if [ -z "$oldformat" ]
then
say "old $CONFIG has no format information - backing up ..."
if mv "$CONFIG" "$CONFIG.bak"
then
say "... done"
else
die "failed to backup old $CONFIG."
fi
elif [ "$oldformat" = "$format" ]
then
say "matching format statement found in $CONFIG."
else
# XXX compare versions and try to upgrade if appropriate
die "differing format statement found in $CONFIG ($executable instead of $format)."
fi
elif echo "/SELF/ /FORMAT/ $format" > "$CONFIG"
then
say "$CONFIG initialized."
else
die "failed to initialize $CONFIG"
fi
}
lookup_matches() {
local pkgname name executable rest p n e r
pkgname="$1"
name="$2"
executable="$3"
[ -r "$CONFIG" ] || return 1
cat "$CONFIG" | \
while read p n e r
do
[ "$pkgname" = "" -o "$p" = "$pkgname" ] && \
[ "$name" = "" -o "$n" = "$name" ] && \
[ "$executable" = "" -o "$e" = "$executable" ] && \
echo "$p $n $e"
done
return 0
}
config_add() {
local pkgname name executable matches
pkgname="$1"
name="$2"
executable="$3"
if [ ! -e "$CONFIG" ]
then
config_init
fi
if [ ! -r "$CONFIG" ]
then
die "$CONFIG not readable."
else
matches=`lookup_matches "$pkgname" "$name" "$executable"`
if [ -n "$matches" ]
then
say "already registered in $CONFIG: $pkgname $name $executable"
return
elif [ ! -w "$CONFIG" ]
then
die "$CONFIG is not writeable"
fi
fi
if echo "$pkgname $name $executable" >> "$CONFIG"
then
say "now added to config: $pkgname $name $executable"
else
die "write failed while trying to add to $CONFIG: $pkgname $name $executable"
fi
}
config_delete() {
local pkgname name executable matches
pkgname="$1"
name="$2"
executable="$3"
if [ ! -e "$CONFIG" ]
then
die "failed to delete from config: $CONFIG does not exist."
elif [ ! -r "$CONFIG" ]
then
die "$CONFIG not readable."
else
matches=`lookup_matches "$pkgname" "$name" "$executable"`
if [ -z "$matches" ]
then
say "no entry found in config for: $pkgname $name $executable."
elif [ ! -w "$CONFIG" ]
then
die "$CONFIG is not writeable."
elif [ ! -w `dirname "$CONFIG"` ]
then
die "config directory `dirname $CONFIG` is not writeable."
elif awk '$1 != "'"$pkgname"'" || \
$2 != "'"$name"'" || \
$3 != "'"$executable"'"' \
"$CONFIG" > "$CONFIG.new"
then
if diff -q "$CONFIG" "$CONFIG.new" >/dev/null
then
say "failed to delete from config (match problem): $pkgname $name $executable."
rm "$CONFIG.new"
elif mv -f "$CONFIG.new" "$CONFIG"
then
say "deleted from config: $pkgname $name $executable."
else
die "failed to update $CONFIG."
fi
else
die "failed to create new config $CONFIG.new."
fi
fi
}
register() {
local pkgname name executable savedwd
pkgname="$1"
name="$2"
executable="$3"
savedwd=$PWD
if [ ! -e "$executable" ]
then
die "won't register ${name}: $executable does not exist"
elif [ ! -x "$executable" ]
then
die "won't register ${name}: $executable is not executable"
elif cd "$PREFIX/bin"
then
if [ -e "$name" ]
then
if [ -L "$name" ]
then
target=`readlink $name`
if [ "$target" = "$ME" ]
then
say "symlink $name -> $ME already exists."
else
die "symlink $name already exists, but does not point to $ME (instead to $target)."
fi
else
die "$name already exists, but is not a symlink."
fi
else
if ln -s "$ME" "$name"
then
say "symlink $name -> $ME created."
else
die "failed to create symlink $name -> $ME."
fi
fi
config_add "$pkgname" "$name" "$executable"
else
die "can't cd to $PREFIX/bin."
fi
cd "$savedwd"
}
unregister() {
local pkgname name executable savedwd matches count
pkgname="$1"
name="$2"
executable="$3"
savedwd=$PWD
if [ ! -e "$executable" ]
then
say "while unregistering ${name}: $executable does not exist"
elif [ ! -x "$executable" ]
then
say "while unregistering ${name}: $executable is not executable"
fi
if cd "$PREFIX/bin"
then
if [ ! -e "$name" ]
then
say "symlink $name does not exist."
elif [ ! -L "$name" ]
then
say "$name exists, but is not a symlink."
else
target=`readlink $name`
if [ "$target" != "$ME" ]
then
say "symlink $name exists, but does not point to $ME (instead to $target)."
else
matches=`lookup_matches "" "$name"`
count=`printf "%s" "$matches" | wc -l`
if [ "$count" -eq 0 -o \
\( "$count" -eq 1 -a \
`expr "$matches" : "${pkgname}-"` -gt 0 \) ]
then
if rm -f "$name"
then
say "symlink $name removed."
else
die "failed to remove symlink $name -> $ME."
fi
else
say "not removing symlink $name because of further config entries."
fi
fi
fi
config_delete "$pkgname" "$name" "$executable"
else
die "can't cd to $PREFIX/bin."
fi
cd "$savedwd"
}
deinstall_pkg() {
local pkgname matches count p n e r
pkgname="$1"
matches=`lookup_matches "$pkgname"`
if [ $? -ne 0 ]
then
die "reading $CONFIG failed."
elif [ -z "$matches" ]
then
say "no entries for package $package in $CONFIG."
else
count=`printf "%s" "$matches" | wc -l`
say "trying to unregister $count entries for package $pkgname."
cat "$CONFIG" | \
while read p n e r
do
if [ "$p" = "$pkgname" ]
then
if [ -z "$n" ]
then
warn "in $CONFIG: incomplete config entry (no name) found for package $pkgname."
elif [ -z "$e" ]
then
warn "in $CONFIG: incomplete config entry (no executable) found for package $pkgname, name $name."
else
unregister "$pkgname" "$n" "$e"
fi
fi
done
fi
}
#%%PKG-INSTALL-START%%
case "$2" in
POST-INSTALL)
PKGNAME="$1"
install_self_to "$PKG_PREFIX" "$PKGNAME"
"$MYSELF_INSTALLED" config-init || die "config-init of $CONFIG failed."
for exename in %%EXENAMES%%
do
exepath="$PKG_PREFIX/%%EXEBINDIR%%/$exename"
if [ -x "$exepath" ]
then
"$MYSELF_INSTALLED" register "$PKGNAME" "$exename" "$exepath" \
|| die "failure during registration - aborting."
else
die "not an executable: $exepath."
fi
done
;;
DEINSTALL)
PKGNAME="$1"
PREFIX="$PKG_PREFIX"
CONFIG="$PREFIX/etc/$ME.conf"
MYSELF_INSTALLED="$PREFIX/bin/$ME"
doitmyself=0
if [ ! -e "$MYSELF_INSTALLED" ]
then
say "$ME was not (or is no longer) installed as $MYSELF_INSTALLED."
doitmyself=1
elif [ ! -x "$MYSELF_INSTALLED" ]
then
say "$MYSELF_INSTALLED is not executable."
doitmyself=1
fi
if [ "$doitmyself" -eq 0 ]
then
"$MYSELF_INSTALLED" deinstall-pkg "$PKGNAME"
else
deinstall_pkg "$PKGNAME"
fi
deinstall_self_from "$PKG_PREFIX"
;;
esac
#%%PKG-INSTALL-END%%
list_all_packages() {
grep -v "^/" "$CONFIG" | \
awk '{ print $1 }' | \
uniq
}
list_packages_for_name() {
local name="$1"
lookup_matches "" "$name" "" | \
awk '{ print $1 }'
}
# main
case "$1" in
""|-h|--help)
usage
;;
#%%PKG-INSTALL-START%%
install-self-to)
install_self_to "$2" "$3"
;;
deinstall-self-from)
deinstall_self_from "$2"
;;
#%%PKG-INSTALL-END%%
version)
version
;;
which)
which "$2"
;;
config-init)
config_init
;;
register)
register "$2" "$3" "$4"
;;
unregister)
unregister "$2" "$3" "$4"
;;
deinstall-pkg)
deinstall_pkg "$2"
;;
list-all-packages)
list_all_packages "$2"
;;
list-packages-for-name)
list_packages_for_name "$2"
;;
esac
|