#!/usr/bin/env bash
# Copyright (C) 2013, 2017, 2024 Luke T. Shumaker <lukeshu@parabola.nu>
#
# License: GNU GPLv3+
#
# This file is part of Parabola.
#
# Parabola 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 3 of the License, or
# (at your option) any later version.
#
# Parabola 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 Parabola.  If not, see <http://www.gnu.org/licenses/>.

. "$(librelib messages)"

# Make sure these match pkgbuild-check-nonfree
declare -ri _E_ERROR=1
declare -ri _E_LIC_UNKNOWN=4
declare -ri _E_LIC_NOGPL=8
declare -ri _E_LIC_NONFREE=16
declare -ri _E_DEP_NONFREE=32
declare -ri _E_PKG_NONFREE=64
declare -ri _E_BITS=(1 4 8 16 32 64)

usage() {
	print "Usage: %s [OPTIONS] STATUS" "${0##*/}"
	print "Summarizes a status code from pkgbuild-check-nonfree."
	echo
	prose 'It thresholds the issues it finds, only failing for error-level
	       issues, and ignoring warnings.  Unless `-q` is specified, it also
	       prints a summary of the issues it found.'
	echo
	print 'Options:'
	flag \
		'-q' 'Be quiet' \
		'-h, --help' 'Show this message'
}

main() {
	local quiet=false
	local args mode=run
	if ! args="$(getopt -n "${0##*/}" -o 'qh' -l 'help' -- "$@")"; then
		mode=errusage
	else
		eval "set -- $args"
		local flag
		while true; do
			flag=$1
			shift
			case "$flag" in
				-q) quiet=true ;;
				-h | --help) mode=usage ;;
				--) break ;;
				*) panic 'unhandled flag: %q' "$flag" ;;
			esac
		done
		if [[ $mode == run ]]; then
			if [[ $# -ne 1 ]]; then
				gnuerror 'require exactly 1 argument, got %d' "$#"
				mode=errusage
			elif ! [[ $1 =~ ^[0-9]+$ ]]; then
				gnuerror 'STATUS must be an integer'
				mode=errusage
			fi
		fi
	fi
	case "$mode" in
		errusage)
			print "Try '%s --help' for more information." "${0##*/}" >&2
			return $EXIT_INVALIDARGUMENT
			;;
		usage)
			usage
			return $EXIT_SUCCESS
			;;
		run) : ;;
		*) panic 'invalid mode: %q' "$mode" ;;
	esac

	if $quiet; then
		error() { :; }
		warning() { :; }
	fi

	parse "$1"
}

parse() {
	[[ $# == 1 ]] || panic 'malformed call to parse'
	declare -i s=$1

	declare -i ret=$EXIT_SUCCESS
	declare -i i
	for i in "${_E_BITS[@]}"; do
		if [[ $((s & i)) -gt 0 ]]; then
			case $i in
				$_E_ERROR)
					# could be anything, assume the worst
					error "There was an error processing the PKGBUILD"
					ret=$EXIT_FAILURE
					;;
				$_E_LIC_UNKNOWN)
					warning "This PKGBUILD has an unknown license"
					;;
				$_E_LIC_NOGPL)
					warning "This PKGBUILD has a GPL-incompatible license"
					;;
				$_E_LIC_NONFREE)
					error "This PKGBUILD has a known nonfree license"
					ret=$EXIT_FAILURE
					;;
				$_E_DEP_NONFREE)
					error "This PKGBUILD depends on known nonfree packages"
					ret=$EXIT_FAILURE
					;;
				$_E_PKG_NONFREE)
					error "This PKGBUILD is for a known nonfree package"
					ret=$EXIT_FAILURE
					;;
			esac
		fi
	done
	return $ret
}

main "$@"
