Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 24487)
+++ trunk/ippTools/share/Makefile.am	(revision 24512)
@@ -168,4 +168,7 @@
      pxadmin_create_mirror_tables.sql \
      pxadmin_drop_tables.sql \
+     pubtool_definerun.sql \
+     pubtool_pending.sql \
+     pubtool_revert.sql \
      pztool_find_completed_exp.sql \
      pztool_pendingimfile.sql \
Index: trunk/ippTools/share/camtool_find_processedexp.sql
===================================================================
--- trunk/ippTools/share/camtool_find_processedexp.sql	(revision 24487)
+++ trunk/ippTools/share/camtool_find_processedexp.sql	(revision 24512)
@@ -5,4 +5,5 @@
     rawExp.exp_tag,
     rawExp.exp_name,
+    rawExp.exp_time,
     rawExp.camera,
     rawExp.telescope,
Index: trunk/ippTools/share/difftool_skyfile.sql
===================================================================
--- trunk/ippTools/share/difftool_skyfile.sql	(revision 24487)
+++ trunk/ippTools/share/difftool_skyfile.sql	(revision 24512)
@@ -5,4 +5,11 @@
     diffRun.state,
     diffRun.workdir,
+    diffRun.bothways,
+    camProcessedExp.zpt_obs,
+    camProcessedExp.zpt_stdev,
+    camProcessedExp.zpt_lq,
+    camProcessedExp.zpt_uq,
+    rawExp.exp_time,
+    rawExp.camera,
     warp1,
     stack1,
@@ -13,7 +20,11 @@
 JOIN diffInputSkyfile USING(diff_id, skycell_id)
 JOIN warpRun
+-- NOTE: joining input only!
+-- This is so that we can get the correct zero point
+-- XXX This needs to be more clever to handle diffs between stacks
     ON warpRun.warp_id = diffInputSkyfile.warp1
 JOIN fakeRun USING(fake_id)
 JOIN camRun USING(cam_id)
+JOIN camProcessedExp USING(cam_id)
 JOIN chipRun USING(chip_id)
 JOIN rawExp USING(exp_id)
Index: trunk/ippTools/share/pubtool_definerun.sql
===================================================================
--- trunk/ippTools/share/pubtool_definerun.sql	(revision 24512)
+++ trunk/ippTools/share/pubtool_definerun.sql	(revision 24512)
@@ -0,0 +1,28 @@
+-- Get runs to publish
+SELECT
+    client_id,
+    stage_id
+FROM (
+    -- Get diffs to publish
+    SELECT
+        client_id,
+        diff_id AS stage_id
+    FROM publishClient
+    JOIN diffRun
+    WHERE publishClient.stage = 'diff'
+        AND diffRun.state = 'full'
+    -- WHERE hook %s
+    UNION
+    -- Get cameras to publish
+    SELECT
+        client_id,
+        cam_id AS stage_id
+    FROM publishClient
+    JOIN camRun
+    WHERE publishClient.stage = 'camera'
+        AND camRun.state = 'full'
+    -- WHERE hook %s
+    ) AS publishToDo
+-- Only get stuff that hasn't been published
+LEFT JOIN publishRun USING(client_id, stage_id)
+WHERE publishRun.client_id IS NULL
Index: trunk/ippTools/share/pubtool_pending.sql
===================================================================
--- trunk/ippTools/share/pubtool_pending.sql	(revision 24512)
+++ trunk/ippTools/share/pubtool_pending.sql	(revision 24512)
@@ -0,0 +1,48 @@
+SELECT
+    publishToDo.*
+FROM (
+    -- Get the diffs
+    -- The following is only appropriate for a diff where warp1 is set; otherwise it's more difficult to get the camera name
+    SELECT DISTINCT
+        publishRun.pub_id,
+        publishClient.product,
+        publishClient.stage,
+        publishClient.workdir,
+        diffRun.diff_id AS stage_id,
+        rawExp.camera
+    FROM publishRun
+    JOIN publishClient USING(client_id)
+    JOIN diffRun
+        ON diffRun.diff_id = publishRun.stage_id
+    JOIN diffInputSkyfile USING(diff_id)
+    -- Need to do something fancy here to get the camera name for a stack
+    LEFT JOIN warpRun ON warpRun.warp_id = diffInputSkyfile.warp1
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun USING(cam_id)
+    JOIN chipRun USING(chip_id)
+    JOIN rawExp USING(exp_id)
+    WHERE publishClient.stage = 'diff'
+        AND publishRun.state = 'new'
+        AND diffRun.state = 'full'
+        -- WHERE hook %s
+    UNION
+    SELECT
+        publishRun.pub_id,
+        publishClient.product,
+        publishClient.stage,
+        publishClient.workdir,
+        camRun.cam_id AS stage_id,
+        rawExp.camera
+    FROM publishRun
+    JOIN publishClient USING(client_id)
+    JOIN camRun
+        ON camRun.cam_id = publishRun.stage_id
+    JOIN chipRun USING(chip_id)
+    JOIN rawExp USING(exp_id)
+    WHERE publishClient.stage = 'camera'
+        AND publishRun.state ='new'
+        AND camRun.state = 'full'
+        -- WHERE hook %s
+) AS publishToDo
+LEFT JOIN publishDone USING(pub_id)
+WHERE publishDone.pub_id IS NULL
Index: trunk/ippTools/share/pubtool_revert.sql
===================================================================
--- trunk/ippTools/share/pubtool_revert.sql	(revision 24512)
+++ trunk/ippTools/share/pubtool_revert.sql	(revision 24512)
@@ -0,0 +1,5 @@
+DELETE FROM publishDone
+USING publishDone, publishRun, publishClient
+WHERE publishDone.pub_id = publishRun.pub_id
+    AND publishRun.client_id = publishClient.client_id
+    AND publishDone.fault != 0
Index: trunk/ippTools/share/pxadmin_create_tables.sql
===================================================================
--- trunk/ippTools/share/pxadmin_create_tables.sql	(revision 24487)
+++ trunk/ippTools/share/pxadmin_create_tables.sql	(revision 24512)
@@ -1369,4 +1369,43 @@
 
 
+
+-- Tables to support publishing of detections to a Science Client
+
+-- Clients to which we send stuff
+CREATE TABLE publishClient (
+    client_id BIGINT AUTO_INCREMENT, -- unique identifier
+    product VARCHAR(64),             -- product name
+    stage VARCHAR(64) NOT NULL, -- stage of interest (chip, camera, diff, etc.)
+    workdir VARCHAR(255) NOT NULL, -- working directory
+    comment VARCHAR(255),            -- for human memory
+    PRIMARY KEY(client_id)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+-- Publishing a set of data (e.g., a specific diffRun)
+CREATE TABLE publishRun (
+    pub_id BIGINT AUTO_INCREMENT, -- unique identifier
+    client_id BIGINT NOT NULL,  -- link to publishClient
+    stage_id BIGINT NOT NULL,   -- link to various stage tables
+    label VARCHAR(64),          -- label for run
+    state VARCHAR(64),          -- state of run (new, full, etc.)
+    PRIMARY KEY(pub_id),
+    KEY(client_id),
+    KEY(stage_id),
+    KEY(label),
+    KEY(state),
+    FOREIGN KEY(client_id) REFERENCES publishClient(client_id)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+-- Publishing a file within a set
+CREATE TABLE publishDone (
+    pub_id BIGINT AUTO_INCREMENT, -- link to publishRun
+    path_base VARCHAR(255),     -- base path of output
+    fault SMALLINT NOT NULL DEFAULT 0, -- Fault code
+    PRIMARY KEY(pub_id),
+    KEY(fault),
+    FOREIGN KEY(pub_id) REFERENCES publishRun(pub_id)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+
 -- This comment line is here to avoid empty query error.
 -- Another way to avoid that problem is to omit the semicolon above but I think that is untidy.
