#!/usr/bin/perl -s

  use Text::ParseWords;
  
  sub trim {
      my( $string ) = $_[ 0 ] ;
      $string =~ s/^\s+// ;
      $string =~ s/\s+$// ;
      return $string ;
  }
  
  sub suffix_id {
    my( $id, $count, $multiple ) = @_ ;
    if( ! $multiple ) {
        return $id ;
    } else {
        return $id . "_" . ( $count < 10 ? "0" :"" ) . $count ;
    }
  }
  
  sub construct_output {
  
  my $id  = $_[ 0 ] ;
  my @english = parse_line(",", 1, $_[ 1 ] ) ;
  my @translated = parse_line(",", 1, $_[ 2 ] ) ;
  
  my $multiple = scalar( @english ) > 1 ? 1 : 0 ;
    
  if( length( $translated[ 0 ] ) == 0 ) {
      @translated = () ;
  }
  
  my $engcount = scalar( @english ) ;
  my $trancount = scalar( @translated ) ;
  my $diff = $engcount - $trancount  ;
  
  if( $diff < 0 ) { 
  #we have more translations than are required
      splice( @translated, $diff ) ;
  } elsif( $diff > 0 ) { 
  #we have fewer translations than need, so use the english
      splice( @translated, $trancount, 0, 
          @english[ $engcount- $diff .. $engcount - 1 ] ) ;
  }

  my $i = 0;
  foreach $t ( @translated ) {
      $t = trim( $t ) ;
      #check for empty or malformed string
      if( length( $t ) < 3 ) {
          $t = trim( @english[ $i ] ) ;
      }
      $_[ 3 ] .= "    " . suffix_id( $id, $i, $multiple ) . ",\n";
      #$_[ 4 ] .= "    $t, /* " . suffix_id( $id, $i, $multiple ) . " */\n" ;
      $_[ 4 ] .= "    $t,\n" ;
      $i++ ;
  } 
  
  if( $multiple ) {
      $_[ 5 ] .= "#define $id" . "_COUNT $engcount\n" ;
  }
}



if(!$ARGV[0]) {
    print <<MOO
Usage: lang.pl [-p=<prefix>] <language file>

When running this program. <prefix>.h and <prefix>.c will be created in the
"current directory". <prefix> is "lang" by default.
MOO
;
    exit;
}

my $prefix = $p;
if(!$prefix) {
    $prefix="lang";
}

my $input = $ARGV[0];

open(HFILE, ">$prefix.h");
open(CFILE, ">$prefix.c");

print HFILE <<MOO
/* This file was automaticly generated using genlang */
/*
 * The str() macro/functions is how to access strings that might be
 * translated. Use it like str(MACRO) and expect a string to be
 * returned!
 */
#define str(x) language_strings[x]

/* this is the array with all the strings */
extern unsigned char *language_strings[];

/* The enum below contains all available strings */
enum {
MOO
    ;

print CFILE <<MOO
/* This file was automaticly generated using genlang, the strings come
   from "$input" */

unsigned char *language_strings[]={
MOO
    ;
	 
my $ids = "" ;
my $strings = "" ;
my $defines = "" ;

open(LANG, "<$input");
while(<LANG>) {
    $line++;
    if($_ =~ / *\#/) {
        # comment
        next;
    }
    # get rid of DOS newlines
    $_ =~ s/\r//g;
    if($_ =~ / *([a-z]+): *(.*)/) {
        ($var, $value) = ($1, $2);
        # print "$var => $value\n";

        $set{$var} = $value;

        if( (($var eq "new") && $value && ($value !~ /^\"(.*)\"\W*$/)) ||
            (($var eq "eng") && ($value !~ /^\"(.*)\"\W*$/)) ) {
            print "$input:$line:missing quotes for ".$set{'id'}."\n";
            $errors++;
            next;
        }

        if($var eq "new") {
            # the last one for a single phrase

#            if(!$value || ($value eq "\"\"") ) {
#                # if not set, get the english version
#                $value = $set{'eng'};
#            }

				 construct_output( $set{'id'}, $set{'eng'}, $value, 
					  $ids, $strings, $defines ) ;
            #print HFILE "    ".$set{'id'}.",\n";
            #print CFILE "    $value,\n";

            undef %set;
        }

    }

}
close(LANG);


print HFILE "$ids\n\n" ;
print CFILE"$strings\n\n" ;


print HFILE <<MOO
    LANG_LAST_INDEX_IN_ARRAY /* this is not a string, this is a marker */
};
/* end of generated enum list */
MOO
    ;

print HFILE "$defines\n\n" ;

print CFILE <<MOO
};
/* end of generated string list */
MOO
    ;

close(CFILE);
close(HFILE);

exit $errors;
