/* $Id$ */

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include "throw.h"
#include "cmds.h"

static void Main(int ac, char * const av[])
{
	struct
	{
		const char *cmd;
		void (*hnd) (int ac, char * const av[]);
	} static const cmds[] = 
	{
		{ "test", CmdTest },
		{ "get", CmdGet },
		{ "put", CmdPut },
		{ "export", CmdExport },
		{ "import", CmdImport },
		{ NULL, NULL }
	}, *cmdp;

	if (ac < 2)
	{
		printf("Usage: wouxun <command> [command parameters]\n");

		printf("Valid commands:");

		for (cmdp = cmds; cmdp->cmd; ++cmdp)
			printf(" %s", cmdp->cmd);

		printf("\n");

		return;
	}

	for (cmdp = cmds; cmdp->cmd; ++cmdp)
		if (!strcmp(av[1], cmdp->cmd))
			break;

	if (!cmdp->cmd)
		Throw("Unknown command: %s", av[1]);

	cmdp->hnd(ac - 1, av + 1);
}

int main(int ac, char * const av[])
{
	try
	{
		Main(ac, av);
	}
	catch (const std::string &s)
	{
		fprintf(stderr, "wouxun: %s\n", s.c_str());
		return EXIT_FAILURE;
	};

	return EXIT_SUCCESS;
}
