summaryrefslogtreecommitdiff
path: root/scripts/deps
blob: bd7eff95eddc833a64dcf1539a4f36a0524fa3dd (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
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
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
#
# scripts/deps: install krino's optional content extractors with the host's
# system package manager, per docs/design.md §14.
#
# --print prints the exact command this script would run (and the reason,
# when one platform's package list differs from the others), then exits
# without running anything and without asking for privileges. `make deps`
# calls this script with no flag; plain `make`/`make build` never calls it
# at all, so plain `make` never asks for privileges (§14).
#
# Detection:
#   - Debian family - /etc/os-release's ID *or* ID_LIKE names "debian" as
#     one of its (possibly multi-word) values. Devuan is ID=devuan,
#     ID_LIKE=debian: keying on ID=debian alone would leave it unrecognised.
#     -> apt-get install poppler-utils catdoc antiword
#   - FreeBSD (uname -s) -> pkg install poppler-utils antiword. catdoc is
#     left out on purpose: it pulls in tcl/tk.
#   - OpenBSD (uname -s) -> pkg_add poppler-utils catdoc antiword
#   - anything else: print what is needed and exit non-zero. This script
#     never guesses a package manager.
#
# Privileges: doas if present, else sudo; if neither exists (and we are not
# already root), the command is printed for the user to run as root
# themselves, rather than attempting it unprivileged and failing silently.

set -eu

print_only=0
case "${1:-}" in
	--print) print_only=1 ;;
	"") ;;
	*)
		echo "usage: $0 [--print]" >&2
		exit 2
		;;
esac

# is_debian_family: true if /etc/os-release's ID or ID_LIKE contains
# "debian" as one of its space-separated words.
is_debian_family() {
	[ -r /etc/os-release ] || return 1
	(
		. /etc/os-release
		for word in ${ID:-} ${ID_LIKE:-}; do
			[ "$word" = "debian" ] && exit 0
		done
		exit 1
	)
}

# privileged CMD...: CMD prefixed with doas or sudo, whichever is found
# first; unprefixed if we are already root or if neither exists (the
# caller decides what to do in that last case).
privileged() {
	if [ "$(id -u)" = 0 ]; then
		printf '%s\n' "$*"
	elif command -v doas >/dev/null 2>&1; then
		printf 'doas %s\n' "$*"
	elif command -v sudo >/dev/null 2>&1; then
		printf 'sudo %s\n' "$*"
	else
		printf '%s\n' "$*"
	fi
}

note=""
if is_debian_family; then
	pkgcmd="apt-get install poppler-utils catdoc antiword"
elif [ "$(uname -s)" = "FreeBSD" ]; then
	pkgcmd="pkg install poppler-utils antiword"
	note="catdoc left out on FreeBSD: it pulls in tcl/tk."
elif [ "$(uname -s)" = "OpenBSD" ]; then
	pkgcmd="pkg_add poppler-utils catdoc antiword"
else
	echo "krino's optional extractors: poppler-utils (pdftotext), catdoc, antiword" >&2
	echo "no supported package manager detected (uname -s: $(uname -s), no debian-family /etc/os-release)" >&2
	echo "install them yourself with whatever this system uses" >&2
	exit 1
fi

cmd=$(privileged $pkgcmd)

[ -n "$note" ] && echo "$note" >&2

if [ "$print_only" = 1 ]; then
	echo "$cmd"
	exit 0
fi

if [ "$(id -u)" != 0 ] && ! command -v doas >/dev/null 2>&1 && ! command -v sudo >/dev/null 2>&1; then
	echo "neither doas nor sudo found; run this yourself as root:" >&2
	echo "$cmd" >&2
	exit 1
fi

echo "$cmd"
exec $cmd