#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Tiny;
use Path::Class;
use Date::Calc qw(Add_Delta_Days);
use autodie;

my $draftArgv = shift(@ARGV);

if (!$draftArgv) {
  print "\nUsage: doMinutes.pl YYYY-MM-DD(Draft) [YYYY-MM-DD(Approved)]\n";
  die();
}

## setup date stuff
my %monthName     =  qw (01   January
                         02   Febuary
                         03   March
                         04   April
                         05   May
                         06   June
                         07   July
                         08   August
                         09   September
                         10   October
                         11  November
                         12  December                  
                       );
sub leadingZero {
  # add leading zeros to months and days less than 10
  my $dateStr = shift(@_);
  if($dateStr !~ m/^[0-3][0-9]/) {
    $dateStr = "0".$dateStr;
  }
  return $dateStr;
}

my @draftDate = split(/-/, $draftArgv);
my $draftYear = $draftDate[0];
my $draftMonth = leadingZero($draftDate[1]);
my $draftDay = leadingZero($draftDate[2]);

my ($approvedYear, $approvedMonth, $approvedDay);
my ($nextYear, $nextMonth, $nextDay);

my $approvedArgv = shift(@ARGV);
if(!$approvedArgv) {
  ($approvedYear, $approvedMonth, $approvedDay) = Add_Delta_Days($draftYear, $draftMonth, $draftDay, -14);
  $approvedMonth = leadingZero($approvedMonth); 
  $approvedDay = leadingZero($approvedDay); 
} else {
  my @approvedDate = split(/-/, $approvedArgv);
  $approvedYear = $approvedDate[0];
  $approvedMonth = leadingZero($approvedDate[1]);
  $approvedDay = leadingZero($approvedDate[2]);
}

my $nextArgv = shift(@ARGV);
if(!$nextArgv) {
  ($nextYear, $nextMonth, $nextDay) = Add_Delta_Days($draftYear, $draftMonth, $draftDay, 14);
  $nextMonth = leadingZero($nextMonth); 
  $nextDay = leadingZero($nextDay); 
} else {
  my @nextDate = split(/-/, $nextArgv);
  $nextYear = $nextDate[0];
  $nextMonth = leadingZero($nextDate[1]);
  $nextDay = leadingZero($nextDate[2]);
}

print "Drafting minutes for: $draftYear-$draftMonth-$draftDay.\n";  
print "Approving minutes for: $approvedYear-$approvedMonth-$approvedDay.\n";  

my ($dir, $file, $file_handle);

## fetch the drafted minutes and write them to local file
my $filename = "https://www.w3.org/$draftYear/$draftMonth/$draftDay-webappsec-minutes.html";
my $draftFilename = "$draftYear-$draftMonth-$draftDay-webappsec-minutes.html";
if(-e "./draft-minutes" . $draftFilename) {
  print "Found draft file already.";
} else {
  my $response = HTTP::Tiny->new->get($filename);
  die "Failed: to retrieve $filename\n" unless $response->{success};
  print "Retrieved " . length($response->{content}) . " bytes of $filename.\n";
  $dir = dir("./draft-minutes");
  $file = $dir->file($draftFilename);
  $file_handle = $file->openw();
  $file_handle->print($response->{content});
  print "Wrote $file\n";
  system("cvs add $file");
}

## fixup approved minutes
my $approvedExists = "true";
$dir = dir("./draft-minutes");
my $approvedFilename = "$approvedYear-$approvedMonth-$approvedDay-webappsec-minutes.html";

if(-e "./draft-minutes/".$approvedFilename) {

  $file = $dir->file($approvedFilename);
  $file_handle = $file->openr();

  my $approved;

  while(my $line = $file_handle->getline() ) {
    if($line =~ m/<div class="diagnostics">/) {
      $approved .= "</body>\n</html>";
      last;
    }
    if($line =~ m/- DRAFT -/) { next; };
    $approved .= $line; 

  } 

  #print $approved;

  $dir = dir("./minutes");
  print($dir."\n");
  print($approvedFilename."\n");
  $file = $dir->file($approvedFilename);
  $file_handle = $file->openw();
  $file_handle->print($approved);
  print "Wrote $file\n";
  system("cvs add $file");

} else { 
  print("WARNING: No draft minutes found for approval at ./draft-minutes/$approvedFilename \n");
  $approvedExists = "false";
}

## fixup Overview.html
my $fullDraftFilename = "https://www.w3.org/2011/webappsec/draft-minutes/$draftFilename";
my $fullApprovedFilename = "https://www.w3.org/2011/webappsec/minutes/$approvedFilename"; 

$dir = dir(".");
$file = $dir->file("Minutes.html");
$file_handle = $file->openr();

my $overview;

while(my $line = $file_handle->getline() ) {

  if($approvedExists 
  && $line =~ "DRAFT Teleconference Minutes, $approvedYear-$approvedMonth-$approvedDay"){
    next; 
  }

  $overview .= $line;

  if($line =~ m/id="minutes"/) {
  
     $overview .= "<p>DRAFT Teleconference Minutes, $draftYear-$draftMonth-$draftDay: <a href='$fullDraftFilename'>$fullDraftFilename</a></p>\n";
     $overview .= "<p>Teleconference Minutes, $approvedYear-$approvedMonth-$approvedDay: <a href='$fullApprovedFilename'>$fullApprovedFilename</a></p>\n";

  }

}

$file_handle = $file->openw(); 
$file_handle->print($overview);
print("Fixed up $file\n");

#print($overview);

print <<EOF;
############
#
# Now run:

cvs commit -m 'New draft minutes for $draftYear-$draftMonth-$draftDay, approved minutes for $approvedYear-$approvedMonth-$approvedDay, updated Overview.html' ./draft-minutes/$draftFilename ./minutes/$approvedFilename Overview.html

#
###########
EOF
