#!/import/perl/bin/perl -w
# $Id: wizard_diff,v 1.2 1993/01/08 09:00:37 putz Exp $
# wizard_diff [-convert] file1 [-convert] file2

$outfile = "wizdiff";

open(ONLY2, ">$outfile.2") || die "open $outfile.2 failed\n";

# first collect records that are in file 1

$convert = 0; $fname = shift;
$convert = 1, $fname = shift if $fname eq "-convert";
&wizread($fname, 1, $convert);

# now list records that are only in file 2
# and remove records that are in both file 1 and file 2

$convert = 0; $fname = shift;
$convert = 1, $fname = shift if $fname eq "-convert";
&wizread($fname, 2, $convert);

close(ONLY2);

open(ONLY1, ">$outfile.1") || die "open $outfile.1 failed\n";

# now list records that are only in file 1
# (i.e. those not removed above)

foreach $app (@apps) {
    while (($rec, $which) = each(%infirst)) {
	next unless $rec =~ s/^$app//;
	$inapp = $app, print ONLY1 "[$app]\n" unless $inapp eq $app;
	print ONLY1 $rec;
    }
    print ONLY1 "[^]\n" if $inapp eq $app;
}

close(ONLY1);

sub wizread {
    local($ifile, $which, $convert) = @_;
    local(*INFILE, $skip, $app, $inapp);
    open(INFILE, $ifile) || die "failed to open input file $ifile\n";
    while (<INFILE>) {
	if (/^\[\^\]$/) {
	    $skip = 0, next if $skip;
	    $skip = 0;
	    next if $which == 1;
##	    print;
	    print ONLY2 if $inapp eq $app;
	    next;
	} elsif ($convert && /^\[-/ || /^\[D ALARM/ || /^\[PERIOD/ || /^\[TEL FILE/ || /^\[TEL FREE/ || /^\[BUS FREE/ || /^\[BUSINESS/ || /^\[OUTLINE/ || /^\[USER'S  DIC/ ) {
	    $skip = 1;
	    next;
	} elsif ($skip) {
	    next;
	} elsif (/^\[(.*)\]$/) {
	    $app = $1;
	    $striplast = $convert && /^\[TEL     /;
	    $apps[++$#apps] = $app unless $knownapp{$app}++;
	    next if $which == 1;
##	    print;
	    next;
	}
	s:([^\\]*\\){5}$:: if $striplast;	# strip last 5 fields
	if ($which == 1) {
	    $infirst{$app.$_} = 1;
	} else {
	    if ($infirst{$app.$_} == 1) {
		delete $infirst{$app.$_};
##	    print "  ";
##	    print;
	    } else {
##		print "2 ";
##		print;
		$inapp = $app, print ONLY2 "[^$app]\n" if $inapp ne $app;
		print ONLY2 $_;
	    }
	}
    }
    close(INFILE);
}

