#!/usr/bin/perl

use LWP::Simple;
use Alvin::BatchLoad;
use strict 'vars';
use Getopt::Std;
use File::stat;
use LWP::UserAgent;
use Time::Local;
use Date::Format;
use HTTP::Response;
use File::Basename;
use Filesys::DiskUsage qw/du/;

package main;

my %opt;

my %prettyNames = (
    'banc' => 'Bancroft',
    'envi' => 'Environmental Design',
    'eart' => 'Earth Sciences',
    'eda'  => 'EDA',
    'dm'   => 'Doe/Moffitt',
    'engi' => 'Engineering',
    'irle' => 'IRLE',
    'eal'  => 'EAL',
    'edpsych' => 'Ed/Psych',
    'busi' => 'Business',
    'ucla' => 'UCLA',
    'math' => 'Math',
    'bios' => 'Biosciences',
    'phys' => 'Physics',
    'law'  => 'Law',
    'ahc'  => 'Art History',
    'anth' => 'Anthropology',
    'mrc'  => 'Media Resource Center',
    'igs'  => 'IGS',
    'music'  => 'Music',
    'roho'  => 'ROHO',
    'uci'  => 'UCI',
    'ucla'  => 'UCLA',
    );
#-------------------------------------------------------------------------

getopts('o:i:d:s:', \%opt);
if ( !defined $opt{'o'} or (!defined $opt{'s'} )) {
    printHelp();
    exit;
}

my $outFile = $opt{'o'};
my ($name, $path, $suffix ) = fileparse( $outFile, "\.[^.]*" );
my $spreadsheet = $opt{'s'};

# check if spreadsheet exist
die "spreasheet $spreadsheet does not exist\n" unless -e $spreadsheet;

my($day, $month, $year) = (localtime)[3,4,5];
$month = sprintf '%02d', $month+1;
$day   = sprintf '%02d', $day;
$year  = $year+1900;
my $today = "$month$day$year";

# create a log
my $dateString = localtime();
open LOG, ">$outFile.LOG" or die $!;
print LOG "$dateString START\n";

my $data = new Alvin::BatchLoad::DataSource (filename => $spreadsheet, sheetno => 1, header_rowno => 1);

my %listDirs = ();  # these are Dir that are on the public Dir list
foreach my $rec ($data->records) {
    my $current = [];
    push (@$current, $rec->get('directory'));
    push (@$current, $rec->get('owner'));
    # put the value under directory
    $listDirs{ $rec->get('directory') } = $current;
}

%listDirs = processFile(%listDirs);
printHash(%listDirs); # debug

# Build the output
open OUT, ">$outFile.csv" or die $!;
print OUT "Directory, Owner, File Count, DU count\n";
foreach my $key ( keys %listDirs )  {
    print OUT "$listDirs{$key}->[0],$listDirs{$key}->[1],$listDirs{$key}->[2],$listDirs{$key}->[3]\n"; # comma delimited
}
close OUT;

# Build the Summary for all
my %summary = (); 
foreach my $key ( keys %listDirs )  {
    my $owner = $listDirs{$key}->[1];
    #print "owner: $owner\n"; #DEBUG
    if (exists $summary{$owner}) {  
       # if the owner already exists add the current DU and file count to the one already present
	$summary{$owner}->[0] +=  1; # add one to the projects count
	$summary{$owner}->[1] +=  $listDirs{$key}->[2]; 
	$summary{$owner}->[2] +=  $listDirs{$key}->[3]; 
    }
    else {
	my $current = [];
	push (@$current, 1);
	push (@$current, $listDirs{$key}->[2]);
	push (@$current, $listDirs{$key}->[3]);
	$summary{$owner} = $current;
    }
}

printHash(%summary); #DEBUG

my $summaryFile = (join('',$outFile,'_Summary','.csv'));
open SUMMARY, ">$summaryFile" or die $!;
print SUMMARY "Library,No of Projects,Megabytes,Item Count\n";

foreach my $key ( keys %summary )  {
    my $name = $key;
    if (exists $prettyNames{$key}) {
	$name = $prettyNames{$key};
    }
    print SUMMARY "$name,$summary{$key}->[0],$summary{$key}->[1],$summary{$key}->[2]\n";
}

$dateString = localtime();
print LOG "$dateString END\n";
close LOG;

print "Done with script\n";

#--------------- SUBS ---------------------------------------------
sub printHelp() {
    print "usage: countFiles.pl -o <outputFIle>  -s <Spreadsheet>\n\nWhere -o output use <DateRange>.csv\n<Spreadsheet> is a .xls file with the list of directories to count\n";
}
#---------------------
sub processFile {
    my (%localDirs) = @_;
    my $thisDir;
    my $cmd;
    my $basePA = "/nfsDirs/data/pa/";
    my $baseDA = "/nfsDirs/data2/da/";

    foreach my $key (keys %localDirs) {
	print "Now on directory: $key\n"; #DEBUG
	my $fileCount = 0;
	my $duCount = 0; 
	# get the current array, the key = directory
	# count files and du in /data/pa 
	my $dirPA = $basePA.$key; 
	if (-d $dirPA) {
	    $fileCount = trim (`/usr/bin/find -L $dirPA -name mets -prune -o -type f | wc -l`);
	    my @duLine = split (/\s+/, `/usr/bin/du -smL $dirPA`);
	    $duCount = trim($duLine[0]);
	    #my $duCount = du($dirPA);
	}
	else {
	    print LOG "Directory $dirPA does not exist\n";
	}
	# count files and du in /data2/da
	my $dirDA = $baseDA.$key; 
	if (-d $dirDA) {
	    my $tmpFileCount= trim(`/usr/bin/find -L $dirDA -name mets -prune -o -type f | wc -l`);
	    $fileCount = $fileCount + $tmpFileCount;
	    my @tmpDuLine = split ( /\s+/, `/usr/bin/du -smL $dirDA`);
	    $duCount = trim($tmpDuLine[0]) + $duCount;
	    #$duCount = $duCount + du($dirDA);
	}
	else {
	    print LOG "Directory $dirDA does not exist\n";
	}
	print "dir: $dirDA - $fileCount   &  $duCount\n";  # DEBUG
	# push it on the array an back into %localDirs
	#my $thisArray = %localDirs{$key}; 
	#push (@$thisArray, $fileCount);
	#push (@$thisArray, $duCount);
	#$localDirs{$key} = $thisArray;
	push @{$localDirs{$key}}, $duCount;
	push @{$localDirs{$key}}, $fileCount;
    }
    return %localDirs;
}
#------------------------------------
sub du {

}
#------------------------------------
sub printHash1 {
    my %thisHash = @_;
    print "Hash @_\n";
    foreach my $key ( keys %thisHash )  {
	foreach (@{$thisHash{$key}}) {
	    print "$_\n";
	}
    }
}

#------------------------------------
sub printHash {
    my %thisHash = @_;
    foreach my $key ( keys %thisHash )  {
	print "$key,$thisHash{$key}->[0], $thisHash{$key}->[1], $thisHash{$key}->[2], $thisHash{$key}->[3]\n";
    }
}
#--------------------------------------------------
# misc. trimming stuff
sub ltrim { my $s = shift; $s =~ s/^\s+//;       return $s };
sub rtrim { my $s = shift; $s =~ s/\s+$//;       return $s };
sub  trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
