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


use Getopt::Std;
use DateTime;
use PS::IPP::Config;

getopts('D:',\%opt);

my $count_list = shift;
my $ipprc = PS::IPP::Config->new("GPC1");
my $nebcon = $ipprc->nebulous();


if (exists($opt{D})) {
    my $datestring = $opt{D};
    my $usage_data = "neb:///ipp_diskspace/${datestring}/usage_report.dat";
    my $real_file  = $ipprc->file_resolve($usage_data);
    open(DATA,$real_file) || die "Cannot find usage_report.dat\n";
}

while(<DATA>) {
    chomp;
    if ($_ =~ /^\s+/) {
	next;
    }
    ($stage,$state,$label,undef,$rate,$count) = split /\s+/;
    $data{$stage}{$state}{L} = $label;
    $data{$stage}{$state}{R} = $rate;
    $cumulative{$label} = 0;
}
$total = 0;
open(D,"$count_list");
while(<D>) {
    chomp;
    ($stage,$state,$count) = split /\s+/;
    unless(exists($data{$stage}{$state})) {
	next;
    }
    $label = $data{$stage}{$state}{L};
    $VpN   = $data{$stage}{$state}{R};
    $V = $count * $VpN;
    printf("%-8s %-18s %-10s %14.4f %12.6f %9d\n",
	   $stage,$state,$label,$V,$VpN,$count);
    $cumulative{$label} += $V;
    $total += $V;
}
foreach $l (keys %cumulative) {
    print STDERR "\t$l  \t$cumulative{$l}\n";
}
print STDERR "\tSUM      \t$total\n";

    
	


__DATA__
CAMERA   cleaned            PRODUCT          296.4215     0.171540      1728
CAMERA   drop               PRODUCT            0.1375     0.000671       205
CAMERA   full               PRODUCT        96885.7234     0.388503    249382
CAMERA   goto_purged        TRANSIENT         54.3905     0.388503       140
CAMERA   new                PRODUCT            0.7720     0.000671      1151
CAMERA   purged             PRODUCT            0.1715     0.171540         1
CAMERA   wait               PRODUCT            0.7036     0.000671      1049
CHIP     cleaned            PRODUCT        26777.0553     0.001840  14549732
CHIP     error_cleaned      PRODUCT         2163.6842     0.105432     20522
CHIP     error_scrubbed     PRODUCT          232.8603     0.039798      5851
CHIP     full               TRANSIENT      39127.3368     0.119770    326686
CHIP     purged             PRODUCT          514.9939     0.001840    279830
CHIP     scrubbed           PRODUCT           11.0294     0.001840      5993
CHIP     update             TRANSIENT         17.0801     0.105432       162
DESTREAK censored           PRODUCT            0.6473     0.029424        22
DESTREAK cleaned            PRODUCT           36.6759     0.000076    482521
DESTREAK drop               PRODUCT            0.0000     0.000000      1045
DESTREAK error_cleaned      PRODUCT            7.9444     0.029424       270
DESTREAK failed_revert      PRODUCT            0.0000     0.000000         1
DESTREAK full               TRANSIENT       4597.8025     0.029424    156261
DESTREAK goto_cleaned       TRANSIENT          9.7099     0.029424       330
DESTREAK new                PRODUCT            0.0000     0.000000        18
DESTREAK new.hold           PRODUCT            0.0000     0.000000        10
DESTREAK restored           PRODUCT            4.2959     0.029424       146
DESTREAK todebug            PRODUCT            0.0294     0.029424         1
DESTREAK update             TRANSIENT        351.6446     0.029424     11951
DESTREAK wait               PRODUCT            0.0000     0.000000         4
DETREND  drop               PRODUCT           18.5816     0.120660       154
DETREND  register           PRODUCT           89.4761    44.738033         2
DETREND  stop               PRODUCT        15692.5025    49.976122       314
DETREND  wait               PRODUCT           38.3698     0.120660       318
DIFF     cleaned            PRODUCT        20727.1251     0.002301   9009076
DIFF     full               TRANSIENT      48536.1541     0.147039    330091
DIFF     purged             PRODUCT          362.3386     0.002301    157491
DIFF     update             TRANSIENT        159.2223     0.125967      1264
DIST     cleaned            PRODUCT           12.6523     0.000027    471189
DIST     drop               PRODUCT            0.0000     0.000000      9070
DIST     full               TRANSIENT      10451.6987     0.052522    198998
DIST     goto_cleaned       TRANSIENT         27.8890     0.052522       531
DIST     new                PRODUCT            0.0000     0.000000         2
MAGIC    cancel             PRODUCT            0.0040     0.004025         1
MAGIC    censored           PRODUCT            0.3139     0.004025        78
MAGIC    drop               PRODUCT            0.0000     0.000000       438
MAGIC    full               PRODUCT          771.2421     0.004025    191634
MAGIC    new                PRODUCT            0.0000     0.000000       297
MAGIC    wait               PRODUCT            0.0000     0.000000         9
RAW      NULL               PERMANENT          0.0000     0.000394         0
RAW      check_burntool     PERMANENT         19.4277     0.043269       449
RAW      compressed         PERMANENT        720.8267     0.048541     14850
RAW      corrupt            PERMANENT          0.0174     0.000471        37
RAW      error_compressed   PERMANENT        292.1491     0.043346      6740
RAW      error_lossy        PERMANENT        120.6904     0.027103      4453
RAW      full               PERMANENT     607420.2114     0.043346  14013433
RAW      goto_lossy         PERMANENT        130.8652     0.048541      2696
RAW      keep               PERMANENT      24975.4561     0.043346    576194
RAW      lossy              PERMANENT      24720.0428     0.005666   4363026
RAW      missing            PERMANENT          0.0099     0.000394        25
RAW      pending_burntool   PERMANENT       9220.6406     0.043269    213101
RAW      roc                PERMANENT      73422.4326     0.021908   3351366
STACK    cleaned            PRODUCT           86.8525     0.003704     23447
STACK    drop               PRODUCT            0.0000     0.000000      8862
STACK    error_cleaned      PRODUCT          914.9882     0.164241      5571
STACK    full               PRODUCT        53913.0776     0.175869    306553
STACK    goto_cleaned       TRANSIENT         14.7747     0.175889        84
STACK    goto_purged        TRANSIENT         26.9356     0.164241       164
STACK    new                PRODUCT            0.0000     0.000000     12917
STACK    purged             PRODUCT           18.2914     0.003704      4938
STACK    wait               PRODUCT            0.0000     0.000000       120
WARP     cleaned            PRODUCT         6547.3188     0.000306  21418513
WARP     drop               PRODUCT            0.0000     0.000000         1
WARP     error_cleaned      PRODUCT         2210.2057     0.066446     33263
WARP     error_scrubbed     PRODUCT            4.4519     0.066446        67
WARP     full               TRANSIENT      36791.1577     0.066446    553697
WARP     purged             PRODUCT           18.1011     0.000306     59215
WARP     scrubbed           PRODUCT           11.6047     0.000306     37963
