#!/usr/bin/python3

#	cve-manager : CVE management tool
#	Copyright (C) 2017-2026 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 collected_info_processor.analyse          import O_ANALYSE, Analyse
from collected_info_processor.collect_new_info import O_COLLECT, \
	GetPendingUrls, CollectInfo
from collected_info_processor.common           import PLATFORMS, \
	ReadCollectedInfo, UpdateCollectedInfo
from collected_info_processor.complement_urls  import O_COMPL, \
	ComplementCollectedInfo
from collected_info_processor.join             import O_JOIN, Join
from collected_info_processor.reset            import O_RESET, Reset

DESCRIPTION = '''
	Process collected info in various ways; Collected info is a CSV file, where
	each line has the following format: "<initial_url_i>, <url_i1> <url_i2> .. 
	<url_in> | <lang_i1> <lang_i2> .. <lang_im>"'''

OPERATIONS = (O_COMPL, O_RESET, O_COLLECT, O_ANALYSE, O_JOIN)

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

argparser = argparse.ArgumentParser(description=DESCRIPTION)
argparser.add_argument(
	'-o', '--operation',
	metavar='OPERATION_NAME', type=str, choices=OPERATIONS, required=True,
	help=f'The operation to be performed ({", ".join(OPERATIONS)})'
	)
argparser.add_argument(
	'-f', '--file_path',
	metavar='ABS_FILE_PATH', type=str, required=True,
	help='Absolute path of a file that contains collected info'
	)
argparser.add_argument(
	'-a', '--added_info',
	metavar='ABS_FILE_PATH', type=str, required=True,
	help='Absolute path of a file that contains collected info that will be '
	'added to info from the file specified in the "--file_path" parameter'
	)
argparser.add_argument(
	'-p', '--platforms',
	metavar='PLATFORM_NAME', type=str, nargs='+', choices=sorted(PLATFORMS),
	default = [],
	help='Names of platforms which whould be engaged in specified operation ' \
	f'({", ".join(sorted(PLATFORMS))})'
	)
argparser.add_argument(
	'--rewrite',
	action='store_true',
	help='Write complemented info back to the initial file'
	)
argparser.add_argument(
	'--local',
	action='store_true',
	help='Try to find things in the current dir, not in $PATH'
	)
args = argparser.parse_args()

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

if __name__ == '__main__':

	new_file_path = ''

	if args.operation in (O_COMPL, O_COLLECT, O_ANALYSE, O_JOIN):
		collected_info, warnings = ReadCollectedInfo(args.file_path)
		for w in warnings:
			print(f'[WARNING: {w}]', file=sys.stderr)
		if args.operation == O_ANALYSE:
			Analyse(collected_info)
		elif args.operation == O_JOIN:
			added_info, warnings = ReadCollectedInfo(args.added_info)
			for w in warnings:
				print(f'[WARNING: {w}]', file=sys.stderr)
			Join(collected_info, added_info)
		else:
			if args.operation == O_COMPL:
				update = ComplementCollectedInfo(collected_info)
			else:
				pending_url = GetPendingUrls(collected_info, args.platforms)
				if not pending_url:
					print('There are no pending URLs, nothing to do')
					exit(0)
				update = CollectInfo(pending_url, args.local)
			new_file_path = UpdateCollectedInfo(
				update, args.operation, args.file_path, args.rewrite)
	elif args.operation == O_RESET:
		if not args.platforms:
			print('[ERROR: Platform names are not specified]')
			exit(3)
		new_file_path = Reset(args.platforms, args.file_path, args.rewrite)

	if new_file_path:
		print(f'Result is saved to "{new_file_path}"')

	exit(0)
