summaryrefslogtreecommitdiff
path: root/intltool-update.in
blob: 661d8fe6ac31f17682542f99160830090dedfbd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
#!@INTLTOOL_PERL@ -w
# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-

#
#  The Intltool Message Updater
#
#  Copyright (C) 2000-2003 Free Software Foundation.
#
#  Intltool is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License 
#  version 2 published by the Free Software Foundation.
#
#  Intltool is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  As a special exception to the GNU General Public License, if you
#  distribute this file as part of a program that contains a
#  configuration script generated by Autoconf, you may include it under
#  the same distribution terms that you use for the rest of that program.
#
#  Authors: Kenneth Christiansen <kenneth@gnu.org>
#           Maciej Stachowiak
#           Darin Adler <darin@bentspoon.com>

## Release information
my $PROGRAM = "intltool-update";
my $VERSION = "0.35.0";
my $PACKAGE = "intltool";

## Loaded modules
use strict;
use Getopt::Long;
use Cwd;
use File::Copy;
use File::Find;

## Scalars used by the option stuff
my $HELP_ARG 	   = 0;
my $VERSION_ARG    = 0;
my $DIST_ARG	   = 0;
my $POT_ARG	   = 0;
my $HEADERS_ARG    = 0;
my $MAINTAIN_ARG   = 0;
my $REPORT_ARG     = 0;
my $VERBOSE	   = 0;
my $GETTEXT_PACKAGE = "";
my $OUTPUT_FILE    = "";

my @languages;
my %varhash = ();
my %po_files_by_lang = ();

# Regular expressions to categorize file types.
# FIXME: Please check if the following is correct

my $xml_support =
"xml(?:\\.in)*|".	# http://www.w3.org/XML/ (Note: .in is not required)
"ui|".			# Bonobo specific - User Interface desc. files
"lang|".		# ?
"glade2?(?:\\.in)*|".	# Glade specific - User Interface desc. files (Note: .in is not required)
"scm(?:\\.in)*|".	# ? (Note: .in is not required)
"oaf(?:\\.in)+|".	# DEPRECATED: Replaces by Bonobo .server files 
"etspec|".		# ?
"server(?:\\.in)+|".	# Bonobo specific
"sheet(?:\\.in)+|".	# ?
"schemas(?:\\.in)+|".	# GConf specific
"pong(?:\\.in)+|".	# DEPRECATED: PONG is not used [by GNOME] any longer.
"kbd(?:\\.in)+";	# GOK specific. 

my $ini_support =
"icon(?:\\.in)+|".	# http://www.freedesktop.org/Standards/icon-theme-spec
"desktop(?:\\.in)+|".	# http://www.freedesktop.org/Standards/menu-spec
"caves(?:\\.in)+|".	# GNOME Games specific
"directory(?:\\.in)+|".	# http://www.freedesktop.org/Standards/menu-spec
"soundlist(?:\\.in)+|".	# GNOME specific
"keys(?:\\.in)+|".	# GNOME Mime database specific
"theme(?:\\.in)+|".	# http://www.freedesktop.org/Standards/icon-theme-spec
"service(?:\\.in)+";    # DBus specific

my $buildin_gettext_support = 
"c|y|cs|cc|cpp|c\\+\\+|h|hh|gob|py";

## Always flush buffer when printing
$| = 1;

## Sometimes the source tree will be rooted somewhere else.
my $SRCDIR = ".";
my $POTFILES_in;

$SRCDIR = $ENV{"srcdir"} if $ENV{"srcdir"};
$POTFILES_in = "<$SRCDIR/POTFILES.in";

my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');

## Handle options
GetOptions 
(
 "help" 	       => \$HELP_ARG,
 "version" 	       => \$VERSION_ARG,
 "dist|d"	       => \$DIST_ARG,
 "pot|p"	       => \$POT_ARG,
 "headers|s"	       => \$HEADERS_ARG,
 "maintain|m"	       => \$MAINTAIN_ARG,
 "report|r"	       => \$REPORT_ARG,
 "verbose|x"	       => \$VERBOSE,
 "gettext-package|g=s" => \$GETTEXT_PACKAGE,
 "output-file|o=s"     => \$OUTPUT_FILE,
 ) or &Console_WriteError_InvalidOption;

&Console_Write_IntltoolHelp if $HELP_ARG;
&Console_Write_IntltoolVersion if $VERSION_ARG;

my $arg_count = ($DIST_ARG > 0)
    + ($POT_ARG > 0)
    + ($HEADERS_ARG > 0)
    + ($MAINTAIN_ARG > 0)
    + ($REPORT_ARG > 0);

&Console_Write_IntltoolHelp if $arg_count > 1;

# --version and --help don't require a module name
my $MODULE = $GETTEXT_PACKAGE || &FindPackageName || "unknown";

if ($POT_ARG)
{
    &GenerateHeaders;
    &GeneratePOTemplate;
}
elsif ($HEADERS_ARG)
{
    &GenerateHeaders;
}
elsif ($MAINTAIN_ARG)
{
    &FindLeftoutFiles;
}
elsif ($REPORT_ARG)
{
    &GenerateHeaders;
    &GeneratePOTemplate;
    &Console_Write_CoverageReport;
}
elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
{
    my $lang = $ARGV[0];

    ## Report error if the language file supplied
    ## to the command line is non-existent
    &Console_WriteError_NotExisting("$SRCDIR/$lang.po")
        if ! -s "$SRCDIR/$lang.po";

    if (!$DIST_ARG)
    {
	print "Working, please wait..." if $VERBOSE;
	&GenerateHeaders;
	&GeneratePOTemplate;
    }
    &POFile_Update ($lang, $OUTPUT_FILE);
    &Console_Write_TranslationStatus ($lang, $OUTPUT_FILE);
} 
else 
{
    &Console_Write_IntltoolHelp;
}

exit;

#########

sub Console_Write_IntltoolVersion
{
    print <<_EOF_;
${PROGRAM} (${PACKAGE}) $VERSION
Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.

Copyright (C) 2000-2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
_EOF_
    exit;
}

sub Console_Write_IntltoolHelp
{
    print <<_EOF_;
Usage: ${PROGRAM} [OPTION]... LANGCODE
Updates PO template files and merge them with the translations.

Mode of operation (only one is allowed):
  -p, --pot                   generate the PO template only
  -s, --headers               generate the header files in POTFILES.in
  -m, --maintain              search for left out files from POTFILES.in
  -r, --report                display a status report for the module
  -d, --dist                  merge LANGCODE.po with existing PO template

Extra options:
  -g, --gettext-package=NAME  override PO template name, useful with --pot
  -o, --output-file=FILE      write merged translation to FILE
  -x, --verbose               display lots of feedback
      --help                  display this help and exit
      --version               output version information and exit

Examples of use:
${PROGRAM} --pot    just create a new PO template
${PROGRAM} xy       create new PO template and merge xy.po with it

Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
or send email to <xml-i18n-tools\@gnome.org>.
_EOF_
    exit;
}

sub echo_n
{
    my $str = shift;
    my $ret = `echo "$str"`;

    $ret =~ s/\n$//; # do we need the "s" flag?

    return $ret;
}

sub POFile_DetermineType ($) 
{
   my $type = $_;
   my $gettext_type;

   my $xml_regex     = "(?:" . $xml_support . ")";
   my $ini_regex     = "(?:" . $ini_support . ")";
   my $buildin_regex = "(?:" . $buildin_gettext_support . ")";

   if ($type =~ /\[type: gettext\/([^\]].*)]/) 
   {
	$gettext_type=$1;
   }
   elsif ($type =~ /schemas(\.in)+$/) 
   {
	$gettext_type="schemas";
   }
   elsif ($type =~ /glade2?(\.in)*$/) 
   {
       $gettext_type="glade";
   }
   elsif ($type =~ /scm(\.in)*$/) 
   {
       $gettext_type="scheme";
   }
   elsif ($type =~ /keys(\.in)+$/) 
   {
       $gettext_type="keys";
   }

   # bucket types

   elsif ($type =~ /$xml_regex$/) 
   {
       $gettext_type="xml";
   }
   elsif ($type =~ /$ini_regex$/) 
   { 
       $gettext_type="ini";
   }
   elsif ($type =~ /$buildin_regex$/) 
   {
       $gettext_type="buildin";
   }
   else
   { 
       $gettext_type="unknown"; 
   }

   return "gettext\/$gettext_type";
}

sub TextFile_DetermineEncoding ($) 
{
    my $gettext_code="ASCII"; # All files are ASCII by default
    my $filetype=`file $_ | cut -d ' ' -f 2`;

    if ($? eq "0")
    {
	if ($filetype =~ /^(ISO|UTF)/)
	{
	    chomp ($gettext_code = $filetype);
	}
	elsif ($filetype =~ /^XML/)
	{
	    $gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
	}
    }

    return $gettext_code;
}

sub isNotValidMissing
{
    my ($file) = @_;

    return if $file =~ /^\{arch\}\/.*$/;
    return if $file =~ /^$varhash{"PACKAGE"}-$varhash{"VERSION"}\/.*$/;
}

sub FindLeftoutFiles
{
    my (@buf_i18n_plain,
	@buf_i18n_xml,
	@buf_i18n_xml_unmarked,
	@buf_i18n_ini,
	@buf_potfiles,
	@buf_potfiles_ignore,
	@buf_allfiles,
	@buf_allfiles_sorted,
	@buf_potfiles_sorted
    );

    ## Search and find all translatable files
    find sub { 
	push @buf_i18n_plain,        "$File::Find::name" if /\.($buildin_gettext_support)$/;
	push @buf_i18n_xml,          "$File::Find::name" if /\.($xml_support)$/;
	push @buf_i18n_ini,          "$File::Find::name" if /\.($ini_support)$/;
	push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
	}, "..";


    open POTFILES, $POTFILES_in or die "$PROGRAM:  there's no POTFILES.in!\n";
    @buf_potfiles = grep !/^(#|\s*$)/, <POTFILES>;
    close POTFILES;

    foreach (@buf_potfiles) {
	s/^\[.*]\s*//;
    }

    print "Searching for missing translatable files...\n" if $VERBOSE;

    ## Check if we should ignore some found files, when
    ## comparing with POTFILES.in
    foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
    {
	(-s $ignore) or next;

	if ("$ignore" eq "POTFILES.ignore")
	{
	    print "The usage of POTFILES.ignore is deprecated. Please consider moving the\n".
		  "content of this file to POTFILES.skip.\n";
	}

	print "Found $ignore: Ignoring files...\n" if $VERBOSE;
	open FILE, "<$ignore" or die "ERROR: Failed to open $ignore!\n";
	    
	while (<FILE>)
	{
	    push @buf_potfiles_ignore, $_ unless /^(#|\s*$)/;
	}
	close FILE;

	@buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
    }

    foreach my $file (@buf_i18n_plain)
    {
	my $in_comment = 0;
	my $in_macro = 0;

	open FILE, "<$file";
	while (<FILE>)
	{
	    # Handle continued multi-line comment.
	    if ($in_comment)
	    {
		next unless s-.*\*/--;
		$in_comment = 0;
	    }

	    # Handle continued macro.
	    if ($in_macro)
	    {
		$in_macro = 0 unless /\\$/;
		next;
	    }

	    # Handle start of macro (or any preprocessor directive).
	    if (/^\s*\#/)
	    {
		$in_macro = 1 if /^([^\\]|\\.)*\\$/;
		next;
	    }

	    # Handle comments and quoted text.
	    while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
	    {
		my $match = $1;
		if ($match eq "/*")
		{
		    if (!s-/\*.*?\*/--)
		    {
			s-/\*.*--;
			$in_comment = 1;
		    }
		}
		elsif ($match eq "//")
		{
		    s-//.*--;
		}
		else # ' or "
		{
		    if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
		    {
			warn "mismatched quotes at line $. in $file\n";
			s-$match.*--;
		    }
		}
	    }	    

	    if (/\.GetString ?\(QUOTEDTEXT/)
	    {
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
                    ## Remove the first 3 chars and add newline
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
                }
		last;
	    }

	    if (/_\(QUOTEDTEXT/)
	    {
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
                    ## Remove the first 3 chars and add newline
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
                }
		last;
	    }
	}
	close FILE;
    }

    foreach my $file (@buf_i18n_xml) 
    {
	open FILE, "<$file";
	
	while (<FILE>) 
	{
	    # FIXME: share the pattern matching code with intltool-extract
	    if (/\s_[-A-Za-z0-9._:]+\s*=\s*\"([^"]+)\"/ || /<_[^>]+>/ || /translatable=\"yes\"/)
	    {
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
                }
		last;
	    }
	}
	close FILE;
    }

    foreach my $file (@buf_i18n_ini)
    {
	open FILE, "<$file";
	while (<FILE>) 
	{
	    if (/_(.*)=/)
	    {
                if (defined isNotValidMissing (unpack("x3 A*", $file))) {
                    push @buf_allfiles, unpack("x3 A*", $file) . "\n";
                }
		last;
	    }
	}
	close FILE;
    }

    foreach my $file (@buf_i18n_xml_unmarked)
    {
        if (defined isNotValidMissing (unpack("x3 A*", $file))) {
            push @buf_allfiles, unpack("x3 A*", $file) . "\n";
        }
    }


    @buf_allfiles_sorted = sort (@buf_allfiles);
    @buf_potfiles_sorted = sort (@buf_potfiles);

    my %in2;
    foreach (@buf_potfiles_sorted) 
    {
	$in2{$_} = 1;
    }

    my @result;

    foreach (@buf_allfiles_sorted)
    {
	if (!exists($in2{$_}))
	{
	    push @result, $_
	}
    }

    my @buf_potfiles_notexist;

    foreach (@buf_potfiles_sorted)
    {
	chomp (my $dummy = $_);
	if ("$dummy" ne "" and ! -f "../$dummy")
	{
	    push @buf_potfiles_notexist, $_;
	}
    }

    ## Save file with information about the files missing
    ## if any, and give information about this procedure.
    if (@result + @buf_potfiles_notexist > 0)
    {
	if (@result) 
	{
	    print "\n" if $VERBOSE;
	    unlink "missing";
	    open OUT, ">missing";
	    print OUT @result;
	    close OUT;
	    warn "\e[1mThe following files contain translations and are currently not in use. Please\e[0m\n".
	         "\e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.\e[0m\n\n";
	    print STDERR @result, "\n";
	    warn "If some of these files are left out on purpose then please add them to\n".
		 "POTFILES.skip instead of POTFILES.in. A file \e[1m'missing'\e[0m containing this list\n".
		 "of left out files has been written in the current directory.\n";
	}
	if (@buf_potfiles_notexist)
	{
	    unlink "notexist";
	    open OUT, ">notexist";
	    print OUT @buf_potfiles_notexist;
	    close OUT;
	    warn "\n" if ($VERBOSE or @result);
	    warn "\e[1mThe following files do not exist anymore:\e[0m\n\n";
	    warn @buf_potfiles_notexist, "\n";
	    warn "Please remove them from POTFILES.in or POTFILES.skip. A file \e[1m'notexist'\e[0m\n".
		 "containing this list of absent files has been written in the current directory.\n";
	}
    }

    ## If there is nothing to complain about, notify the user
    else {
	print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
    }
}

sub Console_WriteError_InvalidOption
{
    ## Handle invalid arguments
    print STDERR "Try `${PROGRAM} --help' for more information.\n";
    exit 1;
}

sub GenerateHeaders
{
    my $EXTRACT = "@INTLTOOL_EXTRACT@";
    chomp $EXTRACT;

    $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};

    ## Generate the .h header files, so we can allow glade and
    ## xml translation support
    if (! -x "$EXTRACT")
    {
	print STDERR "\n *** The intltool-extract script wasn't found!"
	     ."\n *** Without it, intltool-update can not generate files.\n";
	exit;
    }
    else
    {
	open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.\n";
	
	while (<FILE>) 
	{
	   chomp;
	   next if /^\[\s*encoding/;

	   ## Find xml files in POTFILES.in and generate the
	   ## files with help from the extract script

	   my $gettext_type= &POFile_DetermineType ($1);

	   if (/\.($xml_support|$ini_support)$/ || /^\[/)
	   {
	       s/^\[[^\[].*]\s*//;

	       my $filename = "../$_";

	       if ($VERBOSE)
	       {
		   system ($EXTRACT, "--update", "--srcdir=$SRCDIR",
			   "--type=$gettext_type", $filename);
	       } 
	       else 
	       {
	 	   system ($EXTRACT, "--update", "--type=$gettext_type", 
			   "--srcdir=$SRCDIR", "--quiet", $filename);
	       }
	   }
       }
       close FILE;
   }
}

#
# Generate .pot file from POTFILES.in
#
sub GeneratePOTemplate
{
    my $XGETTEXT = $ENV{"XGETTEXT"} || "@INTLTOOL_XGETTEXT@";
    my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
    chomp $XGETTEXT;

    if (! -x $XGETTEXT)
    {
	print STDERR " *** xgettext is not found on this system!\n".
		     " *** Without it, intltool-update can not extract strings.\n";
	exit;
    }

    print "Building $MODULE.pot...\n" if $VERBOSE;

    open INFILE, $POTFILES_in;
    unlink "POTFILES.in.temp";
    open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");

    my $gettext_support_nonascii = 0;

    # checks for GNU gettext >= 0.12
    my $dummy = `$XGETTEXT --version --from-code=UTF-8 >$devnull 2>$devnull`;
    if ($? == 0)
    {
	$gettext_support_nonascii = 1;
    }
    else
    {
	# urge everybody to upgrade gettext
	print STDERR "WARNING: This version of gettext does not support extracting non-ASCII\n".
		     "         strings. That means you should install a version of gettext\n".
		     "         that supports non-ASCII strings (such as GNU gettext >= 0.12),\n".
		     "         or have to let non-ASCII strings untranslated. (If there is any)\n";
    }

    my $encoding = "ASCII";
    my $forced_gettext_code;
    my @temp_headers;
    my $encoding_problem_is_reported = 0;

    while (<INFILE>) 
    {
	next if (/^#/ or /^\s*$/);

	chomp;

	my $gettext_code;

	if (/^\[\s*encoding:\s*(.*)\s*\]/)
	{
	    $forced_gettext_code=$1;
	}
	elsif (/\.($xml_support|$ini_support)$/ || /^\[/)
	{
	    s/^\[.*]\s*//;
            print OUTFILE "../$_.h\n";
	    push @temp_headers, "../$_.h";
	    $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
	} 
	else 
	{
	    if ($SRCDIR eq ".") {
	        print OUTFILE "../$_\n";
	    } else {
	        print OUTFILE "$SRCDIR/../$_\n";
	    }
	    $gettext_code = &TextFile_DetermineEncoding ("../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
	}

	next if (! $gettext_support_nonascii);

	if (defined $forced_gettext_code)
	{
	    $encoding=$forced_gettext_code;
	}
	elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
	{
	    if ($encoding eq "ASCII")
	    {
		$encoding=$gettext_code;
	    }
	    elsif ($gettext_code ne "ASCII")
	    {
		# Only report once because the message is quite long
		if (! $encoding_problem_is_reported)
		{
		    print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
				 "         but $PROGRAM thinks that most of the source files are in\n".
				 "         $encoding encoding, while \"$_\" is (likely) in\n".
		       		 "         $gettext_code encoding. If you are sure that all translatable strings\n".
				 "         are in same encoding (say UTF-8), please \e[1m*prepend*\e[0m the following\n".
				 "         line to POTFILES.in:\n\n".
				 "                 [encoding: UTF-8]\n\n".
				 "         and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
				 "(such warning message will only be reported once.)\n";
		    $encoding_problem_is_reported = 1;
		}
	    }
	}
    }

    close OUTFILE;
    close INFILE;

    unlink "$MODULE.pot";
    my @xgettext_argument=("$XGETTEXT",
			   "--add-comments",
			   "--directory\=\.",
			   "--output\=$MODULE\.pot",
			   "--files-from\=\.\/POTFILES\.in\.temp");
    my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
    push @xgettext_argument, $XGETTEXT_KEYWORDS;
    my $MSGID_BUGS_ADDRESS = &FindMakevarsBugAddress;
    push @xgettext_argument, "--msgid-bugs-address\=$MSGID_BUGS_ADDRESS" if $MSGID_BUGS_ADDRESS;
    push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
    push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
    my $xgettext_command = join ' ', @xgettext_argument;

    # intercept xgettext error message
    print "Running $xgettext_command\n" if $VERBOSE;
    my $xgettext_error_msg = `$xgettext_command 2>\&1`;
    my $command_failed = $?;

    unlink "POTFILES.in.temp";

    print "Removing generated header (.h) files..." if $VERBOSE;
    unlink foreach (@temp_headers);
    print "done.\n" if $VERBOSE;

    if (! $command_failed)
    {
	if (! -e "$MODULE.pot")
	{
	    print "None of the files in POTFILES.in contain strings marked for translation.\n" if $VERBOSE;
	}
	else
	{
	    print "Wrote $MODULE.pot\n" if $VERBOSE;
	}
    }
    else
    {
	if ($xgettext_error_msg =~ /--from-code/)
	{
	    # replace non-ASCII error message with a more useful one.
	    print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCII\n".
			 "       string marked for translation. Please make sure that all strings marked\n".
			 "       for translation are in uniform encoding (say UTF-8), then \e[1m*prepend*\e[0m the\n".
			 "       following line to POTFILES.in and rerun $PROGRAM:\n\n".
			 "           [encoding: UTF-8]\n\n";
	}
	else
	{
	    print STDERR "$xgettext_error_msg";
	    if (-e "$MODULE.pot")
	    {
		# is this possible?
		print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
			     "       Please consult error message above if there is any.\n";
	    }
	    else
	    {
		print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
			     "       error message above if there is any.\n";
	    }
	}
	exit (1);
    }
}

sub POFile_Update
{
    -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";

    my $MSGMERGE = $ENV{"MSGMERGE"} || "@INTLTOOL_MSGMERGE@";
    my ($lang, $outfile) = @_;

    print "Merging $SRCDIR/$lang.po with $MODULE.pot..." if $VERBOSE;

    my $infile = "$SRCDIR/$lang.po";
    $outfile = "$SRCDIR/$lang.po" if ($outfile eq "");

    # I think msgmerge won't overwrite old file if merge is not successful
    system ("$MSGMERGE", "-o", $outfile, $infile, "$MODULE.pot");
}

sub Console_WriteError_NotExisting
{
    my ($file) = @_;

    ## Report error if supplied language file is non-existing
    print STDERR "$PROGRAM: $file does not exist!\n";
    print STDERR "Try '$PROGRAM --help' for more information.\n";
    exit;
}

sub GatherPOFiles
{
    my @po_files = glob ("./*.po");

    @languages = map (&POFile_GetLanguage, @po_files);

    foreach my $lang (@languages) 
    {
	$po_files_by_lang{$lang} = shift (@po_files);
    }
}

sub POFile_GetLanguage ($)
{
    s/^(.*\/)?(.+)\.po$/$2/;
    return $_;
}

sub Console_Write_TranslationStatus
{
    my ($lang, $output_file) = @_;
    my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";

    $output_file = "$SRCDIR/$lang.po" if ($output_file eq "");

    system ("$MSGFMT", "-o", "$devnull", "--verbose", $output_file);
}

sub Console_Write_CoverageReport
{
    my $MSGFMT = $ENV{"MSGFMT"} || "@INTLTOOL_MSGFMT@";

    &GatherPOFiles;

    foreach my $lang (@languages) 
    {
	print "$lang: ";
	&POFile_Update ($lang, "");
    }

    print "\n\n * Current translation support in $MODULE \n\n";

    foreach my $lang (@languages)
    {
	print "$lang: ";
	system ("$MSGFMT", "-o", "$devnull", "--verbose", "$SRCDIR/$lang.po");
    }
}

sub SubstituteVariable
{
    my ($str) = @_;
    
    # always need to rewind file whenever it has been accessed
    seek (CONF, 0, 0);

    # cache each variable. varhash is global to we can add
    # variables elsewhere.
    while (<CONF>)
    {
	if (/^(\w+)=(.*)$/)
	{
	    ($varhash{$1} = $2) =~  s/^["'](.*)["']$/$1/;
	}
    }
    
    if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
    {
	my $rest = $3;
	my $untouched = $1;
	my $sub = "";
        # Ignore recursive definitions of variables
        $sub = $varhash{$2} if defined $varhash{$2} and $varhash{$2} !~ /\${?$2}?/;

	return SubstituteVariable ("$untouched$sub$rest");
    }
    
    # We're using Perl backticks ` and "echo -n" here in order to 
    # expand any shell escapes (such as backticks themselves) in every variable
    return echo_n ($str);
}

sub CONF_Handle_Open
{
    my $base_dirname = getcwd();
    $base_dirname =~ s@.*/@@;

    my ($conf_in, $src_dir);

    if ($base_dirname =~ /^po(-.+)?$/) 
    {
	if (-f "Makevars") 
	{
	    my $makefile_source;

	    local (*IN);
	    open (IN, "<Makevars") || die "can't open Makevars: $!";

	    while (<IN>) 
	    {
		if (/^top_builddir[ \t]*=/) 
		{
		    $src_dir = $_;
		    $src_dir =~ s/^top_builddir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;

		    chomp $src_dir;
                    if (-f "$src_dir" . "/configure.ac") {
                        $conf_in = "$src_dir" . "/configure.ac" . "\n";
                    } else {
                        $conf_in = "$src_dir" . "/configure.in" . "\n";
                    }
		    last;
		}
	    }
	    close IN;

	    $conf_in || die "Cannot find top_builddir in Makevars.";
	}
	elsif (-f "../configure.ac") 
	{
	    $conf_in = "../configure.ac";
	} 
	elsif (-f "../configure.in") 
	{
	    $conf_in = "../configure.in";
	} 
	else 
	{
	    my $makefile_source;

	    local (*IN);
	    open (IN, "<Makefile") || return;

	    while (<IN>) 
	    {
		if (/^top_srcdir[ \t]*=/) 
		{
		    $src_dir = $_;		    
		    $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;

		    chomp $src_dir;
		    $conf_in = "$src_dir" . "/configure.in" . "\n";

		    last;
		}
	    }
	    close IN;

	    $conf_in || die "Cannot find top_srcdir in Makefile.";
	}

	open (CONF, "<$conf_in");
    }
    else
    {
	print STDERR "$PROGRAM: Unable to proceed.\n" .
		     "Make sure to run this script inside the po directory.\n";
	exit;
    }
}

sub FindPackageName
{
    my $version;
    my $domain = &FindMakevarsDomain;
    my $name = $domain || "untitled";

    &CONF_Handle_Open;

    my $conf_source; {
	local (*IN);
	open (IN, "<&CONF") || return $name;
	seek (IN, 0, 0);
	local $/; # slurp mode
	$conf_source = <IN>;
	close IN;
    }

    # priority for getting package name:
    # 1. GETTEXT_PACKAGE
    # 2. first argument of AC_INIT (with >= 2 arguments)
    # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)

    # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m 
    # the \s makes this not work, why?
    if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
    {
	($name, $version) = ($1, $2);
	$name    =~ s/[\[\]\s]//g;
	$version =~ s/[\[\]\s]//g;
	$varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
	$varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
	$varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
	$varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
    }
    
    if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m) 
    {
	($name, $version) = ($1, $2);
	$name    =~ s/[\[\]\s]//g;
	$version =~ s/[\[\]\s]//g;
	$varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
	$varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
	$varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
	$varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
    }

    # \s makes this not work, why?
    $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
    
    # m4 macros AC_PACKAGE_NAME, AC_PACKAGE_VERSION etc. have same value
    # as corresponding $PACKAGE_NAME, $PACKAGE_VERSION etc. shell variables.
    $name =~ s/\bAC_PACKAGE_/\$PACKAGE_/g;

    $name = $domain if $domain;

    $name = SubstituteVariable ($name);
    $name =~ s/^["'](.*)["']$/$1/;

    return $name if $name;
}


sub FindPOTKeywords
{

    my $keywords = "--keyword\=\_ --keyword\=N\_ --keyword\=U\_ --keyword\=Q\_";
    my $varname = "XGETTEXT_OPTIONS";
    my $make_source; {
	local (*IN);
	open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
	seek (IN, 0, 0);
	local $/; # slurp mode
	$make_source = <IN>;
	close IN;
    }

    $keywords = $1 if $make_source =~ /^$varname[ ]*=\[?([^\n\]]+)/m;
    
    return $keywords;
}

sub FindMakevarsDomain
{

    my $domain = "";
    my $makevars_source; { 
	local (*IN);
	open (IN, "<Makevars") || return $domain;
	seek (IN, 0, 0);
	local $/; # slurp mode
	$makevars_source = <IN>;
	close IN;
    }

    $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=\[?([^\n\]\$]+)/m;
    $domain =~ s/^\s+//;
    $domain =~ s/\s+$//;
    
    return $domain;
}

sub FindMakevarsBugAddress
{

    my $address = "";
    my $makevars_source; { 
	local (*IN);
	open (IN, "<Makevars") || return undef;
	seek (IN, 0, 0);
	local $/; # slurp mode
	$makevars_source = <IN>;
	close IN;
    }

    $address = $1 if $makevars_source =~ /^MSGID_BUGS_ADDRESS[ ]*=\[?([^\n\]\$]+)/m;
    $address =~ s/^\s+//;
    $address =~ s/\s+$//;
    
    return $address;
}