#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
This script uses the Generator class to generate the scripts corresponding to some simulations.
'''

import argparse

from hateno import jsonfiles
from hateno.folder import Folder
from hateno.generator import Generator
from hateno.errors import *

def addArguments(parser):
	parser.add_argument('--config', type = str, help = 'name of the config to use')
	parser.add_argument('--output-dir', type = str, help = 'folder to create')
	parser.add_argument('--empty-output', action = 'store_true', help = 'ensure the output directory is empty')
	parser.add_argument('folder_path', type = str, help = 'path to the simulations folder')
	parser.add_argument('simulations_list', type = str, help = 'path to the file where the simulations list can be found')

def action(args):
	generator = Generator(args.folder_path)

	simulations = jsonfiles.read(args.simulations_list, allow_generator = True)
	generator.add(simulations)

	if args.output_dir is None:
		print('\n'.join(generator.parse()['data_lists']['COMMAND_LINES']))
		exit()

	try:
		generator.generate(args.output_dir, args.config, empty_dest = args.empty_output)

	except DestinationFolderExistsError:
		print('The output directory already exists.')

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description = 'Generate command lines and scripts to create simulations.')
	addArguments(parser)
	action(parser.parse_args())
