#!/usr/bin/perl -w

use strict;
use XML::Simple;
use Data::Diff;
use Data::Dumper;

die "usage: $0 swish-prog-input-1 swish-prog-input-2" if ($#ARGV < 1);

my ($file1, $file2) = @ARGV;

my ($in1, $in2);

open($in1, $file1) || die "$file1: $!";
open($in2, $file2) || die "$file2: $!";

sub read_record {
	my $fh = shift || die;

	my $header = 1;

	my ($path, $size);

	while(<$fh>) {
		chomp;
		$path = $1 if (/^Path-Name:\s*(.+)$/i);
		$size = $1 if (/^Content-Length:\s*(.+)$/);

		if (/^$/) {
			$header = 0;
		}

		next if ($header);

		print STDERR "$path  [$size]\n";

		my $xml;
		my $s = read *{$fh}, $xml, $size;
		warn "read differs from decleared size! $s != $size" if ($s != $size);

		$xml =~ s/md5=[a-zA-Z0-9]{32}//gs;
		$xml =~ s/_userid=\d+//gs;

		return $xml;
	}
}

while(! eof($in1) && ! eof($in2)) {
	my $a = read_record($in1);
	my $b = read_record($in2);

	next if ($a eq $b);

	my $diff = Data::Diff->new( XMLin($a), XMLin($b) );

	my $raw = $diff->raw;

	print Dumper($raw->{diff}) if ($raw->{diff});
}
