#!/usr/bin/python3

#	anti-cppcheck : utility for processing reports with static analysis
#	Copyright (C) 2019-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
import anticppcheck.machinery as machinery
from anticppcheck.common import argparser
from anticppcheck.common import PrintExamples

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

argparser.add_argument(
	'-s', '--space', metavar='DIR_PATH', type=str,
	help='Mounting point of the basealt-server\'s "/space" dir')
argparser.add_argument(
	'-b', '--branch', metavar='BRANCH_NAME', type=str,
	help='Name of the targeted branch')
argparser.add_argument(
	'-f', '--force', action='store_true',
	help='Force overwriting of existing .anticppcheck files')
args = argparser.parse_args()

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Usage examples

EXAMPLES = [(
	'Add comments to nvrpm err/warn messages of a report-file '
	'and write a resulting content into an output file',
	'--type nvrpm --path REPORT_FILE_NAME --comments COMMENTS_FILE_NAME '
	'--output OUTPUT_FILE_NAME'
	),(
	'Add comments to herodotos err/warn messages of a report-file and write '
	'a resulting content into .anticppcheck file of the same directory '
	'(with force overwriting)',
	'--type herodotos --path REPORT_FILE_NAME --comments COMMENTS_FILE_NAME '
	'--auto_output --force'
	)]

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

if __name__ == '__main__':

	# Printing the usage examples
	if args.examples:
		PrintExamples(__file__, EXAMPLES)
		exit(0)

	# Checking required arguments
	if any(not arg for arg in \
			(args.type, args.analyzer, args.path, args.comments)):
		argparser.error('--type, --analyzer, --path and --comments are required')

	# Checking given combinations of params
	if bool(args.space) != bool(args.branch):
		argparser.error('--space and --branch can only be used simultaneously')

	# Creating the machine that instantly parses a given report file(s)
	#	and a given comments file and holds the data for later requests
	try:
		machine = machinery.Machine(args.type,
			args.analyzer,
			args.path,
			args.comments)
	except Exception as e:
		print('Error: ' + str(e))
		exit(1)

	# For every report forming an output string and writing it to a file
	reports = machine.ReportsWithComments(args.space, args.branch)
	for report_filepath, report in reports.items():
		if not report:
			continue
		# Generating an output file name first
		filepath = machinery.AutoFilePath(report_filepath, args.force)
		with open(filepath, 'w') as f:
			f.write(report)

	exit(0)
