Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 26907)
+++ trunk/ippTools/share/Makefile.am	(revision 26912)
@@ -195,4 +195,5 @@
      magicdstool_definebyquery_warp.sql \
      magicdstool_definebyquery_diff.sql \
+     magicdstool_definecopy_chip.sql \
      magicdstool_getrunids.sql \
      magicdstool_getskycells.sql \
Index: trunk/ippTools/share/magicdstool_definecopy_chip.sql
===================================================================
--- trunk/ippTools/share/magicdstool_definecopy_chip.sql	(revision 26912)
+++ trunk/ippTools/share/magicdstool_definecopy_chip.sql	(revision 26912)
@@ -0,0 +1,33 @@
+SELECT DISTINCT       -- DISTINCT because we are going though diffInputSkyfile
+    magicRun.magic_id,
+    chipRun.exp_id,
+    'chip' AS stage,
+    chipRun.chip_id AS stage_id,
+    camRun.cam_id,
+    chipRun.label,
+    CAST(NULL AS SIGNED) AS inv_magic_id
+FROM magicRun
+    JOIN magicMask USING(magic_id)
+    JOIN diffRun USING(diff_id)
+    JOIN diffInputSkyfile USING(diff_id)
+    JOIN warpRun
+        ON (!magicRun.inverse AND warp1 = warp_id)
+        OR ( magicRun.inverse AND warp2 = warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun AS oldCamRun USING(cam_id)
+    -- This ends the usual JOIN down to the raw level
+    JOIN chipRun
+        ON chipRun.exp_id = magicRun.exp_id
+        AND chipRun.chip_id != oldCamRun.chip_id -- Not allowed to copy yourself
+    JOIN camRun ON
+        camRun.chip_id = chipRun.chip_id
+    JOIN rawExp
+        ON rawExp.exp_id = chipRun.exp_id
+    LEFT JOIN magicDSRun
+        ON magicRun.magic_id = magicDSRun.magic_id
+        AND magicDSRun.stage  = 'chip'
+WHERE magicRun.state = 'full'
+    -- HOOK: NEWLINE HERE TO ACTIVATE %s AND magicDSRun.magic_ds_id IS NULL
+    AND chipRun.magicked  = 0
+    AND chipRun.state = 'full'
+    AND camRun.state = 'full'
