#!/usr/bin/python3

#	cmp-branches : Utility for comparing ALT Linux package repositories
#	Copyright (C) 2024 Alexey Appolonov
#
#	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 3 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/>.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

import argparse
from cmp_branches.helpers import CheckAbsPath, GetPackagesOfMultipleRepo, \
	GetPackagesOfMultipleDistro

DESCRIPTION = 'Compare packages of two ALT branches'

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Parsing the arguments

argparser = argparse.ArgumentParser(description=DESCRIPTION)
argparser.add_argument(
	'-b', '--branches',
	metavar='BRANCH_NAMES', type=str, nargs=2, required=True,
	help='Names of branches'
	)
argparser.add_argument(
	'-a', '--alt_root_dir',
	metavar='ABS_PATH', type=str, default='/',
	help='Absolute path of a directory where the ALT server is mounted'
	)
argparser.add_argument(
	'-d', '--distro',
	metavar='DISTRO_NAME', nargs='+', type=str, default=[],
	help='File paths of distro lists'
	)
args = argparser.parse_args()

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

def Annotation(branches):

	print(f'{branches[0]} bin packages that are missing in {branches[1]}:')

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

def Record(bin_name, src_name, distro):

	print(f'{bin_name} (src: {src_name})' +
		(f' [AFFECTED DISTRO: {", ".join(distro)}]' if affected_distro else ''))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

def Err(msg):

	print('[ERROR' + (f': {msg}' if msg else '') + ']')
	exit(1)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

if __name__ == '__main__':

	err = CheckAbsPath(args.alt_root_dir)
	if err:
		Err(err)

	repo_packages, err = GetPackagesOfMultipleRepo(args.branches,
		args.alt_root_dir)
	if err:
		Err(err)

	distro_packages, err = GetPackagesOfMultipleDistro(args.distro)
	if err:
		Err(err)

	first_record = True

	for bin_name, src_name in repo_packages[args.branches[0]].items():
		if bin_name not in repo_packages[args.branches[1]]:
			if first_record:
				Annotation(args.branches)
			first_record = False
			affected_distro = []
			for distro_name, packages in distro_packages.items():
				if bin_name in packages:
					affected_distro.append(distro_name)
			Record(bin_name, src_name, affected_distro)

	exit(0)
