#! /usr/bin/env perl
use warnings;

use Sys::Hostname;
use PS::IPP::Config;
%stages = ("RAW" => '\.ota',
	   "DIST" => 'dist',
	   "DESTREAK" => 'destreak',
	   "CHIP" => '\.ch\.',
	   "CAMERA" => '\.cm\.',
	   "WARP" => '\.wrp\.',
	   "DIFF" => '\.dif\.',
	   "STACK" => '\.stk\.',
	   "MAGIC" => '\.mgc\.',
	   "DETREND" => '\.det'
    );

@products = ('\.log','\.trace','\.mdc$','\.stats$',
	     '\.smf$','\.cmf$',
	     '\.burn\.tbl$','\.tgz$',
	     '\.b1\.fits$','\.b2\.fits',
	     '\.b1\.jpg$','\.b2\.jpg$',
	     '\.b1\.jpeg$','\.b2\.jpeg$',
	     '\.png$',
	     '\.mdl\.fits$','\.ptn$','\.psf$',
	     '\.skycell$','kernel$',
	     '\.mk\.fits$','\.mask\.fits$','\.wt\.fits$',
	     '\.exp\.fits$','\.num\.fits$','\.expwt\.fits$',
	     '\.fits\.fz$',
	     '\.fits$');

%product_names = ('\.log' => "LOG",'\.trace' => "TRACE",'\.mdc$' => "MDC",'\.stats$' => "STATS",
		  '\.smf$' => "CATALOG",'\.cmf$' => "CATALOG",
		  '\.burn\.tbl$' => "BTTABLE",'\.tgz$' => "BUNDLE",
		  '\.b1\.fits$' => "B1FITS",'\.b2\.fits' => "B2FITS",
		  '\.b1\.jpg$' => "B1JPG",'\.b2\.jpg$' => "B2JPG",
		  '\.b1\.jpeg$' => "B1JPG",'\.b2\.jpeg$' => "B2JPG",
		  '\.png$' => "PNG",
		  '\.mdl\.fits$' => "MDL",'\.ptn$' => "PTN" ,'\.psf$' => "PSF",
		  '\.skycell$' => "SKYCELL",'kernel$' => "KERNEL",
		  '\.mk\.fits$' => "MASK",'\.mask\.fits$' => "MASK",'\.wt\.fits$' => "WEIGHT",
		  '\.exp\.fits$' => "MISCFITS",'\.num\.fits$' => "MISCFITS",'\.expwt\.fits$' => "MISCFITS",
		  '\.fits\.fz$' => "FITSCOMP",
		  '\.fits$' => "FITS");

my $hostname = hostname();
my $neb = shift(@ARGV);
unless(defined($neb)) {
    $neb = "${hostname}.0";
}
my $ipprc = PS::IPP::Config->new("GPC1");
my $nebcon = $ipprc->nebulous();

my @lt = gmtime;
$dt = DateTime->now;
$dt->subtract( days => $lt[6]);
my $datestring = $dt->ymd;
my $outfile = "neb:///ipp_diskspace/${datestring}/${neb}.neb_usage.dat";
my $out_fh = $ipprc->file_create_open($outfile);

my $neb_dir = "/data/${neb}/nebulous/";

my $total_volume = 0;
my $used_volume  = 0;
open(DF,"df -B 1 /export/${neb}/ |") || die "Cannot run df\n";
while(<DF>) {
    chomp;
    if ($_ =~ /${neb}/) {
	$used_volume = (split /\s+/, $_)[2];
    }
}
close(DF);

open(FIND,"find $neb_dir -type f -printf \"%f %s\n\" |") || die "Cannot run find.\n";


while (<FIND>) {
    chomp;
    ($file,$size) = split /\s+/;
    
    my $stage = 'OTHER';
    my $product = 'UNKNOWN';

    foreach $k (keys %stages) {
	if ($file =~ /$stages{$k}/) {
	    $stage = $k;
	    last;
	}
    }
    foreach $k (@products) {
	if ($file =~ /$k/) {
	    $product = $product_names{$k};
	    unless (defined($product)) {
		print "$k $product\n";
		die;
	    }
	    last;
	}
    }
    
    $count{$stage}{$product} += 1;
    $volume{$stage}{$product} += $size;
    $total_volume += $size;
}
close(FIND);




foreach $s (sort (keys %count)) {
    foreach $p (sort (keys %{ $count{$s} })) {
	print $out_fh "$s $p $count{$s}{$p} $volume{$s}{$p}\n";
    }
}
if ($used_volume > 0) {
    my $external_volume = $used_volume - $total_volume;
    print $out_fh "EXTERNAL UNKNOWN 1 $external_volume\n";
}
close($out_fh);


	
