#!/usr/bin/perl -W
open (ASCFILEGISMU, "gismu.txt");
open (ASCFILECMAVO, "cmavo.txt");

local %count;

$count{gimsu} = 0;
$count{cmavo} = 0;
$count{rasfi} = 0;
$count{gismuref} = 0;

print CSFILEGISMU "'gismuID', 'gismu', 'glossword', 'hintword', 'gismu places', 'lobjan textbook code', '1993 frequency count', 'comments'\n";
print CSFILERASFI "'rasfiID', 'rasfi', 'gismu'\n";
print CSFILEGISMUREF "'gismurefID', 'referencedGismu', 'referencingGismu'\n";
print CSFILECMAVO "'cmavoID', 'cmavo', 'selma\\'o', 'gismu places', 'comments'\n";

local $line;
local %rasfi;

mkdir "man8jbo";

foreach $line (<ASCFILEGISMU>)
{  

	if (substr($line, 1,5) ne '01436')
	{
		my $gismu =		substr($line,   1,   5 -   1 + 1); 	$gismu =~ s/\s*$//;		$gismu =~ s/^\s*//;		$gismu =~ s/\'/\\\'/;		
		my $pavrasfi =		substr($line,   7,   9 -   7 + 1); 	$pavrasfi =~ s/\s*$//;		$pavrasfi =~ s/^\s*//;		$pavrasfi =~ s/\'/\\\'/;	
		my $relrasfi = 		substr($line,  11,  13 -  11 + 1); 	$relrasfi =~ s/\s*$//;		$relrasfi =~ s/^\s*//;		$relrasfi =~ s/\'/\\\'/;	
		my $cibrasfi = 		substr($line,  15,  18 -  15 + 1); 	$cibrasfi =~ s/\s*$//;		$cibrasfi =~ s/^\s*//;		$cibrasfi =~ s/\'/\\\'/;	
		my $glivalsi =		substr($line,  20,  39 -  20 + 1); 	$glivalsi =~ s/\s*$//;		$glivalsi =~ s/^\s*//;		$glivalsi =~ s/\'/\\\'/;	
		my $samdyglivalsi = 	substr($line,  41,  60 -  41 + 1); 	$samdyglivalsi =~ s/\s*$//;	$samdyglivalsi =~ s/^\s*//;	$samdyglivalsi =~ s/^\'/\\\'/;	
		my $selbridi = 		substr($line,  62, 157 -  62 + 1); 	$selbridi =~ s/\s*$//;		$selbridi =~ s/^\s*//;		$selbridi =~ s/\'/\\\'/;	
		my $numbercode = 	substr($line, 159, 160 - 159 + 1); 	$numbercode =~ s/\s*$//;	$numbercode =~ s/^\s*//;	$numbercode =~ s/\'/\\\'/;	
		my $frequencycount = 	substr($line, 161, 164 - 161 + 1); 	$frequencycount =~ s/\s*$//;	$frequencycount =~ s/^\s*//;	$frequencycount =~ s/\'/\\\'/;	
		my $comments =		substr($line, 164); 			$comments =~ s/\s*$//;		$comments =~ s/^\s*//;		$comments =~ s/\'/\\\'/;	
		open (MANFILE, ">man8jbo/$gismu.8jbo");
		print MANFILE ".TH \"", $gismu, " - ", $glivalsi, "\" 8jbo\n";
		print MANFILE ".SH glico bangu valsi\n", $glivalsi, " ", $samdyglivalsi, "\n";
		print MANFILE ".SH selbridi\n", $selbridi, "\n";
		print MANFILE ".SH rasfi\n";
		print MANFILE "$pavrasfi "  if ($pavrasfi ne "");
		print MANFILE "$relrasfi "  if ($relrasfi ne "");
		print MANFILE "$cibrasfi\n" if ($cibrasfi ne "");
		print MANFILE ".SH notes\n", $comments, "'\n\n";
		print MANFILE "Lojban book code: ", $numbercode, "\n\n";
		print MANFILE "1993 Frequency number: ", $frequencycount, "\n";

		$rasfi{$pavrasfi} = $gismu if ($pavrasfi ne "");
                $rasfi{$relrasfi} = $gismu if ($relrasfi ne "");
		$rasfi{$cibrasfi} = $gismu if ($cibrasfi ne "");

		if ($comments =~ m/\(cf\. (.*)\)/)
		{
			print MANFILE ".SH See also\n";
			foreach (split(/, /, $1))
			{
				print MANFILE "$_(8jbo) ";
			}
			print MANFILE "\n";
		}
		close(MANFILE);
		system("gzip -f \"man8jbo/$gismu.8jbo\"");


	} else {
		print "Err", $line, "\n";
	}
}

foreach $line (<ASCFILECMAVO>)
{  

	if (substr($line, 0,5) ne '01090')
	{
		my $cmavo =		substr($line,   0,  11 -   0 + 0); 	$cmavo =~ s/\s*$//;		$cmavo =~ s/^\s*//;	$cmavo =~ s/\'/\\\'/;		
		my $selmao =		substr($line,  11,  19 -  11 + 1); 	$selmao =~ s/\s*$//;		$selmao =~ s/^\s*//;	$selmao =~ s/\'/\\\'/;	
		my $glivalsi =		substr($line,  20,  39 -  20 + 1); 	$glivalsi =~ s/\s*$//;		$glivalsi =~ s/^\s*//;	$glivalsi =~ s/\'/\\\'/;	
		my $selbridi = 		substr($line,  62, 157 -  62 + 1); 	$selbridi =~ s/\s*$//;		$selbridi =~ s/^\s*//;	$selbridi =~ s/\'/\\\'/;	
		my $comments =		substr($line, 164); 			$comments =~ s/\s*$//;		$comments =~ s/^\s*//;	$comments =~ s/\'/\\\'/;	

		open (MANFILE, ">man8jbo/$cmavo.8jbo");
		print MANFILE ".TH \"", $cmavo, " - ", $glivalsi, "\" 8jbo\n";
		print MANFILE ".SH glico bangu valsi\n", $glivalsi, " ", $samdyglivalsi, "\n";
		if (defined $rasfi{$cmavo})
		{
			print MANFILE ".SH rasfi shared with gismu\n", $rasfi{$cmavo}, "\n"; 
			delete $rasfi{$cmavo};
		}
		print MANFILE ".SH selbridi\n$selbridi\n";
		print MANFILE ".SH selmao\n";
		print MANFILE "$selmao\n";
		print MANFILE ".SH notes\n", $comments, "'\n\n";
		if ($comments =~ m/\(cf\. (.*)\)/)
		{
			print MANFILE ".SH See also\n";
			foreach (split(/, /, $1))
			{
				print MANFILE "$_(8jbo) ";
			}
			print MANFILE "\n";
		}
		close(MANFILE);
		system("gzip -f \"man8jbo/$cmavo.8jbo\"");
	} else {
		print "Err", $line, "\n";
	}
}

