# include "pcontrol.h"

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

  int JobID, HostID;

  Stack *stack = NULL;
  Job *job = NULL;
  Host *host = NULL;

  if (argc != 3) {
    gprint (GP_LOG, "USAGE: check job (JobID)\n");
    gprint (GP_LOG, "USAGE: check host (HostID)\n");
    return (FALSE);
  }

  if (!strcasecmp (argv[1], "JOB")) {
    JobID = GetID (argv[2]);
    if (!JobID) {
      gprint (GP_ERR, "invalid job id %s\n", argv[2]);
      return (FALSE);
    }

    stack = GetJobStack (PCONTROL_JOB_ALLJOBS);
    job = PullStackByID (stack, JobID);
    if (job == NULL) {
      gprint (GP_LOG, "job not found\n");
      return (FALSE);
    }

    gprint (GP_LOG, "STATUS %s\n", GetJobStackName(job[0].stack));
    gprint (GP_LOG, "EXITST %d\n", job[0].exit_status);
    gprint (GP_LOG, "STDOUT %d\n", job[0].stdout.size);
    gprint (GP_LOG, "STDERR %d\n", job[0].stderr.size);
    gprint (GP_LOG, "DTIME %lf\n", job[0].dtime);
    if (job[0].realhost) {
	gprint (GP_LOG, "HOSTNAME %s\n", job[0].realhost);
    } else {
	gprint (GP_LOG, "HOSTNAME NONE\n");
    }
    PushStack (stack, STACK_BOTTOM, job, job[0].JobID, job[0].argv[0]);
    return (TRUE);
  }

  if (!strcasecmp (argv[1], "HOST")) {
    HostID = atoi (argv[2]);

    stack = GetHostStack (PCONTROL_HOST_ALLHOSTS);
    host = PullStackByID (stack, HostID);
    if (host == NULL) {
      gprint (GP_LOG, "host not found\n");
      return (FALSE);
    }
    gprint (GP_LOG, "host %s\n", GetHostStackName(host[0].stack));
    PushStack (stack, STACK_BOTTOM, host, host[0].HostID, host[0].hostname);
    return (TRUE);
  }

  gprint (GP_LOG, "unknown item to check\n");
  return (FALSE);
}
