# include "pcontrol.h"

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

  Job *job;
  Host *host;
  int JobID, HostID, StackID;

  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 = atoi (argv[2]);

    SetCheckPoint ();  // ensure the JOB is on one of the stacks
    job = PullJobByID (JobID, &StackID);
    if (job == NULL) {
      gprint (GP_LOG, "job not found\n");
      ClearCheckPoint ();
      return (FALSE);
    }
    gprint (GP_LOG, "STATUS %s\n", GetJobStackName(StackID));
    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");
    }
    PutJob (job, StackID, STACK_BOTTOM);
    ClearCheckPoint ();
    return (TRUE);
  }

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

    SetCheckPoint ();  // ensure the HOST is on one of the stacks
    host = PullHostByID (HostID, &StackID);
    if (host == NULL) {
      gprint (GP_LOG, "host not found\n");
      ClearCheckPoint ();
      return (FALSE);
    }
    gprint (GP_LOG, "host %s\n", GetHostStackName(StackID));
    PutHost (host, StackID, STACK_BOTTOM);
    ClearCheckPoint ();
    return (TRUE);
  }

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