#! /usr/local/bin/perl -w

use strict;

my %ipa2tipa = (
  101=>"p",  102=>"b",  103=>"t",  104=>"d",
  109=>"k",  110=>"g",
  114=>"m",  116=>"n",  119=>"N",  124=>"R",
  128=>"f",  129=>"v",  130=>"T",  131=>"D",
  132=>"s",  133=>"z",  134=>"S",  135=>"Z",
  146=>"h",
  151=>"\\*r",  153=>"j",  155=>"l",
  169=>"\\*w",  170=>"w",
  213=>"\\textteshlig{}",  214=>"\\textdyoghlig{}",
  301=>"i",  302=>"e",  303=>"E",  304=>"a",
  305=>"A",  306=>"O",  307=>"o",  308=>"u",
  313=>"6",  314=>"2",
  319=>"I",  320=>"Y",  321=>"U",  322=>"@",
  325=>"\\ae{}",  326=>"3",  327=>"\\textrhookschwa{}",
  419=>"\\textrhoticity{}",
  501=>"\"",  502=>"\"\"",  503=>":",  506=>".",
);

while ( <> ) {
    while ( 1 ) {
	if ( /^$/ || /^\#.*$/ ) {
	    print "\n";
	    last;
	} elsif ( s/^\[326\]\[419\]// ) {
	    print "\\textrhookrevepsilon{}";
	} elsif ( s/^\[(\d+)\]\[431\]// ) {
	    if ( defined($ipa2tipa{$1}) ) {
		print "\\s{$ipa2tipa{$1}}";
	    } else {
		die "Unknown IPA code ($1)";
	    }
	} elsif ( s/^\[(\d+)\]// ) {
	    if ( defined($ipa2tipa{$1}) ) {
		print $ipa2tipa{$1};
	    } else {
		die "Unknown IPA code ($1)";
	    }
	} elsif ( s/^(\s+)// ) {
	    print $1;
	} elsif ( s/^(.)// ) {
	    print STDERR "Line remainder is $_";
	    die "Unrecognized character ($1)";
	}
    }
}
