#!/usr/bin/python3

#	control++ : System configuration tool
#	Copyright (C) 2017-2019 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 os
import argparse
import subprocess
import ax.printer
import importlib; tests = importlib.import_module('tests-common')

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Defines

TARGETS = ['bl', 'wl', 'all']
BW_LIST_MODESETTING = [os.path.join(tests.EXEC_DIR, 'bw-list-modesetting'),
	'--indent', '--list']

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

argparser = argparse.ArgumentParser()
argparser.add_argument(
	'-t', '--targets',
	type=str, choices=TARGETS, required=True,
	help='Targets of the testing (' + ' | '.join(TARGETS) + ')',
	)
args = argparser.parse_args()

if 'all' in args.targets:
	args.targets = TARGETS[:-1]

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Initialising the printer

printer = ax.printer.Printer(
	plain = True,
	silent = False,
	block_printing = False
	)

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

if __name__ == '__main__':

	# Running the tests
	command = []
	success = 0
	for target in args.targets:
		if target == 'bl':
			command = BW_LIST_MODESETTING + ['black']
		elif target == 'wl':
			command = BW_LIST_MODESETTING + ['white']
		printer.LineBegin("Running '" + ' '.join(command) + "'")
		printer.LineEnd('[IN PROGRESS]')
		res = subprocess.run(command)
		printer.LineRestart()
		if res.returncode == 0:
			printer.LineEnd('[V]')
			success += 1
		else:
			printer.LineEnd('[X]')

	# Summarizing
	print(success, 'of', len(args.targets), 'tests have succeeded.')
	exit(0 if success == len(args.targets) else 1)
