Index: trunk/ippTools/src/disttool.c
===================================================================
--- trunk/ippTools/src/disttool.c	(revision 25567)
+++ trunk/ippTools/src/disttool.c	(revision 25835)
@@ -32,5 +32,4 @@
 
 static bool definebyqueryMode(pxConfig *config);
-static bool definerunMode(pxConfig *config);
 static bool updaterunMode(pxConfig *config);
 static bool revertrunMode(pxConfig *config);
@@ -79,5 +78,4 @@
     switch (config->mode) {
         MODECASE(DISTTOOL_MODE_DEFINEBYQUERY, definebyqueryMode);
-        MODECASE(DISTTOOL_MODE_DEFINERUN, definerunMode);
         MODECASE(DISTTOOL_MODE_UPDATERUN, updaterunMode);
         MODECASE(DISTTOOL_MODE_REVERTRUN, revertrunMode);
@@ -123,46 +121,4 @@
 }
 
-static bool definerunMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
-    PXOPT_LOOKUP_S64(stage_id, config->args, "-stage_id",  true, false);
-    PXOPT_LOOKUP_STR(outroot, config->args, "-outroot", true, false);
-    PXOPT_LOOKUP_S64(target_id, config->args, "-target_id",  true, false);
-
-    // optional
-    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
-    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
-    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
-
-    // TODO: check that stage has an expected value
-    // XXX: all of the following concerns will be managed properly by definebyquery
-
-    // TODO: should we check that stage_id actually exists for stage
-    // in magicdstool we queue off of a magic_id so the stage_id, exp_id, and cam_id get looked up
-    // when the run is queued
-
-    if (!distRunInsert(config->dbh,
-            0,          // dist_id
-            target_id,
-            stage,
-            stage_id,
-            0,
-            set_label,
-            outroot,
-            clean,
-            no_magic,
-            "new",
-            NULL,       // time_stamp
-            0           // fault
-            )) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
-}
 
 static bool definebyqueryMode(pxConfig *config)
@@ -178,4 +134,6 @@
     PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
     PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
+    PXOPT_LOOKUP_STR(set_note, config->args, "-set_note", false, false);
+
     PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
@@ -191,4 +149,5 @@
 
     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
+    PXOPT_LOOKUP_STR(dist_group, config->args, "-dist_group", false, false);
 
     psString query = NULL;
@@ -209,4 +168,7 @@
             psStringAppend(&query, " AND (chipRun.label = '%s')", label);
         }
+        if (dist_group) {
+            psStringAppend(&query, " AND (chipRun.dist_group = '%s')", dist_group);
+        }
     } else if (!strcmp(stage, "chip")) {
         magicRunType = "chipRun";
@@ -222,4 +184,7 @@
             psStringAppend(&query, " AND (chipRun.label = '%s')", label);
         }
+        if (dist_group) {
+            psStringAppend(&query, " AND (chipRun.dist_group = '%s')", dist_group);
+        }
     } else if (!strcmp(stage, "camera")) {
         magicRunType = "camRun";    // This is used below to set the magicked business
@@ -235,4 +200,7 @@
             psStringAppend(&query, " AND (camRun.label = '%s')", label);
         }
+        if (dist_group) {
+            psStringAppend(&query, " AND (camRun.dist_group = '%s')", dist_group);
+        }
     } else if (!strcmp(stage, "fake")) {
         magicRunType = "fakeRun";
@@ -246,4 +214,7 @@
         if (label) {
             psStringAppend(&query, " AND (fakeRun.label = '%s')", label);
+        }
+        if (dist_group) {
+            psStringAppend(&query, " AND (fakeRun.dist_group = '%s')", dist_group);
         }
         // fake stage doesn't require magic
@@ -262,4 +233,7 @@
             psStringAppend(&query, " AND (warpRun.label = '%s')", label);
         }
+        if (dist_group) {
+            psStringAppend(&query, " AND (warpRun.dist_group = '%s')", dist_group);
+        }
 
     } else if (!strcmp(stage, "diff")) {
@@ -276,4 +250,7 @@
             psStringAppend(&query, " AND (diffRun.label = '%s')", label);
         }
+        if (dist_group) {
+            psStringAppend(&query, " AND (diffRun.dist_group = '%s')", dist_group);
+        }
 
     } else if (!strcmp(stage, "stack")) {
@@ -288,4 +265,7 @@
         if (label) {
             psStringAppend(&query, " AND (stackRun.label = '%s')", label);
+        }
+        if (dist_group) {
+            psStringAppend(&query, " AND (stackRun.dist_group = '%s')", dist_group);
         }
         // stack stage doesn't require magic
@@ -371,5 +351,5 @@
         psS64 magic_ds_id = psMetadataLookupS64(NULL, md, "magicked");
         psS64 target_id = psMetadataLookupS64(NULL, md, "target_id");
-        psString target_label = psMetadataLookupStr(NULL, md, "label");
+        psString stage_label = psMetadataLookupStr(NULL, md, "label");
         bool clean = psMetadataLookupBool(NULL, md, "clean");
 
@@ -381,5 +361,5 @@
             new_label = set_label;
         } else {
-            new_label = target_label;
+            new_label = stage_label;
         }
         distRunRow *row = distRunRowAlloc(
@@ -395,5 +375,6 @@
                 "new",
                 NULL,   // time_stamp
-                0       // fault
+                0,       // fault
+                set_note    // note does not propagate
                 );
 
@@ -1239,5 +1220,5 @@
 
     // required
-    PXOPT_LOOKUP_STR(label, config->args, "-label", true, false);
+    PXOPT_LOOKUP_STR(dist_group, config->args, "-dist_group", true, false);
     PXOPT_LOOKUP_STR(filter, config->args, "-filter", true, false);
     PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
@@ -1250,5 +1231,5 @@
     distTargetRow *row = distTargetRowAlloc(
             0,          // target_id
-            label,
+            dist_group,
             filter,
             stage,
@@ -1287,5 +1268,5 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "==");
     PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
@@ -1323,5 +1304,5 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
@@ -1518,5 +1499,5 @@
     PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", false, false);
     PXOPT_LOOKUP_STR(stage, config->args,        "-stage", false, false);
-    PXOPT_LOOKUP_STR(label, config->args,        "-label", false, false);
+    PXOPT_LOOKUP_STR(dist_group, config->args,   "-dist_group", false, false);
     PXOPT_LOOKUP_STR(filter, config->args,       "-filter", false, false);
     PXOPT_LOOKUP_BOOL(clean, config->args,       "-clean", false);
@@ -1528,6 +1509,6 @@
             error = true;
         }
-        if (!label) {
-            psError(PS_ERR_UNKNOWN, !error, "label is required if target_id is not supplied");
+        if (!dist_group) {
+            psError(PS_ERR_UNKNOWN, !error, "dist_group is required if target_id is not supplied");
             error = true;
         }
@@ -1539,5 +1520,5 @@
     // optional
     PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
+    PXOPT_LOOKUP_STR(state, config->args,  "-set_state", false, false);
     if (state) {
         if (strcmp(state, "enabled") && strcmp(state, "disabled")) {
@@ -1555,5 +1536,5 @@
     PXOPT_COPY_STR(config->args, where, "-dest_name", "rcDestination.name", "==");
     PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "LIKE");
+    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
@@ -1658,5 +1639,5 @@
     PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "LIKE");
+    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
