#!/usr/bin/perl

use Date::Parse;
use POSIX qw(strftime);

our $DEBUG = 0;
our $DRYRUN = 0;

sub sanitizeDate($) {
  my $date = shift;
  if (str2time($date)) {
    return strftime("%F %T %z", localtime str2time($date));
  } else {
    return $date;
  }
}

sub findIndex($@) {
  my ($needle, @haystack) = @_;
  foreach my $i (0..$#haystack) {
    if ($haystack[$needle] eq $needle) {
      return $i;
    }
  }
  return undef;
}

open(LOG, ">>", "update-scripts-version.log") or warn "Couldn't open log for writing";

open(SUCCESS, ">>", "success");
open(FAILURE, ">>", "failed");

foreach my $file (@ARGV) {
  unless (open(OLD, "<", $file)) { warn "Can't open $file for reading"; next; }
  my @lines = <OLD>;
  chomp @lines;
  close(OLD);
  my $date = sanitizeDate($lines[0]);
  my $pkgfile = $lines[1];
  if ($lines[8] =~ m/Scripts security update to Joomla/) {
    $date = sanitizeDate($lines[10]);
    $pkgfile = $lines[11];
  }
  unless (
	  $pkgfile =~ m|File: \`/mit/scripts/deploy([^']+?)\' \-\> \`([^'-]+)-([^']*)\.tar\.gz\'|# ` 
	 ) {
    warn "Can't extract package from $file";
    next;
  }
  my $pkgfile = $1;
  my $package = $2;
  my $version = $3;
  if ($package eq "gallery") {
    $package = "gallery2";
    $version =~ s/-typical$//;
  }
  $out = <<"EOF";
$date
unknown
/afs/athena.mit.edu/contrib/scripts/deploy$pkgfile
/afs/athena.mit.edu/contrib/scripts/deploy/$package-$version
EOF
  if ($lines[-1] eq "mediawiki-1.11.0") {
    my $date = sanitizeDate($lines[-3]);
    $version = "1.11.0";
    $out .= <<"EOF";

$date
unknown
/afs/athena.mit.edu/contrib/scripts/deploydev/mediawiki-1.5.8-to-1.11.0
/afs/athena.mit.edu/contrib/scripts/deploy/mediawiki-1.11.0
EOF
  }
  my @upgrades = grep { m|^\+\+\+| } @lines;
  foreach my $upgrade (@upgrades) {
    if ($upgrade =~ m|^\+\+\+ Upgraded to (.+?) on (.+?): (.+)$|) {
      $version = $1;
      my $upgrade = $3;
      $date = sanitizeDate($2);
      $out .= <<"EOF";

$date
unknown
$upgrade
/afs/athena.mit.edu/contrib/scripts/deploy/$package-$version
EOF
    } elsif ( $upgrade =~ m|^\+\+\+ Scripts security update to Joomla (.+)\.$| ) {
      $version = $1;
      $date = sanitizeDate($lines[0]);
      $out .= <<"EOF";

$date
unknown
$upgrade
/afs/athena.mit.edu/contrib/scripts/deploy/$package-$version
EOF
    }
  }
  $date = sanitizeDate(scalar localtime);
  $out .= <<"EOF";

$date
quentin\@QUICHE-LORRAINE.MIT.EDU
/afs/athena.mit.edu/contrib/scripts/deploydev/updates/update-scripts-version.pl
/afs/athena.mit.edu/contrib/scripts/deploy/$package-$version
EOF
  unless ($DRYRUN) {
    my $success = 1;
    unless (rename($file, $file.".oldversion")) {
      warn "ERROR: Can't move $file out of the way";
      $success = 0;
    }
    unless (open(OUT, ">", $file)) {
      warn "ERROR: Can't open $file for writing";
      $success = 0;
    }
    print OUT $out;
    close(OUT);
    if ($success) { print SUCCESS "$file\n"; }
    else { print FAILURE "$file\n"; }
    print LOG "$file $package-$version\n";
  }
  print $out if $DEBUG;
}

close(LOG);
