#!/usr/bin/env bash
# Copyright (C) 2013-2014, 2016-2017, 2024 Luke T. Shumaker <lukeshu@parabola.nu>
#
# 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

	export TEXTDOMAIN='librelib'

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

	. "$(librelib messages)"

	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() {
		if [[ $# -eq 0 ]]; then
			print "Usage: %s COMMAND [ARGUMENTS]" "${0##*/}"
			print '   or: %s {-h|--help}' "${0##*/}"
			print "Tool for working with the nonfree software blacklist."
			echo
			print "Commands:"
			flag_args=()
			while read -r sec; do
				sec="$(usage-inside "$sec")"

				cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p')
				desc="$(_ "$(sed -n 2p <<<"$sec")")"
				flag_args+=("$cmd" "${desc//blacklist-/${0##*/} }")
			done < <(usage-outside)
			flag "${flag_args[@]}"
			echo
			print 'Options:'
			flag \
				'-h, --help' 'Show this message'
		else
			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
					exit $EXIT_SUCCESS
				fi
			done < <(usage-outside)
		fi
	}

	main() {
		local args mode=run
		if ! args="$(getopt -n "${0##*/}" -o 'h' -l 'help' -- "$@")"; then
			mode=errusage
		else
			eval "set -- $args"
			local flag
			while true; do
				flag=$1
				shift
				case "$flag" in
					-h | --help) mode=usage ;;
					--) break ;;
					*) panic 'unhandled flag: %q' "$flag" ;;
				esac
			done
			if [[ $mode == run && $# -eq 0 ]]; then
				gnuerror 'requires a command'
				mode=errusage
			fi
		fi
		case "$mode" in
			errusage)
				print "Try '%s --help' for more information." "${0##*/}" >&2
				return $EXIT_INVALIDARGUMENT
				;;
			usage)
				usage "$@"
				return $EXIT_SUCCESS
				;;
			run) "blacklist-$@" ;;
			*) panic 'invalid mode: %q' "$mode" ;;
		esac
	}

	main "$@"
fi
