#!/bin/sh -eu
#
# Copyright (C) 2023 Evgeny Sinelnikov <sin@altlinux.org>
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

. shell-error

PROG="${0##*/}"
PROG_VERSION=@PROG_VERSION@

show_usage()
{
	[ -z "$*" ] || message "$*"
	echo "Try \`$PROG --help' for more information." >&2
	exit 1
}

show_help()
{
	cat <<-EOF
	Usage: $PROG [options] <license>

	License format: <VENDOR>_<PRODUCT>_License[/<VERSION>]

	Options:
	  --target <target>     set target platform;
	  --distbranch <branch> set distribution branch;

	  -l,--list             print list of known distro licenses;
	  -v,--version          print program version and exit;
	  -h,--help             show this text and exit.

	Report bugs to https://bugzilla.altlinux.org/

	EOF
	exit
}

print_version()
{
	cat <<-EOF
	$PROG version $PROG_VERSION
	Written by Evgeny Sinelnikov <sin@altlinux.org>

	Copyright (C) 2023 Evgeny Sinelnikov <sin@altlinux.org>
	This is free software; see the source for copying conditions.  There is NO
	warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	EOF
	exit
}

TEMP=$(getopt -n $PROG -o v,h,l -l target:,distbranch:,list,version,help -- "$@") ||
	show_usage
eval set -- "$TEMP"

target="$(rpm --showrc |sed -ne 's/^install arch[[:space:]]*:[[:space:]]*\([^[:space:]]\+\).*/\1/p')"
distbranch=$(rpm --eval %_priority_distbranch | cut -d _ -f 1)
mode=check

license=
license_path=

licenses_dir="/usr/share/distro-licenses"
licenses_regexp='^[[:alpha:]][[:alnum:]]\+_[[:alpha:]][[:alnum:]]\+_License\($\|/[[:digit:]]\([[:digit:].]*[[:digit:]]\)*$\)'

valid_targets="x86_64 aarch64 ppc64le"
valid_distbranches="sisyphus p10 p11"

while :; do
	case "$1" in
		-h|--help) show_help
			;;
		-v|--version) print_version
			;;
		--target) shift; target="$1"
			;;
		--distbranch) shift; distbranch="$1"
			;;
		-l|--list) mode=list
			;;
		--) shift; break
			;;
	esac
	shift
done

test -n "$target" && echo "$valid_targets" | grep -q -w "$target" ||
	fatal "target '$target' is not valid"

test -n "$distbranch" && echo "$valid_distbranches" | grep -q -w "$distbranch" ||
	fatal "distribution branch '$distbranch' is not valid"

license_check()
{
	local allowed_distbranch_list="$license_path/distbranch.list"
	local allowed_target_list="$license_path/target.list"

	if [ -f "$allowed_distbranch_list" ]; then
		cat "$allowed_distbranch_list" | grep -q -w "$distbranch" ||
			fatal "distribution branch '$distbranch' is not allowed"
	fi

	if [ -f "$allowed_target_list" ]; then
		cat "$allowed_target_list" | grep -q -w "$target" ||
			fatal "distribution target '$target' is not allowed"
	fi
}

case "$mode" in
	list)
		find "$licenses_dir" -mindepth 1 -maxdepth 2 -type d |
			sed "s,^$licenses_dir/,," | grep "$licenses_regexp"
		;;

	check)
		[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'

		license="$1"; shift
		echo "$license" | grep -q  "$licenses_regexp" ||
			fatal "license name '$license' is not valid"

		license_path="$licenses_dir/$license"
		[ -d "$license_path" ] ||
			fatal "license directory '$license_path' not found"

		license_check
		;;
esac
