Index: trunk/ippScripts/scripts/magic_destreak.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak.pl	(revision 29555)
+++ trunk/ippScripts/scripts/magic_destreak.pl	(revision 29561)
@@ -42,5 +42,5 @@
 # Parse the command-line arguments
 my ($magic_ds_id, $camera, $streaks, $inv_streaks, $exp_id, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base, $cam_reduction);
-my ($streaks_path_base, $inv_streaks_path_base);
+my ($streaks_path_base, $inv_streaks_path_base, $run_state);
 my ($outroot, $recoveryroot, $magicked);
 my ($replace, $release);
@@ -50,4 +50,5 @@
            'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
            'camera=s'       => \$camera,     # camera for evaluating file rules
+           'run-state=s'    => \$run_state,   # state of run (new or update)
            'streaks_path_base=s'      => \$streaks_path_base,    # path_base for streaks data
            'inv_streaks_path_base=s'  => \$inv_streaks_path_base, #path_base for streaks from inverse diff
@@ -77,8 +78,9 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --magic_ds_id --camera --streaks --stage --stage_id --component --uri --path_base --outroot --magicked",
+pod2usage( -msg => "Required options: --magic_ds_id --camera --run_state --streaks --stage --stage_id --component --uri --path_base --outroot --magicked",
            -exitval => 3) unless
     defined $magic_ds_id and
     defined $camera and
+    defined $run_state and
     defined $streaks and
     defined $streaks_path_base and
@@ -113,4 +115,8 @@
     &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
 }
+
+&my_die("Invalid value for run-state: $run_state", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR)
+    unless ($run_state eq 'new') or ($run_state eq 'update');
+
 $inv_streaks_path_base = undef if defined($inv_streaks_path_base) and ($inv_streaks_path_base eq "NULL");
 $inv_streaks = undef if defined($inv_streaks) and ($inv_streaks eq "NULL");
@@ -479,11 +485,16 @@
 # Input result into database
 {
-    my $command = "$magicdstool -adddestreakedfile";
+    my $command = "$magicdstool";
     $command   .= " -magic_ds_id $magic_ds_id";
     $command   .= " -component $component";
     $command   .= " -setmagicked" if $replace;
-    $command   .= " -backup_path_base $backup_path_base" if $backup_path_base;
-    $command   .= " -recovery_path_base $recovery_path_base" if $recovery_path_base;
-    $command   .= " $statsFlags" if $statsFlags;
+    if ($run_state eq 'new') {
+        $command .= " -adddestreakedfile";
+        $command .= " -backup_path_base $backup_path_base" if $backup_path_base;
+        $command .= " -recovery_path_base $recovery_path_base" if $recovery_path_base;
+        $command .= " $statsFlags" if $statsFlags;
+    } else {
+        $command .= " -tofullfile";
+    }
     $command   .= " -dbname $dbname" if defined $dbname;
 
@@ -581,5 +592,11 @@
     $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
 
-    my $command = "$magicdstool -adddestreakedfile";
+    my $command = "$magicdstool";
+    
+    if ($run_state eq 'new') {
+        $command .= " -adddestreakedfile";
+    } else {
+        $command .= " -updatedestreakedfile";
+    }
     $command   .= " -magic_ds_id $magic_ds_id";
     $command   .= " -component $component";
Index: trunk/ippScripts/scripts/magic_destreak_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29555)
+++ trunk/ippScripts/scripts/magic_destreak_cleanup.pl	(revision 29561)
@@ -89,5 +89,5 @@
 $q1 .= " WHERE magic_ds_id = $magic_ds_id";
 
-my $q2 = "SELECT * from magicDSFile where magic_ds_id = $magic_ds_id";
+my $q2 = "SELECT * from magicDSFile WHERE (data_state = 'full' OR data_state = 'update') AND magic_ds_id = $magic_ds_id";
 
 my $stmt1 = $dbh->prepare($q1);
@@ -245,21 +245,17 @@
             delete_files($rimage, $rmask, $rweight, $rsources, undef, $bimage, $bmask, $bweight, $bsources);
         }
-}
-
-$dbh = DBI->connect($dsn, $dbuser, $dbpassword) or die "Cannot connect to mysql server\n";
-
-if (!$no_update and ($num_components > 0)) {
-    my $result = $dbh->do("DELETE FROM magicDSFile WHERE magic_ds_id = ?", undef, $magic_ds_id);
-    # my $result = $stmt3->do($magic_ds_id);
-    my_die("attempt to delete magicDSFiles failed", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR) if $result eq "0E0";
-}
-
-if (!$no_update) {
-    my $result = $dbh->do("UPDATE magicDSRun SET state = 'cleaned' WHERE magic_ds_id = ?", undef, $magic_ds_id);
-    my_die("attempt to update magicDSRun.state failed", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR) if $result eq "0E0";
-} else {
-    print STDERR "skipping update of magicDSRun\n";
-}
-
+        my $command = "$magicdstool -tocleanedfile -magic_ds_id $magic_ds_id -component $component";
+        $command   .= " -dbname $dbname" if defined $dbname;
+
+        unless ($no_update) {
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                carp("failed to update database for $magic_ds_id");
+            }
+        } else {
+            print "Skipping command: $command\n";
+        }
+}
 
 ### Pau.
@@ -292,5 +288,5 @@
     $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
 
-    my $command = "$magicdstool -updaterun -set_state failed_cleanup";
+    my $command = "$magicdstool -updaterun -set_state error_cleaned";
     $command   .= " -magic_ds_id $magic_ds_id";
     $command   .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/magic_destreak_revert.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak_revert.pl	(revision 29555)
+++ trunk/ippScripts/scripts/magic_destreak_revert.pl	(revision 29561)
@@ -38,5 +38,5 @@
 
 # Parse the command-line arguments
-my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $bothways, $cam_path_base, $cam_reduction, $magicked);
+my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $bothways, $cam_path_base, $cam_reduction, $magicked, $run_state);
 my ($outroot, $recoveryroot, $replace, $release, $bytes, $md5sum);
 my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
@@ -47,4 +47,5 @@
            'stage=s'        => \$stage,      # raw, chip, warp, or diff
            'stage_id=s'     => \$stage_id,   # exp_id, chip_id, warp_id, or diff_id
+           'run-state=s'    => \$run_state, # current state of run
            'component=s'    => \$component,  # the class_id or skycell_id
            'path_base=s'    => \$path_base,  # path_base of the input
@@ -74,4 +75,5 @@
     defined $path_base and
     defined $magicked and
+    defined $run_state and
     defined $outroot;
 
@@ -248,4 +250,5 @@
 {
     my $command = "$magicdstool -revertdestreakedfile -i_am_sure";
+    $command   .= " -state $run_state";
     $command   .= " -magic_ds_id $magic_ds_id";
     $command   .= " -component $component";
