#! /usr/local/bin/perl -w

#use DBI;
use Nebulous::Client;
# 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";
# }

# my $dbh = DBI->connect(
#     "DBI:mysql:database=$db:host=$dbhost",
#     $dbuser,
#     $dbpass,
#     {
#         RaiseError => 1,
#         PrintError => 0,
#         AutoCommit => 0,
#     },
#     );
my $server;
$server = $ENV{'NEB_SERVER'} unless $server;


my $neb = Nebulous::Client->new(
    proxy => "$server",
    );



my @pb = ();
open(PB,"pb.dat");
while (<PB>) {
    chomp;
    if ($_ =~ /path_base/) {
	next;
    }
    push @pb, "${_}%";
}
close(PB);

foreach my $path_base (@pb) {
    my $size = 0;
#    print "$path_base $size\n";
#    die;
    my $keys = $neb->find_objects_wildcard($path_base);

    foreach my $key (@{ $keys }) {
	my $uris;
	eval {$uris = $neb->find_instances($key) };
	if ($@) { 
	    print "# $key NO AVAILABLE\n";
	    next;
	}
	my $count = $#{ $uris } + 1;
	my $psize = 0;
	if (defined ($uris)) {
	    my $file = URI->new(@$uris[0])->file;
	    my @stat = stat($file);
	    $psize = $count * $stat[7];
	}
	print "# $key $count $psize\n";
	$size += $psize;
    }
    print "$path_base $size\n";
}

