#!/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.generator import Generator
from hateno.generator.errors import *
from hateno.utils import jsonfiles, utils

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('simulations_list', type = str, help = 'path to the file where the simulations list can be found')

def main(args):
	folder = utils.findFolder()

	if folder is None:
		print('No Hateno folder found')
		return

	generator = Generator(folder)

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

	if args.output_dir is None:
		print('\n'.join(generator.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)
	main(parser.parse_args())
