#!/usr/bin/perl

if (@ARGV != 3) {
	print STDERR "Use:\n";
	print STDERR "  showdf <showg-script> <old-file> <new-file>\n";
	print STDERR "to create a showg diagram of glyphs differing in two files\n";
	exit 1;
}

$cmd = shift @ARGV;
$oldf = shift @ARGV;
$newf = shift @ARGV;

open(O, "<$oldf") or die "Unable to open '$oldf'\n";
open(N, "<$newf") or die "Unable to open '$newf'\n";

while(<O>) {
	last if(/CharStrings/);
}
while(<N>) {
	last if(/CharStrings/);
}

undef @symlist;
$sym = '';
$inlist = 0;
$nstop = 0;

while($so = <O>) {
	if($so =~ m|^(/\S+)\s+\{|) {
		$sym = $1;
		$inlist = 0;
		#printf STDERR "found sym $sym\n";
		if (!$nstop || $sn !~ m|^$sym\s+\{|) {
			while($sn = <N>) {
				#print STDERR "+$sn";
				last if($sn =~ m|^${sym}\s+\{|);
			}
		}
		$nstop = 0;
	} elsif(!$nstop) {
		$sn = <N>;
		#print STDERR "<$so>$sn\n";
		if($so ne $sn) {
			if(!$inlist) {
				$inlist = 1;
				push(@symlist, $sym);
			}
			if($sn =~ m|^(/\S+)\s+\{|) {
				$nstop = 1;
				#printf STDERR "stop at $1\n";
			}
		}
	}
}
unshift(@symlist, $cmd, '-c', $oldf, $newf);
#printf("%s\n", join(' ', @symlist));
exec @symlist;
