#!/usr/bin/perl

open(SCHEMATA, "< /home/kurisuto/documents/linguistics/norse_class/schema_weak_verbs") || die "Fatal error:$!\n";

while (<SCHEMATA>) {
    chop;
    
    # Skip empty lines and comments.
    if (($_ eq "") || ($_ =~ /^\#/)) { next; }
    
    push @schema, $_;
    
}


open(LEMMATA, "< /home/kurisuto/documents/linguistics/norse_class/lemmata_weak_verbs") || die "Fatal error:$!\n";

while (<LEMMATA>) {

#    print "***$_";
    
    chop;
    
    # Skip empty lines and comments.
    if (($_ eq "") || ($_ =~ /^\#/)) { next; }
    
    # Extract the stuff from this line.
    ($class, $infinitive, $preterite, $pp, $gloss) = split(/\t/);
    if ($class eq "2") {
	$gloss = $preterite;
    }
    
    # Get the present stem, and figure out if this is a middle.
    $present = $infinitive;
    $middle = 0;
    if ($present =~ /[sz]k$/) {
	$present =~ s/[sz]k$//;
	$middle = 1;
    }
    $present =~ s/j?a$//;
    
    # Get the preterite stem.
    $preterite =~ s/[sz]k$//;
    $preterite =~ s/i$//;
    if ($class eq "2") {
	$preterite = $present . "a";
    }
    
    # Get the pp.
    $pp =~ s/[sz]k$//;
    if ($class eq "2") {
	$pp = $present . "ar";
    }
    
    
    foreach $form (@schema) {
	
	($s_class, $stem, $pos, $sound_change, $ending) = split (/\t/, $form);
	if ($s_class ne $class) { next; }
	
	
	if ($stem eq "inf") { $output_stem = $present;  }
	elsif ($stem eq "pret") { $output_stem = $preterite;  }
	elsif ($stem eq "pp") { $output_stem = $pp;  }


	$basecopy = $output_stem;
        if ($sound_change eq "uml") {
            $basecopy =~ s/au/ey/;
            $basecopy =~ s/j//;
            $basecopy =~ s/\&o-hook;//;
            $basecopy =~ s/a/e/;
            $basecopy =~ s///;
            $basecopy =~ s/o//;
            $basecopy =~ s//\&oelig;/;
            $basecopy =~ s/u/y/;
            $basecopy =~ s///;
        }
        if ($sound_change eq "u") {
            $basecopy =~ s/a/\&o-hook;/;
        }

	@basecopy_list = ($basecopy);
	if ($basecopy =~ /\(i\)/) {
	    @basecopy_list = ($basecopy, $basecopy);
	    $basecopy_list[0] =~ s/\(i\)//;
	    $basecopy_list[1] =~ s/\(i\)/i/;
	}

	# In the pp middle, we need to lop off the ~dr.
	
	foreach $base (@basecopy_list) {

	    print $base;
	    print $ending;
	    print "\t";
	    
	    print $infinitive;
	    print "+";
	    print $pos;
	    print "\t";
	    
	    print $gloss;
	    print "\n";

	}
	
    }
    
    
    
}

