/******************************************************************************
 *
 *                Copyright (c) 2017-2019 by Löwenware Ltd
 *             Please, refer LICENSE file for legal information
 *
 ******************************************************************************/

/**
 * @file option.c
 * @author Ilja Kartašov <ik@lowenware.com>
 * @brief 
 *
 * @see https://lowenware.com/
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "option.h"

uint32_t g_options = 0;
const char *g_format = NULL;
const char *g_source = NULL;
const char *g_target = NULL;


static void
option_set_command(int command)
{
	if (!(g_options & 0xFF)) {
		g_options |= command;
	}
}


int
option_read_args(int argc, char **argv)
{
	int i;
	char c = 0;

	for (i = 1; i < argc; i++) {
		switch (c) {
		case 'f':
			g_format = argv[i];
			break;

		default:
			if (strcmp(argv[i], "-f") == 0) {
				c = 'f';
			} else if (!strncmp(argv[i], "--format=", 9)) {
				g_format = &argv[i][9];
			} else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
				g_options |= OPTION_FLAG_HELP;
			} else if (!strcmp(argv[i], "compose")) {
				option_set_command(OPTION_COMMAND_COMPOSE);
			} else {
				return i;
			}
			continue;
		}
		c = 0;
	}

	if (c) {
		fprintf(stderr, "No value for -%c argument", c);
		return -1;
	}

	return i - 1;
}

