Index: trunk/ippTools/share/disttool_definebyquery_select.sql
===================================================================
--- trunk/ippTools/share/disttool_definebyquery_select.sql	(revision 23547)
+++ trunk/ippTools/share/disttool_definebyquery_select.sql	(revision 23566)
@@ -20,9 +20,8 @@
     WHERE distTarget.state = 'enabled'
         AND rcInterest.state = 'enabled'
-        AND chipRun.state = 'full'       -- XXX: we need a completed chip stage magicDSRun because we
+        AND chipRun.state = 'full'       -- Note: we need a completed chip stage magicDSRun because we
                                          -- need the mask file to NAN masked pixels
         -- to support cameras that don't need magic disttool adds the magicked restriction 
         -- \nAND rawExp.magicked AND chipRun.magicked here:
-        AND rawExp.magicked AND chipRun.magicked
         -- raw magicked HOOK %s
         AND distRun.dist_id IS NULL      -- no existing distRun 
@@ -46,5 +45,5 @@
             -- If camera requires files to be magicked before distibution
             -- the following gets added by disttool
-            -- AND chipRun.magicked 
+            AND chipRun.magicked 
             -- chip magicked HOOK %s
            ) 
Index: trunk/ippTools/share/disttool_pendingcomponent.sql
===================================================================
--- trunk/ippTools/share/disttool_pendingcomponent.sql	(revision 23547)
+++ trunk/ippTools/share/disttool_pendingcomponent.sql	(revision 23566)
@@ -2,4 +2,5 @@
 SELECT
     distRun.dist_id,
+    distRun.label,
     stage,
     stage_id,
@@ -8,5 +9,5 @@
     rawExp.camera,
     outroot,
-    rawImfile.uri as path_base,
+    rawImfile.uri as path_base,         -- change this once rawImfile has path_base
     chipProcessedImfile.path_base as chip_path_base,
     NULL as state,
@@ -15,8 +16,20 @@
 FROM distRun
 JOIN rawExp ON exp_id = stage_id
-JOIN rawImfile using(exp_id)
+JOIN (                      -- find the last magicked chip run 
+    SELECT
+        exp_id,
+        MAX(chip_id) AS chip_id
+    FROM chipRun
+    WHERE 
+        chipRun.state = 'full'
+        AND chipRun.exp_id = exp_id
+        --   AND chipRun.magicked
+        -- magicked hook 1 %s
+        GROUP BY exp_id
+    ) AS bestChipRun 
+    USING(exp_id)
+JOIN rawImfile USING(exp_id)
 JOIN chipProcessedImfile
-    ON distRun.chip_id = chipProcessedImfile.chip_id
-    AND rawImfile.class_id = chipProcessedImfile.class_id
+    USING(exp_id, chip_id, class_id)
 LEFT JOIN distComponent 
     ON distRun.dist_id = distComponent.dist_id 
@@ -26,4 +39,6 @@
     AND distRun.stage = 'raw'
     AND distComponent.dist_id IS NULL
+    -- if magicked add AND rawImfile.magicked here
+    -- where hook 1 %s
 
 -- chip stage
@@ -31,4 +46,5 @@
 SELECT
     distRun.dist_id,
+    distRun.label,
     stage,
     stage_id,
@@ -53,7 +69,67 @@
     AND distRun.stage = 'chip'
     AND distComponent.dist_id IS NULL
-UNION
-SELECT
-    distRun.dist_id,
+    -- where hook 2 %s
+UNION
+SELECT
+    distRun.dist_id,
+    distRun.label,
+    stage,
+    stage_id,
+    chipProcessedImfile.class_id AS component,
+    clean,
+    rawExp.camera,
+    outroot,
+    camProcessedExp.path_base,
+    chipProcessedImfile.path_base as chip_path_base,
+    camRun.state,
+    NULL,
+    0
+FROM distRun
+JOIN camRun ON camRun.cam_id = distRun.stage_id
+JOIN camProcessedExp USING(cam_id)
+JOIN chipRun USING(chip_id)
+JOIN chipProcessedImfile USING(exp_id, chip_id)
+JOIN rawExp using(exp_id)
+LEFT JOIN distComponent 
+    ON distRun.dist_id = distComponent.dist_id 
+    AND chipProcessedImfile.class_id = distComponent.component
+WHERE
+    distRun.state = 'new'
+    AND distRun.stage = 'camera'
+    AND distComponent.dist_id IS NULL
+    -- where hook 3 %s
+UNION
+SELECT
+    distRun.dist_id,
+    distRun.label,
+    stage,
+    stage_id,
+    fakeProcessedImfile.class_id AS component,
+    clean,
+    rawExp.camera,
+    outroot,
+    fakeProcessedImfile.path_base,
+    NULL,
+    fakeRun.state,
+    NULL,
+    0
+FROM distRun
+JOIN fakeRun ON fakeRun.fake_id = distRun.stage_id
+JOIN fakeProcessedImfile USING(fake_id)
+JOIN camRun USING(cam_id)
+JOIN chipRun USING(chip_id, exp_id)
+JOIN rawExp using(exp_id)
+LEFT JOIN distComponent 
+    ON distRun.dist_id = distComponent.dist_id 
+    AND fakeProcessedImfile.class_id = distComponent.component
+WHERE
+    distRun.state = 'new'
+    AND distRun.stage = 'fake'
+    AND distComponent.dist_id IS NULL
+    -- where hook 4 %s
+UNION
+SELECT
+    distRun.dist_id,
+    distRun.label,
     stage,
     stage_id,
@@ -81,7 +157,9 @@
     AND distRun.stage = 'warp'
     AND distComponent.dist_id IS NULL
-UNION
-SELECT
-    distRun.dist_id,
+    -- where hook 5 %s
+UNION
+SELECT
+    distRun.dist_id,
+    distRun.label,
     stage,
     stage_id,
@@ -108,7 +186,9 @@
     AND distRun.stage = 'diff'
     AND distComponent.dist_id IS NULL
+    -- where hook 6 %s
 UNION
 SELECT DISTINCT
     distRun.dist_id,
+    distRun.label,
     stage,
     stage_id,
@@ -152,3 +232,4 @@
     AND distRun.stage = 'stack'
     AND distComponent.dist_id IS NULL
+    -- where hook 7 %s
 ) as Foo
Index: trunk/ippTools/share/pxadmin_create_tables.sql
===================================================================
--- trunk/ippTools/share/pxadmin_create_tables.sql	(revision 23547)
+++ trunk/ippTools/share/pxadmin_create_tables.sql	(revision 23566)
@@ -1249,5 +1249,4 @@
     stage       VARCHAR(64),
     stage_id    BIGINT,
-    chip_id     BIGINT,
     label       VARCHAR(64),
     outroot     VARCHAR(255),
