summaryrefslogtreecommitdiff
path: root/src/lib/libreblacklist
blob: 6c354fe44543b7d17026f05200f1b1ecbfbf7cb0 (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
#!/usr/bin/env bash
# Copyright (C) 2013-2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# License: GNU GPLv2+
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if [[ "${0##*/}" != libreblacklist ]]; then
	. "$(librelib blacklist)"
else
	set -euE

	lib_file="$(librelib blacklist)"
	. "$lib_file"

	usage-outside() {
		sed -n '/^# Usage:/,/()/p' "$lib_file" |
		    tr '\n' '\r' | sed 's/\s*()\s*[{(]/\n/g'
	}
	# The output format of this is:
	#  - The first line is "Usage:"
	#  - The second line is a brief description
	#  - The last line is the command name (prefixed with "blacklist-")
	#  - The in-between lines are the extended description.
	usage-inside() {
		sed 's/\r/\n/g'<<<"$1"|sed -e '/^$/d' -e 's/^# //'
	}

	usage() {
		export TEXTDOMAIN='librelib'
		export TEXTDOMAINDIR='/usr/share/locale'
		. "$(librelib messages)"
		if [[ $# -eq 0 ]]; then
			print "Usage: %s [-h] COMMAND [ARGUMENTS]" "${0##*/}"
			print "Tool for working with the nonfree software blacklist"
			echo
			print "Commands:"
			usage-outside | while read -r sec; do sec="$(usage-inside "$sec")"
							      cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p')
							      desc="$(_ "$(sed -n 2p <<<"$sec")")"
							      flag "$cmd" "${desc//blacklist-/${0##*/} }"
			done
		else
			usage-outside | while read -r sec; do sec="$(usage-inside "$sec")"
							      cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p')
							      if [[ "$cmd" == "$1" ]]; then
								      <<<"$sec" sed '$d' |
									  while read -r line; do print "$line"; done |
									  sed "s/blacklist-/${0##*/} /g" |
									  fmt -us
								      return 0
							      fi
			done
		fi
	}

	main() {
		if [[ $# -eq 0 ]]; then
			usage >&2
			exit 1
		fi
		_blacklist_cmd=$1
		shift
		if [[ $_blacklist_cmd == -h ]]; then
			usage "$@"
		else
			"blacklist-$_blacklist_cmd" "$@"
		fi
	}

	main "$@"
fi