# include "pantasks.h"

int controller_host    	PROTO((int, char **));
int controller_exit    	PROTO((int, char **));
int controller_status  	PROTO((int, char **));
int controller_jobstack PROTO((int, char **));
int controller_run     	PROTO((int, char **));
int controller_stop    	PROTO((int, char **));
int controller_check   	PROTO((int, char **));
int controller_output  	PROTO((int, char **));
int controller_pulse   	PROTO((int, char **));

static Command controller_cmds[] = {
  {"exit",     controller_exit,     "shutdown controller"},
  {"host",     controller_host,     "define host for controller"},
  {"check",    controller_check,    "check controller host/job"},
  {"run",      controller_run,      "start controller operation / set run levels"},
  {"stop",     controller_run,      "stop controller (no disconnect)"},
  {"status",   controller_status,   "check controller status"},
  {"jobstack", controller_jobstack, "check controller status"},
  {"output",   controller_output,   "print controller output"},
  {"pulse",    controller_pulse,    "set controller pulse"},
};

int controller (int argc, char **argv) {

  int i, N, status;
  CommandF *func;

  if (argc < 2) {
    gprint (GP_ERR, "USAGE: controller (command) ... \n");
    return (FALSE);
  }

  if (!strcasecmp (argv[1], "help")) {
    N = sizeof (controller_cmds) / sizeof (Command);

    for (i = 0; i < N; i++) {
      gprint (GP_LOG, "%-15s %s\n", controller_cmds[i].name, controller_cmds[i].help);
    }
    return (TRUE);
  }

  func = FindControllerCommand (argv[1]);
  if (func == NULL) {
    gprint (GP_ERR, "invalid controller command\n");
    return (FALSE);
  }

  status = (*func)(argc - 1, argv + 1);
  return (status);
}

CommandF *FindControllerCommand (char *cmd) {

  int i, N;

  N = sizeof (controller_cmds) / sizeof (Command);

  for (i = 0; i < N; i++) {
    if (!strcmp (controller_cmds[i].name, cmd)) {
      return (controller_cmds[i].func);
    }
  }
  return (NULL);
}
