#!/usr/bin/perl
##
## ***********************************************************
##
## This script is usefull (not for all perhaps) to start
## translating AbiWord (www.abisource.com) interface.
##
## The script generates the zero translated strings file.
## All strings are commented and include English version of strings.
## So, a translator can easily replace string and then uncomment it.
##
## Running the script put in command line:
## 1. SHORTCUT_FOR_A_NEW_LANGUAGE (for example: xx-XX)
## 2. ENCODING (for example: UTF-8)
## 3. YES or NO for preserving English shortcuts (sugested: NO)
##
## ***********************************************************

##
## time and date
##
my($RetTime, $StartTimeArg, $sec, $minute, $hour, $mday, $mon, $year);
($sec,$minute,$hour,$mday,$mon,$year) = localtime();
if ($mday < 10) {$day = "0" . "$mday";}
  else {$day = "$mday";}
$mon++; # month is 0 based.
if ($mon < 10) {$month = "0" . "$mon";}
  else {$month = "$mon";}
$year = $year + 1900;
$datetime = "$day-" . "$month-" . "$year " . "$hour:" . "$minute";
print STDERR "\nStarted at ";
printf STDERR ("%02d-%02d-%02d %02d:%02d", $mday,$mon,$year,$hour,$minute);
print STDERR "\n";

##
## input and output
##
if ($#ARGV == 2) {
 ($newlang, $encoding, $shortcuts) = @ARGV;
  $strings_file = ".\/@ARGV[0].strings";
  $shortcuts =~ tr/A-Z/a-z/;
  if ($shortcuts eq "yes" ) {
    $shortcuts = "YES";
  }
  else {
    $shortcuts = "NO";
  }
  print STDERR "\nINPUT\n";
  print STDERR "  files: @srcfiles\n";
  print STDERR "  language: $newlang\n";
  print STDERR "  encoding: $encoding\n";
  print STDERR "  coping shortcuts: $shortcuts\n";
  print STDERR "OUTPUT\n";
  print STDERR "  strings file: $strings_file\n";
  print STDERR "\n";
}
else {
  print "\nHELP\n";
  print "  Stopped! Please enter three (no more, no less) paramaters!\n";
  die "  Try once again...\n\n";
}

##
## two classes of en-US strings from _Id.h files
##
@srcfiles = ("./ap_String_Id.h", "./xap_String_Id.h");
foreach my $file (@srcfiles) {
  open(CLASS, "< $file" )
    or die "Cannot open $file";
  $file =~ /\.\/(.*)_String_Id.h/;
  $string_class = $1;
  $string_class =~ tr/a-z/A-Z/;
  unshift(@srclang, $string_class);
  while (<CLASS>) {
    next unless /^\s*dcl\((.*)\s*,\s*\"(.*)\"/;
    my ($dlg,$string) = ($1,$2);
##
## some important replacing
    if ($shortcuts eq "YES" ) {
      $string =~ s/&/&amp\;/;
    }
    else {
      $string =~ s/&//;
    }
    $string =~ s/>/&gt\;/g;
    $string =~ s/</&lt\;/g;
##    
    $dlgs{$dlg}{$string_class} = $string;
  }
  close(CLASS);
}

##
## strings processing
##
open(NEW, "> $strings_file") 
  or die "Cannot write to strings file: $strings_file";
print NEW "<\?xml version=\"1.0\" encoding=\"$encoding\"\?>\n";
print NEW "<!-- ==============================================================  -->\n";
print NEW "<!-- This file contains AbiWord Strings.  AbiWord is an Open Source  -->\n";
print NEW "<!-- word processor developed by AbiSource, Inc.  Information about  -->\n";
print NEW "<!-- this application can be found at http://www.abisource.com       -->\n";
print NEW "<!-- This file contains the string translations for one language.    -->\n";
print NEW "<!-- This file is covered by the GNU Public License (GPL).           -->\n";
print NEW "<!-- ==============================================================  -->\n\n";
print NEW "<!-- Translatation maintained by YOUR_NAME and EMAIL for contact. -->\n";
print NEW "<!-- Last translator's update: DAY-MONTH-YEAR.                    -->\n";
print NEW "<!-- Strings autogenerated at $datetime                    -->\n";
print NEW "\n<AbiStrings app=\"AbiWord\" ver=\"1.0\" language=\"$newlang\">\n";
$total = 0;
foreach $string_class (@srclang) {
  print NEW "<Strings    class=\"$string_class\"\n\n";
  foreach my $dlg (sort keys %dlgs) {
  next unless $dlg;
    if ($dlgs{$dlg}{$string_class}) {
      $nowdlgs = $dlgs{$dlg}{$string_class};
      $total++;
      print NEW "# $dlg=\"$nowdlgs\"\n";
    }
  }
  print NEW "\n\/>\n";
}

print NEW "</AbiStrings>\n";
close NEW;
print STDERR "STATS for lang $newlang:\n";
print STDERR "  total $total strings.\n\n";

##
##
##
