/*
 * Authors: Xiangfu Liu <xiangfu.z@gmail.com>
 *
 * 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include "inflash_version.h"
#include "command_line.h"
#include "ingenic_usb.h"
#include "ingenic_cfg.h"

extern struct ingenic_dev ingenic_dev;
extern struct hand hand;

static void help(void)
{
	printf("Usage: inflash [options] ...(must run as root)\n"
	       "  -h --help\t\t\tPrint this help message\n"
	       "  -v --version\t\t\tPrint the version number\n"
	       "  -c --command\t\t\tDirect run the command\n\n"
	       "  <run without options to enter commands via inflash prompt>\n\n"
	       "Report bugs to <xiangfu.z@gmail.com>.\n"
		);
}

static void print_version(void)
{
	printf("inflash version: %s\n", INFLASH_VERSION);
}

static struct option opts[] = {
	{ "help", 0, 0, 'h' },
	{ "version", 0, 0, 'v' },
	{ "command", 1, 0, 'c' },
	{ 0, 0, 0, 0 }
};

int main(int argc, char **argv)
{
	int command = 0;
	char *cptr;
	char com_buf[256] = {0};

	printf("inflash - Ingenic XBurst USB Boot Utility\n"
	       "(c) 2009 Ingenic Semiconductor Inc., Qi Hardware Inc., Xiangfu Liu, Marek Lindner\n"
	       "This program is Free Software and comes with ABSOLUTELY NO WARRANTY.\n\n");

	while(1) {
		int c, option_index = 0;
		c = getopt_long(argc, argv, "hvc:", opts,
				&option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			help();
			exit(EXIT_SUCCESS);
		case 'v':
			print_version();
			exit(EXIT_SUCCESS);
		case 'c':
			command = 1;
			strcpy(com_buf, optarg);
			break;
		default:
			help();
			exit(2);
		}
	}

	if ((getuid()) || (getgid())) {
		fprintf(stderr, "Error - you must be root to run '%s'\n", argv[0]);
		return EXIT_FAILURE;
	}

	if (usb_ingenic_init(&ingenic_dev) < 1)
	 	return EXIT_FAILURE;

	if (parse_configure(&hand, CONFIG_FILE_PATH) < 1)
		return EXIT_FAILURE;

	if (command) {		/* direct run command */
		command_handle(com_buf);
		printf("\n");
		goto out;
	}

	while (1) {
		printf("\ninflash :> ");
		cptr = fgets(com_buf, 256, stdin);
		if (cptr == NULL)
			continue;

		if (command_handle(com_buf) == -1 )
			break;
	}

out:
	usb_ingenic_cleanup(&ingenic_dev);
	return EXIT_SUCCESS;
}
