#!/usr/local/bin/perl

# fixdiff:
#
# diff -c の出力の
#
#   *** (\d+),(\d+) ****
#   --- (\d+),(\d+) ----
#
# となっている部分の行番号を後に続く行数に合わせて修正する．

# Usage: fixdiff diff-1 > diff-2

$debug=0;

 FileUnit: while (1) {
     while (<>) {
	 print STDERR "1----> $_" if $debug;
	 if (/^([*-]{3}) (\d+),(\d+) ([*-]{4})/) {
	     ($form_a, $from, $to, $form_b)=($1, $2, $3, $4);
	     last;
	 }
	 print;
	 last FileUnit if (eof(ARGV));
     }
     @diff=();
     while (<>) {
	 last FileUnit if (eof(ARGV));
	 print STDERR "2----> $_" if $debug;
	 if (/^([*-]{3}) (\d+),(\d+) ([*-]{4})/) {
	     @tmp=($1, $2, $3, $4);
	     if ($from+$#diff != $to && @diff != 0) {
		 print STDERR "$form_a Unmatch lines $from -> $to !(+" 
		     . (0 + $#diff) . ")\n";
	     }
	     $to=$from+$#diff if (@diff != 0);
	     print "$form_a $from,$to $form_b\n";
	     print @diff;
	     @diff=();
	     ($form_a, $from, $to, $form_b)=@tmp;
	     next;
	 }
	 if (/^(\*+)$/) {
	     $tmp=$1;
	     if ($from+$#diff != $to && @diff != 0) {
		 print STDERR "$form_a Unmatch lines $from -> $to !(+" 
		     . (0 + $#diff) . ")\n";
	     }
	     $to=$from+$#diff if (@diff != 0);
	     print "$form_a $from,$to $form_b\n";
	     print @diff;
	     @diff=();
	     print "$tmp\n";
	     last;
	 }
	 if (/^diff -c/ || /^Only in/ || eof(ARGV)) {
	     push(@diff, $_) if (eof(ARGV));
	     if ($from+$#diff != $to && @diff != 0) {
		 print STDERR "$form_a Unmatch lines $from -> $to !(+" 
		     . (0 + $#diff) . ")\n";
	     }
	     $to=$from+$#diff if (@diff != 0);
	     print "$form_a $from,$to $form_b\n";
	     print @diff;
	     print;
	     @diff=();
	     last;
	 }
	 push(@diff, $_);
     }
 }

