about summary refs log tree commit diff
path: root/xi
blob: 6a8c64838214b425114bb49d70e22d42c9ab084b (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
#!/bin/sh
# xi PKGS... - like xbps-install -S, but take cwd repo and sudo/su into account

if [ -n "$XBPS_HOSTDIR" ]; then
	XBPS_BINPKGS="$XBPS_HOSTDIR/binpkgs"
else
	XBPS_DISTDIR="$(xdistdir 2>/dev/null)" || XBPS_DISTDIR=.
	XBPS_BINPKGS="$XBPS_DISTDIR/hostdir/binpkgs"
fi

set -- \
	--repository="$XBPS_BINPKGS" \
	--repository="$XBPS_BINPKGS/nonfree" \
	--repository="$XBPS_BINPKGS/multilib" \
	--repository="$XBPS_BINPKGS/multilib/nonfree" \
	--repository="$XBPS_BINPKGS/debug" \
	"$@"

if BRANCH=$(git symbolic-ref -q --short HEAD 2>/dev/null) && [ -n "$BRANCH" ]; then
	set -- \
		--repository="$XBPS_BINPKGS/$BRANCH" \
		--repository="$XBPS_BINPKGS/$BRANCH/nonfree" \
		--repository="$XBPS_BINPKGS/$BRANCH/multilib" \
		--repository="$XBPS_BINPKGS/$BRANCH/multilib/nonfree" \
		--repository="$XBPS_BINPKGS/$BRANCH/debug" \
		"$@"
fi

which_sudo() {
	if [ "$(id -u)" = "0" ]; then
		return
	elif command -v sudo >/dev/null && sudo -l | grep -q -e ' ALL$' -e xbps-install; then
		echo sudo
	elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then
		echo doas
	else
		echo su
	fi
}

do_install() {
	if [ "$SUDO" = su ]; then
		su root -c 'xbps-install "$@"' -- sh "$@"
	else
		$SUDO xbps-install "$@"
	fi
}

SUDO=$(which_sudo)
do_install -S "$@"
status=$?
if [ $status -eq 16 ]; then
	do_install -u xbps &&
	do_install -S "$@"
else
	exit $status
fi