Index: /trunk/ippTools/src/difftool.c
===================================================================
--- /trunk/ippTools/src/difftool.c	(revision 11810)
+++ /trunk/ippTools/src/difftool.c	(revision 11811)
@@ -131,4 +131,7 @@
     }
 
+    // get the assigned p5_id
+    run->p5_id = psDBLastInsertID(config->dbh);
+
     bool simple = false;
     {
@@ -297,14 +300,15 @@
     psString query = psStringCopy( 
         "SELECT\n"
-        "   p4Scfile.*\n"
+        "   p4Scfile.*,\n"
+        "   p5InputScfile.template\n"
         " FROM p5Run\n"
         " JOIN p5InputScfile\n"
         "   USING(p5_id)\n"
         " JOIN p4Scfile\n"
-        "   ON p5InputScile.p4_id      = p4Scfile.p4_id\n"
-        "   ON p5InputScile.skycell_id = p4Scfile.skycell_id\n"
-        "   ON p5InputScile.tess_id    = p4Scfile.tess_id\n"
-        "   ON p5InputScile.exp_tag    = p4Scfile.exp_tag\n"
-        "   ON p5InputScile.p3_version = p4Scfile.p3_versiong\n"
+        "   ON p5InputScfile.p4_id      = p4Scfile.p4_id\n"
+        "  AND p5InputScfile.skycell_id = p4Scfile.skycell_id\n"
+        "  AND p5InputScfile.tess_id    = p4Scfile.tess_id\n"
+        "  AND p5InputScfile.exp_tag    = p4Scfile.exp_tag\n"
+        "  AND p5InputScfile.p3_version = p4Scfile.p3_version\n"
         " WHERE\n"
         "   p5Run.state = 'run'\n" 
@@ -389,24 +393,24 @@
     // find all rawImfiles matching the default query
     psString query = psStringCopy( 
-        "SELECT\n"
-        "   p4Scfile.*\n"
+        "SELECT DISTINCT\n"
+        "   p5Run.p5_id\n"
         " FROM p5Run\n"
         " JOIN p5InputScfile\n"
         "   USING(p5_id)\n"
         " JOIN p4Scfile\n"
-        "   ON p5InputScile.p4_id      = p4Scfile.p4_id\n"
-        "   ON p5InputScile.skycell_id = p4Scfile.skycell_id\n"
-        "   ON p5InputScile.tess_id    = p4Scfile.tess_id\n"
-        "   ON p5InputScile.exp_tag    = p4Scfile.exp_tag\n"
-        "   ON p5InputScile.p3_version = p4Scfile.p3_versiong\n"
+        "   ON p5InputScfile.p4_id      = p4Scfile.p4_id\n"
+        "  AND p5InputScfile.skycell_id = p4Scfile.skycell_id\n"
+        "  AND p5InputScfile.tess_id    = p4Scfile.tess_id\n"
+        "  AND p5InputScfile.exp_tag    = p4Scfile.exp_tag\n"
+        "  AND p5InputScfile.p3_version = p4Scfile.p3_version\n"
         " LEFT JOIN p5DiffScfile\n"
-        "   USING(p5_id, skycell_id, tess_id, exp_tag, p3_version)\n"
+        "   ON p5InputScfile.p5_id      = p5DiffScfile.p5_id\n"
+        "  AND p5InputScfile.skycell_id = p5DiffScfile.skycell_id\n"
+        "  AND p5InputScfile.tess_id    = p5DiffScfile.tess_id\n"
         " WHERE\n"
         "   p5Run.state = 'run'\n" 
-        "   AND p4DiffScfile.p5_id IS NULL\n"
-        "   AND p4DiffScfile.skycell_id IS NULL\n"
-        "   AND p4DiffScfile.tess_id IS NULL\n"
-        "   AND p4DiffScfile.exp_tag IS NULL\n"
-        "   AND p4DiffScfile.p3_version IS NULL\n"
+        "   AND p5DiffScfile.p5_id IS NULL\n"
+        "   AND p5DiffScfile.skycell_id IS NULL\n"
+        "   AND p5DiffScfile.tess_id IS NULL\n"
     );
 
@@ -511,14 +515,4 @@
     }
 
-    psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_tag");
-        return false;
-    }
-    if (!exp_tag) {
-        psError(PS_ERR_UNKNOWN, true, "-exp_tag is required");
-        return false;
-    }
-
     psString uri = psMetadataLookupStr(&status, config->args, "-uri");
     if (!status) {
@@ -531,11 +525,4 @@
     }
 
-    // defaults to 0
-    psS32 p3_version = psMetadataLookupS32(&status, config->args, "-p3_version");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p3_version");
-        return false;
-    }
-
     // optional
     psF64 bg = psMetadataLookupF64(&status, config->args, "-bg");
@@ -552,11 +539,8 @@
 
 
-    // XXX need to validate that this coresponds to an p4InputImfile
     if (!p5DiffScfileInsert(config->dbh, 
             (psS32)atoi(p5_id),
             skycell_id,
             tess_id,
-            exp_tag,
-            p3_version,
             uri,
             bg,
@@ -587,5 +571,5 @@
         "   p5DiffScfile.*\n"
         " FROM p5Run\n"
-        " JOIN p5Scfile\n"
+        " JOIN p5DiffScfile\n"
         "   USING(p5_id)\n"
         " WHERE\n"
@@ -657,7 +641,7 @@
 }
 
-static bool setp5RunState(pxConfig *config, const char *p4_id, const char *state)
-{
-    PS_ASSERT_PTR_NON_NULL(p4_id, false);
+static bool setp5RunState(pxConfig *config, const char *p5_id, const char *state)
+{
+    PS_ASSERT_PTR_NON_NULL(p5_id, false);
     PS_ASSERT_PTR_NON_NULL(state, false);
 
@@ -670,15 +654,15 @@
     ) {
         psError(PS_ERR_UNKNOWN, false,
-                "invalid p4Run state: %s", state);
-        return false;
-    }
-
-    char *query = "UPDATE p4Run SET state = '%s' WHERE p4_id = '%s'";
-    if (!p_psDBRunQuery(config->dbh, query, state, p4_id)) {
+                "invalid p5Run state: %s", state);
+        return false;
+    }
+
+    char *query = "UPDATE p5Run SET state = '%s' WHERE p5_id = '%s'";
+    if (!p_psDBRunQuery(config->dbh, query, state, p5_id)) {
         psError(PS_ERR_UNKNOWN, false,
-                "failed to change state for p4_id %s", p4_id);
-        return false;
-    }
-
-    return true;
-}
+                "failed to change state for p5_id %s", p5_id);
+        return false;
+    }
+
+    return true;
+}
Index: /trunk/ippTools/src/difftoolConfig.c
===================================================================
--- /trunk/ippTools/src/difftoolConfig.c	(revision 11810)
+++ /trunk/ippTools/src/difftoolConfig.c	(revision 11811)
@@ -127,8 +127,4 @@
     psMetadataAddStr(adddiffscfileArgs, PS_LIST_TAIL, "-tess_id",  0,
             "define tessellation ID (required)", NULL);
-    psMetadataAddStr(adddiffscfileArgs, PS_LIST_TAIL, "-exp_tag", 0,
-            "define exposure tag (required)", NULL);
-    psMetadataAddS32(adddiffscfileArgs, PS_LIST_TAIL, "-p3_version", 0,
-            "define phase 3 version of exposure tag (required)", 0);
     psMetadataAddStr(adddiffscfileArgs, PS_LIST_TAIL, "-uri", 0,
             "define URI of file (required)", 0);
@@ -146,8 +142,4 @@
     psMetadataAddStr(diffscfileArgs, PS_LIST_TAIL, "-tess_id",  0,
             "define tessellation ID (required)", NULL);
-    psMetadataAddStr(diffscfileArgs, PS_LIST_TAIL, "-exp_tag", 0,
-            "define exposure tag (required)", NULL);
-    psMetadataAddS32(diffscfileArgs, PS_LIST_TAIL, "-p3_version", 0,
-            "define phase 3 version of exposure tag (required)", 0);
     psMetadataAddU64(diffscfileArgs, PS_LIST_TAIL, "-limit",  0,
             "limit result set to N items", 0);
@@ -249,4 +241,14 @@
     psString str = NULL;
     bool status = false;
+    if ((str = psMetadataLookupStr(&status, config->args, "-p5_id"))) {
+        if (!psMetadataAddS32(config->where, PS_LIST_TAIL, "p5_id", 0, "==", (psS32)atoi(str))) {
+            psError(PS_ERR_UNKNOWN, false, "failed to add item p5_id");
+            goto FAIL; 
+        } 
+    } 
+}
+{
+    psString str = NULL;
+    bool status = false;
     if ((str = psMetadataLookupStr(&status, config->args, "-p4_id"))) {
         if (!psMetadataAddS32(config->where, PS_LIST_TAIL, "p4_id", 0, "==", (psS32)atoi(str))) {
