#!/opt/ActivePerl/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;

#-------------------------------------------------------------------------

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 $outRaw = join('',$path,$name, ".raw");

open RAW, ">$outRaw" or die $!;
print RAW "start\n";

my $spreadsheet = $opt{'s'};

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;
}

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


# 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
}

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

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 = "/data/pa/";
    my $baseDA = "/data2/da/";

    foreach my $key (keys %localDirs) {
	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 -skL $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 -skL $dirDA`);
	    $duCount = trim($tmpDuLine[0]) + $duCount;
	    #$duCount = $duCount + du($dirDA);
	}
	else {
	    print LOG "Directory $dirDA does not exist\n";
	}

	# 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}}, $fileCount;
	push @{$localDirs{$key}}, $duCount;
    }
    return %localDirs;
}
#------------------------------------
sub printHash1 {
    my %thisHash = @_;
    foreach my $key ( keys %thisHash )  {
	foreach (@{$thisHash{$key}}) {
	    print "$_\n";
	}
    }
}

#------------------------------------
sub printHash {
    my %thisHash = @_;
    foreach my $key ( keys %thisHash )  {
	print "$key is $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 };
