#! /usr/bin/perl
# lig.pl
# ligature processing.
#Start: Sat Sep  6 14:00:47 EDT 1997
#EndSat Sep  6 14:10:07 EDT 1997
#End debug: Sat Sep  6 14:22:43 EDT 1997 (I forgot how unshift works!)

%lig = (); # Set of all pairs to replacement text.

$state = -1; # -1 = before anything; 0 = in ligtable; 1 = processing text.
while ( $line = <> ) {
#print "state = $state\n";
#print $line;
	if ( $line=~ m/^\<ligtable\>/ ) {
		$state = 0;
#print " enter table \n";
	} elsif ( $line=~ m|^\<\/ligtable\>| ) {
		$state = 1;
	} elsif ( $state == 0 && $line =~ m/^\s*([^\s]+)\s+([^\s]+)/ ) {
		$lig{$1} = $2;
#print "Ligature $1 $2\n";
	} elsif ( $state == 1 ) {
		# Process this line.
		@l = split(/\(*/,$line); # Break into chars.
		while ( $#l >= 1 ) {
			$a = shift(@l);
			$b = shift(@l);
#print @l;
#print "\ta = .$a. b = .$b.\n";
			$ab = "$a$b";
			if ( $lig{$ab} ne "" ) {  # We matched a ligature.
#print "\nMatched ligature $lig{$ab}";
				unshift( @l, $lig{$ab} ); 
			} else { # No ligature.
				print "$a"; unshift( @l, $b ); 
			}
		}
		print join(//,@l);
	}
}
