use File::Find;

my %hMessagesGL = ();

sub PreCreateForm ()
{
   my $cFindCmd;

   &SetField("lang", &GetAttr("Lang"));
   &SetField("base_lang", &GetAttr("BaseLang"));

   if (&GetAttr("UrlParams"))
   {
        &SetField("search_tag", &GetAttr("UrlParams"));
   }
   else
   {
      &SetField("search_tag", "FIXME");
   }


   $cFindCmd = sprintf("find %s -name '*_%s.po' -follow -print | sort | xargs fgrep -l %s",
                       &GetAttr("BaseDir"),
                       &GetAttr("Lang"),
                       &GetField("search_tag"));

   print STDERR "Find-command: $cFindCmd\n";

   open FIND, ($cFindCmd . "|")
       or die sprintf(i18n("Can't call %s"),
                      $cFindCmd);

   $cFile = <FIND>;
   close FIND;

   chomp $cFile;

   if($cFile)
   {
      &SetField("file", "$cFile");

      &ScanPoFile($cFile);

      foreach $cKey (keys %hMessagesGL)
      {
         my $cSearchKey = "^" . &GetField("search_tag") . ".*";
         # this one is to translate
         if($hMessagesGL{$cKey} =~ /$cSearchKey/)
         {
            my $cTransText;

            print STDERR "find found $cFile\n";

            &SetField("file", $cFile);
            &SetField("trans_text_base_lang", "$cKey");
            $cTransText = i18n("$cKey");
            &SetField("trans_text", $cTransText);

            return;
         }
      } #  foreach $cKey (keys %hMessagesGL)

   }


   {
      &MySetVal("State Init", "BtnNew", "-");
      &SetField("file", i18n("<Nothing to translate>"));
   }
}

sub MyBtnNew()
{
   %hMessagesGL = ();

   &ScanPoFile(&GetField("file"));

   $hMessagesGL{&GetField("trans_text_base_lang")} =
       &GetField("trans_text");

   WritePoFile(&GetField("file"));

}


# This code is from tools/CreatePoFiles.pl ...
sub ScanPoFile($)
{

   my $cPoFileName = shift;

   my $cMsgId  = "";
   my $cBuffer = "";

   open PO, "<$cPoFileName"
       or die "Can't open $cPoFileName";

   print STDERR "Scan $cPoFileName ...\n";

   while (<PO>)
   {
      chomp;

      # we found a msgid "..."-line here
      if (/^msgid +\"(.*)\"\s*$/)
      {
         # first, handle the msgid from the last loop
         if($cMsgId)
         {
            $cBuffer =~ s/\\n/\n/g;
            $cBuffer =~ s/\\"/"/g;

            $hMessagesGL{$cMsgId} = $cBuffer;
         }

         $cMsgId   = "";
         $cBuffer  = $1;
      }
      # we found a msgstr "..."-line here
      elsif(/^msgstr +\"(.*)\"\s*$/)
      {
         my $cMessage = $1;

         $cBuffer =~ s/\\n/\n/g;
         $cBuffer =~ s/\\"/"/g;

         $cMsgId = $cBuffer;
         $cBuffer = $cMessage;
      }
      # we found a continuation of a msgstr or msgid here
      elsif(/^\"(.*)\"\s*$/)
      {
         # append to buffer
         $cBuffer .= $1;
      }
   }

   if($cMsgId)
   {
      $cBuffer =~ s/\\n/\n/g;
      $cBuffer =~ s/\\"/"/g;

      $hMessagesGL{$cMsgId} = $cBuffer;
   }

   close PO;

   foreach (keys %hMessagesGL)
   {
      printf STDERR ("KEY  :'%s'\nVALUE:'%s'\n",
                     $_, $hMessagesGL{$_});
   }

}



sub WritePoFile($)
{

   my $cPoFileName = shift;

   return
       unless %hMessagesGL;

   open PO, ">$cPoFileName"
       or die "Can't open $cPoFileName";

   foreach (sort keys %hMessagesGL)
   {
      my $cLine;

      if($_)
      {


         my $cMsgId  = $_;
         my $cMsgStr = $hMessagesGL{$_};

         printf STDERR "BEFORE: '$cMsgId'=>'$cMsgStr'\n";

         $cMsgId =~ s/\n/\\n\"\n\"/g;
         $cMsgId = '"' . $cMsgId . '"';

         $cMsgStr =~ s/\n/\\n\"\n\"/g;
         $cMsgStr = '"' . $cMsgStr . '"';

         printf STDERR "AFTER: '$cMsgId'=>'$cMsgStr'\n";

         printf PO ("msgid  %s\nmsgstr %s\n\n",
                    $cMsgId, $cMsgStr);
      }
   }

   close PO;

}


1;


