/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
 * SPDX-License-Identifier: GPL-3.0-or-later */

#include "emu_args.h"

#include "common.h"
#include "ovni.h"
#include "path.h"
#include "models.h"
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

static char progname[] = "ovniemu";
static char version[] = OVNI_LIB_VERSION;

static void
usage(void)
{
	rerr("%s -- version %s\n", progname, version);
	rerr("\n");
	rerr("Usage: %s [-c offsetfile] [-abdlh] tracedir\n", progname);
	rerr("\n");
	rerr("Options:\n");
	rerr("  -c offsetfile      Use the given offset file to correct\n");
	rerr("                     the clocks among nodes. It can be\n");
	rerr("                     generated by the ovnisync program\n");
	rerr("\n");
	rerr("  -a                 Enable all models (experimental)\n");
	rerr("\n");
	rerr("  -b                 Enable breakdown model (experimental)\n");
	rerr("\n");
	rerr("  -d                 Enable debug output (very verbose)\n");
	rerr("\n");
	rerr("  -l                 Enable linter mode. Extra tests will\n");
	rerr("                     be performed.\n");
	rerr("\n");
	rerr("  -h                 Show help.\n");
	rerr("\n");
	rerr("  tracedir           The output trace dir generated by ovni.\n");
	rerr("\n");
	rerr("The output PRV files are placed in the tracedir directory.\n");
	rerr("\n");
	rerr("Available emulation models:\n");
	models_print();

	exit(EXIT_FAILURE);
}

void
emu_args_init(struct emu_args *args, int argc, char *argv[])
{
	memset(args, 0, sizeof(struct emu_args));

	int opt;
	while ((opt = getopt(argc, argv, "abdc:lh")) != -1) {
		switch (opt) {
			case 'c':
				args->clock_offset_file = optarg;
				break;
			case 'l':
				args->linter_mode = 1;
				break;
			case 'a':
				args->enable_all_models = 1;
				break;
			case 'b':
				args->breakdown = 1;
				break;
			case 'd':
				enable_debug();
				break;
			case 'h':
			default: /* '?' */
				usage();
		}
	}

	if (optind >= argc) {
		err("missing tracedir");
		usage();
	}

	args->tracedir = argv[optind];
	path_remove_trailing(args->tracedir);
}
