Index: trunk/ippTasks/science.cleanup.pro
===================================================================
--- trunk/ippTasks/science.cleanup.pro	(revision 29656)
+++ trunk/ippTasks/science.cleanup.pro	(revision 29671)
@@ -8,4 +8,6 @@
     diff.cleanup.on
     stack.cleanup.on
+    chip.bg.cleanup.on
+    warp.bg.cleanup.on
 end
 
@@ -934,2 +936,308 @@
 
 
+book init chipBGPendingCleanup
+
+macro chip.bg.cleanup.status
+  book listbook chipBGPendingCleanup
+end
+
+macro chip.bg.cleanup.reset
+  book init chipBGPendingCleanup
+end
+
+macro chip.bg.cleanup.on
+  task chip.bg.cleanup.load
+    active true
+  end
+  task chip.bg.cleanup.run
+    active true
+  end
+end
+
+macro chip.bg.cleanup.off
+  task chip.bg.cleanup.load
+    active false
+  end
+  task chip.bg.cleanup.run
+    active false
+  end
+end
+
+# this variable will cycle through the known database names
+$chip_bg_cleanup_DB = 0
+
+task	       chip.bg.cleanup.load
+  host         local
+
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 30
+  npending     1
+  active       false
+
+  stdout NULL
+  stderr $LOGDIR/chip.bg.cleanup.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    $run = bgtool -pendingcleanupchiprun
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$chip_bg_cleanup_DB
+      $run = $run -dbname $DB:$chip_bg_cleanup_DB
+      $chip_bg_cleanup_DB ++
+      if ($chip_bg_cleanup_DB >= $DB:n) set chip_bg_cleanup_DB = 0
+    end
+    add_poll_args run
+    add_poll_labels run
+    command $run
+  end
+
+  # success
+  task.exit    0
+    # convert 'stdout' to book format
+    ipptool2book stdout chipBGPendingCleanup -key chip_bg_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook chipBGPendingCleanup
+    end
+
+    # delete existing entries in the appropriate pantaskStates
+    process_cleanup chipBGPendingCleanup
+  end
+
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+# run the ipp_cleanup.pl script on pending images
+task	       chip.bg.cleanup.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 300
+  active       false
+
+  task.exec
+    book npages chipBGPendingCleanup -var N
+    if ($N == 0) 
+        periods -exec $RUNEXEC
+        break
+    end
+    if ($NETWORK == 0) break
+    
+    # look for new images in chipBGPendingCleanup (pantaskState == INIT)
+    book getpage chipBGPendingCleanup 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword chipBGPendingCleanup $pageName pantaskState RUN
+    book getword chipBGPendingCleanup $pageName camera -var CAMERA
+    book getword chipBGPendingCleanup $pageName state -var CLEANUP_MODE
+    book getword chipBGPendingCleanup $pageName chip_bg_id -var CHIP_BG_ID
+    book getword chipBGPendingCleanup $pageName workdir -var WORKDIR_TEMPLATE
+    book getword chipBGPendingCleanup $pageName exp_tag -var EXP_TAG
+    book getword chipBGPendingCleanup $pageName dbname -var DBNAME
+
+    # specify choice of local or remote host based on camera and chip (class_id)
+    set.host.for.camera $CAMERA FPA
+    set.workdir.by.camera $CAMERA FPA $WORKDIR_TEMPLATE $default_host WORKDIR
+    sprintf logfile "%s/%s/%s.bgc.%s.cleanup.log" $WORKDIR $EXP_TAG $EXP_TAG $CHIP_BG_ID
+
+    stdout $LOGDIR/chip.bg.cleanup.log
+    stderr $LOGDIR/chip.bg.cleanup.log
+
+    # XXX is everything listed here needed?
+    $run = ipp_cleanup.pl --stage chip_bg --stage_id $CHIP_BG_ID --camera $CAMERA --mode $CLEANUP_MODE --logfile $logfile
+    add_standard_args run
+
+    # save the pageName for future reference below
+    options $pageName
+
+    # create the command line
+    if ($VERBOSE > 1)
+      echo command $run
+    end
+    periods -exec 0.05
+    command $run
+  end
+
+  # default exit status
+  task.exit    default
+    process_exit chipBGPendingCleanup $options:0 $JOB_STATUS
+  end
+
+  task.exit    crash
+    showcommand crash
+    book setword chipBGPendingCleanup $options:0 pantaskState CRASH
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword chipBGPendingCleanup $options:0 pantaskState TIMEOUT
+  end
+end
+
+book init warpBGPendingCleanup
+
+macro warp.bg.cleanup.status
+  book listbook warpBGPendingCleanup
+end
+
+macro warp.bg.cleanup.reset
+  book init warpBGPendingCleanup
+end
+
+macro warp.bg.cleanup.on
+  task warp.bg.cleanup.load
+    active true
+  end
+  task warp.bg.cleanup.run
+    active true
+  end
+end
+
+macro warp.bg.cleanup.off
+  task warp.bg.cleanup.load
+    active false
+  end
+  task warp.bg.cleanup.run
+    active false
+  end
+end
+
+# this variable will cycle through the known database names
+$warp_bg_cleanup_DB = 0
+
+task	       warp.bg.cleanup.load
+  host         local
+
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 300
+  npending     1
+  active       false
+
+  stdout NULL
+  stderr $LOGDIR/warp.bg.cleanup.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    $run = bgtool -pendingcleanupwarprun
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$warp_bg_cleanup_DB
+      $run = $run -dbname $DB:$warp_bg_cleanup_DB
+      $warp_bg_cleanup_DB ++
+      if ($warp_bg_cleanup_DB >= $DB:n) set warp_bg_cleanup_DB = 0
+    end
+    add_poll_args run
+    add_poll_labels run
+    command $run
+  end
+
+  # success
+  task.exit    0
+    # convert 'stdout' to book format
+    ipptool2book stdout warpBGPendingCleanup -key warp_bg_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook warpBGPendingCleanup
+    end
+
+    # delete existing entries in the appropriate pantaskStates
+    process_cleanup warpBGPendingCleanup
+  end
+
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+# run the ipp_cleanup.pl script on pending images
+task	       warp.bg.cleanup.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 300
+  active       false
+
+  task.exec
+    book npages warpBGPendingCleanup -var N
+    if ($N == 0) 
+        periods -exec $RUNEXEC
+        break
+    end
+    if ($NETWORK == 0) break
+    
+    # look for new images in warpBGPendingCleanup (pantaskState == INIT)
+    book getpage warpBGPendingCleanup 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword warpBGPendingCleanup $pageName pantaskState RUN
+    book getword warpBGPendingCleanup $pageName camera -var CAMERA
+    book getword warpBGPendingCleanup $pageName state -var CLEANUP_MODE
+    book getword warpBGPendingCleanup $pageName warp_bg_id -var WARP_BG_ID
+    book getword warpBGPendingCleanup $pageName workdir -var WORKDIR_TEMPLATE
+    book getword warpBGPendingCleanup $pageName exp_tag -var EXP_TAG
+    book getword warpBGPendingCleanup $pageName dbname -var DBNAME
+
+    # specify choice of local or remote host based on camera and warp 
+    set.workdir.by.camera $CAMERA $WARP_BG_ID $WORKDIR_TEMPLATE $default_host WORKDIR
+    sprintf logfile "%s/%s/%s.bgw.%s.cleanup.log" $WORKDIR $EXP_TAG $EXP_TAG $WARP_BG_ID
+
+    stdout $LOGDIR/warp.bg.cleanup.log
+    stderr $LOGDIR/warp.bg.cleanup.log
+
+    # XXX is everything listed here needed?
+    $run = ipp_cleanup.pl --stage warp_bg --stage_id $WARP_BG_ID --camera $CAMERA --mode $CLEANUP_MODE --logfile $logfile
+    add_standard_args run
+
+    # save the pageName for future reference below
+    options $pageName
+
+    # create the command line
+    if ($VERBOSE > 1)
+      echo command $run
+    end
+    periods -exec 0.05
+    command $run
+  end
+
+  # default exit status
+  task.exit    default
+    process_exit warpBGPendingCleanup $options:0 $JOB_STATUS
+  end
+
+  task.exit    crash
+    showcommand crash
+    book setword warpBGPendingCleanup $options:0 pantaskState CRASH
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword warpBGPendingCleanup $options:0 pantaskState TIMEOUT
+  end
+end
