--- ./intltool-update.in.in.bak	2003-06-04 11:23:22.000000000 +0800
+++ ./intltool-update.in.in	2003-07-20 14:54:28.000000000 +0800
@@ -3,7 +3,7 @@
 #
 #  The Intltool Message Updater
 #
-#  Copyright (C) 2000-2002 Free Software Foundation.
+#  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 
@@ -55,6 +55,7 @@
 my $conf_file; # remove later
 my %varhash = ();
 my %po_files_by_lang = ();
+my $encoding = "ASCII";
 
 # Regular expressions to categorize file types.
 # FIXME: Please check if the following is correct
@@ -112,38 +113,38 @@
 # --version and --help don't require a module name
 my $MODULE = $GETTEXT_PACKAGE || &find_package_name;
 
-if ($DIST_ARG) 
+if ($DIST_ARG)
 {
-    if ($ARGV[0] =~ /^[a-z]/)
+    if ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
     {
         &update_po_file ($ARGV[0], $OUTPUT_FILE);
         &print_status ($ARGV[0], $OUTPUT_FILE);
-    } 
-    else 
+    }
+    else
     {
         &print_help;
     }
-} 
-elsif ($POT_ARG) 
+}
+elsif ($POT_ARG)
 {
     &generate_headers;
     &generate_po_template;
-} 
-elsif ($HEADERS_ARG) 
+}
+elsif ($HEADERS_ARG)
 {
     &generate_headers;
-} 
-elsif ($MAINTAIN_ARG) 
+}
+elsif ($MAINTAIN_ARG)
 {
     &find_leftout_files;
-} 
-elsif ($REPORT_ARG) 
+}
+elsif ($REPORT_ARG)
 {
     &print_report;
-} 
-else 
+}
+else
 {
-    if ($ARGV[0] =~ /^[a-z]/) 
+    if ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
     {
         &main ($ARGV[0], $OUTPUT_FILE);
     } 
@@ -162,7 +163,7 @@
     ## Print version information
     print "${PROGRAM} (${PACKAGE}) $VERSION\n";
     print "Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.\n\n";
-    print "Copyright (C) 2000-2002 Free Software Foundation, Inc.\n";
+    print "Copyright (C) 2000-2003 Free Software Foundation, Inc.\n";
     print "This is free software; see the source for copying conditions.  There is NO\n";
     print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
     exit;
@@ -171,15 +172,20 @@
 sub print_help
 {
     ## Print usage information
-    print "Usage: ${PROGRAM} [OPTIONS] ...LANGCODE\n";
+    print "Usage: ${PROGRAM} [OPTIONS]... LANGCODE\n";
     print "Updates PO template files and merge them with the translations.\n\n";
-    print "  -p, --pot              generate the PO template only\n";
-    print "  -s, --headers          generate the header files in POTFILES.in\n";
-    print "  -m, --maintain         search for left out files from POTFILES.in\n";
-    print "  -r, --report           display a status report for the module.\n";
-    print "  -x, --verbose          display lots of feedback\n";
-    print "      --help             display this help and exit\n";
-    print "      --version          output version information and exit\n";
+    print "Mode of operation (only one is allowed):\n";
+    print "  -p, --pot                   generate the PO template only\n";
+    print "  -s, --headers               generate the header files in POTFILES.in\n";
+    print "  -m, --maintain              search for left out files from POTFILES.in\n";
+    print "  -r, --report                display a status report for the module\n";
+    print "  -d, --dist                  merge LANGCODE.po with existing PO template\n";
+    print "\nExtra options:\n";
+    print "  -g, --gettext-package=NAME  override PO template name, useful with --pot\n";
+    print "  -o, --output-file=FILE      write merged translation to FILE\n";
+    print "  -x, --verbose               display lots of feedback\n";
+    print "      --help                  display this help and exit\n";
+    print "      --version               output version information and exit\n";
     print "\nExamples of use:\n";
     print "${PROGRAM} --pot    just creates a new PO template from the source\n";
     print "${PROGRAM} da       created new PO template and updated the da.po file\n\n";
@@ -251,6 +257,38 @@
    return "gettext\/$gettext_type";
 }
 
+sub determine_code ($) 
+{
+   my $comments = $_;
+   my $code = $_;
+   my $filename;
+   my $type;
+   my $gettext_code="ASCII"; # All files are ASCII by default
+
+   if ($comments =~ /^[^#]/)
+   {
+       $code =~ s/^\[[^\[].*]\s*//;
+       $filename = "../$code";
+       $type=`file $filename | cut -d ' ' -f 2`;
+       if ($? eq "0")
+       {
+           if (!($type =~ /^[^ISO]/) or !($type =~ /^[^UTF]/))
+           {
+               $gettext_code=$type;
+	       chomp $gettext_code;
+	   }
+	   elsif (!($type =~ /^[^XML]/))
+	   {
+	       $gettext_code="UTF-8"; # We asume that .glade and other .xml files are UTF-8
+	   }
+       }
+
+   }
+   
+   return $gettext_code;
+}
+
+
 sub find_leftout_files
 {
     my (@buf_i18n_plain,
@@ -280,12 +318,13 @@
 
 
     open POTFILES, "POTFILES.in" or die "$PROGRAM:  there's no POTFILES.in!\n";
-
-    @buf_potfiles = grep /^[^#]/, <POTFILES>;
+    @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
@@ -305,11 +344,14 @@
             }
 	    print "Found $ignore: Ignoring files...\n" if $VERBOSE;
             @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
+
+	    close FILE;
         }
     }
 
     foreach my $file (@buf_i18n_plain)
     {
+	print "Searching in \"$file\"\n" if $VERBOSE;
         my $in_comment = 0;
 	my $in_macro = 0;
 
@@ -375,6 +417,7 @@
 	    
     foreach my $file (@buf_i18n_xml) 
     {
+	print "Searching in \"$file\"\n" if $VERBOSE;
         open FILE, "<$file";
         
 	while (<FILE>) 
@@ -385,10 +428,12 @@
                 last;
             }
         }
+	close FILE;
     }
 
     foreach my $file (@buf_i18n_ini)
     {
+	print "Searching in \"$file\"\n" if $VERBOSE;
         open FILE, "<$file";
         while (<FILE>) 
 	{
@@ -398,6 +443,7 @@
                 last;
             }
         }
+	close FILE;
     }
 
     foreach my $file (@buf_i18n_xml_unmarked)
@@ -421,28 +467,54 @@
     {
         if (!exists($in2{$_}))
 	{
-            push @result, $_
-        }
+	    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) 
+    if (@result + @buf_potfiles_notexist > 0)
     {
-	print "\n" if $VERBOSE;
-        open OUT, ">missing";
-        print OUT @result;
-        print STDERR "The following files contain translations and are currently not in use. Please\n";
-	print STDERR "consider adding these to the POTFILES.in file, located in the po/ directory.\n\n";
-	print STDERR @result, "\n";
-	print STDERR "If some of these files are left out on purpose then please add them to\n";
-	print STDERR "POTFILES.skip instead of POTFILES.in. A file 'missing' containing this list\n";
-        print STDERR "of left out files has been written in the current directory.\n";
+	if (@result) 
+	{
+	    print "\n" if $VERBOSE;
+	    open OUT, ">missing";
+	    print OUT @result;
+	    close OUT;
+	    print STDERR "\e[1mThe following files contain translations and are currently not in use. Please\e[0m\n";
+	    print STDERR "\e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.\e[0m\n\n";
+	    print STDERR @result, "\n";
+	    print STDERR "If some of these files are left out on purpose then please add them to\n";
+	    print STDERR "POTFILES.skip instead of POTFILES.in. A file \e[1m'missing'\e[0m containing this list\n";
+	    print STDERR "of left out files has been written in the current directory.\n";
+	}
+	if (@buf_potfiles_notexist)
+	{
+	    open OUT, ">notexist";
+	    print OUT @buf_potfiles_notexist;
+	    close OUT;
+	    print STDERR "\n" if ($VERBOSE or @result);
+	    print STDERR "\e[1mThe following files do not exist anymore:\e[0m\n\n";
+	    print STDERR @buf_potfiles_notexist, "\n";
+	    print STDERR "Please remove them from POTFILES.in or POTFILES.skip. A file \e[1m'notexist'\e[0m\n";
+	    print STDERR "containing this list of absent files has been written in the current\n";
+	    print STDERR "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;
+	print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
     }
 }
 
@@ -470,7 +542,7 @@
     }
     else
     {
-        open FILE, "<POTFILES.in";
+        open (FILE, "<POTFILES.in") or die "$PROGRAM: POTFILES.in not found.\n";
         
 	while (<FILE>) 
 	{
@@ -514,17 +586,50 @@
     open INFILE, "<POTFILES.in.old";
     open OUTFILE, ">POTFILES.in";
 
+    my $forced_gettext_code;
+
     while (<INFILE>) 
     {
+	next if (/^#/ or /^\s*$/);
+
+	print if $VERBOSE;
         chomp;
-        if (/\.($xml_extension|$ini_extension)$/ || /^\[/) 
+
+	my $gettext_code;
+
+	if (/\.($xml_extension|$ini_extension)$/ || /^\[/)
 	{
-            s/^\[.*]\s*//;
-	    print OUTFILE "$_.h\n";
+	    if (/^\[\s*encoding:\s*(.*)\s*\]/)
+	    {
+		$forced_gettext_code=$1;
+	    }
+	    else
+	    {
+		s/^\[.*]\s*//;
+		print OUTFILE "$_.h\n";
+		defined $forced_gettext_code or $gettext_code = &determine_code ("$_.h");
+	    }
         } 
         else 
         {
             print OUTFILE "$_\n";
+	    defined $forced_gettext_code or $gettext_code = &determine_code ("$_");
+        }
+
+	if (defined $forced_gettext_code)
+	{
+	    $encoding=$forced_gettext_code;
+	}
+	elsif ($gettext_code ne "" and $encoding ne $gettext_code)
+	{
+	    if ($encoding eq "ASCII")
+	    {
+	        $encoding=$gettext_code;
+	    }
+	    elsif ($gettext_code ne "ASCII")
+	    {
+	        print STDERR "Warning: You should use the same file encoding for all your project files, but you have $encoding and $gettext_code files ($_).\n";
+	    }
         }
     }
     
@@ -534,6 +639,7 @@
     system ("xgettext", "--default-domain\=$MODULE", 
 			"--directory\=\.\.",
 	   		"--add-comments", 
+			"--from-code\=$encoding", 
 			"--keyword\=\_", 
 			"--keyword\=N\_", 
 			"--keyword\=U\_",
@@ -556,8 +662,9 @@
 
     if (!-e "$MODULE.po") 
     {
-        print STDERR "WARNING: It seems that none of the files in POTFILES.in ".
-              	     "contain marked strings\n";
+        print STDERR "WARNING: Either none of the files in POTFILES.in contain marked strings,\n".
+		     "         or xgettext failed to generate PO template file. Please consult\n".
+		     "         error message above if there is any.\n";
         exit (1);
     }
 
@@ -570,6 +677,8 @@
 
 sub update_po_file
 {
+    -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
+
     my ($lang, $output_file) = @_;
     my ($infile, $outfile);
 
@@ -636,7 +745,7 @@
 	$output_file = "$lang.po";
     }
 
-    system ("msgfmt", "--statistics", $output_file);
+    system ("msgfmt", "-o", "/dev/null", "--statistics", $output_file);
     print "\n";
 }
 
@@ -657,7 +766,7 @@
     foreach my $lang (@languages)
     {
         print "$lang: ";
-	system ("msgfmt", "--statistics", "$lang.po");
+	system ("msgfmt", "-o", "/dev/null", "--statistics", "$lang.po");
     }
 }
 
@@ -675,7 +784,7 @@
 	}
     }
     
-    if ($str =~ /^(.*)\${?([A-Z]+)}?(.*)$/)
+    if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
     {
 	my $rest = $3;
 	my $untouched = $1;
@@ -754,21 +863,26 @@
 
     # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m 
     # the \s makes this not work, why?
-    if ($conf_source =~ /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\]]+)/m)
+    if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
     {
-	$name = $1;
+	($name = $1) =~ s/[\[\]\s]//g;
+	($varhash{"AC_PACKAGE_VERSION"} = $2) =~ s/[\[\]\s]//g;
     }
     
-    if ($conf_source =~ /^AC_INIT\([\s\[]*([^,\)\s\]]+)\]?\s*,/m) 
+    if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m) 
     {
-	$name = $1;
-	$varhash{"AC_PACKAGE_NAME"} = $1;
+	($name = $1) =~ s/[\[\]\s]//g;
+	($varhash{"AC_PACKAGE_NAME"} = $1) =~ s/[\[\]\s]//g;
+	($varhash{"AC_PACKAGE_VERSION"} = $2) =~ s/[\[\]\s]//g;
     }
 
     # \s makes this not work, why?
     $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
     
-    $name = "\$AC_PACKAGE_NAME" if "$name" eq "AC_PACKAGE_NAME";
+    # prepend '$' to auto* internal variables, usually they are
+    # used in configure.in/ac without the '$'
+    $name =~ s/AC_/\$AC_/g;
+    $name =~ s/\$\$/\$/g;
     
     $name = substitute_var ($name);
 
