Index: trunk/ippTools/src/remotetool.c
===================================================================
--- trunk/ippTools/src/remotetool.c	(revision 37054)
+++ trunk/ippTools/src/remotetool.c	(revision 37833)
@@ -21,4 +21,7 @@
 static bool listrunMode(pxConfig *config);
 static bool listcomponentMode(pxConfig *config);
+
+// list remoteRuns with all remoteComponents in state 'prep_done' or 'prep_fail'
+static bool doneprepMode(pxConfig *config);
 
 // Update
@@ -26,6 +29,8 @@
 static bool updaterunMode(pxConfig *config);
 static bool updatepollMode(pxConfig *config);
+static bool updatecomponentMode(pxConfig *config);
 static bool revertrunMode(pxConfig *config);
 static bool revertauthMode(pxConfig *config);
+static bool revertcomponentMode(pxConfig *config);
 
 # define MODECASE(caseName, func) \
@@ -47,12 +52,15 @@
 
   switch (config->mode) {
-    MODECASE(REMOTETOOL_MODE_DEFINEBYQUERY, definebyqueryMode);
-    MODECASE(REMOTETOOL_MODE_LISTRUN,       listrunMode);
-    MODECASE(REMOTETOOL_MODE_LISTCOMPONENT, listcomponentMode);
-    MODECASE(REMOTETOOL_MODE_DROPCOMPONENT, dropcomponentMode);
-    MODECASE(REMOTETOOL_MODE_UPDATERUN,     updaterunMode);
-    MODECASE(REMOTETOOL_MODE_UPDATEPOLL,    updatepollMode);
-    MODECASE(REMOTETOOL_MODE_REVERTRUN,     revertrunMode);
-    MODECASE(REMOTETOOL_MODE_REVERTAUTH,    revertauthMode);
+    MODECASE(REMOTETOOL_MODE_DEFINEBYQUERY,   definebyqueryMode);
+    MODECASE(REMOTETOOL_MODE_LISTRUN,         listrunMode);
+    MODECASE(REMOTETOOL_MODE_DONEPREP,         doneprepMode);
+    MODECASE(REMOTETOOL_MODE_LISTCOMPONENT,   listcomponentMode);
+    MODECASE(REMOTETOOL_MODE_DROPCOMPONENT,   dropcomponentMode);
+    MODECASE(REMOTETOOL_MODE_UPDATERUN,       updaterunMode);
+    MODECASE(REMOTETOOL_MODE_UPDATEPOLL,      updatepollMode);
+    MODECASE(REMOTETOOL_MODE_UPDATECOMPONENT, updatecomponentMode);
+    MODECASE(REMOTETOOL_MODE_REVERTRUN,       revertrunMode);
+    MODECASE(REMOTETOOL_MODE_REVERTAUTH,      revertauthMode);
+    MODECASE(REMOTETOOL_MODE_REVERTCOMPONENT, revertcomponentMode);
 
   default:
@@ -92,5 +100,5 @@
   PXOPT_LOOKUP_BOOL(simple,    config->args, "-simple",       false);
   PXOPT_LOOKUP_BOOL(pretend,   config->args, "-pretend",      false);
-  
+
   set_label = set_label ? set_label : label;
 
@@ -105,5 +113,5 @@
       return(false);
     }
-    
+
     if (label) {
       psStringAppend(&whereOption, "\n AND (label = '%s')", label);
@@ -116,9 +124,9 @@
       return(false);
     }
-    
+
     if (label) {
       psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
-  }    
+  }
   else if (!strcmp(stage,"warp")) {
     query = pxDataGet("remotetool_definebyquery_warp.sql");
@@ -127,5 +135,5 @@
       return(false);
     }
-    
+
     if (label) {
       // Because warp has to check to see if something other than the state is set correctly, I have to use this wonky way to pass the label in.
@@ -133,5 +141,5 @@
       psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
-  }    
+  }
   else if (!strcmp(stage,"stack")) {
     query = pxDataGet("remotetool_definebyquery_stack.sql");
@@ -140,9 +148,20 @@
       return(false);
     }
-    
+
     if (label) {
       psStringAppend(&whereOption, "\n AND (label = '%s')", label);
     }
-  }    
+  }
+  else if (!strcmp(stage,"staticsky")) {
+    query = pxDataGet("remotetool_definebyquery_staticsky.sql");
+    if (!query) {
+      psError(PXTOOLS_ERR_SYS,false, "failed to retreive SQL statement");
+      return(false);
+    }
+
+    if (label) {
+      psStringAppend(&whereOption, "\n AND (label = '%s')", label);
+    }
+  }
   else {
     psError(PS_ERR_UNKNOWN, true, "unknown value for stage: %s", stage);
@@ -191,17 +210,17 @@
   // Insert the top level run object
   remoteRunRow *run = remoteRunRowAlloc(0, // remote_id
-					"new", // state
-					stage,
-					set_label,
-					path_base,
-					-1, // job_id
-					NULL, // last_poll
-					0   // fault
-					  );
+                                        "new", // state
+                                        stage,
+                                        set_label,
+                                        path_base,
+                                        -1, // job_id
+                                        NULL, // last_poll
+                                        0   // fault
+                                          );
   if (!run) {
-    psError(PS_ERR_UNKNOWN, false, "failed to alloc lapRun object");
+    psError(PS_ERR_UNKNOWN, false, "failed to alloc remoteRun object");
     return(true);
   }
-  
+
   if (!remoteRunInsertObject(config->dbh, run)) {
     if (!psDBRollback(config->dbh)) {
@@ -213,5 +232,5 @@
   }
   psS64 remote_id = psDBLastInsertID(config->dbh);
-  
+
   psArray *list = psArrayAllocEmpty(limit);
   for (long i=0; i < psArrayLength(output); i++) {
@@ -219,14 +238,13 @@
     psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id");
 
-    remoteComponentRow *comp = remoteComponentRowAlloc(remote_id,
-						       stage_id);
+    remoteComponentRow *comp = remoteComponentRowAlloc(remote_id, stage_id, 0, "new", NULL);
     if (!comp) {
-      psError(PS_ERR_UNKNOWN, false, "failed to alloc lapRun object");
+      psError(PS_ERR_UNKNOWN, false, "failed to alloc remoteComponent object");
       return(true);
     }
-    
+
     if (!remoteComponentInsertObject(config->dbh, comp)) {
       if (!psDBRollback(config->dbh)) {
-	psError(PS_ERR_UNKNOWN, false, "database error");
+        psError(PS_ERR_UNKNOWN, false, "database error");
       }
       psError(PS_ERR_UNKNOWN, false, "database error");
@@ -237,5 +255,5 @@
     psArrayAdd(list, list->n, comp);
     psFree(comp);
-    
+
   }
   psFree(run);
@@ -245,5 +263,5 @@
     return false;
   }
-  
+
   if (!remoteComponentPrintObjects(stdout, list, !simple)) {
     if (!psDBRollback(config->dbh)) {
@@ -254,8 +272,8 @@
     return false;
   }
-  
+
   psFree(list);
   psFree(output);
-					
+
   return true;
 }
@@ -265,5 +283,5 @@
 {
   psMetadata *where = psMetadataAlloc();
-  
+
   PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
   PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
@@ -274,14 +292,13 @@
   PXOPT_COPY_TIME(config->args, where, "-poll_begin", "last_poll", ">=");
   PXOPT_COPY_TIME(config->args, where, "-poll_end", "last_poll", "<=");
-  
+
   PXOPT_COPY_S16(config->args, where, "-fault", "fault", "==");
 
   PXOPT_LOOKUP_S16(limit,   config->args, "-limit",  false, false);
   PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
 
   psArray *remRuns = NULL;
   remRuns = psDBSelectRows(config->dbh, "remoteRun", where, limit);
-  
+
   if (!ippdbPrintMetadatas(stdout, remRuns, "remoteRun", !simple)) {
     psError(PS_ERR_UNKNOWN, false, "failed to print array");
@@ -299,23 +316,139 @@
 
   PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-stage_id",  "stage_id",  "==");
+  PXOPT_COPY_S64(config->args, where, "-jobs",      "jobs",      "==");
+
+  PXOPT_COPY_STR(config->args, where, "-state", "remoteComponent.state", "==");
+  PXOPT_COPY_STR(config->args, where, "-label", "remoteRun.label", "==");
+
   PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
-  psArray *remComps = NULL;
-  remComps = psDBSelectRows(config->dbh, "remoteComponent", where, 0);
-  
-  if (!ippdbPrintMetadatas(stdout, remComps, "remoteComponent", !simple)) {
+  PXOPT_LOOKUP_S16(limit,   config->args, "-limit",  false, false);
+
+  psString query = pxDataGet("remotetool_listcomponent.sql");
+  if (!query) {
+      psError(PXTOOLS_ERR_SYS,false, "failed to retreive SQL statement");
+      return(false);
+  }
+
+  if (psListLength(where->list)) {
+      psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+      psStringAppend(&query, " WHERE %s", whereClause);
+      psFree(whereClause);
+  }
+  psFree(where);
+
+  if (limit) {
+    psString limitString = psDBGenerateLimitSQL(limit);
+    psStringAppend(&query, " %s", limitString);
+    psFree(limitString);
+  }
+
+  if (!p_psDBRunQuery(config->dbh, query)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+      psFree(query);
+      return false;
+  }
+  psFree(query);
+
+  psArray *output = p_psDBFetchResult(config->dbh);
+  if (!output) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+      return false;
+  }
+  if (!psArrayLength(output)) {
+    psTrace("remotetool", PS_LOG_INFO, "no rows found");
+    psFree(output);
+    return true;
+  }
+
+  if (!ippdbPrintMetadatas(stdout, output, "remoteComponent", !simple)) {
     psError(PS_ERR_UNKNOWN, false, "failed to print array");
-    psFree(remComps);
-    return false;
-  }
-  psFree(remComps);
+    psFree(output);
+    return false;
+  }
+  psFree(output);
 
   return true;
 }
 
+// List
+static bool doneprepMode(pxConfig *config)
+{
+  psMetadata *where = psMetadataAlloc();
+
+  PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "==");
+
+  PXOPT_COPY_STR(config->args, where, "-stage",  "stage",  "==");
+  PXOPT_COPY_STR(config->args, where, "-state", "remoteRun.state", "==");
+  PXOPT_COPY_STR(config->args, where, "-label",  "label",  "==");
+
+  PXOPT_LOOKUP_S16(limit,   config->args, "-limit",  false, false);
+  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+  psString query = pxDataGet("remotetool_doneprep.sql");
+  if (!query) {
+      psError(PXTOOLS_ERR_SYS,false, "failed to retreive SQL statement");
+      return(false);
+  }
+
+  psString whereDone = NULL;
+  psString whereJobs = NULL;
+  if (psListLength(where->list)) {
+      psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+      psStringAppend(&whereDone, " \n AND %s", whereClause);
+      psStringAppend(&whereJobs, " \n WHERE %s", whereClause);
+      psFree(whereClause);
+  }
+
+  if (limit) {
+    psString limitString = psDBGenerateLimitSQL(limit);
+    psStringAppend(&query, " %s", limitString);
+    psFree(limitString);
+  }
+
+  // fprintf (stderr, "query: %s\n", query);
+  // fprintf (stderr, "whereDone: %s\n", whereDone);
+  // fprintf (stderr, "whereJobs: %s\n", whereJobs);
+
+  if (!p_psDBRunQueryF(config->dbh, query, whereDone, whereJobs)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+      psFree(whereDone);
+      psFree(whereJobs);
+      psFree(query);
+      return false;
+  }
+  psFree(whereDone);
+  psFree(whereJobs);
+  psFree(query);
+
+  psArray *output = p_psDBFetchResult(config->dbh);
+  if (!output) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+      return false;
+  }
+  if (!psArrayLength(output)) {
+    psTrace("remotetool", PS_LOG_INFO, "no rows found");
+    psFree(output);
+    return true;
+  }
+
+  if (!ippdbPrintMetadatas(stdout, output, "remoteRun", !simple)) {
+    psError(PS_ERR_UNKNOWN, false, "failed to print array");
+    psFree(output);
+    return false;
+  }
+  psFree(output);
+
+  return true;
+}
+
 // Update
 
 static bool dropcomponentMode(pxConfig *config)
 {
+  fprintf (stderr, "do we really want to use -dropcomponent?\n");
+  exit (1);
+
   // Assert that we have a unique component to operate on.
   PXOPT_LOOKUP_S64(remote_id,config->args, "-remote_id",true, false);
@@ -329,5 +462,5 @@
     return false;
   }
-  
+
   if (!psDBTransaction(config->dbh)) {
     psError(PS_ERR_UNKNOWN, false, "database error");
@@ -341,6 +474,4 @@
   }
   psFree(query);
-  
-  
 
   // point of no return
@@ -349,5 +480,5 @@
     return false;
   }
-  
+
   return true;
 }
@@ -364,5 +495,5 @@
   // Wheres
   PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
-  
+
   // Values to set
   PXOPT_COPY_STR(config->args, values, "-set_label", "label", "==");
@@ -374,9 +505,5 @@
   psFree(values);
 
-  if (rows < 1) {
-    psError(PS_ERR_UNKNOWN,false, "no rows affected");
-    return(false);
-  }
-  
+  if (rows < 1) fprintf (stderr, "no rows changed (run may already be in desired state)\n");
   return(true);
 }
@@ -393,5 +520,5 @@
   psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
   psFree(where);
-  
+
   psString query = NULL;
   query = pxDataGet("remotetool_updatepoll.sql");
@@ -412,7 +539,30 @@
   return(true);
 }
-  
-
-  
+
+static bool updatecomponentMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+  PXOPT_LOOKUP_S64(remote_id,  config->args, "-remote_id",    true, false);
+  psAssert(remote_id,"This should have failed before this point.");
+
+  psMetadata *where = psMetadataAlloc();
+  psMetadata *values = psMetadataAlloc();
+
+  // Wheres
+  PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-stage_id",  "stage_id",  "==");
+  PXOPT_COPY_STR(config->args, where, "-state",     "state",     "==");
+
+  // Values to set
+  PXOPT_COPY_S32(config->args, values, "-set_jobs",      "jobs",      "==");
+  PXOPT_COPY_STR(config->args, values, "-set_path_base", "path_base", "==");
+  PXOPT_COPY_STR(config->args, values, "-set_state", "state", "==");
+
+  long rows = psDBUpdateRows(config->dbh, "remoteComponent", where, values);
+  psFree(values);
+
+  if (rows < 1) fprintf (stderr, "no rows changed (component may already be in desired state)\n");
+  return true;
+}
 
 static bool revertrunMode(pxConfig *config)
@@ -426,33 +576,7 @@
   PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
   PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
-  
+  PXOPT_COPY_S16(config->args, where, "-fault", "fault", "==");
   // Value to set
-  PXOPT_COPY_S16(config->args, values, "-fault", "fault", "==");
-  long rows = psDBUpdateRows(config->dbh, "remoteRun", where, values);
-  psFree(values);
-  psFree(where);
-  if (rows < 1) {
-    psError(PS_ERR_UNKNOWN,false, "no rows affected");
-    return(false);
-  }
-
-  return(true);
-}
-
-static bool revertauthMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-
-  psMetadata *where = psMetadataAlloc();
-  psMetadata *values = psMetadataAlloc();
-
-  // required
-  PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
-
-  psMetadataAddStr(where, PS_LIST_TAIL, "state", 0, "==", "auth"); 
-
-  // Value to set
-  psMetadataAddStr(values, PS_LIST_TAIL, "state", 0, "==", "run");
+  psMetadataAddS16(values,PS_LIST_TAIL,"fault",0, "==", 0);
   
   long rows = psDBUpdateRows(config->dbh, "remoteRun", where, values);
@@ -466,3 +590,58 @@
   return(true);
 }
-    
+
+static bool revertcomponentMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config,false);
+  psMetadata *where = psMetadataAlloc();
+  psMetadata *values = psMetadataAlloc();
+
+  // required
+  PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
+  psMetadataAddStr(where,PS_LIST_TAIL,"state",0, "LIKE", "%fail");
+  
+  // value to set
+  psMetadataAddStr(values, PS_LIST_TAIL, "state",0, "==", "retry");
+  
+
+  long rows = psDBUpdateRows(config->dbh, "remoteComponent", where, values);
+  psFree(values);
+  psFree(where);
+  if (rows < 1) {
+    psError(PS_ERR_UNKNOWN,false, "no rows affected");
+    return(false);
+  }
+
+  return(true);
+}
+  
+  
+
+static bool revertauthMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+
+  psMetadata *where = psMetadataAlloc();
+  psMetadata *values = psMetadataAlloc();
+
+  // required
+  PXOPT_COPY_S64(config->args, where, "-remote_id", "remote_id", "==");
+  PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+
+  psMetadataAddStr(where, PS_LIST_TAIL, "state", 0, "==", "auth");
+
+  // Value to set
+  psMetadataAddStr(values, PS_LIST_TAIL, "state", 0, "==", "run");
+
+  long rows = psDBUpdateRows(config->dbh, "remoteRun", where, values);
+  psFree(values);
+  psFree(where);
+  if (rows < 1) {
+    psError(PS_ERR_UNKNOWN,false, "no rows affected");
+    return(false);
+  }
+
+  return(true);
+}
+
