#!/usr/bin/perl

use warnings;
use strict;
use Text::CSV;
use Data::Dumper;

local $SIG{__WARN__} = sub { die $_[0] };

my @rows;
my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                or die "Cannot use CSV: ".Text::CSV->error_diag ();

open my $fh, "<:encoding(utf8)", "google_doc_gismu.csv" or die "google_doc_gismu.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
#  print "row: ".Dumper(\$row)."\n";
#  print $row->[4]."\t".$row->[5]."\n";
  if( $row->[4] && ! $row->[5] )
  {
    # Is this a bare gismu?
    if( $row->[4] =~ m{^\w+$} )
    {
      print $row->[4];
      my $grepper="^".$row->[4];
      my $place=qx{grep '$grepper' /home/rlpowell/lojban/gismu.4col | head -1 | cut -f 4};
      $place =~ s{\(cf.[^)]*\)}{}g;
      $place =~ s{[[][^]]*[]]/}{}g;
      $place =~ s{[[][^]]*[]]}{}g;
      $place =~ s{;.*}{}g;
      $place =~ s{(\w+)/(is made of/|\w+/)*(is made of|\w+)}{$1}g;
      $place =~ s{\s+}{ }g;
      print "\t$place\n";
    } else {
      print $row->[4];
      my $grepper=$row->[4];
      $grepper =~ s/^lo se (\w*)/${1}2/;
      $grepper =~ s/^lo te (\w*)/${1}3/;
      $grepper =~ s/^lo ve (\w*)/${1}4/;
      $grepper =~ s/^lo xe (\w*)/${1}5/;
      $grepper =~ s/^lo (\w*)/${1}1/;
      $grepper =~ s/$/:/;
      my $place=qx{grep '$grepper' '/tmp/jbofihe-0.38/places.dat' | head -1};
      chomp $place;
      if( ! $place )
      {
        print "\tgrepper $grepper didn't find anything\n";
        exit;
      }

      my @word=split /:/, $place;
      my @info=split /;/, $word[1];
#  print "place: ".Dumper(\@word)."\n";
#  print "place: ".Dumper(\@info)."\n";

      my $an="a";
      if( $info[1] =~ /^[aeiouAEIOU]/ )
      {
        $an="an";
      }

      if( $info[0] eq 'P' )
      {
        $info[1] =~ s/\*//;
        print "\t$an ".$info[1]." thing\n";
        next;
      }

      if( $info[0] eq 'D' )
      {
        $info[1] =~ s/\*//;
        print "\t$an ".$info[1]."\n";
        next;
      }

      if( $info[0] eq 'E' )
      {
        $info[1] =~ s/\*//;
        print "\t$an ".$info[1]."\n";
        next;
      }

      if( $info[0] eq 'S' )
      {
        $info[1] =~ s/\*//;
        print "\t$an ".$info[1]."\n";
        next;
      }

      if( $info[0] eq 'A' )
      {
        $info[1] =~ s/\*//;
        print "\t$an ".$info[1]."er\n";
        next;
      }

      if( $info[0] eq 'R' )
      {
        $info[1] =~ s/\*//;
        print "\t$an thing ".$info[1]."\n";
        next;
      }

      if( $info[0] eq 'I' )
      {
        if( $info[1] =~ /\*/ )
        {
          my @bits=split /\*/, $info[1];
          $bits[0] =~ s/e$//;
          $bits[1] =~ s/^\s*//;

          my $an2="a";
          if( $bits[1] =~ /^[aeiouAEIOU]/ )
          {
            $an2="an";
          }

          print "\t$an thing ".$bits[0]."ing $an2 ".$bits[1]."\n";
          next;
        } else {
          print "\t$an ".$info[1]." thing\n";
          next;
        }
      }

      print "\nplaces.dat type ".$info[0]." not recognized.\n";
      exit;
    }
  } else {
    if( $row->[4] ) {
      print $row->[4]."\t".$row->[5]."\n";
    }
  }
}
$csv->eof or $csv->error_diag();
close $fh;

