#!/usr/bin/perl
use File::Copy;
use File::Path;
use Getopt::Long;
use Time::Local;
use Date::Manip;
use Cwd;

system("mkdir -p /tmp/btplots");

my $dbtimelog = "/tmp/btplots/dologerrors.log";
#my $dbtimelog = "./dologerrors.log";
#print "Trying to open logfile... $dbtimelog\n";
open(OLDOUT, ">&STDOUT");
open(OLDERR, ">&STDERR");
open(STDOUT, ">$dbtimelog") or die "Could not open $dbtimelog: $!";
open(STDERR, ">&STDOUT");
select(STDERR);
 $| = 1;
 select(STDOUT);
  $| = 1;

chdir("/p/condor/public/html/developers/download_stats");

my $tooldir = "/p/condor/home/tools/build-test-plots/";
#my $tooldir = ".";
my $datalocation = "/p/condor/public/license/";
#my @datafiles = ( "sendfile-v6.0", "sendfile-v6.1", "sendfile-v6.2", "sendfile-v6.3", "sendfile-v6.4", "sendfile-v6.5", "sendfile-v6.6", "sendfile-v6.7", "sendfile-v6.8", "sendfile-v6.9","sendfile-v7.0", "sendfile-v7.1" );
#my @datafiles = ( "sendfile-v6.6", "sendfile-v6.7", "sendfile-v6.8", "sendfile-v6.9","sendfile-v7.0", "sendfile-v7.1" );
#my @datafiles = ( "sendfile-v6.8", "sendfile-v6.9", "sendfile-v7.0", "sendfile-v7.1" );
my @datafiles = ( "problem-v7.6", "problem-v7.7" );
#my @datafiles = ( "problem-v7.0", "problem-v7.1" );
#my @datafiles = ( "sendfile-v6.8" );
my @datapoints; 
my %errors = (
	"dlprob_no_prob" => 0,
	"dlprob_file_not_found" => 0,
	"dlprob_timed_out" => 0,
	"dlprob_hung" => 0,
	"dlprob_failed_restart" => 0,
	"dlprob_corrupted" => 0,
);

GetOptions (
		'help' => \$help,
		'plot' => \$plot,
);

my %months = (
    "Jan" => "January",
    "Feb" => "February",
    "Mar" => "March",
    "Apr" => "April",
    "May" => "May",
    "Jun" => "June",
    "Jul" => "July",
    "Aug" => "August",
    "Sep" => "September",
    "Oct" => "October",
    "Nov" => "November",
    "Dec" => "December",
);

my %previousmonth = (
    "Feb" => "January",
    "Mar" => "February",
    "Apr" => "March",
    "May" => "April",
    "Jun" => "May",
    "Jul" => "June",
    "Aug" => "July",
    "Sep" => "August",
    "Oct" => "September",
    "Nov" => "October",
    "Dec" => "November",
    "Jan" => "December",
);

my %nextmonth = (
    "Dec" => "January",
    "Jan" => "February",
    "Feb" => "March",
    "Mar" => "April",
    "Apr" => "May",
    "May" => "June",
    "Jun" => "July",
    "Jul" => "August",
    "Aug" => "September",
    "Sep" => "October",
    "Oct" => "November",
    "Nov" => "December",
);

my %monthdays = (
    "Jan" => 31,
    "Feb" => 28,
    "Mar" => 31,
    "Apr" => 30,
    "May" => 31,
    "Jun" => 30,
    "Jul" => 31,
    "Aug" => 31,
    "Sep" => 30,
    "Oct" => 31,
    "Nov" => 30,
    "Dec" => 31,
);

my %nummonths = (
    "Jan" => 1,
    "Feb" => 2,
    "Mar" => 3,
    "Apr" => 4,
    "May" => 5,
    "Jun" => 6,
    "Jul" => 7,
    "Aug" => 8,
    "Sep" => 9,
    "Oct" => 10,
    "Nov" => 11,
    "Dec" => 12,
);

my %monthtoregnum = (
    "January" => 1,
	"February" => 2,
	"March" => 3,
    "April" => 4,
    "May" => 5,
    "June" => 6,
    "July" => 7,
    "August" => 8,
    "September" => 9,
    "October" => 10,
    "November" => 11,
    "December" => 12,
);

my %monthtonum = (
    "January" => 0,
	"February" => 1,
	"March" => 2,
    "April" => 3,
    "May" => 4,
    "June" => 5,
    "July" => 6,
    "August" => 7,
    "September" => 8,
    "October" => 9,
    "November" => 10,
    "December" => 11,
);

my %monthnums = (
    1 => "Jan",
    2 => "Feb",
    3 => "Mar",
    4 => "Apr",
    5 => "May",
    6 => "Jun",
    7 => "Jul",
    8 => "Aug",
    9 => "Sep",
    10 => "Oct",
    11 => "Nov",
    12 => "Dec",
);

my $date = "";
my $lastdate = "";
my $firstdate = "";
my $lastmonth = "";
my $lastyear = "";
my $lastday = "";
my $thisyear = "";

if ( $help )    { help() and exit(0); }

foreach $log (@datafiles) {
	ScanDownloadErrors ("$datalocation" . "$log",\@datapoints);
	DropData($log);

	my $currenttitle = $log . " Breakdown";
	my $uniquegraphcmd = "$tooldir/make-graphs-dwnloads --firstdate \"$firstdate\" --detail errorbreakdown --daily --input \"$log.data\" --output \"$log.png\" --title \"$currenttitle\"";
	system("$uniquegraphcmd");

}
exit(0);

sub ScanDownloadErrors 
{
	my $log = shift;
	my $arrayref = shift;
	my $datafile = "$log";
	print "examine $datafile\n";
	$newrecord = 0;
	@{$arrayref} = ();

	open(LOGDATA,"<$datafile") || die "logdata bad: $!\n";
	my $line = "";
	my @error = ();
	my $skiprecord = 1;
	my $firstreset = 1;

	$date = "";
	$lastdate = "";
	$firstdate = "";

	while(<LOGDATA>) {
		chomp;
		$line = $_;
		if($line =~ /^(\d+)\s+(\d+\.\d+\.\d+\.\d+).*(dlprob.*)$/) {
			#print "$1 $2\n";
			my $errorcode = $3;
			my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($1);
			$mon += 1;
			$year += 1900;
			#print "$months{$monthnums{$mon}} $mday $year $errorcode\n";
			$date = "$months{$monthnums{$mon}} $mday $year";
			$thisyear = $year;
			if($lastdate eq "") {
				$lastdate = $date;
				$firstdate = $date;
				$lastyear = $year;
				$lastday = $mday;
				$lastmonth = $monthnums{$mon};
			}
			if($lastdate ne $date) {
				my $save = "";
				print "Last was <$lastdate> new is <$date>\n";
				$save = "$lastdate, $errors{\"dlprob_no_problem\"}, $errors{\"dlprob_file_not_found\"}, $errors{\"dlprob_timed_out\"}, $errors{\"dlprob_hung\"}, $errors{\"dlprob_failed_restart\"}, $errors{\"dlprob_corrupted\"}";
				push @{$arrayref}, $save;
				print "Saved <<<$save>>>\n";
				fillholes($monthnums{$mon}, $mday, $year, $arrayref);
				$lastdate = $date;
				$lastday = $mday;
				$lastyear = $year;
				$lastmonth = $monthnums{$mon};
				%errors = (
					"dlprob_no_problem" => 0,
					"dlprob_file_not_found" => 0,
					"dlprob_timed_out" => 0,
					"dlprob_hung" => 0,
					"dlprob_failed_restart" => 0,
					"dlprob_corrupted" => 0,
				);
			}
			$errors{$errorcode} += 1;
		}
    }
}

# fillholes($months{$monthnums{$mon}}, $mday);
sub fillholes
{
	my $thismonth = shift;
	my $thisday = shift;
	my $thisyear = shift;
	my $arrayref = shift;
	my $date = "";
	my $save = "";

	print "Want to fill data between $lastmonth $lastday and $thismonth $thisday\n";
	if($lastmonth eq $thismonth) {
		my $currentday = $lastday + 1;
		while($currentday < $thisday) {
			$date = "$months{$thismonth} $currentday $thisyear";
			$save = "$date, 0,0,0,0,0,0";
			print "Dropping: $save\n";
			push @{$arrayref}, $save;
			$currentday += 1;
		}
		print "do a day fill\n";
	} else {
		print "fill to next month\n";
		# first fill last month
		my $currentday = $lastday + 1;
		my $endday = $monthdays{$lastmonth};
		while($currentday <= $endday) {
			$date = "$months{$lastmonth} $currentday $lastyear";
			$save = "$date, 0,0,0,0,0,0";
			print "Dropping: $save\n";
			push @{$arrayref}, $save;
			$currentday += 1;
		}
		# then fill to this day
		$currentday = 1;
		$endday = $thisday;
		while($currentday < $endday) {
			$date = "$months{$thismonth} $currentday $thisyear";
			$save = "$date, 0,0,0,0,0,0";
			print "Dropping: $save\n";
			push @{$arrayref}, $save;
			$currentday += 1;
		}
	}
}

sub DropData 
{
	my $errorlog = shift;
	my $outfile = "$log.data";

	open(DF,">$outfile") or die "Could not create data file for $log:$!\n";
	foreach  $key (@datapoints) {
			print DF  "$key\n";
	}
	close(DF);
}

sub help 
{
    print "Usage: writefile.pl --megs=#
Options:
	[-h/--help]				See this
	[-d/--data]				file holding test data
	\n";
}
