home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Linux / gimp-2.2.0.tar.gz / gimp-2.2.0.tar / gimp-2.2.0 / intltool-update.in < prev    next >
Text File  |  2004-11-07  |  27KB  |  1,053 lines

  1. #!@INTLTOOL_PERL@ -w
  2. # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
  3.  
  4. #
  5. #  The Intltool Message Updater
  6. #
  7. #  Copyright (C) 2000-2003 Free Software Foundation.
  8. #
  9. #  Intltool is free software; you can redistribute it and/or
  10. #  modify it under the terms of the GNU General Public License 
  11. #  version 2 published by the Free Software Foundation.
  12. #
  13. #  Intltool is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. #  General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. #
  22. #  As a special exception to the GNU General Public License, if you
  23. #  distribute this file as part of a program that contains a
  24. #  configuration script generated by Autoconf, you may include it under
  25. #  the same distribution terms that you use for the rest of that program.
  26. #
  27. #  Authors: Kenneth Christiansen <kenneth@gnu.org>
  28. #           Maciej Stachowiak
  29. #           Darin Adler <darin@bentspoon.com>
  30.  
  31. ## Release information
  32. my $PROGRAM = "intltool-update";
  33. my $VERSION = "0.32.1";
  34. my $PACKAGE = "intltool";
  35.  
  36. ## Loaded modules
  37. use strict;
  38. use Getopt::Long;
  39. use Cwd;
  40. use File::Copy;
  41. use File::Find;
  42.  
  43. ## Scalars used by the option stuff
  44. my $HELP_ARG        = 0;
  45. my $VERSION_ARG    = 0;
  46. my $DIST_ARG       = 0;
  47. my $POT_ARG       = 0;
  48. my $HEADERS_ARG    = 0;
  49. my $MAINTAIN_ARG   = 0;
  50. my $REPORT_ARG     = 0;
  51. my $VERBOSE       = 0;
  52. my $GETTEXT_PACKAGE = "";
  53. my $OUTPUT_FILE    = "";
  54.  
  55. my @languages;
  56. my %varhash = ();
  57. my %po_files_by_lang = ();
  58.  
  59. # Regular expressions to categorize file types.
  60. # FIXME: Please check if the following is correct
  61.  
  62. my $xml_support =
  63. "xml(?:\\.in)*|".    # http://www.w3.org/XML/ (Note: .in is not required)
  64. "ui|".            # Bonobo specific - User Interface desc. files
  65. "lang|".        # ?
  66. "glade2?(?:\\.in)*|".    # Glade specific - User Interface desc. files (Note: .in is not required)
  67. "scm(?:\\.in)*|".    # ? (Note: .in is not required)
  68. "oaf(?:\\.in)+|".    # DEPRECATED: Replaces by Bonobo .server files 
  69. "etspec|".        # ?
  70. "server(?:\\.in)+|".    # Bonobo specific
  71. "sheet(?:\\.in)+|".    # ?
  72. "schemas(?:\\.in)+|".    # GConf specific
  73. "pong(?:\\.in)+|".    # DEPRECATED: PONG is not used [by GNOME] any longer.
  74. "kbd(?:\\.in)+";    # GOK specific. 
  75.  
  76. my $ini_support =
  77. "icon(?:\\.in)+|".    # http://www.freedesktop.org/Standards/icon-theme-spec
  78. "desktop(?:\\.in)+|".    # http://www.freedesktop.org/Standards/menu-spec
  79. "caves(?:\\.in)+|".    # GNOME Games specific
  80. "directory(?:\\.in)+|".    # http://www.freedesktop.org/Standards/menu-spec
  81. "soundlist(?:\\.in)+|".    # GNOME specific
  82. "keys(?:\\.in)+|".    # GNOME Mime database specific
  83. "theme(?:\\.in)+";    # http://www.freedesktop.org/Standards/icon-theme-spec
  84.  
  85. my $buildin_gettext_support = 
  86. "c|y|cs|cc|cpp|c\\+\\+|h|hh|gob|py";
  87.  
  88. ## Always flush buffer when printing
  89. $| = 1;
  90.  
  91. ## Sometimes the source tree will be rooted somewhere else.
  92. my $SRCDIR = ".";
  93. my $POTFILES_in;
  94.  
  95. $SRCDIR = $ENV{"srcdir"} if $ENV{"srcdir"};
  96. $POTFILES_in = "<$SRCDIR/POTFILES.in";
  97.  
  98. ## Handle options
  99. GetOptions 
  100. (
  101.  "help"            => \$HELP_ARG,
  102.  "version"            => \$VERSION_ARG,
  103.  "dist|d"           => \$DIST_ARG,
  104.  "pot|p"           => \$POT_ARG,
  105.  "headers|s"           => \$HEADERS_ARG,
  106.  "maintain|m"           => \$MAINTAIN_ARG,
  107.  "report|r"           => \$REPORT_ARG,
  108.  "verbose|x"           => \$VERBOSE,
  109.  "gettext-package|g=s" => \$GETTEXT_PACKAGE,
  110.  "output-file|o=s"     => \$OUTPUT_FILE,
  111.  ) or &Console_WriteError_InvalidOption;
  112.  
  113. &Console_Write_IntltoolHelp if $HELP_ARG;
  114. &Console_Write_IntltoolVersion if $VERSION_ARG;
  115.  
  116. my $arg_count = ($DIST_ARG > 0)
  117.     + ($POT_ARG > 0)
  118.     + ($HEADERS_ARG > 0)
  119.     + ($MAINTAIN_ARG > 0)
  120.     + ($REPORT_ARG > 0);
  121.  
  122. &Console_Write_IntltoolHelp if $arg_count > 1;
  123.  
  124. # --version and --help don't require a module name
  125. my $MODULE = $GETTEXT_PACKAGE || &FindPackageName;
  126.  
  127. if ($POT_ARG)
  128. {
  129.     &GenerateHeaders;
  130.     &GeneratePOTemplate;
  131. }
  132. elsif ($HEADERS_ARG)
  133. {
  134.     &GenerateHeaders;
  135. }
  136. elsif ($MAINTAIN_ARG)
  137. {
  138.     &FindLeftoutFiles;
  139. }
  140. elsif ($REPORT_ARG)
  141. {
  142.     &GenerateHeaders;
  143.     &GeneratePOTemplate;
  144.     &Console_Write_CoverageReport;
  145. }
  146. elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
  147. {
  148.     my $lang = $ARGV[0];
  149.  
  150.     ## Report error if the language file supplied
  151.     ## to the command line is non-existent
  152.     &Console_WriteError_NotExisting("$lang.po") if ! -s "$lang.po";
  153.  
  154.     if (!$DIST_ARG)
  155.     {
  156.     print "Working, please wait..." if $VERBOSE;
  157.     &GenerateHeaders;
  158.     &GeneratePOTemplate;
  159.     }
  160.     &POFile_Update ($lang, $OUTPUT_FILE);
  161.     &Console_Write_TranslationStatus ($lang, $OUTPUT_FILE);
  162. else 
  163. {
  164.     &Console_Write_IntltoolHelp;
  165. }
  166.  
  167. exit;
  168.  
  169. #########
  170.  
  171. sub Console_Write_IntltoolVersion
  172. {
  173.     print <<_EOF_;
  174. ${PROGRAM} (${PACKAGE}) $VERSION
  175. Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
  176.  
  177. Copyright (C) 2000-2003 Free Software Foundation, Inc.
  178. This is free software; see the source for copying conditions.  There is NO
  179. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  180. _EOF_
  181.     exit;
  182. }
  183.  
  184. sub Console_Write_IntltoolHelp
  185. {
  186.     print <<_EOF_;
  187. Usage: ${PROGRAM} [OPTION]... LANGCODE
  188. Updates PO template files and merge them with the translations.
  189.  
  190. Mode of operation (only one is allowed):
  191.   -p, --pot                   generate the PO template only
  192.   -s, --headers               generate the header files in POTFILES.in
  193.   -m, --maintain              search for left out files from POTFILES.in
  194.   -r, --report                display a status report for the module
  195.   -d, --dist                  merge LANGCODE.po with existing PO template
  196.  
  197. Extra options:
  198.   -g, --gettext-package=NAME  override PO template name, useful with --pot
  199.   -o, --output-file=FILE      write merged translation to FILE
  200.   -x, --verbose               display lots of feedback
  201.       --help                  display this help and exit
  202.       --version               output version information and exit
  203.  
  204. Examples of use:
  205. ${PROGRAM} --pot    just create a new PO template
  206. ${PROGRAM} xy       create new PO template and merge xy.po with it
  207.  
  208. Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
  209. or send email to <xml-i18n-tools\@gnome.org>.
  210. _EOF_
  211.     exit;
  212. }
  213.  
  214. sub POFile_DetermineType ($) 
  215. {
  216.    my $type = $_;
  217.    my $gettext_type;
  218.  
  219.    my $xml_regex     = "(?:" . $xml_support . ")";
  220.    my $ini_regex     = "(?:" . $ini_support . ")";
  221.    my $buildin_regex = "(?:" . $buildin_gettext_support . ")";
  222.  
  223.    if ($type =~ /\[type: gettext\/([^\]].*)]/) 
  224.    {
  225.     $gettext_type=$1;
  226.    }
  227.    elsif ($type =~ /schemas(\.in)+$/) 
  228.    {
  229.     $gettext_type="schemas";
  230.    }
  231.    elsif ($type =~ /glade2?(\.in)*$/) 
  232.    {
  233.        $gettext_type="glade";
  234.    }
  235.    elsif ($type =~ /scm(\.in)*$/) 
  236.    {
  237.        $gettext_type="scheme";
  238.    }
  239.    elsif ($type =~ /keys(\.in)+$/) 
  240.    {
  241.        $gettext_type="keys";
  242.    }
  243.  
  244.    # bucket types
  245.  
  246.    elsif ($type =~ /$xml_regex$/) 
  247.    {
  248.        $gettext_type="xml";
  249.    }
  250.    elsif ($type =~ /$ini_regex$/) 
  251.    { 
  252.        $gettext_type="ini";
  253.    }
  254.    elsif ($type =~ /$buildin_regex$/) 
  255.    {
  256.        $gettext_type="buildin";
  257.    }
  258.    else
  259.    { 
  260.        $gettext_type="unknown"; 
  261.    }
  262.  
  263.    return "gettext\/$gettext_type";
  264. }
  265.  
  266. sub TextFile_DetermineEncoding ($) 
  267. {
  268.     my $gettext_code="ASCII"; # All files are ASCII by default
  269.     my $filetype=`file $_ | cut -d ' ' -f 2`;
  270.  
  271.     if ($? eq "0")
  272.     {
  273.     if ($filetype =~ /^(ISO|UTF)/)
  274.     {
  275.         chomp ($gettext_code = $filetype);
  276.     }
  277.     elsif ($filetype =~ /^XML/)
  278.     {
  279.         $gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
  280.     }
  281.     }
  282.  
  283.     return $gettext_code;
  284. }
  285.  
  286. sub isNotValidMissing
  287. {
  288.     my ($file) = @_;
  289.  
  290.     return if $file =~ /^\{arch\}\/.*$/;
  291.     return if $file =~ /^$varhash{"PACKAGE"}-$varhash{"VERSION"}\/.*$/;
  292. }
  293.  
  294. sub FindLeftoutFiles
  295. {
  296.     my (@buf_i18n_plain,
  297.     @buf_i18n_xml,
  298.     @buf_i18n_xml_unmarked,
  299.     @buf_i18n_ini,
  300.     @buf_potfiles,
  301.     @buf_potfiles_ignore,
  302.     @buf_allfiles,
  303.     @buf_allfiles_sorted,
  304.     @buf_potfiles_sorted
  305.     );
  306.  
  307.     ## Search and find all translatable files
  308.     find sub { 
  309.     push @buf_i18n_plain,        "$File::Find::name" if /\.($buildin_gettext_support)$/;
  310.     push @buf_i18n_xml,          "$File::Find::name" if /\.($xml_support)$/;
  311.     push @buf_i18n_ini,          "$File::Find::name" if /\.($ini_support)$/;
  312.     push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
  313.     }, "..";
  314.  
  315.  
  316.     open POTFILES, $POTFILES_in or die "$PROGRAM:  there's no POTFILES.in!\n";
  317.     @buf_potfiles = grep !/^(#|\s*$)/, <POTFILES>;
  318.     close POTFILES;
  319.  
  320.     foreach (@buf_potfiles) {
  321.     s/^\[.*]\s*//;
  322.     }
  323.  
  324.     print "Searching for missing translatable files...\n" if $VERBOSE;
  325.  
  326.     ## Check if we should ignore some found files, when
  327.     ## comparing with POTFILES.in
  328.     foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
  329.     {
  330.     (-s $ignore) or next;
  331.  
  332.     if ("$ignore" eq "POTFILES.ignore")
  333.     {
  334.         print "The usage of POTFILES.ignore is deprecated. Please consider moving the\n".
  335.           "content of this file to POTFILES.skip.\n";
  336.     }
  337.  
  338.     print "Found $ignore: Ignoring files...\n" if $VERBOSE;
  339.     open FILE, "<$ignore" or die "ERROR: Failed to open $ignore!\n";
  340.         
  341.     while (<FILE>)
  342.     {
  343.         push @buf_potfiles_ignore, $_ unless /^(#|\s*$)/;
  344.     }
  345.     close FILE;
  346.  
  347.     @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
  348.     }
  349.  
  350.     foreach my $file (@buf_i18n_plain)
  351.     {
  352.     my $in_comment = 0;
  353.     my $in_macro = 0;
  354.  
  355.     open FILE, "<$file";
  356.     while (<FILE>)
  357.     {
  358.         # Handle continued multi-line comment.
  359.         if ($in_comment)
  360.         {
  361.         next unless s-.*\*/--;
  362.         $in_comment = 0;
  363.         }
  364.  
  365.         # Handle continued macro.
  366.         if ($in_macro)
  367.         {
  368.         $in_macro = 0 unless /\\$/;
  369.         next;
  370.         }
  371.  
  372.         # Handle start of macro (or any preprocessor directive).
  373.         if (/^\s*\#/)
  374.         {
  375.         $in_macro = 1 if /^([^\\]|\\.)*\\$/;
  376.         next;
  377.         }
  378.  
  379.         # Handle comments and quoted text.
  380.         while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
  381.         {
  382.         my $match = $1;
  383.         if ($match eq "/*")
  384.         {
  385.             if (!s-/\*.*?\*/--)
  386.             {
  387.             s-/\*.*--;
  388.             $in_comment = 1;
  389.             }
  390.         }
  391.         elsif ($match eq "//")
  392.         {
  393.             s-//.*--;
  394.         }
  395.         else # ' or "
  396.         {
  397.             if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
  398.             {
  399.             warn "mismatched quotes at line $. in $file\n";
  400.             s-$match.*--;
  401.             }
  402.         }
  403.         }        
  404.  
  405.         if (/\.GetString ?\(QUOTEDTEXT/)
  406.         {
  407.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  408.                     ## Remove the first 3 chars and add newline
  409.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  410.                 }
  411.         last;
  412.         }
  413.  
  414.         if (/_\(QUOTEDTEXT/)
  415.         {
  416.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  417.                     ## Remove the first 3 chars and add newline
  418.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  419.                 }
  420.         last;
  421.         }
  422.     }
  423.     close FILE;
  424.     }
  425.  
  426.     foreach my $file (@buf_i18n_xml) 
  427.     {
  428.     open FILE, "<$file";
  429.     
  430.     while (<FILE>) 
  431.     {
  432.         # FIXME: share the pattern matching code with intltool-extract
  433.         if (/\s_(.*)=\"/ || /<_[^>]+>/ || /translatable=\"yes\"/)
  434.         {
  435.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  436.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  437.                 }
  438.         last;
  439.         }
  440.     }
  441.     close FILE;
  442.     }
  443.  
  444.     foreach my $file (@buf_i18n_ini)
  445.     {
  446.     open FILE, "<$file";
  447.     while (<FILE>) 
  448.     {
  449.         if (/_(.*)=/)
  450.         {
  451.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  452.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  453.                 }
  454.         last;
  455.         }
  456.     }
  457.     close FILE;
  458.     }
  459.  
  460.     foreach my $file (@buf_i18n_xml_unmarked)
  461.     {
  462.         if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  463.             push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  464.         }
  465.     }
  466.  
  467.  
  468.     @buf_allfiles_sorted = sort (@buf_allfiles);
  469.     @buf_potfiles_sorted = sort (@buf_potfiles);
  470.  
  471.     my %in2;
  472.     foreach (@buf_potfiles_sorted) 
  473.     {
  474.     $in2{$_} = 1;
  475.     }
  476.  
  477.     my @result;
  478.  
  479.     foreach (@buf_allfiles_sorted)
  480.     {
  481.     if (!exists($in2{$_}))
  482.     {
  483.         push @result, $_
  484.     }
  485.     }
  486.  
  487.     my @buf_potfiles_notexist;
  488.  
  489.     foreach (@buf_potfiles_sorted)
  490.     {
  491.     chomp (my $dummy = $_);
  492.     if ("$dummy" ne "" and ! -f "../$dummy")
  493.     {
  494.         push @buf_potfiles_notexist, $_;
  495.     }
  496.     }
  497.  
  498.     ## Save file with information about the files missing
  499.     ## if any, and give information about this procedure.
  500.     if (@result + @buf_potfiles_notexist > 0)
  501.     {
  502.     if (@result) 
  503.     {
  504.         print "\n" if $VERBOSE;
  505.         unlink "missing";
  506.         open OUT, ">missing";
  507.         print OUT @result;
  508.         close OUT;
  509.         warn "\e[1mThe following files contain translations and are currently not in use. Please\e[0m\n".
  510.              "\e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.\e[0m\n\n";
  511.         print STDERR @result, "\n";
  512.         warn "If some of these files are left out on purpose then please add them to\n".
  513.          "POTFILES.skip instead of POTFILES.in. A file \e[1m'missing'\e[0m containing this list\n".
  514.          "of left out files has been written in the current directory.\n";
  515.     }
  516.     if (@buf_potfiles_notexist)
  517.     {
  518.         unlink "notexist";
  519.         open OUT, ">notexist";
  520.         print OUT @buf_potfiles_notexist;
  521.         close OUT;
  522.         warn "\n" if ($VERBOSE or @result);
  523.         warn "\e[1mThe following files do not exist anymore:\e[0m\n\n";
  524.         warn @buf_potfiles_notexist, "\n";
  525.         warn "Please remove them from POTFILES.in or POTFILES.skip. A file \e[1m'notexist'\e[0m\n".
  526.          "containing this list of absent files has been written in the current directory.\n";
  527.     }
  528.     }
  529.  
  530.     ## If there is nothing to complain about, notify the user
  531.     else {
  532.     print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
  533.     }
  534. }
  535.  
  536. sub Console_WriteError_InvalidOption
  537. {
  538.     ## Handle invalid arguments
  539.     print STDERR "Try `${PROGRAM} --help' for more information.\n";
  540.     exit 1;
  541. }
  542.  
  543. sub GenerateHeaders
  544. {
  545.     my $EXTRACT = "@INTLTOOL_EXTRACT@";
  546.     chomp $EXTRACT;
  547.  
  548.     $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};
  549.  
  550.     ## Generate the .h header files, so we can allow glade and
  551.     ## xml translation support
  552.     if (! -x "$EXTRACT")
  553.     {
  554.     print STDERR "\n *** The intltool-extract script wasn't found!"
  555.          ."\n *** Without it, intltool-update can not generate files.\n";
  556.     exit;
  557.     }
  558.     else
  559.     {
  560.     open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.\n";
  561.     
  562.     while (<FILE>) 
  563.     {
  564.        chomp;
  565.        next if /^\[\s*encoding/;
  566.  
  567.        ## Find xml files in POTFILES.in and generate the
  568.        ## files with help from the extract script
  569.  
  570.        my $gettext_type= &POFile_DetermineType ($1);
  571.  
  572.        if (/\.($xml_support|$ini_support)$/ || /^\[/)
  573.        {
  574.            s/^\[[^\[].*]\s*//;
  575.  
  576.            my $filename = "../$_";
  577.  
  578.            if ($VERBOSE)
  579.            {
  580.            system ($EXTRACT, "--update", "--srcdir=$SRCDIR",
  581.                "--type=$gettext_type", $filename);
  582.            } 
  583.            else 
  584.            {
  585.             system ($EXTRACT, "--update", "--type=$gettext_type", 
  586.                "--srcdir=$SRCDIR", "--quiet", $filename);
  587.            }
  588.        }
  589.        }
  590.        close FILE;
  591.    }
  592. }
  593.  
  594. #
  595. # Generate .pot file from POTFILES.in
  596. #
  597. sub GeneratePOTemplate
  598. {
  599.     my $XGETTEXT = $ENV{"XGETTEXT"} || "@INTLTOOL_XGETTEXT@";
  600.     my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
  601.     chomp $XGETTEXT;
  602.  
  603.     if (! -x $XGETTEXT)
  604.     {
  605.     print STDERR " *** xgettext is not found on this system!\n".
  606.              " *** Without it, intltool-update can not extract strings.\n";
  607.     exit;
  608.     }
  609.  
  610.     print "Building $MODULE.pot...\n" if $VERBOSE;
  611.  
  612.     open INFILE, $POTFILES_in;
  613.     unlink "POTFILES.in.temp";
  614.     open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");
  615.  
  616.     my $gettext_support_nonascii = 0;
  617.  
  618.     # checks for GNU gettext >= 0.12
  619.     my $dummy = `$XGETTEXT --version --from-code=UTF-8 >/dev/null 2>/dev/null`;
  620.     if ($? == 0)
  621.     {
  622.     $gettext_support_nonascii = 1;
  623.     }
  624.     else
  625.     {
  626.     # urge everybody to upgrade gettext
  627.     print STDERR "WARNING: This version of gettext does not support extracting non-ASCII\n".
  628.              "         strings. That means you should install a version of gettext\n".
  629.              "         that supports non-ASCII strings (such as GNU gettext >= 0.12),\n".
  630.              "         or have to let non-ASCII strings untranslated. (If there is any)\n";
  631.     }
  632.  
  633.     my $encoding = "ASCII";
  634.     my $forced_gettext_code;
  635.     my @temp_headers;
  636.     my $encoding_problem_is_reported = 0;
  637.  
  638.     while (<INFILE>) 
  639.     {
  640.     next if (/^#/ or /^\s*$/);
  641.  
  642.     chomp;
  643.  
  644.     my $gettext_code;
  645.  
  646.     if (/^\[\s*encoding:\s*(.*)\s*\]/)
  647.     {
  648.         $forced_gettext_code=$1;
  649.     }
  650.     elsif (/\.($xml_support|$ini_support)$/ || /^\[/)
  651.     {
  652.         s/^\[.*]\s*//;
  653.         print OUTFILE "$_.h\n";
  654.         push @temp_headers, "../$_.h";
  655.         $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
  656.     } 
  657.     else 
  658.     {
  659.         if ($SRCDIR eq ".") {
  660.             print OUTFILE "$_\n";
  661.         } else {
  662.             print OUTFILE "$SRCDIR/../$_\n";
  663.         }
  664.         $gettext_code = &TextFile_DetermineEncoding ("../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
  665.     }
  666.  
  667.     next if (! $gettext_support_nonascii);
  668.  
  669.     if (defined $forced_gettext_code)
  670.     {
  671.         $encoding=$forced_gettext_code;
  672.     }
  673.     elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
  674.     {
  675.         if ($encoding eq "ASCII")
  676.         {
  677.         $encoding=$gettext_code;
  678.         }
  679.         elsif ($gettext_code ne "ASCII")
  680.         {
  681.         # Only report once because the message is quite long
  682.         if (! $encoding_problem_is_reported)
  683.         {
  684.             print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
  685.                  "         but $PROGRAM thinks that most of the source files are in\n".
  686.                  "         $encoding encoding, while \"$_\" is (likely) in\n".
  687.                         "         $gettext_code encoding. If you are sure that all translatable strings\n".
  688.                  "         are in same encoding (say UTF-8), please \e[1m*prepend*\e[0m the following\n".
  689.                  "         line to POTFILES.in:\n\n".
  690.                  "                 [encoding: UTF-8]\n\n".
  691.                  "         and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
  692.                  "(such warning message will only be reported once.)\n";
  693.             $encoding_problem_is_reported = 1;
  694.         }
  695.         }
  696.     }
  697.     }
  698.  
  699.     close OUTFILE;
  700.     close INFILE;
  701.  
  702.     unlink "$MODULE.pot";
  703.     my @xgettext_argument=("$XGETTEXT",
  704.                "--add-comments",
  705.                "--directory\=\.\.",
  706.                "--output\=$MODULE\.pot",
  707.                "--files-from\=\.\/POTFILES\.in\.temp");
  708.     my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
  709.     push @xgettext_argument, $XGETTEXT_KEYWORDS;
  710.     push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
  711.     push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
  712.     my $xgettext_command = join ' ', @xgettext_argument;
  713.  
  714.     # intercept xgettext error message
  715.     print "Running $xgettext_command\n" if $VERBOSE;
  716.     my $xgettext_error_msg = `$xgettext_command 2>\&1`;
  717.     my $command_failed = $?;
  718.  
  719.     unlink "POTFILES.in.temp";
  720.  
  721.     print "Removing generated header (.h) files..." if $VERBOSE;
  722.     unlink foreach (@temp_headers);
  723.     print "done.\n" if $VERBOSE;
  724.  
  725.     if (! $command_failed)
  726.     {
  727.     if (! -e "$MODULE.pot")
  728.     {
  729.         print "None of the files in POTFILES.in contain strings marked for translation.\n" if $VERBOSE;
  730.     }
  731.     else
  732.     {
  733.         print "Wrote $MODULE.pot\n" if $VERBOSE;
  734.     }
  735.     }
  736.     else
  737.     {
  738.     if ($xgettext_error_msg =~ /--from-code/)
  739.     {
  740.         # replace non-ASCII error message with a more useful one.
  741.         print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCII\n".
  742.              "       string marked for translation. Please make sure that all strings marked\n".
  743.              "       for translation are in uniform encoding (say UTF-8), then \e[1m*prepend*\e[0m the\n".
  744.              "       following line to POTFILES.in and rerun $PROGRAM:\n\n".
  745.              "           [encoding: UTF-8]\n\n";
  746.     }
  747.     else
  748.     {
  749.         print STDERR "$xgettext_error_msg";
  750.         if (-e "$MODULE.pot")
  751.         {
  752.         # is this possible?
  753.         print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
  754.                  "       Please consult error message above if there is any.\n";
  755.         }
  756.         else
  757.         {
  758.         print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
  759.                  "       error message above if there is any.\n";
  760.         }
  761.     }
  762.     exit (1);
  763.     }
  764. }
  765.  
  766. sub POFile_Update
  767. {
  768.     -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
  769.  
  770.     my $MSGMERGE = $ENV{"MSGMERGE"} || "@INTLTOOL_MSGMERGE@";
  771.     my ($lang, $outfile) = @_;
  772.  
  773.     print "Merging $lang.po with $MODULE.pot..." if $VERBOSE;
  774.  
  775.     my $infile = "$lang.po";
  776.     $outfile = "$lang.po" if ($outfile eq "");
  777.  
  778.     # I think msgmerge won't overwrite old file if merge is not successful
  779.     system ("$MSGMERGE", "-o", $outfile, $infile, "$MODULE.pot");
  780. }
  781.  
  782. sub Console_WriteError_NotExisting
  783. {
  784.     my ($file) = @_;
  785.  
  786.     ## Report error if supplied language file is non-existing
  787.     print STDERR "$PROGRAM: $file does not exist!\n";
  788.     print STDERR "Try '$PROGRAM --help' for more information.\n";
  789.     exit;
  790. }
  791.  
  792. sub GatherPOFiles
  793. {
  794.     my @po_files = glob ("./*.po");
  795.  
  796.     @languages = map (&POFile_GetLanguage, @po_files);
  797.  
  798.     foreach my $lang (@languages) 
  799.     {
  800.     $po_files_by_lang{$lang} = shift (@po_files);
  801.     }
  802. }
  803.  
  804. sub POFile_GetLanguage ($)
  805. {
  806.     s/^(.*\/)?(.+)\.po$/$2/;
  807.     return $_;
  808. }
  809.  
  810. sub Console_Write_TranslationStatus
  811. {
  812.     my ($lang, $output_file) = @_;
  813.     my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
  814.  
  815.     $output_file = "$lang.po" if ($output_file eq "");
  816.  
  817.     system ("$MSGFMT", "-o", "/dev/null", "--statistics", $output_file);
  818. }
  819.  
  820. sub Console_Write_CoverageReport
  821. {
  822.     my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";
  823.  
  824.     &GatherPOFiles;
  825.  
  826.     foreach my $lang (@languages) 
  827.     {
  828.     print "$lang: ";
  829.     &POFile_Update ($lang, "");
  830.     }
  831.  
  832.     print "\n\n * Current translation support in $MODULE \n\n";
  833.  
  834.     foreach my $lang (@languages)
  835.     {
  836.     print "$lang: ";
  837.     system ("$MSGFMT", "-o", "/dev/null", "--statistics", "$lang.po");
  838.     }
  839. }
  840.  
  841. sub SubstituteVariable
  842. {
  843.     my ($str) = @_;
  844.     
  845.     # always need to rewind file whenever it has been accessed
  846.     seek (CONF, 0, 0);
  847.  
  848.     # cache each variable. varhash is global to we can add
  849.     # variables elsewhere.
  850.     while (<CONF>)
  851.     {
  852.     if (/^(\w+)=(.*)$/)
  853.     {
  854.         ($varhash{$1} = $2) =~  s/^["'](.*)["']$/$1/;
  855.     }
  856.     }
  857.     
  858.     if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
  859.     {
  860.     my $rest = $3;
  861.     my $untouched = $1;
  862.     my $sub = $varhash{$2};
  863.     
  864.     return SubstituteVariable ("$untouched$sub$rest");
  865.     }
  866.     
  867.     # We're using Perl backticks ` and "echo -n" here in order to 
  868.     # expand any shell escapes (such as backticks themselves) in every variable
  869.     return `echo -n "$str"`;
  870. }
  871.  
  872. sub CONF_Handle_Open
  873. {
  874.     my $base_dirname = getcwd();
  875.     $base_dirname =~ s@.*/@@;
  876.  
  877.     my ($conf_in, $src_dir);
  878.  
  879.     if ($base_dirname =~ /^po(-.+)?$/) 
  880.     {
  881.     if (-f "Makevars") 
  882.     {
  883.         my $makefile_source;
  884.  
  885.         local (*IN);
  886.         open (IN, "<Makevars") || die "can't open Makevars: $!";
  887.  
  888.         while (<IN>) 
  889.         {
  890.         if (/^top_builddir[ \t]*=/) 
  891.         {
  892.             $src_dir = $_;
  893.             $src_dir =~ s/^top_builddir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
  894.  
  895.             chomp $src_dir;
  896.                     if (-f "$src_dir" . "/configure.ac") {
  897.                         $conf_in = "$src_dir" . "/configure.ac" . "\n";
  898.                     } else {
  899.                         $conf_in = "$src_dir" . "/configure.in" . "\n";
  900.                     }
  901.             last;
  902.         }
  903.         }
  904.         close IN;
  905.  
  906.         $conf_in || die "Cannot find top_builddir in Makevars.";
  907.     }
  908.     elsif (-f "../configure.ac") 
  909.     {
  910.         $conf_in = "../configure.ac";
  911.     } 
  912.     elsif (-f "../configure.in") 
  913.     {
  914.         $conf_in = "../configure.in";
  915.     } 
  916.     else 
  917.     {
  918.         my $makefile_source;
  919.  
  920.         local (*IN);
  921.         open (IN, "<Makefile") || return;
  922.  
  923.         while (<IN>) 
  924.         {
  925.         if (/^top_srcdir[ \t]*=/) 
  926.         {
  927.             $src_dir = $_;            
  928.             $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
  929.  
  930.             chomp $src_dir;
  931.             $conf_in = "$src_dir" . "/configure.in" . "\n";
  932.  
  933.             last;
  934.         }
  935.         }
  936.         close IN;
  937.  
  938.         $conf_in || die "Cannot find top_srcdir in Makefile.";
  939.     }
  940.  
  941.     open (CONF, "<$conf_in");
  942.     }
  943.     else
  944.     {
  945.     print STDERR "$PROGRAM: Unable to proceed.\n" .
  946.              "Make sure to run this script inside the po directory.\n";
  947.     exit;
  948.     }
  949. }
  950.  
  951. sub FindPackageName
  952. {
  953.     my $version;
  954.     my $domain = &FindMakevarsDomain;
  955.     my $name = $domain || "untitled";
  956.  
  957.     &CONF_Handle_Open;
  958.  
  959.     my $conf_source; {
  960.     local (*IN);
  961.     open (IN, "<&CONF") || return $name;
  962.     seek (IN, 0, 0);
  963.     local $/; # slurp mode
  964.     $conf_source = <IN>;
  965.     close IN;
  966.     }
  967.  
  968.     # priority for getting package name:
  969.     # 1. GETTEXT_PACKAGE
  970.     # 2. first argument of AC_INIT (with >= 2 arguments)
  971.     # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
  972.  
  973.     # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m 
  974.     # the \s makes this not work, why?
  975.     if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
  976.     {
  977.     ($name, $version) = ($1, $2);
  978.     $name    =~ s/[\[\]\s]//g;
  979.     $version =~ s/[\[\]\s]//g;
  980.     $varhash{"AC_PACKAGE_NAME"} = $name;
  981.     $varhash{"PACKAGE"} = $name;
  982.     $varhash{"AC_PACKAGE_VERSION"} = $version;
  983.     $varhash{"VERSION"} = $version;
  984.     }
  985.     
  986.     if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m) 
  987.     {
  988.     ($name, $version) = ($1, $2);
  989.     $name    =~ s/[\[\]\s]//g;
  990.     $version =~ s/[\[\]\s]//g;
  991.     $varhash{"AC_PACKAGE_NAME"} = $name;
  992.     $varhash{"PACKAGE"} = $name;
  993.     $varhash{"AC_PACKAGE_VERSION"} = $version;
  994.     $varhash{"VERSION"} = $version;
  995.     }
  996.  
  997.     # \s makes this not work, why?
  998.     $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
  999.     
  1000.     # prepend '$' to auto* internal variables, usually they are
  1001.     # used in configure.in/ac without the '$'
  1002.     $name =~ s/AC_/\$AC_/g;
  1003.     $name =~ s/\$\$/\$/g;
  1004.  
  1005.     $name = $domain if $domain;
  1006.  
  1007.     $name = SubstituteVariable ($name);
  1008.     $name =~ s/^["'](.*)["']$/$1/;
  1009.  
  1010.     return $name if $name;
  1011. }
  1012.  
  1013.  
  1014. sub FindPOTKeywords
  1015. {
  1016.  
  1017.     my $keywords = "--keyword\=\_ --keyword\=N\_ --keyword\=U\_ --keyword\=Q\_";
  1018.     my $varname = "XGETTEXT_OPTIONS";
  1019.     my $make_source; {
  1020.     local (*IN);
  1021.     open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
  1022.     seek (IN, 0, 0);
  1023.     local $/; # slurp mode
  1024.     $make_source = <IN>;
  1025.     close IN;
  1026.     }
  1027.  
  1028.     $keywords = $1 if $make_source =~ /^$varname[ ]*=\[?([^\n\]]+)/m;
  1029.     
  1030.     return $keywords;
  1031. }
  1032.  
  1033. sub FindMakevarsDomain
  1034. {
  1035.  
  1036.     my $domain = "";
  1037.     my $makevars_source; { 
  1038.     local (*IN);
  1039.     open (IN, "<Makevars") || return $domain;
  1040.     seek (IN, 0, 0);
  1041.     local $/; # slurp mode
  1042.     $makevars_source = <IN>;
  1043.     close IN;
  1044.     }
  1045.  
  1046.     $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=\[?([^\n\]\$]+)/m;
  1047.     $domain =~ s/^\s+//;
  1048.     $domain =~ s/\s+$//;
  1049.     
  1050.     return $domain;
  1051. }
  1052.