# include "pcontrol.h"

int PrintJobStack (int Nstack);
int PrintHostStack (int Nstack);

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

  SetCheckPoint ();
  PrintJobStack (PCONTROL_JOB_PENDING);
  PrintJobStack (PCONTROL_JOB_BUSY);
  PrintJobStack (PCONTROL_JOB_DONE);
  PrintJobStack (PCONTROL_JOB_KILL);
  PrintJobStack (PCONTROL_JOB_EXIT);
  PrintJobStack (PCONTROL_JOB_CRASH);

  PrintHostStack (PCONTROL_HOST_OFF);
  PrintHostStack (PCONTROL_HOST_DOWN);
  PrintHostStack (PCONTROL_HOST_IDLE);
  PrintHostStack (PCONTROL_HOST_BUSY);
  PrintHostStack (PCONTROL_HOST_DONE);
  ClearCheckPoint ();
  return (TRUE);
}

int PrintJobStack (int Nstack) {

  int i, j, Nobject;
  Stack *stack;
  Job *job;

  stack = GetJobStack (Nstack);
  ASSERT (stack != NULL, "programming error");

  LockStack (stack);
  Nobject = stack[0].Nobject;
  gprint (GP_LOG, "job stack %s:  %d objects\n", GetJobStackName(Nstack), Nobject);

  for (i = 0; i < Nobject; i++) {
    job = stack[0].object[i];
    ASSERT (job != NULL, "programming error");
    gprint (GP_LOG, "%d  %s  %d  ", i, job[0].hostname, job[0].argc);
    for (j = 0; j < job[0].argc; j++) {
      gprint (GP_LOG, "%s ", job[0].argv[j]);
    }
    PrintID (GP_LOG, job[0].JobID);
    gprint (GP_LOG, "\n");
  }
  UnlockStack (stack);

  return (TRUE);
}

int PrintHostStack (int Nstack) {

  int i, Nobject;
  Stack *stack;
  Host *host;

  stack = GetHostStack (Nstack);

  LockStack (stack);
  Nobject = stack[0].Nobject;
  gprint (GP_LOG, "host stack %s:  %d objects\n", GetHostStackName(Nstack), Nobject);

  for (i = 0; i < Nobject; i++) {
    host = stack[0].object[i];
    gprint (GP_LOG, "%d  %s  ", i, host[0].hostname);
    PrintID (GP_LOG, host[0].HostID);
    gprint (GP_LOG, "\n");
  }
  UnlockStack (stack);

  return (TRUE);
}

// Safe with PTHREAD_MUTEX_INITIALIZER lock
