#!/usr/bin/perl

use POSIX;

if ($ARGV[0] eq '-h') {
  $THIS_SPEC = $ARGV[1];
  $EDITOR_EMAIL = $ARGV[2];
  $EDITOR_NAME = $ARGV[3];
  $EDITOR_AFFILIATION = $ARGV[4];

  $_ = join("", <STDIN>);
  $date = strftime("\%d \%B \%Y", localtime);
  s{(<(?:h1|title)>[^>]*)(</[^>]+>)}{<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">$1 [$THIS_SPEC]$2}g;
  s{(<h2.*?>.*?)\d+ \S+ \d{4}</h2>}{$1$date};
  s{^(\s*This specification is the )\d+ \S+ \d{4}}{$1$date};
  s{<dt>Latest Published Version.*?</dd>}{}s;
  s{http://www\.w3\.org\/html\/wg\/html5\/}{http://dev.w3.org/html5/$THIS_SPEC/}g;
  s{<dt>Previous Versions.*?<dt>}{<dt>}s;
  s{(<dt>.*?)</dl>}{$1<dd><a href="mailto:$EDITOR_EMAIL">$EDITOR_NAME</a>, $EDITOR_AFFILIATION</dd></dl>}s;
  s{http://dev\.w3\.org/html5/spec/Overview\.html}{http://dev.w3.org/html5/$THIS_SPEC/Overview.html}g;
  s{ and in the.*?\.</p>}{.</p>}s;
  s{<dd>Commit-Watchers.*?</dd>}{};
  s{<dt>Real-time.*?<dt>}{<dt>}s;
  s{<dd>Annotated summary.*?</dl>}{</dl>}s;
  s{http://dev\.w3\.org/cvsweb/html5/spec/Overview\.html}{http://dev.w3.org/cvsweb/html5/$THIS_SPEC/Overview.html}g;
  s{<p>The <a [^>]*>WHATWG\s*version.*?<\/p>}{}s;
  s{ or <a href="mailto:whatwg@.*?\),}{}s;
  s{(We maintain ).*?</a> and}{$1}s;
  s{<p>This specification is also being produced.*?</p>}{<p>This specification is a fork of the <a href="http://www.w3.org/TR/html5/">HTML 5</a> specification produced jointly by the <a href="http://www.whatwg.org/">WHATWG</a> and the <a href="http://www.w3.org/">W3C</a>.</p>}s;
  print;
} elsif ($ARGV[0] eq '-m') {
  die "Can't merge upstream changes since previous merge conflicts were not resolved.  Did you forget to manually apply any .rej files and then type 'make resolved'?\n" if -f 'conflict';
  print "Checking upstream for changes...\n";
  $curRev = `head -n 1 source`;
  $curRev =~ /r(\d+)/ or die "Can't determine current upstream revision number.\n";
  $curRev = $1;
  $newRev = `svn info http://svn.whatwg.org/webapps/source | grep ^Revision | sed 's/^.*: //'`;
  chomp $newRev;
  die "No upstream changes to merge.\n" if $curRev eq $newRev;
  system("svn diff -r$curRev:$newRev http://svn.whatwg.org/webapps/source | patch -s -N");
  if ($? != 0) {
    open FH, '>conflict';
    print FH "$newRev\n";
    close FH;
    die "Some or all of the changes between $curRev and $newRev could not be merged.  Inspect the .rej files and apply them manually, then type 'make resolved' to record that the changes have been merged.\n";
  }
  unlink 'source.orig';
  system("(echo '<!--r$newRev upstream; do not delete this line or you will lose the ability to merge upstream changes -->'; tail -n +2 source) >source-merged; mv source-merged source");
  die unless $? == 0;
  print "Successfully merged changes between r$curRev and r$newRev.\n";
} elsif ($ARGV[0] eq '-r') {
  die "No previous merge conflicts to mark as resolved!\n" unless -f 'conflict';
  $curRev = `head -n 1 source`;
  $curRev =~ /r(\d+)/ or die "Can't determine current upstream revision number.\n";
  $curRev = $1;
  $newRev = `cat conflict`;
  chomp $newRev;
  system("(echo '<!--r$newRev upstream; do not delete this line or you will lose the ability to merge upstream changes -->'; tail -n +2 source) >source-merged; mv source-merged source");
  die unless $? == 0;
  unlink 'conflict';
  print "Marked merge conflicts changes between r$curRev and r$newRev as resolved.\n";
} elsif ($ARGV[0] eq '-i') {
  die "Please move ./source out of the way first.\n" if -f 'source';
  print "Fetching current HTML 5 source...\n";
  $curRev = `svn info http://svn.whatwg.org/webapps/source | grep ^Revision | sed 's/^.*: //'`;
  chomp $curRev;
  system("(echo '<!--r$curRev upstream; do not delete this line or you will lose the ability to merge upstream changes -->'; svn cat -r$curRev http://svn.whatwg.org/webapps/source) >source");
  die unless $? == 0;
  print "Got r$curRev.\n";
}

