Index: trunk/tools/stsci_disks/p2_check.pl
===================================================================
--- trunk/tools/stsci_disks/p2_check.pl	(revision 38474)
+++ trunk/tools/stsci_disks/p2_check.pl	(revision 38475)
@@ -3,4 +3,10 @@
 use DBI;
 use Nebulous::Client;
+use Nebulous::Server::SQL;
+#use Nebulous::Util;
+use Nebulous::Key qw( parse_neb_key parse_neb_volume );
+
+use SQL::Interp qw( sql_interp );
+
 
 use IPC::Cmd 0.36 qw( can_run run );
@@ -20,4 +26,5 @@
 my $difftool    = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
 my $stacktool   = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
+my $staticskytool = can_run('staticskytool') or (warn "Can't find staticskytool" and $missing_tools = 1);
 
 if ($missing_tools) {
@@ -26,4 +33,6 @@
 }
 
+my $verbose = 0;
+my $debug   = 0;
 my ($stage,$stage_id,$dbname);
 GetOptions(
@@ -34,4 +43,69 @@
 
 
+my $camera = 'GPC1';
+my $ipprc = PS::IPP::Config->new( $camera ) or my_die("Unable to set_up", $stage, $stage_id);
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+# Grab a volume to use for this pass.
+my @volumes = ();
+for (my $i = 0; $i <= 19; $i++) {
+    for (my $j = 0; $j < 3; $j++) {
+        push @volumes, sprintf("stsci%02d.%1d",$i,$j);
+    }
+}
+my $rand_vol = $volumes[rand @volumes];
+
+# Set up the logs
+my $time = time();
+my $logDest = "neb://any/stsci_shuffle/${stage}/${stage}.${stage_id}.log.${time}";
+
+unless ($debug) {
+    $ipprc->redirect_output($logDest) or &my_die("Could not redirect output to logfile ${logDest}", $stage,$stage_id);
+}
+
+my $manifest_dir = "/data/${rand_vol}/MANIFEST/${stage}";
+system("mkdir -p ${manifest_dir}");
+open(MANIFEST,">${manifest_dir}/${stage}.${stage_id}.MANIFEST") || &my_die( "Could not open manifest file", $stage,$stage_id);
+
+# Set up the nebulous database connection.
+my (    $db, $dbhost, $dbpass, $dbuser);
+my $conffile = "/home/panstarrs/ipp/ippconfig/nebulous.config";
+if (-e $conffile) {
+#    printf STDERR "found config\n";
+    open(IN,$conffile);
+    while(<IN>) {
+        chomp;
+        if ($_ =~ /NEB_DB\s+/) {
+            $db = (split /\s+/,$_)[2];
+        }
+        elsif ($_ =~ /NEB_DBHOST/) {
+            $dbhost = (split /\s+/,$_)[2];
+        }
+        elsif ($_ =~ /NEB_USER/) {
+            $dbuser = (split /\s+/,$_)[2];
+        }
+        elsif ($_ =~ /NEB_PASS/) {
+            $dbpass = (split /\s+/,$_)[2];
+        }
+    }
+    close(IN);
+#    print STDERR  "$db $dbhost $dbuser $dbpass\n";
+}
+
+pod2usage( -msg => "Cannot configure nebulous database", -exitval => 2) unless
+    defined $db && defined $dbhost && defined $dbuser && defined $dbpass;
+
+my $dbh = DBI->connect(
+    "DBI:mysql:database=$db:host=$dbhost",
+    $dbuser,
+    $dbpass,
+    {
+        RaiseError => 1,
+        PrintError => 0,
+        AutoCommit => 0,
+    },
+    );
+my $sql = Nebulous::Server::SQL->new();
+
 my $server = $ENV{'NEB_SERVER'};
 my $neb = Nebulous::Client->new(
@@ -39,15 +113,4 @@
     );
 
-my $camera = 'GPC1';
-my $ipprc = PS::IPP::Config->new( $camera ) or my_die("Unable to set_up", $stage, $stage_id);
-my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
-
-my $time = time();
-my $logDest = "neb://any/stsci_shuffle/${stage}/${stage}.${stage_id}.log.${time}";
-my $ipprc = PS::IPP::Config->new( "GPC1" ) or &my_die( "Could not create config object.", $stage,$stage_id);
-$ipprc->redirect_output($logDest) or &my_die("Could not redirect output to logfile ${logDest}", $stage,$stage_id);
-
-my $manifest_file = $ipprc->file_resolve("neb://any/stsci_shuffle/${stage}/${stage}.${stage_id}.MANIFEST",1);
-open(MANIFEST,">${manifest_file}") || &my_die( "Could not open manifest file", $stage,$stage_id);
 
 # Define which things we want to look at, and where and how many of each.
@@ -102,5 +165,5 @@
 $time = time();
 my $dtime = $time - $otime;
-print "LOG BEGIN $stage $stage_id $time $dtime\n";
+log_print ("LOG BEGIN $stage $stage_id $rand_vol");
 
 foreach my $stageEntry ( @{ $stageData } ) {
@@ -109,5 +172,9 @@
 
     my $path_base = $stageEntry->{path_base};
-    
+
+    my %replicates;
+    my %culls;
+    my %md5sums_for_keys;
+    my %canonical_files;
     foreach my $component (keys %components) {
 	my $key;
@@ -131,4 +198,12 @@
 	}
 
+	# I hate you, nebulous.
+	eval {
+	    $key = parse_neb_key($key);
+	    $key = $key->path;
+	};
+	
+
+
 	# Stealing most of neb-stat here
 	my %md5sum_uniq;
@@ -136,5 +211,7 @@
 	my @files = map {URI->new($_)->file if $_} @$instances;
 	my @stsci_copies = ();
+	my @stsci_volumes = ();
 	my @local_copies = ();
+	my @local_volumes = ();
 	my @existance;
 	my @md5sums;
@@ -149,8 +226,13 @@
 		$md5sum_uniq{$md5sums[$i]} = 1;
 		if ($volume =~ /stsci/) { 
+		    unless (defined($canonical_files{$key})) {
+			$canonical_files{$key} = $files[$i];
+		    }
 		    push @stsci_copies, $files[$i];
+		    push @stsci_volumes, $volume;
 		}
 		elsif ($volume =~ /ipp/) { 
 		    push @local_copies, $files[$i];
+		    push @local_volumes, $volume;
 		}
 		else { &my_die("You can't exist somewhere not ipp and not stsci",$stage,$stage_id,$files[$i]); }
@@ -162,35 +244,51 @@
 	    &my_die("Key $key has inconsistent md5sum values", $stage,$stage_id);
 	}
-	$md5sum{$key} = (keys %md5sum_uniq)[0];
+	$md5sums_for_keys{$key} = (keys %md5sum_uniq)[0];
 
 	# Do the logic at what we found
 	if ($#stsci_copies + 1 < $components{$component}{stsci}) {
 	    # We need to make an stsci copy.
-	    my $vol = choose_random_stsci();
-	    push @{ $replicates{$key} }, $vol;
+	    push @{ $replicates{$key} }, $rand_vol;
 	}
 	if ($#local_copies + 1 < $components{$component}{local}) {
 	    my $vol = get_volume($key);
-	    while ($vol =~ /stsci/) {
-		$vol = get_volume($key);
-	    }
 	    push @{ $replicates{$key} }, $vol;
 	}
 	if ($#stsci_copies + 1 > $components{$component}{stsci}) {
-	    for (my $i = $components{$component}{stsci} + 1; $i <= $#stsci_copies; $i++) {
-		push @{ $culls{$key} }, $stsci_copies[$i];
-	    }
-	}
-	if ($#local_copies + 1 > $components{$component}{stsci}) {
-	    for (my $i = $components{$component}{local} + 1; $i <= $#local_copies; $i++) {
-		push @{ $culls{$key} }, $local_copies[$i];
-	    }
-	}
-    }
-    log_print("SCAN $stage $stage_id $path_base");
-
+	    for (my $i = $components{$component}{stsci}; $i <= $#stsci_copies; $i++) {
+		push @{ $culls{$key} }, $stsci_volumes[$i]; # $stsci_copies[$i];
+	    }
+	}
+	if ($#local_copies + 1 > $components{$component}{local}) {
+	    for (my $i = $components{$component}{local}; $i <= $#local_copies; $i++) {
+		push @{ $culls{$key} }, $local_volumes[$i]; # $local_copies[$i];
+	    }
+	}
+
+	my $copy_check = sprintf("ST: %d %d IPP: %d %d",
+				 $#stsci_copies +1,  $components{$component}{stsci}, 
+				 $#local_copies + 1, $components{$component}{local});
+	log_print("SCAN $stage $stage_id $path_base $component $copy_check");
+    }
+
+
+    if ($debug) { 
+	foreach my $key (keys %replicates) {
+	    foreach my $vol (@{ $replicates{$key} }) {
+		log_print("REP: $key to $vol");
+	    }
+	}
+	foreach my $key (keys %culls) {
+	    foreach my $vol (@{ $culls{$key} }) {
+		log_print("CULL: $key from $vol");
+	    }
+	}
+	die;
+    }
+    
     foreach my $key (keys %replicates) {
 	foreach my $vol (@{ $replicates{$key} }) {
-	    $neb->replicate($key,$vol) or &my_die "failed to replicate a copy of $key to $vol";
+	    log_print("REP: $key to $vol");
+	    $neb->replicate($key,$vol) or &my_die( "failed to replicate a copy of $key to $vol");
 	    if ($@) { die $@; }
 	}
@@ -203,35 +301,54 @@
 	    my $uris = $neb->find_instances($key,$vol);
 	    @$uris = map {URI->new($_)->file if $_} @$uris;
-	    $uri = ${ $uris }[0];
-	    my $tmpmd5 = local_md5sum($uri);
-	    if ($tmpmd5 ne $md5sums{$key}) {
-		&my_die "Post-replication md5sum does not match for $key on $vol! $tmpmd5 != $md5sums{$key}";
-	    }
-	}
-    }
-    log_print("CHECK $stage $stage_id $path_base");
+	    my $uri = ${ $uris }[0];
+	    my ($tmpmd5,undef) = local_md5sum($uri);
+	    if ($tmpmd5 ne $md5sums_for_keys{$key}) {
+		&my_die("Post-replication md5sum does not match for $key on $vol! $tmpmd5 != $md5sums_for_keys{$key}");
+	    }
+	    if ($vol =~ /stsci/) {
+		$canonical_files{$key} = $uri;
+	    }
+	}
+
+    }
+
+
+    foreach my $key (keys %canonical_files) {
+	print MANIFEST "$key $canonical_files{$key} $md5sums_for_keys{$key}\n";
+    }
+
+    log_print("CHCK $stage $stage_id $path_base");
     foreach my $key (keys %culls) {
 	foreach my $vol (@{ $culls{$key} }) {
+	    log_print("CULL $key from $vol");
 	    $neb->cull($key,$vol);
 	}
     }
-    log_print("CULL $stage $stage_id $path_base");
-}
+    log_print("STOP $stage $stage_id $path_base");
+}
+close(MANIFEST);    
     
-    
-
+
+sub my_die {
+    my $message = shift;
+    carp($message);
+    my $exit_code = $PS_EXIT_PROG_ERROR;
+    close(MANIFEST);
+    unlink("${manifest_dir}/${stage}.${stage_id}.MANIFEST");
+
+    exit($exit_code);
+}
 	
 sub log_print {
     my $msg = shift;
-    $otime = $time;
+
     $time = time();
     $dtime = $time - $otime;
-    print "LOG $msg $time $dtime\n";
+    print "LOG $time $dtime $msg\n";
 }
 
 sub get_volume {
     my $key = shift;
-
-
+    
     my ($vol_id, $vol_host, $vol_path, $xattr, $forbidden_cabinet);    
     my $max_used_space = 0.98;
@@ -278,9 +395,21 @@
 	$query->finish;
     };
-
-    return($vol_host);
+    &my_die($@) if $@;
+
+    if (defined($vol_host)) {
+	if ($vol_host eq 'ipp066') { 
+            $vol_host = get_volume($key); 
+            if ($vol_host eq 'ipp066') { 
+                # Really?
+                $vol_host = get_volume($key); 
+#               if ($vol_host eq 'ipp066') {}  #fine.  Be that way.  Jerk.
+#                 
+            }
+        }
+    }
+
+    return($vol_host . ".0");
 }
     
-
 sub local_md5sum {
     my $filename = shift;
@@ -289,5 +418,5 @@
     $host =~ s/\.\d//;
 #    print "$filename $host $volume\n";
-    my $response = `ssh $host md5sum $filename`;
+    my $response = `ssh -x $host md5sum $filename`;
     chomp($response);
     my ($sum, undef) = split /\s+/, $response;
