blob: be7ac3b608fcb59505eea3752739a313065ad418 (
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
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
|
#!/usr/bin/env zsh
# Recommended: hg >= 1.0
PDIR="$HOME/packages"
PKG_ROOT="ssh://derf.homelinux.org/~/var/packages_root"
CL_OPTIONS=(-q)
info=$'\e[0;36m'
error=$'\e[0;31m'
reset=$'\e[0m'
##
## Internal functions for displaying stuff
##
info () {
echo -ne "${info}$*${reset}"
}
warn () {
echo -ne "${error}$*${reset}"
}
die () {
echo -ne "${error}$*${reset}"
exit 100
}
check_installed () {
[ -n "$1" -a -d $PDIR/$1 ] || die "Not installed: $1\n"
}
check_valid () {
[ -d $PDIR/$1/.hg ] || die "Not a valid package name: '$1'\n"
}
clear_line () {
echo -ne "\r \r"
}
# Read local configuration
if ([ -f $HOME/.pkg.conf ]) {
. $HOME/.pkg.conf
}
export PDIR
export PKG_ROOT
##
## Make sure everything's sane
## Warn otherwise
##
# we need sed -r (and sed -i, but not checking for that here)
QUUX=$(echo foox | sed -r 's/^fo{2}(.)$/quu\1/' 2> /dev/null)
if ([ "$QUUX" != 'quux' ]) {
warn "sed is not working properly. This may produce unexpected behaviour.\n"
}
if ([ ! -d $PDIR ]) {
die "$PDIR not found!!\n"
}
##
## Some additional variables related to PKG_ROOT
##
# Protocol
if (echo "$PKG_ROOT" | grep "^ssh" &> /dev/null) {
PKG_PROTO='ssh'
} elif (echo "$PKG_ROOT" | grep "^/" &> /dev/null) {
PKG_PROTO='file'
} else {
false
}
# Host
if ([ "$PKG_PROTO" = "ssh" ]) {
PKG_HOST=$(echo "$PKG_ROOT" | sed 's!^ssh://!!' | sed -r 's!^([^/]*)/.*$!\1!')
}
# Remote path
if ([ "$PKG_PROTO" = "ssh" ]) {
PKG_PATH=$(echo "$PKG_ROOT" | sed 's!^ssh://'"$PKG_HOST"'/!!')
} elif ([ "$PKG_PROTO" = "file" ]) {
PKG_PATH="$PKG_ROOT"
}
##
## Ask the user for confirmation
##
# Default reply: Yes
confirm_yes () {
echo -n "$* [Y/n] "
read -k 1
[ $REPLY != $'\n' ] && echo
if ([ "$REPLY" = 'y' -o "$REPLY" = 'Y' -o "$REPLY" = $'\n' ]) {
true
} else {
false
}
}
# Default reply: No
confirm_no () {
echo -n "$* [y/N] "
read -q
}
##
## Major internal functions
##
## VCS Wrappers
vcs_add () {
cd "$PDIR"
case "$1" in
git) git clone "$2" ;;
hg|'') hg clone "$2" ;;
*) die "Cannot handle repository format '$1'\n" ;;
esac
}
vcs_incoming () {
# [ -d .vcs ] && vcs incoming will not work here, since the [] will return
# something. We need the return value of vcs.
[ -d .hg ] && {hg --quiet incoming; return}
[ -d .git ] && {true ; return}
}
vcs_update () {
[ -d .hg ] && hg pull --update
[ -d .git ] && git pull
}
# Return an understandable priority from the numeric one
real_priority () {
case "$1" in
6) echo "essential" ;;
5) echo "important" ;;
4) echo "required" ;;
3) echo "standard" ;;
2) echo "optional" ;;
1) echo "extra" ;;
*) echo ;;
esac
}
# Execute a hook
exec_hook () {
package="$1"
hook="$2"
if ([ -r $PDIR/$package/hooks/$hook ]) {
info "Executing $hook hook\n"
cd $PDIR/$package
. hooks/$hook
}
}
# Check dependencies and offer to install them
check_deps () {
[ -r $PDIR/$1/dependencies ] || return 0
DEPS=($(cat $PDIR/$1/dependencies))
INSTALL=()
for dep in $DEPS; {
if ([ ! -d $PDIR/$dep ]) {
INSTALL+="$dep"
}
}
if ([ -n "$INSTALL" ]) {
info "$1 has unmet dependencies: "
echo "$INSTALL"
if (confirm_yes "Install dependencies?") {
for pkg in $INSTALL; {
pkg_add "$pkg"
}
} else {
die "Installation aborted"
}
}
}
check_conflicts () {
[ -r $PDIR/$1/conflicts ] || return 0
CONFLICTS=($(cat $PDIR/$1/conflicts))
REMOVE=()
for conflict in $CONFLICTS; {
if ([ -d $PDIR/$conflict ]) {
REMOVE+="$conflict"
}
}
if ([ -n "$REMOVE" ]) {
info "$1 conflicts with the following packages: "
echo "$REMOVE"
if (confirm_no "Remove conflicting packages and proceed with installation?") {
for pkg in $REMOVE; {
pkg_remove "$pkg"
}
} else {
die "Installation aborted"
}
}
}
# Write a packages' files to .collected
# Currently, this is only documentation
populate_collected () {
cd $PDIR/$1 || return
info "Enabling documentation "
if ([ -d man ]) {
for i in man/*/*; {
section=${i:h:t}
manpage=${i:t}
if (podchecker man/$section/$manpage &> /dev/null) {
pod2man -s $section -c "$1 package" -r "/home/derf" man/$section/$manpage > $PDIR/.collected/man/man$section/$manpage.$section
echo -n "+"
} else {
echo -n "."
}
}
}
if ([ -d bin ]) {
for i in bin/*; {
if (podchecker $i &> /dev/null) {
pod2man $i > $PDIR/.collected/man/man1/${i:t}.1
echo -n "+"
} else {
echo -n "."
}
}
}
clear_line
if ([ -d bin ]) {
for i in bin/*(*); {
if ([ "$(readlink $HOME/$i)" != "../$1/$i" ]) {
rm -f "$HOME/$i"
ln -s ../${PDIR//$HOME\/}/$1/$i $HOME/$i
}
}
}
}
# Remove a packages' files from .collected
# Assuming there are no packages with colliding files
# TODO: Make sure there are none
genocide_collected () {
cd $PDIR/$1 || return
info "Removing documentation"
if ([ -d man ]) {
for i in man/*/*; {
section=${i:h:t}
manual=${i:t}
if ([ -e $PDIR/.collected/man/man$section/$manual ]) {
rm $PDIR/.collected/man/man$section/$manual
}
}
}
if ([ -d bin ]) {
for i in bin/*; {
rm -f $PDIR/.collected/man/man1/${i:t}.1
}
}
clear_line
if ([ -d bin ]) {
for i in bin/*(*); {
if ([ "$(readlink $HOME/$i)" = "../${PDIR//$HOME\/}/$1/$i" ]) {
rm -f $HOME/$i
}
}
}
}
##
## Finally - the functions actually doing something
##
pkg_add () {
if ([ -d $PDIR/$1 ]) {
info "Package '$1' is already installed!\n"
return 100
}
cd $PDIR || return 255
info "Retrieving package $1...\n"
vcs_add "$2" "$PKG_ROOT/$1" || return 255
if ([ -r $PDIR/$1/Makefile ]) {
info "Building binaries\n"
cd $PDIR/$1
make
}
exec_hook "$1" "post-add"
check_deps "$1"
check_conflicts "$1"
cd $PDIR/$1
checklinks $CL_OPTIONS
populate_collected "$1"
return 0
}
pkg_remove () {
check_installed "$1"
check_valid "$1"
cd $PDIR/$1
if ([ -r priority ]) {
if ([ $(cat priority) -gt 3 ]) {
confirm_no "Package '$1' is $(real_priority $(cat priority)). Really remove?" || return
}
}
exec_hook "$1" "pre-remove"
genocide_collected "$1"
rm -r $PDIR/$1
info "Package removed.\n"
}
pkg_update () {
check_installed "$1"
cd $PDIR/$1
info "Looking for updates: $1"
NEW=$(vcs_incoming)
if ([ $? = 0 ]) {
clear_line
info "Updating $1"
[ -n "$NEW" ] && info "to $(echo $NEW | tail -n 1)"
info "\n"
vcs_update
check_deps "$1"
if ([ -r Makefile ]) {
info "Building binaries\n"
make
}
exec_hook "$1" "post-update"
checklinks $CL_OPTIONS
populate_collected "$1"
} else {
clear_line
}
cd $PDIR
}
# If no package was specified, update everything
pkg_update_wrapper () {
if ([ -n "$1" ]) {
pkg_update "$1"
} else {
cd $PDIR
for i in *(/); {
pkg_update "$i"
}
}
}
# Change the 'default' url in every package's .hgrc
pkg_changesrc () {
cd $PDIR
for i in *(/); {
if ([ -f $i/.hg/hgrc ]) {
sed -i "s!default = [^:]*://.*\$!default = $1/$i!" $i/.hg/hgrc
}
}
}
pkg_list_installed () {
=ls -1 $PDIR
}
pkg_list_available () {
if ([ "$PKG_PROTO" = 'ssh' ]) {
ssh -q "$PKG_HOST" ls -1 "$PKG_PATH"
} elif ([ "$PKG_PROTO" = 'file' ]) {
ls -1 "$PKG_PATH"
}
}
# Local modifications should not be overseen...
pkg_status () {
check_installed "$1"
cd $PDIR/$1
check_deps "$1"
check_conflicts "$1"
checklinks $CL_OPTIONS
STATUS=$($VCS_CMD $VCS_STATUS)
clear_line
if ([ -n "$STATUS" ]) {
info "Locally modified in $1:\n"
echo "$STATUS"
}
}
# Same as with update - if no package is specified, check all
pkg_status_wrapper () {
if ([ -n "$1" ]) {
pkg_status "$1"
} else {
cd $PDIR
for i in *(/); {
pkg_status "$i"
}
}
}
# Various information related to a package
pkg_info () {
check_installed "$1"
cd $PDIR/$1
[ -z "$1" ] && die "Not enough arguments\n"
# Fetch the infos
NAME="$1"
if ([ -r priority ]) {
PRIORITY=$(cat priority)
PRIORITY_NAME=$(real_priority "$PRIORITY")
}
LOG=$(hg log)
VERSION=$(echo $LOG | grep -m1 'changeset:' | grep -Eo '[0-9]{1,}:[0-9a-f]*')
DATE=$(echo $LOG | grep -m1 'date:' | grep -Eo '[A-Z][a-z]{2}.*')
if ([ -r dependencies ]) {
DEPENDENCIES=$(cat dependencies | tr "\n" " " | sed 's/ /, /g' | sed 's/, $//')
}
if ([ -r tags ]) {
TAGS=$(cat tags | tr "\n" " " | sed 's/ /, /g' | sed 's/, $//')
}
if ([ -d hooks ]) {
HOOKS=$(ls hooks)
}
if ([ -r Makefile ]) {
MAKEFILE=1
}
SIZE=$(du -sh .hg | grep -o '.*[KMG]')
if ([ -r description ]) {
DESCRIPTION=$(cat description)
}
show_info () {
[ -z "$2" ] && return
info "$1: "
echo "$2"
}
show_info "Package" "$NAME"
show_info "Priority" "$PRIORITY ($PRIORITY_NAME)"
show_info "Version" "$VERSION"
show_info "Date" "$DATE"
show_info "Depends" "$DEPENDENCIES"
show_info "Tags" "$TAGS"
show_info "Hooks" "$HOOKS"
show_info "Repostiry Size" "$SIZE"
show_info "Description" "$DESCRIPTION"
}
pkg_log () {
check_installed "$1"
hg -R $PDIR/$1 glog | less
}
pkg_changelog () {
[ -r $PDIR/$1/changelog ] && view $PDIR/$1/changelog
}
# Almost obsoleted by the bin/* -> man/ stuff
pkg_doc () {
if ([ -r $PDIR/.collected/man/$1 ]) {
man $PDIR/.collected/man/$1
} elif ([ -r $PDIR/.collected/doc/$1 ]) {
less $PDIR/.collected/doc/$1
} else {
echo "No documentation found"
}
}
##
## Now what shall we do...
##
case "$1" in
add) pkg_add "$2" "$3" ;;
changelog) pkg_changelog "$2" ;;
changeroot) pkg_changesrc "$2" ;;
delete) pkg_remove "$2" ;;
doc) pkg_doc "$2" ;;
info) pkg_info "$2" ;;
install) pkg_add "$2" "$3" ;;
list) pkg_list_installed ;;
list-all) pkg_list_available ;;
log) pkg_log "$2" ;;
remove) pkg_remove "$2" ;;
status) pkg_status_wrapper "$2" ;;
update) pkg_update_wrapper "$2" ;;
*) die "wait, what?\n" ;;
esac
|