/* The commands multiplexer. */
/* $Id: cmds.c,v 1.10 2003/07/31 09:27:17 pasky Exp $ */

/*
 * PaVS - Pasky's Version System, advanced version control system
 * Copyright (C) 2003  Petr Baudis
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <stdio.h>
#include <string.h>

#include "checkout.h"
#include "cmds.h"
#include "diff.h"
#include "error.h"
#include "log.h"


void pa_help_common (char *cmdname);
void pa_help_help (char *cmdname);

struct pa_cmd_rec {
	char *name;
	pa_cmd_t handler;
	pa_cmd_help_t help_handler;
	char *desc;
};

static struct pa_cmd_rec pa_cmds[] = {
	{ "checkout",	pa_checkout,	pa_checkout_help,
	  "Check out the data from repository" },

	{ "co",		pa_checkout,	pa_checkout_help,
	  NULL },

	{ "common-options",	NULL,	pa_help_common,
	  "General PaVS usage - options common for all commands" },

	{ "diff",	pa_diff,	NULL,
	  NULL },

	{ "help",	pa_help,	pa_help_help,
	  "Show help information" },

	{ "log",	pa_log,		pa_log_help,
	  "Display history information of file" },

	{ NULL,		NULL,		NULL,
	  NULL }
};

static struct pa_cmd_rec *
pa_cmd_lookup (char *cmdname)
{
	struct pa_cmd_rec *cmd;

	for (cmd = pa_cmds; cmd->name; cmd++)
		if (! strcmp (cmdname, cmd->name))
			return cmd;

	return NULL;
}

pa_cmd_t
pa_cmd_handler (char *cmdname)
{
	struct pa_cmd_rec *cmd = pa_cmd_lookup (cmdname);

	return cmd ? cmd->handler : NULL;
}

pa_cmd_help_t
pa_cmd_help_handler (char *cmdname)
{
	struct pa_cmd_rec *cmd = pa_cmd_lookup (cmdname);

	return cmd ? cmd->help_handler : NULL;
}

enum pa_errcode
pa_help (struct args *args)
{
	struct pa_cmd_rec *cmd;
	char *cmdname = NULL;

	args_shift (args); /* pop 'help' */

	/* Handle arguments */

	while (args->argc > 0) {
		pa_cmd_help_t cmdhelp;

		cmdname = args_shift (args);
		cmdhelp = pa_cmd_help_handler (cmdname);
		if (cmdhelp)
			cmdhelp (cmdname);
		else
			return PEARGINVAL;
	}

	if (cmdname) return 0;

	/* No arguments branch */

	printf ("PaVS - advanced version control tool\n");
	printf ("Usage: pa [COMMON_OPTIONS] COMMAND [COMMAND_OPTIONS] ...\n");
	printf ("Use pa help COMMAND to get more informations about specific command\n");
	printf ("usage, or pa help help to get more informations about this command.\n");
	printf ("\n");
	printf ("PaVS commands are:\n");
	for (cmd = pa_cmds; cmd->name; cmd++) {
		if (! cmd->desc || ! cmd->handler) continue;
		printf ("\t%-12s %s\n", cmd->name, cmd->desc);
	}
#if 0
	printf ("(See 'help synonyms' for list of command aliases.)\n");
#endif
	printf ("(See 'help common-options' for more usage hints.)\n");
	return 0;
}

void
pa_help_common (char *cmdname)
{
	printf ("PaVS - advanced version control tool\n");
	printf ("Usage: pa [COMMON_OPTIONS] COMMAND [COMMAND_OPTIONS] ...\n");
	printf ("See 'pa help' for list of available COMMANDs. You can find specific commands\n");
	printf ("usage and COMMAND_OPTIONS semantics in their help, use 'pa help COMMAND'.\n");
	printf ("\n");
	printf ("COMMON_OPTIONS are general options common for all COMMANDs. They generally\n");
	printf ("affect or further specify the way PaVS operates. Please note that these\n");
	printf ("options are ALWAYS in front of COMMAND while command options are always\n");
	printf ("specified after the COMMAND on the commandline.\n");
	printf ("Available common options are:\n");
	printf ("    There are no common options available so far.\n");
}

void
pa_help_help (char *cmdname)
{
	struct pa_cmd_rec *cmd;

	printf ("Usage: pa help TOPIC ...\n");
	printf ("Prints detailed informations about the specified topic(s).\n");
	printf ("\n");
	printf ("TOPIC can be any valid command or command alias - the given topic contains\n");
	printf ("detailed informations about command operation and usage. There are also few\n");
	printf ("general topics. Valid TOPICs (command aliases excluded) are:\n");
	for (cmd = pa_cmds; cmd->name; cmd++) {
		if (! cmd->desc || ! cmd->help_handler) continue;
		printf ("\t%-12s %s\n", cmd->name, cmd->desc);
	}
}
