#! /usr/bin/env perl

use DBI;
use PS::IPP::Config 1.01 qw( :standard );
use PS::IPP::Metadata::Config;
use PS::IPP::Metadata::List qw( parse_md_list );
use IPC::Cmd 0.36 qw( can_run run );
use Carp;
use Pod::Usage qw( pod2usage );

use Nebulous::Server::SQL;
use Nebulous::Client;
use SQL::Interp qw( sql_interp );
use strict;
use warnings FATAL => qw( all );
use Getopt::Long qw( GetOptions :config auto_help auto_version );
use File::Basename;

my ($host,$volume,$dir);
GetOptions(
    'host=s' => \$host,
    'volume=s' => \$volume,
    'dir=s'   => \$dir) || pod2usage(2 );

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 $host && defined $volume && defined $dir && 
    defined $db && defined $dbhost && defined $dbuser && defined $dbpass;

my $host_check = `hostname`;
chomp($host_check);
if ($host ne $host_check) {
    die "Required to run on $host.  not on $host_check\n";
}

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(
    proxy => "$server",
    );


my $time = time();
my $logDest = "neb://any/stsci_shuffle/sizes/${host}.${volume}/${dir}.log";
my $ipprc = PS::IPP::Config->new( "GPC1" ) or die "Could not create config object.\n";
$ipprc->redirect_output($logDest) or die "Could not redirect output to logfile ${logDest}\n";

my @rsync_vols;
if ($host =~ /stsci0/) {
    push @rsync_vols, 'ipp096.0';
}
else {
    if ($volume == 0) {
	push @rsync_vols, 'ippb06.0';
	push @rsync_vols, 'ipp074.0';
    }
    elsif ($volume == 1) {
	push @rsync_vols, 'ippb06.1';
	push @rsync_vols, 'ipp076.0';
    }
    elsif ($volume == 2) {
	push @rsync_vols, 'ippb06.2';
	push @rsync_vols, 'ipp079.0';
    }
}




my $root = "/data/${host}.${volume}/nebulous/${dir}";
my @subdirectories = <$root/*>;
my $size = 0;
my $limit = 10 * 1024 * 1024; # stat returns bytes, so 100 * meg * k 
my ($size_ota,$size_pv1,$size_pv2,$size_md) = (0,0,0,0);
foreach my $subdir (@subdirectories) {
    my @files = <${subdir}/*>;
    foreach my $f (@files) {
	if ($f =~ /LAP.PV3.20140730/) { next; }
	
	my @stat = stat($f);
	$size += $stat[7];
#	print "$f $stat[7]\n"; die;
	if ($f =~ /ota/) { $size_ota += $stat[7]; }
	if ($f =~ /LAP.ThreePi.20120706/) { $size_pv1 += $stat[7]; }
	if ($f =~ /LAP.ThreePi.20130717/) { $size_pv2 += $stat[7]; }
	if ($f =~ /MD/) { $size_md += $stat[7]; }


# 	if ($stat[7] > $limit) {
# 	    if (0) {
# 		my $ins_id = $f;
# 		$ins_id =~ s/^$subdir\///;
# 		$ins_id =~ s/\..*//;
		
# 		my $key = $f;
# 		$key =~  s/.*\/${ins_id}\.//;
# 		$key =~  s%:%/%g;
		
# 		my $uris = $neb->find_instances($key);
# 		my $found = 0;
# 		foreach my $uri (@{ $uris }) {
# 		    if ($uri eq "file://${f}") {
# 			$found = 1;
# 		    }
# 		}
# 		if ($#{ $uris } == -1) { # true orphan
# 		    if ($key =~ /ecliptic/) {
# 			$key =~ s%/00hr%:00hr%;
# 			$neb->replicate($key);
# 			print "#replicate $key\n";
# 		    }
# 		    $found = -1;
# 		}
# 		print "$f $stat[7] $key $found\n";
# 		if ($found == 0) {
# #		print "rm $f\n";
# 		    unlink($f);
# 		    foreach my $rsync (@rsync_vols) {
# 			my $test_f = $f;
# 			$test_f =~ s%^/data/%%;
# 			$test_f = "/data/${rsync}/stsci_shuffle/${test_f}";
# 			if (-e $test_f) {
# 			    unlink($f);
# #			print "rm $test_f\n";
# 			}
# 		    }
# 		}
# 	    }
# 	}
    }
}
my $Msize = $size / 1024 / 1024;
print "$root $size $Msize\n";
print "OTA $size_ota\n";
print "PV1 $size_pv1\n";
print "PV2 $size_pv2\n";
print "MD $size_md\n";

