#!/usr/bin/perl

# Created by Evgeny Vetrov (2:6037/7)

sub pnt_diff {
	local($ol,$nl)=@_;
	my @npl=split("\n",$nl);
	my @opl=split("\n",$ol);
	my $diff="";
	my $a=0,$d=0,$c=0,$k=0;
	for(my $i=0;$i<=$#opl;$i++) {
		for(my $j=$k;$j<=$#npl;$j++) {
			if ($npl[$j] eq $opl[$i]) {
				if ($c==0 && $d>0 ) {
					$diff.="D".$d."\r\n";
					$d=0;
				} 
				if ($c==0 && $a>0) {
					$diff.="A".$a."\r\n";
					for(;$a>0;$a--) {
						$diff.=$npl[$j-$a]."\n";
					}
					$a=0;
				}
				if ($c>=32000) {
					$diff.="C".$c."\r\n";
					$c=0;
				}
				$c++;
				$k=$j+1;
				last;
			} else {
				$a++;
				if ($c>0) {
					$diff.="C".$c."\r\n";
					$c=0;
				}
			}
		}
		if ($c==0) {
			if ($d>=32000) {
				$diff.="D".$d."\r\n";
				$d=0;
			}
			$d++;
			$a=0;
		}
	}
	if ($c>0) {
		$diff.="C".$c."\r\n";
	}
	if ($d>0) {
		$diff.="D".$d."\r\n";
	}
	if ($k<$nmax) {
		$a=$nmax-$k;
		$diff.="A".$a."\r\n";
		for(;$a>0;$a--) {
			$diff.=$npl[$nmax-$a]."\n";
		}
	}
	return $opl[0]."\n".$diff;
}

sub pntdiff {
	local($nl,$ol)=@_;
	my $npl,$opl;
	open(PL,$ol);
	while(<PL>){
		$opl.=$_;
	}
	close(PL);
	open(PL,$nl);
	while(<PL>){
		$npl.=$_;
	}
	close(PL);
	return pnt_diff($opl,$npl);
}

if( $#ARGV <0 ) {
#	exit;
} else {
	for (my $i=0; $i<=$#ARGV; $i++) {
		if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
			print <<USAGE;
Usage: pntdiff.pl [OPTION] pntseg_old pntseg_new -o out_file
Option:
  -h, --help            This usage.
  -o FILE               Output file.
USAGE
                } elsif ($ARGV[$i] eq "-o") {
			$out_file=$ARGV[$i+1];
			$i++;
                } elsif ($ARGV[$i] =~ /^-.*/) {
                        print STDERR "Unknown option: ",$ARGV[$i],"\r\n";
		} elsif (!$pntseg_old) {
			$pntseg_old=$ARGV[$i];
			if ( ! -f $pntseg_old ) {
				print STDERR "File $pntseg_old Not Found\r\n";
			}
		} elsif (!$pntseg_new) {
			$pntseg_new=$ARGV[$i];
			if ( ! -f $pntseg_new ) {
				print STDERR "File $pntseg_new Not Found\r\n";
			}
		}
        }
	my $ret = pntdiff($pntseg_old,$pntseg_new);
	if ( $out_file ) {
		open(OUT,">$out_file");
		print OUT $ret;
		close(OUT);
	} else {
		print $ret;
	}
}