
#-----------------------------------------------------------------------------

sub handle_TOPLEVEL()
{
    my $pofiles = "";
    my @restfiles = ();
    opendir (THISDIR, ".");
    foreach $entry (readdir(THISDIR)) {
        next if (-d $entry);
        
        next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/ || $entry =~ /.gmo$/);
                 
        if ($entry =~ /\.po$/) {
             next;
        }
        push(@restfiles, $entry);
    }
    closedir (THISDIR);
            
    if (@restfiles) {
        $target_adds{"install-data-am"} .= "install-nls-files ";
        $lines = "install-nls-files:\n";
        $lines .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/$kdelang\n";
        for $file (@restfiles) {
            $lines .= "\t\$(INSTALL_DATA) \$\(srcdir\)/$file \$(DESTDIR)\$(kde_locale)/$kdelang/$file\n";
        }
	$target_adds{"uninstall"} .= "uninstall-nls-files ";
        $lines .= "uninstall-nls-files:\n";
        for $file (@restfiles) {
            $lines .= "\t-rm -f \$(DESTDIR)\$(kde_locale)/$kdelang/$file\n";
        }
        appendLines($lines);
    }
    
    return 0;
}

sub handle_POFILES($$)
{
  my @pofiles = split(" ", $_[0]);
  my $lang = $_[1];

  # Build rules for creating the gmo files
  my $tmp = "";
  my $allgmofiles     = "";
  my $pofileLine   = "POFILES =";
  foreach $pofile (@pofiles)
    {
        $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
        $tmp .= "$1.gmo: $pofile\n";
        $tmp .= "\trm -f $1.gmo; \$(GMSGFMT) -o $1.gmo \$(srcdir)/$pofile\n";
        $tmp .= "\ttest ! -f $1.gmo || touch $1.gmo\n";
        $allgmofiles .= " $1.gmo";
        $pofileLine  .= " $1.po";
    }
  appendLines ($tmp);
  my $lookup = 'POFILES\s*=([^\n]*)';
  if ($MakefileData !~ /\n$lookup/o) {
    appendLines("$pofileLine\nGMOFILES =$allgmofiles");
  } else {
    substituteLine ($lookup, "$pofileLine\nGMOFILES =$allgmofiles");
  }

    if ($allgmofiles) {

        # Add the "clean" rule so that the maintainer-clean does something
        appendLines ("clean-nls:\n\t-rm -f $allgmofiles\n");

	$target_adds{"maintainer-clean"} .= "clean-nls ";

	$lookup = 'DISTFILES\s*=\s*(.*)';
	if ($MakefileData =~ /\n$lookup\n/o) {
	  $tmp = "DISTFILES = \$(GMOFILES) \$(POFILES) $1";
	  substituteLine ($lookup, $tmp);
	}
    }

  $target_adds{"install-data-am"} .= "install-nls ";

  $tmp = "install-nls:\n";
  if ($lang) {
    $tmp  .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES\n";
  }
  $tmp .= "\t\@for base in ";
  foreach $pofile (@pofiles)
    {
      $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
      $tmp .= "$1 ";
    }

  $tmp .= "; do \\\n";
  if ($lang) {
    $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n";
    $tmp .= "\t  test ! -f \$\$base.gmo || \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n"
  } else {
    $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
    $tmp .= "\t  \$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES ; \\\n";
    $tmp .= "\t  test ! -f \$\$base.gmo || \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
  }
  $tmp .= "\tdone\n\n";
  appendLines ($tmp);

  $target_adds{"uninstall"} .= "uninstall-nls ";

  $tmp = "uninstall-nls:\n";
  foreach $pofile (@pofiles)
    {
      $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
      if ($lang) {
	$tmp .= "\trm -f \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/$1.mo\n";
      } else {
	$tmp .= "\trm -f \$(DESTDIR)\$(kde_locale)/$1/LC_MESSAGES/\$(PACKAGE).mo\n";
      }
    }
  appendLines($tmp);

  $target_adds{"all"} .= "all-nls ";

  $tmp = "all-nls: \$(GMOFILES)\n";

  appendLines($tmp);

  $target_adds{"distdir"} .= "distdir-nls ";

  $tmp = "distdir-nls:\$(GMOFILES)\n";
  $tmp .= "\tfor file in \$(POFILES); do \\\n";
  $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
  $tmp .= "\tdone\n";
  $tmp .= "\tfor file in \$(GMOFILES); do \\\n";
  $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
  $tmp .= "\tdone\n";

  appendLines ($tmp);

  if (!$lang) {
    appendLines("merge:\n\t\$(MAKE) -f \$(top_srcdir)/admin/Makefile.common package-merge POFILES=\"\${POFILES}\" PACKAGE=\${PACKAGE}\n\n");
  }
 
}

#-----------------------------------------------------------------------------

# Returns 0 if the line was processed - 1 otherwise.
# Errors are logged in the global $errorflags
sub tag_POFILES ()
{
    my $lookup = 'POFILES\s*=([^\n]*)';
    return 1    if ($MakefileData !~ /\n$lookup/o);
    print STDOUT "POFILES processing <$1>\n"   if ($verbose);

    my $tmp = $1;

    # make sure these are all gone.
    if ($MakefileData =~ /\n\.po\.gmo:\n/)
    {
        print STDERR "Warning: Found old .po.gmo rules in $printname. New po rules not added\n";
        return 1;
    }

    # Either find the pofiles in the directory (AUTO) or use
    # only the specified po files.
    my $pofiles = "";
    if ($tmp =~ /^\s*AUTO\s*$/)
    {
        opendir (THISDIR, ".");
	$pofiles =  join(" ", grep(/\.po$/, readdir(THISDIR)));
        closedir (THISDIR);
        print STDOUT "pofiles found = $pofiles\n"   if ($verbose);
	if (-f "charset" && -f "kdelibs.po") {
	    handle_TOPLEVEL();
	}
    }
    else
    {
        $tmp =~ s/\034/ /g;
        $pofiles = $tmp;
    }
    return 1    if (!$pofiles);        # Nothing to do

    handle_POFILES($pofiles, $kdelang);

    return 0;
}
