Index: trunk/ippScripts/scripts/register_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/register_imfile.pl	(revision 27891)
+++ trunk/ippScripts/scripts/register_imfile.pl	(revision 30049)
@@ -19,6 +19,7 @@
 use File::Basename qw( basename);
 use IPC::Run;
-use IPC::Cmd 0.36 qw( can_run );
+use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Config 1.01 qw( :standard );
+use PS::IPP::Metadata::List qw( parse_md_list );
 use PS::IPP::Metadata::Config;
 use Math::Trig;
@@ -35,4 +36,5 @@
 my $ppStatsFromMetadata = can_run( 'ppStatsFromMetadata' ) or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
 my $ppConfigDump = can_run( 'ppConfigDump' ) or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+my $ippApplyBurntoolSingle = can_run( 'ipp_apply_burntool_single.pl' ) or (warn "Can't find ipp_apply_burntool_single" and $missing_tools = 1);
 
 my ($cache, $exp_id, $tmp_class_id, $tmp_exp_name, $uri, $bytes, $md5sum, $dbname, $verbose, $no_update, $no_op, $logfile);
@@ -82,4 +84,5 @@
 
 my $cmdflags;
+my $burntoolStateGood;
 # Run ppStats on the input file
 {
@@ -114,5 +117,5 @@
     my $burntoolState = 0;
     my $isGPC1 = 0;
-    my $burntoolStateGood = 0;
+    $burntoolStateGood = 0;
     foreach my $line (split /\n/, $out1) {
         if ($line =~ /FPA.BURNTOOL.APPLIED/) {
@@ -174,7 +177,8 @@
 $command .= " -hostname $host" if defined $host;
 $command .= " -dbname $dbname" if defined $dbname;
-$command .= " -data_state full";
+$command .= " -data_state check_burntool";
 $command .= " $cmdflags";
 
+print "$command\n";
 # determine solar-system parameters
 my $longitude = &value_for_flag($cmdflags, 0.0, "-longitude");
@@ -206,4 +210,11 @@
     }
 }
+
+# This might have a race condition
+# $date_end = $dateobs;
+# $date_start = $dateobs - 30 minutes ? dateobs_UTC_midnight?
+# lock file?
+# if exp_type = DARK and date > MIDNIGHT HST { wait }
+# system("ipp_apply_burntool.pl --class_id $class_id --dateobs_begin $date_start --dateobs_end $date_end --dbname gpc1 --logfile /data/$host.0/burntool_logs/$class_id.$start_date.log"); 
 
 $now_time = localtime();
@@ -222,4 +233,91 @@
     print "skipping command: $command\n";
 }
+
+# We now have an imfile in the database, check if we can burntool it.  If not, continue on.
+
+{ 
+    my $mdcParser  = PS::IPP::Metadata::Config->new;
+
+    my $class_id   = &value_for_flag($cmdflags, 0.0, "-class_id");
+    my $check_date       = &value_for_flag($cmdflags, 0.0, "-dateobs");
+    $check_date =~ s/T.*$//;
+    my $exp_name   = $tmp_exp_name;
+    
+
+    my $bt_check_command = "$regtool -checkburntoolimfile ";
+    $bt_check_command .= " -class_id $class_id ";
+    $bt_check_command .= " -date $check_date ";
+    $bt_check_command .= " -valid_burntool $burntoolStateGood ";
+    $bt_check_command .= " -exp_name $exp_name ";
+    $bt_check_command .= " -dbname $dbname" if defined $dbname;
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run ( command => $bt_check_command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform regtool: $error_code", 
+		$class_id, $exp_name, $date, $PS_EXIT_SYS_ERROR);
+    }
+
+    my @whole = split /\n/, (join "", @$stdout_buf);
+    my @single = ();
+    my $exposures;
+    while ( scalar @whole > 0 ) {
+	my $value = shift @whole;
+	push @single, $value;
+	if ($value =~ /^\s*END\s*$/) {
+	    push @single, "\n";
+	    
+	    my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );
+	    &my_die("Unable to parse output from regtool", 
+		    $class_id, $exp_name, $date, $PS_EXIT_SYS_ERROR) unless
+			defined $list;
+	    push @{ $exposures }, @$list;
+	    @single = ();
+	}
+    }
+    
+    # We only care about the final entry, as that contains the exposure we are.
+    
+    my $regtool_update = "$regtool -updateprocessedimfile ";
+    $regtool_update .= "-dbname $dbname " if defined $dbname;
+    $regtool_update .= "-exp_id $exp_id -class_id $class_id ";
+
+    my $burntool_data = pop(@{ $exposures });
+    if ($burntool_data->{burnable} == 0) {
+	$regtool_update .= " -burntool_state 0 -set_state pending_burntool ";
+    }
+    else {
+	my $apply_command = "$ippApplyBurntoolSingle --dbname $dbname ";
+	$apply_command .= " --class_id $class_id --exp_id $exp_id ";
+	$apply_command .= " --this_uri $burntool_data->{uri} ";
+	$apply_command .= " --previous_uri $burntool_data->{previous_uri} " if defined $burntool_data->{previous_uri};
+	$apply_command .= " --imfile_state $burntool_data->{imfile_state} ";
+	$apply_command .= " --verbose " if $verbose;
+	print "$apply_command\n";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    IPC::Cmd::run(command => $apply_command, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    warn ("Unable to perform ipp_apply_burntool_single.pl: $error_code");
+	    exit($error_code);
+	}
+    }    
+
+    unless ($no_update) {
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    IPC::Cmd::run(command => $regtool_update, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    warn ("Unable to perform regtool -addprocessedimfile: $error_code");
+	    exit($error_code);
+	}
+    } else {
+	print "skipping command: $command\n";
+    }
+
+}
+
+
 
 $now_time = localtime();
