aboutsummaryrefslogtreecommitdiffstats
path: root/bosun
blob: 62400294dfc6d92b11a5ed349f6559095d8e4814 (plain) (blame)
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
#!/bin/sh

# allow overriding STOW_DIR by the user
export STOW_DIR=${STOW_DIR:-/etc/portage/stow}

need_root() {
	if test "$(id -u)" -ne 0; then
		printf 'This action requires superuser access.\n'
		exit 1
	fi
}

add() {
	need_root
	stow -S "$@"
}

remove() {
	need_root
	stow -D "$@"
}

flush() {
	need_root
	list | xargs -- stow -D
}

rebuild() {
	need_root
	list | xargs -- stow -R
}

list() {
	subcmd="$1"
	shift
	case $subcmd in
		active) list_active;;
		all) list_all;;
		available) list_available;;
		*) list_active;;
	esac
}

list_active() {
	# XXX: -r for xargs is a GNU extension
	find /etc/portage/ -lname "*stow/*" -print0 2>/dev/null \
		| xargs -r0 readlink \
		| sed 's:.*stow/\([^/]\+\)/.*:\1:' \
		| sort -u
}

list_all() {
	ls -1 "$STOW_DIR"
}

list_available() {
	tmp="$(mktemp)" || exit 1
	list_active > "$tmp"
	list_all | comm -13 "$tmp" - 
	rm -f "$tmp"
}

cmd="$1"
shift
case $cmd in
	add) add "$@";;
	flush) flush;;
	list) list "$@";;
	rebuild) rebuild;;
	remove) remove "$@";;
	*) list_active;;
esac