Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 28941)
+++ trunk/ippTools/share/Makefile.am	(revision 28959)
@@ -153,4 +153,5 @@
 	disttool_definebyquery_fake.sql \
 	disttool_definebyquery_raw.sql \
+	disttool_definebyquery_sky.sql \
 	disttool_definebyquery_stack.sql \
 	disttool_definebyquery_warp.sql \
@@ -166,4 +167,5 @@
 	disttool_pending_fake.sql \
 	disttool_pending_raw.sql \
+	disttool_pending_sky.sql \
 	disttool_pending_stack.sql \
 	disttool_pending_warp.sql \
@@ -328,4 +330,7 @@
 	staticskytool_definebyquery_select.sql \
 	staticskytool_definebyquery_inputs.sql \
+	staticskytool_export_run.sql \
+	staticskytool_export_input.sql \
+	staticskytool_export_result.sql \
 	staticskytool_inputs.sql \
 	staticskytool_todo.sql \
Index: trunk/ippTools/share/disttool_definebyquery_sky.sql
===================================================================
--- trunk/ippTools/share/disttool_definebyquery_sky.sql	(revision 28959)
+++ trunk/ippTools/share/disttool_definebyquery_sky.sql	(revision 28959)
@@ -0,0 +1,27 @@
+SELECT DISTINCT
+    'sky' as stage,
+    staticskyRun.sky_id AS stage_id,
+    CAST(0 AS SIGNED) AS magicked,
+    -- run tag in the form 'sky.$skycell_id.$stack_id'
+    CONCAT_WS('.', 'sky', stackRun.skycell_id, convert(staticskyRun.sky_id, CHAR)) as run_tag,
+    staticskyRun.label,
+    staticskyRun.data_group,
+    distTarget.dist_group,
+    distTarget.target_id,
+    distTarget.clean
+FROM staticskyRun
+JOIN staticskyResult USING(sky_id)
+JOIN staticskyInput USING(sky_id)
+JOIN stackRun using(stack_id)
+JOIN distTarget ON distTarget.stage = 'sky'
+    AND staticskyRun.dist_group = distTarget.dist_group
+JOIN rcInterest USING(target_id)
+LEFT JOIN distRun ON (distRun.stage_id = sky_id)
+    AND distRun.target_id = distTarget.target_id
+    -- JOIN hook %s
+WHERE  distTarget.state = 'enabled'
+    AND rcInterest.state = 'enabled'
+    AND distRun.dist_id IS NULL
+    AND ((staticskyRun.state = 'full') OR (distTarget.clean AND staticskyRun.state = 'cleaned'))
+    -- we shouldn't need to check fault. If faulted it shouldn't be full
+    AND (staticskyResult.fault = 0 AND staticskyResult.quality = 0)
Index: trunk/ippTools/share/disttool_pending_sky.sql
===================================================================
--- trunk/ippTools/share/disttool_pending_sky.sql	(revision 28959)
+++ trunk/ippTools/share/disttool_pending_sky.sql	(revision 28959)
@@ -0,0 +1,50 @@
+SELECT DISTINCT
+    distRun.dist_id,
+    distRun.label,
+    distTarget.dist_group,
+    stage,
+    stage_id,
+    stackRun.skycell_id AS component,
+    clean,
+    rawExp.camera,
+    CONCAT_WS('.', outroot, CONVERT(distRun.dist_id, CHAR)) as outdir,
+    staticskyResult.path_base,
+    CAST(NULL AS CHAR(255)) as chip_path_base,
+    staticskyRun.state,
+    staticskyRun.state AS data_state,
+    staticskyResult.quality,
+    1 AS no_magic,
+    0 AS magicked
+FROM distRun
+JOIN distTarget USING(target_id, stage, clean)
+JOIN staticskyRun
+    ON stage_id = sky_id
+JOIN staticskyResult
+    USING(sky_id)
+JOIN staticskyInput
+    USING(sky_id)
+JOIN stackRun USING(stack_id)
+JOIN stackInputSkyfile USING(stack_id)
+JOIN warpSkyfile
+    ON  stackInputSkyfile.warp_id = warpSkyfile.warp_id
+    AND stackRun.skycell_id       = warpSkyfile.skycell_id
+    AND stackRun.tess_id          = warpSkyfile.tess_id
+JOIN warpRun
+    ON warpRun.warp_id = warpSkyfile.warp_id
+JOIN fakeRun
+    USING(fake_id)
+JOIN camRun
+    USING(cam_id)
+JOIN chipRun
+    ON camRun.chip_id = chipRun.chip_id
+JOIN rawExp 
+     USING (exp_id)
+LEFT JOIN distComponent 
+    ON distRun.dist_id = distComponent.dist_id 
+    AND stackRun.skycell_id = distComponent.component
+WHERE
+    distRun.state = 'new'
+    AND distRun.stage = 'sky'
+    AND distComponent.dist_id IS NULL
+    AND (staticskyRun.state = 'full' OR (distRun.clean AND staticskyRun.state = 'cleaned'))
+    AND (staticskyResult.fault = 0 AND staticskyResult.quality = 0)
Index: trunk/ippTools/share/disttool_toadvance.sql
===================================================================
--- trunk/ippTools/share/disttool_toadvance.sql	(revision 28941)
+++ trunk/ippTools/share/disttool_toadvance.sql	(revision 28959)
@@ -240,4 +240,25 @@
         AND distComponent.fault = 0
 UNION
+-- staticsky stage
+-- NOTE this assumes that there is only one component per staticskyRun
+-- (one skycell)
+SELECT
+    distRun.dist_id,
+    stage,
+    stage_id,
+    outroot,
+    label,
+    clean
+    FROM distRun
+    JOIN staticskyResult on stage_id = sky_id
+    LEFT JOIN distComponent
+        ON distRun.dist_id = distComponent.dist_id
+    WHERE
+        distRun.state = 'new'
+        AND distRun.fault = 0
+        AND distRun.stage = 'sky'
+        AND distComponent.component IS NOT NULL
+        AND distComponent.fault = 0
+UNION
 -- SSdiff stage
 SELECT
Index: trunk/ippTools/share/staticskytool_export_input.sql
===================================================================
--- trunk/ippTools/share/staticskytool_export_input.sql	(revision 28959)
+++ trunk/ippTools/share/staticskytool_export_input.sql	(revision 28959)
@@ -0,0 +1,1 @@
+SELECT staticskyInput.* FROM staticskyInput
Index: trunk/ippTools/share/staticskytool_export_result.sql
===================================================================
--- trunk/ippTools/share/staticskytool_export_result.sql	(revision 28959)
+++ trunk/ippTools/share/staticskytool_export_result.sql	(revision 28959)
@@ -0,0 +1,1 @@
+SELECT staticskyResult.* FROM staticskyResult
Index: trunk/ippTools/share/staticskytool_export_run.sql
===================================================================
--- trunk/ippTools/share/staticskytool_export_run.sql	(revision 28959)
+++ trunk/ippTools/share/staticskytool_export_run.sql	(revision 28959)
@@ -0,0 +1,1 @@
+SELECT staticskyRun.* from staticskyRun
Index: trunk/ippTools/share/staticskytool_result.sql
===================================================================
--- trunk/ippTools/share/staticskytool_result.sql	(revision 28941)
+++ trunk/ippTools/share/staticskytool_result.sql	(revision 28959)
@@ -1,11 +1,12 @@
-SELECT
+SELECT DISTINCT
     staticskyResult.*,
     staticskyRun.state,
-    staticskyRun.tess_id,
-    staticskyRun.skycell_id,
-    staticskyRun.filter,
     staticskyRun.workdir,
     staticskyRun.label,
+    stackRun.tess_id,
+    stackRun.skycell_id,
+    stackRun.filter
 FROM staticskyRun
-JOIN staticskyResult
-USING(stack_id)
+JOIN staticskyResult USING(sky_id)
+JOIN staticskyInput USING(sky_id)
+JOIN stackRun using(stack_id)
Index: trunk/ippTools/src/disttool.c
===================================================================
--- trunk/ippTools/src/disttool.c	(revision 28941)
+++ trunk/ippTools/src/disttool.c	(revision 28959)
@@ -329,4 +329,21 @@
         // stack stage doesn't require magic
         no_magic = true;
+    } else if (!strcmp(stage, "sky")) {
+        magicRunType = "staticskyRun";
+        query = pxDataGet("disttool_definebyquery_sky.sql");
+        if (!query) {
+            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+            psFree(where);
+            return false;
+        }
+
+        if (label) {
+            psStringAppend(&query, " AND (staticskyRun.label = '%s')", label);
+        }
+        if (dist_group) {
+            psStringAppend(&query, " AND (sticskyRun.dist_group = '%s')", dist_group);
+        }
+        // (static)sky stage doesn't require magic
+        no_magic = true;
     } else if (!strcmp(stage, "SSdiff")) {
       magicRunType = "diffRun";
Index: trunk/ippTools/src/staticskytool.c
===================================================================
--- trunk/ippTools/src/staticskytool.c	(revision 28941)
+++ trunk/ippTools/src/staticskytool.c	(revision 28959)
@@ -727,5 +727,4 @@
 bool exportrunMode(pxConfig *config)
 {
-# if (0)
   typedef struct ExportTable {
     char tableName[80];
@@ -737,5 +736,5 @@
   PS_ASSERT_PTR_NON_NULL(config, NULL);
 
-  PXOPT_LOOKUP_S64(det_id, config->args, "-stack_id", true,  false);
+  PXOPT_LOOKUP_S64(det_id, config->args, "-sky_id", true,  false);
   PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
   PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
@@ -753,10 +752,10 @@
   }
   psMetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-stack_id", "stack_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-sky_id", "sky_id", "==");
 
   ExportTable tables [] = {
-    {"stackRun", "staticskytool_export_run.sql"},
-    {"stackInputSkyfile", "staticskytool_export_input_skyfile.sql"},
-    {"stackSumSkyfile", "staticskytool_export_sum_skyfile.sql"},
+    {"staticskyRun", "staticskytool_export_run.sql"},
+    {"staticskyInput", "staticskytool_export_input.sql"},
+    {"staticskyResult", "staticskytool_export_result.sql"},
   };
 
@@ -800,6 +799,6 @@
 
     if (clean) {
-        if (!strcmp(tables[i].tableName, "stackRun")) {
-            if (!pxSetStateCleaned("stackRun", "state", output)) {
+        if (!strcmp(tables[i].tableName, "staticskyRun")) {
+            if (!pxSetStateCleaned("staticskyRun", "state", output)) {
                 psFree(output);
                 psError(PS_ERR_UNKNOWN, false, "pxSetStateClean failed for table %s",  tables[i].tableName);
@@ -821,5 +820,4 @@
     fclose (f);
 
-# endif
     return true;
 }
