#!/usr/bin/perl -w
# merge-list -- merge BannedContent from two wikis
# Copyright (C) 2004  Alex Schroeder <alex@emacswiki.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use LWP::UserAgent;

sub GetRaw {
  my $uri = shift;
  my $ua = LWP::UserAgent->new;
  my $request = HTTP::Request->new('GET', $uri);
  my $response = $ua->request($request);
  return $response->content;
}

sub Main {
  my ($source, $target, $forgiven) = map {GetRaw($_)} @ARGV;
  my (%source, %target);
  map {$source{$_} = 1} grep(/^[ \t]/, split(/\n/, $source));
  map {$target{$_} = 1} grep(/^[ \t]/, split(/\n/, $target));
  # remove all the links that are forgiven...
  foreach $_ (grep(/^[ \t]/, split(/\n/, $forgiven))) {
    delete $source{$_};
    delete $target{$_};
  }
  # merge the source lines to the target lines
  foreach $_ (keys %source) {
    $target{$_} = 1;
  }
  # now produce an updated pages from all the normal lines plus the
  # new target lines.
  my @page = grep(/^[^ \t]|$/, split(/\n/, $target));
  push(@page, "") unless $page[$#page] eq ''; # add empty line if required
  push(@page, sort(keys %target));
  print join("\n", @page);
}

if ($#ARGV != 2) {
  die "Usage: $0 source-url target-url forgiven-url\n";
}

Main();
