libftdi: (gerd) add a real libftdi with autoconf stuff, remove include of usb.h in...
[libftdi] / admin / am_edit
1 #!/usr/bin/perl -w
2
3 # Expands the specialised KDE tags in Makefile.in to (hopefully) valid
4 # make syntax.
5 # When called without file parameters, we work recursively on all Makefile.in
6 # in and below the current subdirectory. When called with file parameters,
7 # only those Makefile.in are changed.
8 # The currently supported tags are
9 #
10 # {program}_METASOURCES
11 # where you have a choice of two styles
12 #   {program}_METASOURCES = name1.moc name2.moc ... [\]
13 #   {program}_METASOURCES = AUTO
14 #       The second style requires other tags as well.
15 #
16 # To install icons :
17 #    KDE_ICON = iconname iconname2 ...
18 #    KDE_ICON = AUTO
19 #
20 # For documentation :
21 #    http://developer.kde.org/documentation/other/developer-faq.html
22 #
23 # and more new tags TBD!
24 #
25 # The concept (and base code) for this program came from automoc,
26 # supplied by the following
27 #
28 # Matthias Ettrich <ettrich@kde.org>      (The originator)
29 # Kalle Dalheimer <kalle@kde.org>      (The original implementator)
30 # Harri Porten  <porten@tu-harburg.de>
31 # Alex Zepeda  <jazepeda@pacbell.net>
32 # David Faure <faure@kde.org>
33 # Stephan Kulow <coolo@kde.org>
34
35 use Cwd;
36 use File::Find;
37 use File::Basename;
38
39 # Prototype the functions
40 sub initialise ();
41 sub processMakefile ($);
42 sub updateMakefile ();
43 sub restoreMakefile ();
44
45 sub removeLine ($$);
46 sub appendLines ($);
47 sub substituteLine ($$);
48
49 sub findMocCandidates ();
50 sub pruneMocCandidates ($);
51 sub checkMocCandidates ();
52 sub addMocRules ();
53
54 sub tag_AUTOMAKE ();
55 sub tag_META_INCLUDES ();
56 sub tag_METASOURCES ();
57 sub tag_POFILES ();
58 sub tag_DOCFILES ();
59 sub tag_LOCALINSTALL();
60 sub tag_IDLFILES();
61 sub tag_UIFILES();
62 sub tag_SUBDIRS();
63 sub tag_ICON();
64 sub tag_CLOSURE();
65 sub tag_DIST();
66
67 # Some global globals...
68 $verbose    = 0;        # a debug flag
69 $thisProg   = "$0";     # This programs name
70 $topdir     = cwd();    # The current directory
71 @makefiles  = ();       # Contains all the files we'll process
72 @foreignfiles = ();
73 $start      = (times)[0]; # some stats for testing - comment out for release
74 $version    = "v0.2";
75 $errorflag  = 0;
76 $cppExt     = "(cpp|cc|cxx|C|c\\+\\+)";
77 $hExt       = "(h|H|hh|hxx|hpp|h\\+\\+)";
78 $progId     = "KDE tags expanded automatically by " . basename($thisProg);
79 $automkCall = "\n";
80 $printname  = "";  # used to display the directory the Makefile is in
81 $use_final  = 1;        # create code for --enable-final
82 $cleantarget = "clean";
83 $dryrun     = 0;
84 $pathoption = 0;
85 $foreign_libtool = 0;
86
87 while (defined ($ARGV[0]))
88 {
89     $_ = shift;
90     if (/^--version$/)
91     {
92         print STDOUT "\n";
93         print STDOUT basename($thisProg), " $version\n",
94                 "This is really free software, unencumbered by the GPL.\n",
95                 "You can do anything you like with it except sueing me.\n",
96                 "Copyright 1998 Kalle Dalheimer <kalle\@kde.org>\n",
97                 "Concept, design and unnecessary questions about perl\n",
98                 "       by Matthias Ettrich <ettrich\@kde.org>\n\n",
99                 "Making it useful by Stephan Kulow <coolo\@kde.org> and\n",
100                 "Harri Porten <porten\@kde.org>\n",
101                 "Updated (Feb-1999), John Birch <jb.nz\@writeme.com>\n",
102                 "Current Maintainer Stephan Kulow\n\n";
103         exit 0;
104     }
105     elsif (/^--verbose$|^-v$/)
106     {
107         $verbose = 1;       # Oh is there a problem...?
108     }
109     elsif (/^-p(.+)$|^--path=(.+)$/)
110     {
111         $thisProg = "$1/".basename($thisProg) if($1);
112         $thisProg = "$2/".basename($thisProg) if($2);
113         warn ("$thisProg doesn't exist\n")      if (!(-f $thisProg));
114         $pathoption=1;
115     }
116     elsif (/^--help$|^-h$/)
117     {
118         print STDOUT "Usage $thisProg [OPTION] ... [dir/Makefile.in]...\n",
119                 "\n",
120                 "Patches dir/Makefile.in generated by automake\n",
121                 "(where dir can be an absolute or relative directory name)\n",
122                 "\n",
123                 "  -v, --verbose      verbosely list files processed\n",
124                 "  -h, --help         print this help, then exit\n",
125                 "  --version          print version number, then exit\n",
126                 "  -p, --path=        use the path to am_edit if the path\n",
127                 "                     called from is not the one to be used\n",
128                 "  --no-final         don't patch for --enable-final\n";
129         
130         exit 0;
131     }
132     elsif (/^--no-final$/)
133     {
134         $use_final = 0;
135         $thisProg .= " --no-final";
136     }
137     elsif (/^--foreign-libtool$/)
138     {
139         $foreign_libtool = 1;
140         $thisProg .= " --foreign-libtool";
141     }
142     elsif (/^-n$/)
143     {
144         $dryrun = 1;
145     }
146     else
147     {
148         # user selects what input files to check
149         # add full path if relative path is given
150         $_ = cwd()."/".$_   if (! /^\//);
151         print "User wants $_\n" if ($verbose);
152         push (@makefiles, $_);
153     }
154 }
155
156 if ($thisProg =~ /^\// && !$pathoption )
157 {
158   print STDERR "Illegal full pathname call performed...\n",
159       "The call to \"$thisProg\"\nwould be inserted in some Makefile.in.\n",
160       "Please use option --path.\n";
161   exit 1;
162 }
163
164 # Only scan for files when the user hasn't entered data
165 if (!@makefiles)
166 {
167     print STDOUT "Scanning for Makefile.in\n"       if ($verbose);
168     find (\&add_makefile, cwd());
169     #chdir('$topdir');
170 } else {
171     print STDOUT "Using input files specified by user\n"   if ($verbose);
172 }
173
174 foreach $makefile (sort(@makefiles))
175 {
176     processMakefile ($makefile);
177     last            if ($errorflag);
178 }
179
180 # Just some debug statistics - comment out for release as it uses printf.
181 printf STDOUT "Time %.2f CPU sec\n", (times)[0] - $start     if ($verbose);
182
183 exit $errorflag;        # causes make to fail if erroflag is set
184
185 #-----------------------------------------------------------------------------
186
187 # In conjunction with the "find" call, this builds the list of input files
188 sub add_makefile ()
189 {
190   push (@makefiles, $File::Find::name) if (/Makefile.in$/);
191 }
192
193 #-----------------------------------------------------------------------------
194
195 # Processes a single make file
196 # The parameter contains the full path name of the Makefile.in to use
197 sub processMakefile ($)
198 {
199     # some useful globals for the subroutines called here
200     local ($makefile)       = @_;
201     local @headerdirs       = ('.');
202     local $haveAutomocTag   = 0;
203     local $MakefileData     = "";
204
205     local $cxxsuffix  = "KKK";
206
207     local @programs = ();  # lists the names of programs and libraries
208     local $program = "";
209
210     local %realObjs = ();  # lists the objects compiled into $program
211     local %sources = ();   # lists the sources used for $program
212     local %finalObjs = (); # lists the objects compiled when final
213     local %realname = ();  # the binary name of program variable
214     local %idlfiles = ();  # lists the idl files used for $program
215     local %globalmocs = ();# list of all mocfiles (in %mocFiles format)
216     local %important = (); # list of files to be generated asap
217     local %uiFiles = ();
218
219     local $allidls = "";
220     local $idl_output = "";# lists all idl generated files for cleantarget
221     local $ui_output = "";# lists all uic generated files for cleantarget
222
223     local %depedmocs = ();
224     
225     local $metasourceTags = 0;
226     local $dep_files      = "";
227     local $dep_finals     = "";
228     local %target_adds    = (); # the targets to add
229     local $kdelang        = "";
230     local @cleanfiles     = ();
231     local $cleanMoc       = "";
232     local $closure_output = "";
233
234     local %varcontent     = ();
235
236     $makefileDir = dirname($makefile);
237     chdir ($makefileDir);
238     $printname = $makefile;
239     $printname =~ s/^\Q$topdir\E\///;
240     $makefile = basename($makefile);
241
242     print STDOUT "Processing makefile $printname\n"   if ($verbose);
243     
244     # Setup and see if we need to do this.
245     return      if (!initialise());
246     
247     tag_AUTOMAKE ();            # Allows a "make" to redo the Makefile.in
248     tag_META_INCLUDES ();       # Supplies directories for src locations
249     
250     foreach $program (@programs) {
251         $sources_changed{$program} = 0;
252         $depedmocs{$program} = "";
253         $important{$program} = "";
254         tag_IDLFILES();             # Sorts out idl rules
255         tag_CLOSURE();
256         tag_UIFILES();             # Sorts out ui rules
257         tag_METASOURCES ();         # Sorts out the moc rules
258         if ($sources_changed{$program}) {
259             my $lookup = "$program" . '_SOURCES\s*=\s*(.*)';
260             substituteLine($lookup, "$program\_SOURCES=" . $sources{$program});
261         }
262         if ($important{$program}) {
263             local %source_dict = ();
264             for $source (split(/[\034\s]+/, $sources{$program})) {
265                 $source_dict{$source} = 1;
266             }
267             for $source (@cleanfiles) {
268                 $source_dict{$source} = 0;
269             }
270             for $source (keys %source_dict) {
271                 next if (!$source);
272                 if ($source_dict{$source}) {
273                     # sanity check
274                     if (! -f $source) {
275                         print STDERR "Error: $source is listed in a _SOURCE line in $printname, but doesn't exist yet. Put it in DISTCLEANFILES!\n";
276                     } else {
277                         $target_adds{"\$(srcdir)/$source"} .= $important{$program};
278                     }
279                 }
280             }
281         }
282     }
283     if ($cleanMoc) {
284         # Always add dist clean tag
285         # Add extra *.moc.cpp files created for USE_AUTOMOC because they
286         # aren't included in the normal *.moc clean rules.
287         appendLines ("$cleantarget-metasources:\n\t-rm -f $cleanMoc\n");
288         $target_adds{"$cleantarget-am"} .= "$cleantarget-metasources ";
289     }
290     
291     tag_DIST() unless ($kdeopts{"noautodist"});
292
293     if ($idl_output) {
294         appendLines ("$cleantarget-idl:\n\t-rm -f $idl_output\n");
295         $target_adds{"$cleantarget-am"} .= "$cleantarget-idl ";
296     }
297
298     if ($ui_output) {
299         appendLines ("$cleantarget-ui:\n\t-rm -f $ui_output\n");
300         $target_adds{"$cleantarget-am"} .= "$cleantarget-ui ";
301     }
302
303     if ($closure_output) {
304         appendLines ("$cleantarget-closures:\n\t-rm -f $closure_output\n");
305         $target_adds{"$cleantarget-am"} .= "$cleantarget-closures ";
306     }
307
308     if ($MakefileData =~ /\nKDE_LANG\s*=\s*(\S*)\s*\n/) {
309         $kdelang = '$(KDE_LANG)'
310     } else {
311         $kdelang = '';
312     }
313
314     tag_POFILES ();             # language rules for po directory
315     tag_DOCFILES ();            # language rules for doc directories
316     tag_LOCALINSTALL();         # add $(DESTDIR) before all kde_ dirs
317     tag_ICON();
318     tag_SUBDIRS();
319
320     my $tmp = "force-reedit:\n";
321     $tmp   .= "\t$automkCall\n\tcd \$(top_srcdir) && perl $thisProg $printname\n\n";
322     appendLines($tmp);
323     
324     make_meta_classes();
325     tag_COMPILE_FIRST();
326     tag_FINAL() if (!$kdeopts{"nofinal"});
327
328     my $final_lines = "final:\n\t\$(MAKE) ";
329     my $final_install_lines = "final-install:\n\t\$(MAKE) ";
330     my $nofinal_lines = "no-final:\n\t\$(MAKE) ";
331     my $nofinal_install_lines = "no-final-install:\n\t\$(MAKE) ";
332
333     foreach $program (@programs) {
334         
335         my $lookup = "$program\_OBJECTS.*=[^\n]*";
336         
337         my $new = "";
338         
339         my @list = split(/[\034\s]+/, $realObjs{$program});
340         
341         if (!$kdeopts{"nofinal"} && @list > 1 && $finalObjs{$program}) {
342             
343             $new .= "$program\_final\_OBJECTS = " . $finalObjs{$program};
344             $new .= "\n$program\_nofinal\_OBJECTS = " . $realObjs{$program};
345             $new .= "\n\@KDE_USE_FINAL_FALSE\@$program\_OBJECTS = \$($program\_nofinal\_OBJECTS)";
346             $new .= "\n\@KDE_USE_FINAL_TRUE\@$program\_OBJECTS = \$($program\_final\_OBJECTS)";
347             
348             $final_lines .= "$program\_OBJECTS=\"\$($program\_final_OBJECTS)\" ";
349             $final_install_lines .= "$program\_OBJECTS=\"\$($program\_final_OBJECTS)\" ";
350             $nofinal_lines .= "$program\_OBJECTS=\"\$($program\_nofinal\_OBJECTS)\" ";
351             $nofinal_install_lines .= "$program\_OBJECTS=\"\$($program\_nofinal_OBJECTS)\" ";
352         } else {
353             $new = "$program\_OBJECTS = " . $realObjs{$program};
354         }
355         substituteLine ($lookup, $new);
356     }
357     appendLines($final_lines . "all-am");
358     appendLines($final_install_lines . "install-am");
359     appendLines($nofinal_lines . "all-am");
360     appendLines($nofinal_install_lines . "install-am");
361
362     my $lookup = '(\@\S+\@)?DEP_FILES\s*=([^\n]*)';
363     if ($MakefileData =~ /\n$lookup\n/o) {
364         my $condition = $1;
365         my $depfiles = $2;
366         my $workfiles;
367
368         if ($dep_finals) {
369             # Add the conditions on every line, since
370             # there may be line continuations in the list.
371             $workfiles = "$dep_files $dep_finals $depfiles";
372             $workfiles =~ s/\034/\034$condition\@KDE_USE_FINAL_TRUE\@\t/g;
373             $lines  = "$condition\@KDE_USE_FINAL_TRUE\@DEP_FILES = $workfiles\n";
374             $workfiles = "$dep_files $depfiles";
375             $workfiles =~ s/\034/\034$condition\@KDE_USE_FINAL_FALSE\@\t/g;
376             $lines .= "$condition\@KDE_USE_FINAL_FALSE\@DEP_FILES = $workfiles\n";
377         } else {
378             $workfiles = "$dep_files $depfiles";
379             $workfiles =~ s/\034/\034$condition\t/g;
380             $lines = $condition . "DEP_FILES = $workfiles\n";
381         }
382         substituteLine($lookup, $lines);
383     }
384
385     my $cvs_lines = "cvs-clean:\n";
386     $cvs_lines .= "\t\$(MAKE) admindir=\$(top_srcdir)/admin -f \$(top_srcdir)/admin/Makefile.common cvs-clean\n";
387     appendLines($cvs_lines);
388
389     $cvs_lines  = "kde-rpo-clean:\n";
390     $cvs_lines .= "\t-rm -f *.rpo\n";
391     appendLines($cvs_lines);
392     $target_adds{"clean"} .= "kde-rpo-clean ";
393
394     my %target_dels = ("install-data-am" => "");
395
396     # some strange people like to do a install-exec, and expect that also
397     # all modules are installed.  automake doesn't know this, so we need to move
398     # this here from install-data to install-exec.
399     if ($MakefileData =~ m/\nkde_module_LTLIBRARIES\s*=/) {
400 #      $target_adds{"install-exec-am"} .= "install-kde_moduleLTLIBRARIES ";
401 #      don't use $target_adds here because we need to append the dependency, not
402 #      prepend it. Fixes #44342 , when a module depends on a lib in the same dir
403 #      and libtool needs it during relinking upon install (Simon)
404       my $lookup = "install-exec-am:([^\n]*)";
405       if($MakefileData =~ /\n$lookup\n/) {
406         substituteLine("$lookup", "install-exec-am: $1 install-kde_moduleLTLIBRARIES");
407       }
408       $target_dels{"install-data-am"} .= "install-kde_moduleLTLIBRARIES ";
409       $target_adds{"install-data-am"} .= " ";
410     }
411
412     my $lines = "";
413
414     foreach $add (keys %target_adds) {
415         my $lookup = quotemeta($add) . ':([^\n]*)';
416         if ($MakefileData =~ /\n$lookup\n/) {
417           my $newlines = $1;
418           my $oldlines = $lookup;
419           if (defined $target_dels{$add}) {
420             foreach $del (split(' ', $target_dels{$add})) {
421               $newlines =~ s/\s*$del\s*/ /g;
422             }
423           }
424           substituteLine($oldlines, "$add: " . $target_adds{$add} . $newlines);
425         } else {
426           $lines .= "$add: " . $target_adds{$add} . "\n";
427         }
428     }
429     if ($lines) {
430         appendLines($lines);
431     }
432
433     my $found = 1;
434
435     while ($found) {
436         if ($MakefileData =~ m/\n(.*)\$\(CXXFLAGS\)(.*)\n/) {
437             my $vor = $1;   # "vor" means before in German
438             my $nach = $2; # "nach" means after in German
439             my $lookup = quotemeta("$1\$(CXXFLAGS)$2");
440             my $replacement = "$1\$(KCXXFLAGS)$2";
441             $MakefileData =~ s/$lookup/$replacement/;
442             $lookup =~ s/\\\$\\\(CXXFLAGS\\\)/\\\$\\\(KCXXFLAGS\\\)/;
443             $replacement = "$vor\$(KCXXFLAGS) \$(KDE_CXXFLAGS)$nach";
444             substituteLine($lookup, $replacement);
445         } else {
446             $found = 0;
447         }
448     }
449
450     if($foreign_libtool == 0) {
451         $lookup = '(\n[^#].*\$\(LIBTOOL\) --mode=link) (\$\(CXXLD\).*\$\(KCXXFLAGS\))';
452
453         if ($MakefileData =~ m/$lookup/ ) {
454             $MakefileData =~ s/$lookup/$1 --tag=CXX $2/;
455         }
456
457         $lookup = '(\n[^#].*\$\(LIBTOOL\) --mode=compile)\s+(\$\(CXX\)\s+)';
458         if ($MakefileData =~ m/$lookup/ ) {
459             $MakefileData =~ s/$lookup/$1 --tag=CXX $2/;
460         }
461     }
462
463     $MakefileData =~ s/\$\(KCXXFLAGS\)/\$\(CXXFLAGS\)/g;
464
465     $lookup = '(.*)cp -pr \$\$/\$\$file \$\(distdir\)/\$\$file(.*)';
466     if ($MakefileData =~ m/\n$lookup\n/) {
467         substituteLine($lookup, "$1cp -pr \$\$d/\$\$file \$(distdir)/\$\$file$2");
468     }
469
470     # Always update the Makefile.in
471     updateMakefile ();
472     return;
473 }
474
475 #-----------------------------------------------------------------------------
476
477 # Beware: This procedure is not complete.  E.g. it also parses lines
478 # containing a '=' in rules (for instance setting shell vars).  For our
479 # usage this us enough, though.
480 sub read_variables ()
481 {
482     while ($MakefileData =~ /\n\s*(\S+)\s*=([^\n]*)/g) {
483         $varcontent{$1} = $2;
484     }
485 }
486
487 # Check to see whether we should process this make file.
488 # This is where we look for tags that we need to process.
489 # A small amount of initialising on the tags is also done here.
490 # And of course we open and/or create the needed make files.
491 sub initialise ()
492 {
493     if (! -r "Makefile.am") {
494         print STDOUT "found Makefile.in without Makefile.am\n" if ($verbose);
495         return 0;
496     }
497
498     # Checking for files to process...
499     open (FILEIN, $makefile)
500       || die "Could not open $makefileDir/$makefile: $!\n";
501     # Read the file
502     # stat(FILEIN)[7] might look more elegant, but is slower as it 
503     # requires stat'ing the file
504     seek(FILEIN, 0, 2);
505     my $fsize = tell(FILEIN);
506     seek(FILEIN, 0, 0);
507     read FILEIN, $MakefileData, $fsize;
508     close FILEIN;
509     print "DOS CRLF within $makefileDir/$makefile!\n" if($MakefileData =~ y/\r//d);
510
511     # Remove the line continuations, but keep them marked
512     # Note: we lose the trailing spaces but that's ok.
513     # Don't mangle line-leading spaces (usually tabs)
514     # since they're important.
515     $MakefileData =~ s/\\\s*\n/\034/g;
516
517     # If we've processed the file before...
518     restoreMakefile ()      if ($MakefileData =~ /$progId/);
519
520     foreach $dir (@foreignfiles) {
521       if (substr($makefileDir,0,length($dir)) eq $dir) {
522         return 0;
523       }
524     }
525
526     %kdeopts = ();
527     $kdeopts{"foreign"} = 0;
528     $kdeopts{"qtonly"} = 0;
529     $kdeopts{"noautodist"} = 0;
530     $kdeopts{"foreign-libtool"} = $foreign_libtool;
531     $kdeopts{"nofinal"} = !$use_final; # default
532
533     read_variables();
534
535     if ($MakefileData =~ /\nKDE_OPTIONS\s*=\s*([^\n]*)\n/) {
536         local @kde_options = split(/[\s\034]/, $1);
537         if (grep(/^foreign$/, @kde_options)) {
538             push(@foreignfiles, $makefileDir . "/");
539             return 0; # don't touch me
540         }
541         for $opt (@kde_options) {
542             if (!defined $kdeopts{$opt}) {
543                 print STDERR "Warning: unknown option $opt in $printname\n";
544             } else {
545                 $kdeopts{$opt} = 1;
546             }
547         }
548     }
549
550     # Look for the tags that mean we should process this file.
551     $metasourceTags = 0;
552     $metasourceTags++    while ($MakefileData =~ /\n[^=\#]*METASOURCES\s*=/g);
553
554     my $pofileTag = 0;
555     $pofileTag++    while ($MakefileData =~ /\nPOFILES\s*=/g);
556     if ($pofileTag > 1)
557       {
558           print STDERR "Error: Only one POFILES tag allowed\n";
559           $errorflag = 1;
560       }
561
562     while ($MakefileData =~ /\n\.SUFFIXES:([^\n]+)\n/g) {
563         my @list=split(' ', $1);
564         foreach $ext (@list) {
565             if ($ext =~ /^\.$cppExt$/) {
566                 $cxxsuffix = $ext;
567                 $cxxsuffix =~ s/\.//g;
568                 print STDOUT "will use suffix $cxxsuffix\n" if ($verbose);
569                 last;
570             }
571         }
572     }
573
574     while ($MakefileData =~ /\n(\S*)_OBJECTS\s*=[ \t\034]*([^\n]*)\n/g) {
575
576         my $program = $1;
577         my $objs = $2; # safe them
578
579         my $ocv = 0;
580
581         my @objlist = split(/[\s\034]+/, $objs);
582         foreach $obj (@objlist) {
583             if ($obj =~ /(\S*)\$\((\S+)\)/ ) {
584                 my $pre = $1;
585                 my $variable = $2;
586                 if ($pre eq '' && exists($varcontent{$variable})) {
587                     my @addlist = split(/[\s\034]+/, $varcontent{$variable});
588                     push(@objlist, @addlist);
589                 } elsif ($variable !~ 'OBJEXT') {
590                     $ocv = 1;
591                 }
592             }
593         }
594
595         next if ($ocv);
596
597         $program =~ s/^am_// if ($program =~ /^am_/);
598
599         my $sourceprogram = $program;
600         $sourceprogram =~ s/\@am_/\@/ if($sourceprogram =~ /^.*\@am_.+/);
601
602         print STDOUT "found program $program\n" if ($verbose);
603         push(@programs, $program);
604
605         $realObjs{$program} = $objs;
606
607         if ($MakefileData =~ /\n$sourceprogram\_SOURCES\s*=\s*(.*)\n/) {
608             $sources{$program} = $1;
609         } 
610         else {
611             $sources{$program} = "";
612             print STDERR "found program with no _SOURCES: $program\n";
613         }
614         
615         my $realprogram = $program;
616         $realprogram =~ s/_/./g; # unmask to regexp
617         if ($MakefileData =~ /\n($realprogram)(\$\(EXEEXT\)?)?:.*\$\($program\_OBJECTS\)/) {
618             $realname{$program} = $1;
619         } else {
620             # not standard Makefile - nothing to worry about
621             $realname{$program} = "";
622         }
623     }
624     
625     my $lookup = '\nDEPDIR\s*=.*';
626     if ($MakefileData !~ /($lookup)\n/o) {
627         $lookup = '\nbindir\s*=.*';
628         if ($MakefileData =~ /($lookup)\n/) {
629             substituteLine ($lookup, "DEPDIR = .deps\n$1");
630         }
631     } 
632
633     my @marks = ('MAINTAINERCLEANFILES', 'CLEANFILES', 'DISTCLEANFILES');
634     foreach $mark (@marks) {
635         while ($MakefileData =~ /\n($mark)\s*=\s*([^\n]*)/g) {
636             foreach $file (split('[\034\s]', $2)) {
637                 $file =~ s/\.\///;
638                 push(@cleanfiles, $file);
639             }
640         }
641     }
642
643     my $localTag = 0;
644     $localTag++ if ($MakefileData =~ /\ninstall-\S+-local:/);
645     
646     return (!$errorflag);
647 }
648
649 #-----------------------------------------------------------------------------
650
651 # Gets the list of user defined directories - relative to $srcdir - where
652 # header files could be located.
653 sub tag_META_INCLUDES ()
654 {
655     my $lookup = '[^=\n]*META_INCLUDES\s*=\s*(.*)';
656     return 1    if ($MakefileData !~ /($lookup)\n/o);
657     print STDOUT "META_INCLUDE processing <$1>\n"       if ($verbose);
658
659     my $headerStr = $2;
660     removeLine ($lookup, $1);
661
662     $headerStr =~ tr/\034/ /;
663     my @headerlist = split(' ', $headerStr);
664
665     foreach $dir (@headerlist)
666     {
667         $dir =~ s#\$\(srcdir\)#.#;
668         if (! -d $dir)
669         {
670             print STDERR "Warning: $dir can't be found. ",
671                             "Must be a relative path to \$(srcdir)\n";
672         }
673         else
674         {
675             push (@headerdirs, $dir);
676         }
677     }
678
679     return 0;
680 }
681
682 #-----------------------------------------------------------------------------
683
684 sub tag_FINAL()
685 {
686     my @final_names = ();
687     
688     foreach $program (@programs) {
689         
690         if ($sources{$program} =~ /\(/) {
691             print STDOUT "found ( in $program\_SOURCES. skipping\n" if ($verbose);
692             next;
693         }
694
695         my $mocs = "";       # Moc files (in this program)
696         my $moc_cpp_added = 0;  # If we added some .moc.cpp files, due to
697                                 # no other .cpp file including the .moc one.
698         
699         my @progsources = split(/[\s\034]+/, $sources{$program});
700         my %shash = ();
701         @shash{@progsources} = 1;  # we are only interested in the existence
702         my %sourcelist = ();
703         
704         foreach $source (@progsources) {
705             my $suffix = $source;
706             $suffix =~ s/^.*\.([^\.]+)$/$1/;
707             
708             $sourcelist{$suffix} .= "$source ";
709         }
710         foreach my $mocFile (keys (%globalmocs))
711         {
712             my ($dir, $hFile, $cppFile) = split ("\035", $globalmocs{$mocFile}, 3);
713             if (defined ($cppFile)) {
714                 $mocs .= " $mocFile.moc" if exists $shash{$cppFile};
715             } else {
716                 $sourcelist{$cxxsuffix} .= "$mocFile.moc.$cxxsuffix ";
717                 $moc_cpp_added = 1;
718             }
719         }
720         foreach $suffix (keys %sourcelist) {
721             
722             # See if this file contains c++ code. (i.e., just check the file's suffix against c++ extensions)
723             my $suffix_is_cxx = 0;
724             if($suffix =~ /($cppExt)$/) {
725               $cxxsuffix = $1;
726               $suffix_is_cxx = 1;
727             }
728             
729             my $mocfiles_in = ($suffix eq $cxxsuffix) && $moc_cpp_added;
730             
731             my @sourcelist = split(/[\s\034]+/, $sourcelist{$suffix});
732             
733             if ((@sourcelist == 1 && !$mocfiles_in) || $suffix_is_cxx != 1 ) {
734                 
735                 # we support IDL on our own
736                 if ($suffix eq "skel" || $suffix =~ /^stub/ || $suffix =~ /^signals/ 
737                     || $suffix eq "h" || $suffix eq "ui" ) {
738                     next;
739                 }
740                 
741                 foreach $file (@sourcelist) {
742                     $file =~ s/\Q$suffix\E$//;
743                     
744                     $finalObjs{$program} .= $file;
745                     if ($program =~ /_la$/) {
746                         $finalObjs{$program} .= "lo ";
747                     } else {
748                         $finalObjs{$program} .= "o ";
749                     }
750                 }
751                 next; # suffix
752             }
753             
754             my $source_deps = "";
755             foreach $source (@sourcelist) {
756                 if (-f $source) {
757                     $source_deps .= " \$(srcdir)/$source";
758                 } else {
759                     $source_deps .= " $source";
760                 }
761             }
762
763             $handling = "$program.all_$suffix.$suffix: \$(srcdir)/Makefile.in" . $source_deps . " " . join(' ', $mocs)  . "\n";
764             $handling .= "\t\@echo 'creating $program.all_$suffix.$suffix ...'; \\\n";
765             $handling .= "\trm -f $program.all_$suffix.files $program.all_$suffix.final; \\\n";
766             $handling .= "\techo \"#define KDE_USE_FINAL 1\" >> $program.all_$suffix.final; \\\n";
767             $handling .= "\tfor file in " . $sourcelist{$suffix} . "; do \\\n";
768             $handling .= "\t  echo \"#include \\\"\$\$file\\\"\" >> $program.all_$suffix.files; \\\n";
769             $handling .= "\t  test ! -f \$\(srcdir\)/\$\$file || egrep '^#pragma +implementation' \$\(srcdir\)/\$\$file >> $program.all_$suffix.final; \\\n";
770             $handling .= "\tdone; \\\n";
771             $handling .= "\tcat $program.all_$suffix.final $program.all_$suffix.files > $program.all_$suffix.$suffix; \\\n";
772             $handling .= "\trm -f $program.all_$suffix.final $program.all_$suffix.files\n";
773
774             appendLines($handling);
775
776             push(@final_names, "$program.all_$suffix.$suffix");
777             my $finalObj = "$program.all_$suffix.";
778             if ($program =~ /_la$/) {
779                 $finalObj .= "lo";
780             } else {
781                 $finalObj .= "o";
782             }
783             $finalObjs{$program} .= $finalObj . " ";
784         }
785     }
786     
787     if (!$kdeopts{"nofinal"} && @final_names >= 1) {
788         # add clean-final target
789         my $lines = "$cleantarget-final:\n";
790         $lines .= "\t-rm -f " . join(' ', @final_names) . "\n" if (@final_names);
791         appendLines($lines);
792         $target_adds{"$cleantarget-am"} .= "$cleantarget-final ";
793         
794         foreach $finalfile (@final_names) {
795             $finalfile =~ s/\.[^.]*$/.P/;
796             $dep_finals .= " \$(DEPDIR)/$finalfile";
797         }
798     }
799 }
800
801 #-----------------------------------------------------------------------------
802
803 sub tag_COMPILE_FIRST()
804 {
805   foreach $program (@programs) {
806     my $lookup = "$program" . '_COMPILE_FIRST\s*=\s*(.*)';
807     if ($MakefileData =~ m/\n$lookup\n/) {
808       my @compilefirst = split(/[\s\034]+/, $1);
809       my @progsources = split(/[\s\034]+/, $sources{$program});
810       my %donesources = ();
811       $handling = "";
812       foreach $source (@progsources) {
813         my @deps  = ();
814         my $sdeps = "";
815         if (-f $source) {
816           $sdeps = "\$(srcdir)/$source";
817         } else {
818           $sdeps = "$source";
819         }
820         foreach $depend (@compilefirst) {
821           next if ($source eq $depend);
822           # avoid cyclic dependencies
823           next if defined($donesources{$depend});
824           push @deps, $depend;
825         }
826         $handling .= "$sdeps: " . join(' ', @deps) . "\n" if (@deps);
827         $donesources{$source} = 1;
828       }
829       appendLines($handling) if (length($handling));
830     }
831   }
832 }
833
834 #-----------------------------------------------------------------------------
835
836
837 # Organises the list of headers that we'll use to produce moc files
838 # from.
839 sub tag_METASOURCES ()
840 {
841     local @newObs           = ();  # here we add to create object files
842     local @deped            = ();  # here we add to create moc files
843     local $mocExt           = ".moc";
844     local %mocFiles         = ();
845
846     my $line = "";
847     my $postEqual = "";
848
849     my $lookup;
850     my $found = "";
851 #print "$program: tag_METASOURCES\n";
852     if ($metasourceTags > 1) {
853         $lookup = $program . '_METASOURCES\s*=\s*(.*)';
854         return 1    if ($MakefileData !~ /\n($lookup)\n/);
855         $found = $1;
856     } else {
857         $lookup = $program . '_METASOURCES\s*=\s*(.*)';
858         if ($MakefileData !~ /\n($lookup)\n/) {
859             $lookup = 'METASOURCES\s*=\s*(.*)';
860             return 1    if ($MakefileData !~ /\n($lookup)\n/o);
861             $found = $1;
862             $metasourceTags = 0; # we can use the general target only once
863         } else {
864             $found = $1;
865         }
866     }
867     print STDOUT "METASOURCE processing <$found>)\n"      if ($verbose);
868     
869     $postEqual = $found;
870     $postEqual =~ s/[^=]*=//;
871     
872     removeLine ($lookup, $found);
873     
874     # Always find the header files that could be used to "moc"
875     return 1    if (findMocCandidates ());
876     
877     if ($postEqual =~ /AUTO\s*(\S*)|USE_AUTOMOC\s*(\S*)/)
878     {
879         print STDERR "$printname: the argument for AUTO|USE_AUTOMOC is obsolete" if ($+);
880         $mocExt = ".moc.$cxxsuffix";
881         $haveAutomocTag = 1;
882     }
883     else
884     {
885         # Not automoc so read the list of files supplied which
886         # should be .moc files.
887
888         $postEqual =~ tr/\034/ /;
889
890         # prune out extra headers - This also checks to make sure that
891         # the list is valid.
892         pruneMocCandidates ($postEqual);
893     }
894
895     checkMocCandidates ();
896     
897     if (@newObs) {
898         my $ext =  ($program =~ /_la$/) ? ".moc.lo " : ".moc.o ";
899         $realObjs{$program} .= "\034" . join ($ext, @newObs) . $ext;
900         $depedmocs{$program} = join (".moc.$cxxsuffix " , @newObs) . ".moc.$cxxsuffix";
901         foreach $file (@newObs) {
902             $dep_files .= " \$(DEPDIR)/$file.moc.P" if($dep_files !~/$file.moc.P/);
903         }
904     }
905     if (@deped) {
906         $depedmocs{$program} .= " ";
907         $depedmocs{$program} .= join('.moc ', @deped) . ".moc";
908         $depedmocs{$program} .= " ";
909     }
910     addMocRules ();
911     @globalmocs{keys %mocFiles}=values %mocFiles;
912 }
913
914 #-----------------------------------------------------------------------------
915
916 # Returns 0 if the line was processed - 1 otherwise.
917 # Errors are logged in the global $errorflags
918 sub tag_AUTOMAKE ()
919 {
920     my $lookup = '.*cd \$\(top_srcdir\)\s+&&[\s\034]+\$\(AUTOMAKE\)(.*)';
921     return 1    if ($MakefileData !~ /\n($lookup)\n/);
922     print STDOUT "AUTOMAKE processing <$1>\n"        if ($verbose);
923
924     my $newLine = $1."\n\tcd \$(top_srcdir) && perl $thisProg $printname";
925     substituteLine ($lookup, $newLine);
926     $automkCall = $1;
927     return 0;
928 }
929
930 #-----------------------------------------------------------------------------
931
932 sub handle_TOPLEVEL()
933 {
934     my $pofiles = "";
935     my @restfiles = ();
936     opendir (THISDIR, ".");
937     foreach $entry (readdir(THISDIR)) {
938         next if (-d $entry);
939         
940         next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/ || $entry =~ /.gmo$/);
941                  
942         if ($entry =~ /\.po$/) {
943              next;
944         }
945         push(@restfiles, $entry);
946     }
947     closedir (THISDIR);
948             
949     if (@restfiles) {
950         $target_adds{"install-data-am"} .= "install-nls-files ";
951         $lines = "install-nls-files:\n";
952         $lines .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/$kdelang\n";
953         for $file (@restfiles) {
954             $lines .= "\t\$(INSTALL_DATA) \$\(srcdir\)/$file \$(DESTDIR)\$(kde_locale)/$kdelang/$file\n";
955         }
956         $target_adds{"uninstall"} .= "uninstall-nls-files ";
957         $lines .= "uninstall-nls-files:\n";
958         for $file (@restfiles) {
959             $lines .= "\t-rm -f \$(DESTDIR)\$(kde_locale)/$kdelang/$file\n";
960         }
961         appendLines($lines);
962     }
963     
964     return 0;
965 }
966
967 #-----------------------------------------------------------------------------
968
969 sub tag_SUBDIRS ()
970 {
971   if ($MakefileData !~ /\nSUBDIRS\s*=\s*\$\(AUTODIRS\)\s*\n/) {
972     return 1;
973   }
974
975   my $subdirs = ".";
976
977   opendir (THISDIR, ".");
978   foreach $entry (readdir(THISDIR)) {
979     next if ($entry eq "CVS" || $entry =~ /^\./);
980     if (-d $entry && -f $entry . "/Makefile.am") {
981       $subdirs .= " $entry";
982       next;
983     }
984   }
985   closedir (THISDIR);
986
987   my $lines = "SUBDIRS =$subdirs\n";
988   substituteLine('SUBDIRS\s*=.*', $lines);
989   return 0;
990 }
991
992 sub tag_IDLFILES ()
993 {
994     my @psources = split(/[\034\s]+/, $sources{$program});
995     my $dep_lines = "";
996     my @cppFiles = ();
997     
998     foreach $source (@psources) {
999         
1000         my $skel = ($source =~ m/\.skel$/);
1001         my $stub = ($source =~ m/\.stub$/);
1002         my $signals = ($source =~ m/\.signals$/);
1003         
1004         if ($stub || $skel || $signals) {
1005             
1006             my $qs = quotemeta($source);
1007             $sources{$program} =~ s/$qs//;
1008             $sources_changed{$program} = 1;
1009             
1010             print STDOUT "adding IDL file $source\n" if ($verbose);
1011             
1012             $source =~ s/\.(stub|skel|signals)$//;
1013             
1014             my $sourcename;
1015             
1016             if ($skel) {
1017                 $sourcename = "$source\_skel";
1018             } elsif ($stub) {
1019                 $sourcename = "$source\_stub";
1020             } else {
1021                 $sourcename = "$source\_signals";
1022             }
1023             
1024             my $sourcedir = '';
1025             if (-f "$makefileDir/$source.h") {
1026                 $sourcedir = '$(srcdir)/';
1027             } else {
1028                 if ($MakefileData =~ /\n$source\_DIR\s*=\s*(\S+)\n/) {
1029                     $sourcedir = $1;
1030                     $sourcedir .= "/" if ($sourcedir !~ /\/$/);
1031                 }
1032             }
1033             
1034             if ($allidls !~ /$source\_kidl/) {
1035                 
1036                 $dep_lines .= "$source.kidl: $sourcedir$source.h \$(DCOP_DEPENDENCIES)\n";
1037                 $dep_lines .= "\t\$(DCOPIDL) $sourcedir$source.h > $source.kidl || ( rm -f $source.kidl ; /bin/false )\n";
1038                 
1039                 $allidls .= $source . "_kidl ";
1040             }
1041             
1042             if ($allidls !~ /$sourcename/) {
1043                 
1044                 $dep_lines_tmp = "";
1045
1046                 if ($skel) {
1047                     $dep_lines .= "$sourcename.$cxxsuffix: $source.kidl\n";
1048                     $dep_lines .= "\t\$(DCOPIDL2CPP) --c++-suffix $cxxsuffix --no-signals --no-stub $source.kidl\n";
1049                 } elsif ($stub) {
1050                     $dep_lines_tmp = "\t\$(DCOPIDL2CPP) --c++-suffix $cxxsuffix --no-signals --no-skel $source.kidl\n";
1051                 } else { # signals
1052                     $dep_lines_tmp = "\t\$(DCOPIDL2CPP) --c++-suffix $cxxsuffix --no-stub --no-skel $source.kidl\n";
1053                 }
1054
1055                 if ($stub || $signals) {
1056                     $target_adds{"$sourcename.$cxxsuffix"} .= "$sourcename.h ";
1057                     $dep_lines .= "$sourcename.h: $source.kidl\n";
1058                     $dep_lines .= $dep_lines_tmp;
1059                 }
1060                 
1061                 $allidls .= $sourcename . " ";
1062             }
1063             
1064             $idlfiles{$program} .= $sourcename . " ";
1065             
1066             if ($program =~ /_la$/) {
1067                 $realObjs{$program} .= " $sourcename.lo";
1068             } else {
1069                 $realObjs{$program} .= " $sourcename.\$(OBJEXT)";
1070             }
1071             $sources{$program} .= " $sourcename.$cxxsuffix";
1072             $sources_changed{$program} = 1;
1073             $important{$program} .= "$sourcename.h " if (!$skel);
1074             $idl_output .= "\\\n\t$sourcename.$cxxsuffix $sourcename.h $source.kidl ";
1075             push(@cleanfiles, "$sourcename.$cxxsuffix");
1076             push(@cleanfiles, "$sourcename.h");
1077             push(@cleanfiles, "$sourcename.kidl");
1078             $dep_files .= " \$(DEPDIR)/$sourcename.P" if ($dep_files !~/$sourcename.P/);
1079         }
1080     }
1081     if ($dep_lines) {
1082         appendLines($dep_lines);
1083     }
1084     
1085     if (0) {
1086         my $lookup = "($program)";
1087         $lookup .= '(|\$\(EXEEXT\))';
1088         $lookup =~ s/\_/./g;
1089         $lookup .= ":(.*..$program\_OBJECTS..*)";
1090         #    $lookup = quotemeta($lookup);
1091         if ($MakefileData =~ /\n$lookup\n/) {
1092             
1093             my $line = "$1$2: ";
1094             foreach $file (split(' ', $idlfiles{$program})) {
1095                 $line .= "$file.$cxxsuffix ";
1096             }
1097             $line .= $3;
1098             substituteLine($lookup, $line);
1099         } else {
1100             print STDERR "no built dependency found $lookup\n";
1101         }
1102     }
1103 }
1104
1105 sub tag_UIFILES ()
1106 {
1107     my @psources = split(/[\034\s]+/, $sources{$program});
1108     my $dep_lines = "";
1109     my @depFiles = ();
1110     
1111     foreach $source (@psources) {
1112
1113         if ($source =~ m/\.ui$/) {
1114
1115             print STDERR "adding UI file $source\n" if ($verbose);
1116
1117             my $qs = quotemeta($source);
1118             $sources{$program} =~ s/$qs//;
1119             $sources_changed{$program} = 1;
1120       
1121             $source =~ s/\.ui$//;
1122
1123             my $sourcedir = '';
1124             if (-f "$makefileDir/$source.ui") {
1125                 $sourcedir = '$(srcdir)/';
1126             }
1127
1128             if (!$uiFiles{$source}) {
1129
1130                 $dep_lines .= "$source.$cxxsuffix: $sourcedir$source.ui $source.h $source.moc\n";
1131                 $dep_lines .= "\trm -f $source.$cxxsuffix\n";
1132                 if (!$kdeopts{"qtonly"}) {
1133                     $dep_lines .= "\techo '#include <klocale.h>' > $source.$cxxsuffix\n";
1134                     my ($mangled_source) = $source;
1135                     $mangled_source =~ s/[^A-Za-z0-9]/_/g;  # get rid of garbage
1136                     $dep_lines .= "\t\$(UIC) -tr \${UIC_TR} -i $source.h $sourcedir$source.ui > $source.$cxxsuffix.temp ; ret=\$\$?; \\\n";
1137                     $dep_lines .= "\tsed -e \"s,\${UIC_TR}( \\\"\\\" ),QString::null,g\" $source.$cxxsuffix.temp | sed -e \"s,\${UIC_TR}( \\\"\\\"\\, \\\"\\\" ),QString::null,g\" | sed -e \"s,image\\([0-9][0-9]*\\)_data,img\\1_" . $mangled_source . ",g\" >> $source.$cxxsuffix ;\\\n";
1138                     $dep_lines .= "\trm -f $source.$cxxsuffix.temp ;\\\n";
1139                 } else {
1140                     $dep_lines .= "\t\$(UIC) -i $source.h $sourcedir$source.ui > $source.$cxxsuffix; ret=\$\$?; \\\n";
1141                 }
1142                 $dep_lines .= "\tif test \"\$\$ret\" = 0; then echo '#include \"$source.moc\"' >> $source.$cxxsuffix; else rm -f $source.$cxxsuffix ; exit \$\$ret ; fi\n\n";
1143                 $dep_lines .= "$source.h: $sourcedir$source.ui\n";
1144                 $dep_lines .= "\t\$(UIC) -o $source.h $sourcedir$source.ui\n\n";
1145                 $dep_lines .= "$source.moc: $source.h\n";
1146                 $dep_lines .= "\t\$(MOC) $source.h -o $source.moc\n";
1147
1148                 $uiFiles{$source} = 1;
1149                 $depedmocs{$program} .= " $source.moc";
1150                 $globalmocs{$source} = "\035$source.h\035$source.cpp";
1151             }
1152             
1153             if ($program =~ /_la$/) {
1154                 $realObjs{$program} .= " $source.lo";
1155             } else {
1156                 $realObjs{$program} .= " $source.\$(OBJEXT)";
1157             }
1158             $sources{$program} .= " $source.$cxxsuffix";
1159             $sources_changed{$program} = 1;
1160             $important{$program} .= "$source.h ";
1161             $ui_output .= "\\\n\t$source.$cxxsuffix $source.h $source.moc ";
1162             push(@cleanfiles, "$source.$cxxsuffix");
1163             push(@cleanfiles, "source.h");
1164             push(@cleanfiles, "$source.moc");
1165             $dep_files .= " \$(DEPDIR)/$source.P" if($dep_files !~/$source.P/ );
1166         }
1167     }
1168     if ($dep_lines) {
1169         appendLines($dep_lines);
1170     }
1171 }
1172
1173 sub tag_ICON()
1174 {
1175     my $lookup = '([^\s]*)_ICON\s*=\s*([^\n]*)';
1176     my $install = "";
1177     my $uninstall = "";
1178
1179     while ($MakefileData =~ /\n$lookup/og) {
1180         my $destdir;
1181         if ($1 eq "KDE") {
1182             $destdir = "kde_icondir";
1183         } else {
1184             $destdir = $1 . "dir";
1185         }
1186         my $iconauto = ($2 =~ /AUTO\s*$/);
1187         my @appnames = ();
1188         if ( ! $iconauto ) {
1189             my @_appnames = split(" ", $2);
1190             print STDOUT "KDE_ICON processing <@_appnames>\n"   if ($verbose);
1191             foreach $appname (@_appnames) {
1192                 push(@appnames, quotemeta($appname));
1193             }
1194         } else {
1195             print STDOUT "KDE_ICON processing <AUTO>\n"   if ($verbose);
1196         }
1197
1198         my @files = ();
1199         opendir (THISDIR, ".");
1200         foreach $entry (readdir(THISDIR)) {
1201             next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/);
1202             next if (! -f $entry);
1203             if ( $iconauto )
1204               {
1205                   push(@files, $entry)
1206                     if ($entry =~ /\.xpm/ || $entry =~ /\.png/ || $entry =~ /\.mng/ || $entry =~ /\.svg/);
1207               } else {
1208                   foreach $appname (@appnames) {
1209                       push(@files, $entry)
1210                         if ($entry =~ /-$appname\.xpm/ || $entry =~ /-$appname\.png/ || $entry =~ /-$appname\.mng/ || $entry =~ /-$appname\.svg/);
1211                   }
1212               }
1213         }
1214         closedir (THISDIR);
1215         
1216         my %directories = ();
1217         
1218         foreach $file (@files) {
1219             my $newfile = $file;
1220             my $prefix = $file;
1221             $prefix =~ s/\.(png|xpm|mng|svg|svgz)$//;
1222             my $appname = $prefix;
1223             $appname =~ s/^[^-]+-// if ($appname =~ /-/) ;
1224             $appname =~ s/^[^-]+-// if ($appname =~ /-/) ;
1225             $appname = quotemeta($appname);
1226             $prefix =~ s/$appname$//;
1227             $prefix =~ s/-$//;
1228             
1229             $prefix = 'lo16-app' if ($prefix eq 'mini');
1230             $prefix = 'lo32-app' if ($prefix eq 'lo');
1231             $prefix = 'hi48-app' if ($prefix eq 'large');
1232             $prefix .= '-app' if ($prefix =~ m/^...$/);
1233             
1234             my $type = $prefix;
1235             $type =~ s/^.*-([^-]+)$/$1/;
1236             $prefix =~ s/^(.*)-[^-]+$/$1/;
1237             
1238             my %type_hash =
1239               (
1240                'action' => 'actions',
1241                'app' => 'apps',
1242                'device' => 'devices',
1243                'filesys' => 'filesystems',
1244                'mime' => 'mimetypes'
1245               );
1246
1247             if (! defined $type_hash{$type} ) {
1248                 print STDERR "unknown icon type $type in $printname ($file)\n";
1249                 next;
1250             }
1251
1252             my %dir_hash =
1253               (
1254                'los' => 'locolor/16x16',
1255                'lom' => 'locolor/32x32',
1256                'him' => 'hicolor/32x32',
1257                'hil' => 'hicolor/48x48',
1258                'lo16' => 'locolor/16x16',
1259                'lo22' => 'locolor/22x22',
1260                'lo32' => 'locolor/32x32',
1261                'hi16' => 'hicolor/16x16',
1262                'hi22' => 'hicolor/22x22',
1263                'hi32' => 'hicolor/32x32',
1264                'hi48' => 'hicolor/48x48',
1265                'hi64' => 'hicolor/64x64',
1266                'hi128' => 'hicolor/128x128',
1267                'hisc' => 'hicolor/scalable',
1268                'cr16' => 'crystalsvg/16x16',
1269                'cr22' => 'crystalsvg/22x22',
1270                'cr32' => 'crystalsvg/32x32',
1271                'cr48' => 'crystalsvg/48x48',
1272                'cr64' => 'crystalsvg/64x64',
1273                'cr128' => 'crystalsvg/128x128',
1274                'crsc' => 'crystalsvg/scalable'
1275               );
1276             
1277             $newfile =~ s@.*-($appname\.(png|xpm|mng|svgz|svg?))@$1@;
1278             
1279             if (! defined $dir_hash{$prefix}) {
1280                 print STDERR "unknown icon prefix $prefix in $printname\n";
1281                 next;
1282             }
1283             
1284             my $dir = $dir_hash{$prefix} . "/" . $type_hash{$type};
1285             if ($newfile =~ /-[^\.]/) {
1286                 my $tmp = $newfile;
1287                 $tmp =~ s/^([^-]+)-.*$/$1/;
1288                 $dir = $dir . "/" . $tmp;
1289                 $newfile =~ s/^[^-]+-//;
1290             }
1291             
1292             if (!defined $directories{$dir}) {
1293                 $install .= "\t\$(mkinstalldirs) \$(DESTDIR)\$($destdir)/$dir\n";
1294                 $directories{$dir} = 1;
1295             }
1296             
1297             $install .= "\t\$(INSTALL_DATA) \$(srcdir)/$file \$(DESTDIR)\$($destdir)/$dir/$newfile\n";
1298             $uninstall .= "\t-rm -f \$(DESTDIR)\$($destdir)/$dir/$newfile\n";
1299             
1300         }
1301     }
1302
1303     if (length($install)) {
1304         $target_adds{"install-data-am"} .= "install-kde-icons ";
1305         $target_adds{"uninstall-am"} .= "uninstall-kde-icons ";
1306         appendLines("install-kde-icons:\n" . $install . "\nuninstall-kde-icons:\n" . $uninstall);
1307     }
1308 }
1309
1310 sub handle_POFILES($$)
1311 {
1312   my @pofiles = split(" ", $_[0]);
1313   my $lang = $_[1];
1314
1315   # Build rules for creating the gmo files
1316   my $tmp = "";
1317   my $allgmofiles     = "";
1318   my $pofileLine   = "POFILES =";
1319   foreach $pofile (@pofiles)
1320     {
1321         $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1322         $tmp .= "$1.gmo: $pofile\n";
1323         $tmp .= "\trm -f $1.gmo; \$(GMSGFMT) -o $1.gmo \$(srcdir)/$pofile\n";
1324         $tmp .= "\ttest ! -f $1.gmo || touch $1.gmo\n";
1325         $allgmofiles .= " $1.gmo";
1326         $pofileLine  .= " $1.po";
1327     }
1328   appendLines ($tmp);
1329   my $lookup = 'POFILES\s*=([^\n]*)';
1330   if ($MakefileData !~ /\n$lookup/o) {
1331     appendLines("$pofileLine\nGMOFILES =$allgmofiles");
1332   } else {
1333     substituteLine ($lookup, "$pofileLine\nGMOFILES =$allgmofiles");
1334   }
1335
1336     if ($allgmofiles) {
1337
1338         # Add the "clean" rule so that the maintainer-clean does something
1339         appendLines ("clean-nls:\n\t-rm -f $allgmofiles\n");
1340
1341         $target_adds{"maintainer-clean"} .= "clean-nls ";
1342
1343         $lookup = 'DISTFILES\s*=\s*(.*)';
1344         if ($MakefileData =~ /\n$lookup\n/o) {
1345           $tmp = "DISTFILES = \$(GMOFILES) \$(POFILES) $1";
1346           substituteLine ($lookup, $tmp);
1347         }
1348     }
1349
1350   $target_adds{"install-data-am"} .= "install-nls ";
1351
1352   $tmp = "install-nls:\n";
1353   if ($lang) {
1354     $tmp  .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES\n";
1355   }
1356   $tmp .= "\t\@for base in ";
1357   foreach $pofile (@pofiles)
1358     {
1359       $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1360       $tmp .= "$1 ";
1361     }
1362
1363   $tmp .= "; do \\\n";
1364   if ($lang) {
1365     $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n";
1366     $tmp .= "\t  if test -f \$\$base.gmo; then \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n";
1367     $tmp .= "\t  elif test -f \$(srcdir)/\$\$base.gmo; then \$(INSTALL_DATA) \$(srcdir)/\$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n";
1368     $tmp .= "\t  fi ;\\\n";
1369   } else {
1370     $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
1371     $tmp .= "\t  \$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES ; \\\n";
1372     $tmp .= "\t  if test -f \$\$base.gmo; then \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
1373     $tmp .= "\t  elif test -f \$(srcdir)/\$\$base.gmo; then \$(INSTALL_DATA) \$(srcdir)/\$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
1374     $tmp .= "\t  fi ;\\\n";
1375   }
1376   $tmp .= "\tdone\n\n";
1377   appendLines ($tmp);
1378
1379   $target_adds{"uninstall"} .= "uninstall-nls ";
1380
1381   $tmp = "uninstall-nls:\n";
1382   foreach $pofile (@pofiles)
1383     {
1384       $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1385       if ($lang) {
1386         $tmp .= "\trm -f \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/$1.mo\n";
1387       } else {
1388         $tmp .= "\trm -f \$(DESTDIR)\$(kde_locale)/$1/LC_MESSAGES/\$(PACKAGE).mo\n";
1389       }
1390     }
1391   appendLines($tmp);
1392
1393   $target_adds{"all"} .= "all-nls ";
1394
1395   $tmp = "all-nls: \$(GMOFILES)\n";
1396
1397   appendLines($tmp);
1398
1399   $target_adds{"distdir"} .= "distdir-nls ";
1400
1401   $tmp = "distdir-nls:\$(GMOFILES)\n";
1402   $tmp .= "\tfor file in \$(POFILES); do \\\n";
1403   $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
1404   $tmp .= "\tdone\n";
1405   $tmp .= "\tfor file in \$(GMOFILES); do \\\n";
1406   $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
1407   $tmp .= "\tdone\n";
1408
1409   appendLines ($tmp);
1410
1411   if (!$lang) {
1412     appendLines("merge:\n\t\$(MAKE) -f \$(top_srcdir)/admin/Makefile.common package-merge POFILES=\"\${POFILES}\" PACKAGE=\${PACKAGE}\n\n");
1413   }
1414  
1415 }
1416
1417 #-----------------------------------------------------------------------------
1418
1419 # Returns 0 if the line was processed - 1 otherwise.
1420 # Errors are logged in the global $errorflags
1421 sub tag_POFILES ()
1422 {
1423     my $lookup = 'POFILES\s*=([^\n]*)';
1424     return 1    if ($MakefileData !~ /\n$lookup/o);
1425     print STDOUT "POFILES processing <$1>\n"   if ($verbose);
1426
1427     my $tmp = $1;
1428
1429     # make sure these are all gone.
1430     if ($MakefileData =~ /\n\.po\.gmo:\n/)
1431     {
1432         print STDERR "Warning: Found old .po.gmo rules in $printname. New po rules not added\n";
1433         return 1;
1434     }
1435
1436     # Either find the pofiles in the directory (AUTO) or use
1437     # only the specified po files.
1438     my $pofiles = "";
1439     if ($tmp =~ /^\s*AUTO\s*$/)
1440     {
1441         opendir (THISDIR, ".");
1442         $pofiles =  join(" ", grep(/\.po$/, readdir(THISDIR)));
1443         closedir (THISDIR);
1444         print STDOUT "pofiles found = $pofiles\n"   if ($verbose);
1445         if (-f "charset" && -f "kdelibs.po") {
1446             handle_TOPLEVEL();
1447         }
1448     }
1449     else
1450     {
1451         $tmp =~ s/\034/ /g;
1452         $pofiles = $tmp;
1453     }
1454     return 1    if (!$pofiles);        # Nothing to do
1455
1456     handle_POFILES($pofiles, $kdelang);
1457
1458     return 0;
1459 }
1460
1461 sub helper_LOCALINSTALL($)
1462 {
1463   my $lookup = "\035" . $_[0] . " *:[^\035]*\035\t";
1464   my $copy = $MakefileData;
1465   $copy =~ s/\n/\035/g;
1466   if ($copy =~ /($lookup.*)$/) {
1467
1468     $install = $1;
1469     $install =~ s/\035$_[0] *:[^\035]*\035//;
1470     my $emptyline = 0;
1471     while (! $emptyline ) {
1472       if ($install =~ /([^\035]*)\035(.*)/) {
1473         local $line = $1;
1474         $install = $2;
1475         if ($line !~ /^\s*$/ && $line !~ /^(\@.*\@)*\t/) {
1476           $emptyline = 1;
1477         } else {
1478           replaceDestDir($line);
1479         }
1480       } else {
1481         $emptyline = 1;
1482       }
1483     }
1484   }
1485
1486 }
1487
1488 sub tag_LOCALINSTALL ()
1489 {
1490   helper_LOCALINSTALL('install-exec-local');
1491   helper_LOCALINSTALL('install-data-local');
1492   helper_LOCALINSTALL('uninstall-local');
1493
1494   return 0;
1495 }
1496
1497 sub replaceDestDir($) {
1498   local $line = $_[0];
1499
1500   if (   $line =~ /^\s*(\@.*\@)*\s*\$\(mkinstalldirs\)/
1501       || $line =~ /^\s*(\@.*\@)*\s*\$\(INSTALL\S*\)/
1502       || $line =~ /^\s*(\@.*\@)*\s*(-?rm.*) \S*$/)
1503   {
1504     $line =~ s/^(.*) ([^\s]+)\s*$/$1 \$(DESTDIR)$2/ if ($line !~ /\$\(DESTDIR\)/);
1505   }
1506
1507   if ($line ne $_[0]) {
1508     $_[0] = quotemeta $_[0];
1509     substituteLine($_[0], $line);
1510   }
1511 }
1512
1513 #---------------------------------------------------------------------------
1514 sub tag_CLOSURE () {
1515     return if ($program !~ /_la$/);
1516
1517     my $lookup = quotemeta($realname{$program}) . ":.*?\n\t.*?\\((.*?)\\) .*\n";
1518     $MakefileData =~ m/$lookup/;
1519     return if ($1 !~ /CXXLINK/);
1520
1521     if ($MakefileData !~ /\n$program\_LDFLAGS\s*=.*-no-undefined/ &&
1522         $MakefileData !~ /\n$program\_LDFLAGS\s*=.*KDE_PLUGIN/ ) {
1523         print STDERR "Report: $program contains undefined in $printname\n" if ($program =~ /^lib/ && $dryrun);
1524         return;
1525     }
1526
1527     my $closure = $realname{$program} . ".closure";
1528     my $lines = "$closure: \$($program\_OBJECTS) \$($program\_DEPENDENCIES)\n";
1529     $lines .= "\t\@echo \"int main() {return 0;}\" > $program\_closure.$cxxsuffix\n";
1530     $lines .= "\t\@\$\(LTCXXCOMPILE\) -c $program\_closure.$cxxsuffix\n";
1531     $lines .= "\t\$\(CXXLINK\) $program\_closure.lo \$($program\_LDFLAGS) \$($program\_OBJECTS) \$($program\_LIBADD) \$(LIBS)\n";
1532     $lines .= "\t\@rm -f $program\_closure.* $closure\n";
1533     $lines .= "\t\@echo \"timestamp\" > $closure\n";
1534     $lines .= "\n";
1535     appendLines($lines);
1536     $lookup = $realname{$program} . ": (.*)";
1537     if ($MakefileData =~ /\n$lookup\n/) {
1538         $lines  = "\@KDE_USE_CLOSURE_TRUE@". $realname{$program} . ": $closure $1";
1539         $lines .= "\n\@KDE_USE_CLOSURE_FALSE@" . $realname{$program} . ": $1";
1540         substituteLine($lookup, $lines);
1541     }
1542     $closure_output .= " $closure";
1543 }
1544
1545 sub tag_DIST () {
1546     my %foundfiles = ();
1547     opendir (THISDIR, ".");
1548     foreach $entry (readdir(THISDIR)) {
1549         next if ($entry eq "CVS" || $entry =~ /^\./  || $entry eq "Makefile" || $entry =~ /~$/ || $entry =~ /^\#.*\#$/);
1550         next if (! -f $entry);
1551         next if ($entry =~ /\.moc/ || $entry =~ /\.moc.$cppExt$/ || $entry =~ /\.lo$/ || $entry =~ /\.la$/ || $entry =~ /\.o/);
1552         next if ($entry =~ /\.all_$cppExt\.$cppExt$/);
1553         $foundfiles{$entry} = 1;
1554     }
1555     closedir (THISDIR);
1556
1557     # doing this for MAINTAINERCLEANFILES would be wrong
1558     my @marks = ("EXTRA_DIST", "DIST_COMMON", '\S*_SOURCES', '\S*_HEADERS', 'CLEANFILES', 'DISTCLEANFILES', '\S*_OBJECTS');
1559     foreach $mark (@marks) {
1560         while ($MakefileData =~ /\n($mark)\s*=\s*([^\n]*)/g) {
1561             foreach $file (split('[\034\s]', $2)) {
1562                 $file =~ s/\.\///;
1563                 $foundfiles{$file} = 0 if (defined $foundfiles{$file});
1564             }
1565         }
1566     }
1567     my @files = ("Makefile", "config.cache", "config.log", "stamp-h",
1568                  "stamp-h1", "stamp-h1", "config.h", "Makefile", 
1569                  "config.status", "config.h", "libtool", "core" );
1570     foreach $file (@files) {
1571         $foundfiles{$file} = 0 if (defined $foundfiles{$file});
1572     }
1573
1574     my $KDE_DIST = "";
1575     foreach $file (keys %foundfiles) {
1576         if ($foundfiles{$file} == 1) {
1577             $KDE_DIST .= "$file ";
1578         }
1579     }
1580     if ($KDE_DIST) {
1581         print "KDE_DIST $printname $KDE_DIST\n" if ($verbose);
1582
1583         my $lookup = "DISTFILES *=(.*)";
1584         if ($MakefileData =~ /\n$lookup\n/o) {
1585             substituteLine($lookup, "KDE_DIST=$KDE_DIST\n\nDISTFILES=$1 \$(KDE_DIST)\n");
1586         }
1587     }
1588 }
1589
1590 #-----------------------------------------------------------------------------
1591 # Returns 0 if the line was processed - 1 otherwise.
1592 # Errors are logged in the global $errorflags
1593 sub tag_DOCFILES ()
1594 {
1595     $target_adds{"all"} .= "docs-am ";
1596
1597     my $lookup = 'KDE_DOCS\s*=\s*([^\n]*)';
1598     goto nodocs    if ($MakefileData !~ /\n$lookup/o);
1599     print STDOUT "KDE_DOCS processing <$1>\n"   if ($verbose);
1600
1601     my $tmp = $1;
1602
1603     # Either find the files in the directory (AUTO) or use
1604     # only the specified po files.
1605     my $files = "";
1606     my $appname = $tmp;
1607     $appname =~ s/^(\S*)\s*.*$/$1/;
1608     if ($appname =~ /AUTO/) {
1609       $appname = basename($makefileDir);
1610       if ("$appname" eq "en") {
1611           print STDERR "Error: KDE_DOCS = AUTO relies on the directory name. Yours is 'en' - you most likely want something else, e.g. KDE_DOCS = myapp\n";
1612           exit(1);
1613       }
1614     }
1615
1616     if ($tmp !~ / - /)
1617     {
1618         opendir (THISDIR, ".");
1619         foreach $entry (readdir(THISDIR)) {
1620           next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/ || $entry eq "core" || $entry eq "index.cache.bz2");
1621           next if (! -f $entry);
1622           $files .= "$entry ";
1623         }
1624         closedir (THISDIR);
1625         print STDOUT "docfiles found = $files\n"   if ($verbose);
1626     }
1627     else
1628     {
1629         $tmp =~ s/\034/ /g;
1630         $tmp =~ s/^\S*\s*-\s*//;
1631         $files = $tmp;
1632     }
1633     goto nodocs if (!$files);        # Nothing to do
1634
1635     if ($files =~ /(^| )index\.docbook($| )/) {
1636
1637       my $lines = "";
1638       my $lookup = 'MEINPROC\s*=';
1639       if ($MakefileData !~ /\n($lookup)/) {
1640         $lines = "MEINPROC=/\$(kde_bindir)/meinproc\n";
1641       }
1642       $lookup = 'KDE_XSL_STYLESHEET\s*=';
1643       if ($MakefileData !~ /\n($lookup)/) {
1644         $lines .= "KDE_XSL_STYLESHEET=/\$(kde_datadir)/ksgmltools2/customization/kde-chunk.xsl\n";
1645       }
1646       $lookup = '\nindex.cache.bz2:';
1647       if ($MakefileData !~ /\n($lookup)/) {
1648          $lines .= "index.cache.bz2: \$(srcdir)/index.docbook \$(KDE_XSL_STYLESHEET) $files\n";
1649          $lines .= "\t\@if test -n \"\$(MEINPROC)\"; then echo \$(MEINPROC) --check --cache index.cache.bz2 \$(srcdir)/index.docbook; \$(MEINPROC) --check --cache index.cache.bz2 \$(srcdir)/index.docbook; fi\n";
1650          $lines .= "\n";
1651       }
1652
1653       $lines .= "docs-am: index.cache.bz2\n";
1654       $lines .= "\n";
1655       $lines .= "install-docs: docs-am install-nls\n";
1656       $lines .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname\n";
1657       $lines .= "\t\@if test -f index.cache.bz2; then \\\n";
1658       $lines .= "\techo \$(INSTALL_DATA) index.cache.bz2 \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/; \\\n";
1659       $lines .= "\t\$(INSTALL_DATA) index.cache.bz2 \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/; \\\n";
1660       $lines .= "\telif test -f  \$(srcdir)/index.cache.bz2; then \\\n";
1661       $lines .= "\techo \$(INSTALL_DATA) \$(srcdir)/index.cache.bz2 \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/; \\\n";
1662       $lines .= "\t\$(INSTALL_DATA) \$(srcdir)/index.cache.bz2 \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/; \\\n";
1663       $lines .= "\tfi\n";
1664       $lines .= "\t-rm -f \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/common\n";
1665       $lines .= "\t\$(LN_S) \$(kde_libs_htmldir)/$kdelang/common \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/common\n";
1666
1667       $lines .= "\n";
1668       $lines .= "uninstall-docs:\n";
1669       $lines .= "\t-rm -rf \$(kde_htmldir)/$kdelang/$appname\n";
1670       $lines .= "\n";
1671       $lines .= "clean-docs:\n";
1672       $lines .= "\t-rm -f index.cache.bz2\n";
1673       $lines .= "\n";
1674       $target_adds{"install-data-am"} .= "install-docs ";
1675       $target_adds{"uninstall"} .= "uninstall-docs ";
1676       $target_adds{"clean-am"} .= "clean-docs ";
1677       appendLines ($lines);
1678     } else {
1679       appendLines("docs-am: $files\n");
1680     }
1681
1682     $target_adds{"install-data-am"} .= "install-nls ";
1683     $target_adds{"uninstall"} .= "uninstall-nls ";
1684
1685     $tmp = "install-nls:\n";
1686     $tmp .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname\n";
1687     $tmp .= "\t\@for base in $files; do \\\n";
1688     $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/\$\$base ;\\\n";
1689     $tmp .= "\t  \$(INSTALL_DATA) \$(srcdir)/\$\$base \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/\$\$base ;\\\n";
1690     $tmp .= "\tdone\n";
1691     if ($appname eq 'common') {
1692       $tmp .= "\t\@echo \"merging common and language specific dir\" ;\\\n";
1693       $tmp .= "\tif test ! -f \$(kde_htmldir)/en/common/kde-common.css; then echo 'no english docs found in \$(kde_htmldir)/en/common/'; exit 1; fi \n";
1694       $tmp .= "\t\@com_files=`cd \$(kde_htmldir)/en/common && echo *` ;\\\n";
1695       $tmp .= "\tcd \$(DESTDIR)\$(kde_htmldir)/$kdelang/common ;\\\n";
1696       $tmp .= "\tif test -n \"\$\$com_files\"; then for p in \$\$com_files ; do \\\n";
1697       $tmp .= "\t  case \" $files \" in \\\n";
1698       $tmp .= "\t    *\" \$\$p \"*) ;; \\\n";
1699       $tmp .= "\t    *) test ! -f \$\$p && echo \$(LN_S) ../../en/common/\$\$p \$(DESTDIR)\$(kde_htmldir)/$kdelang/common/\$\$p && \$(LN_S) ../../en/common/\$\$p \$\$p ;; \\\n";
1700       $tmp .= "\t  esac ; \\\n";
1701       $tmp .= "\tdone ; fi ; true\n";
1702     }
1703     $tmp .= "\n";
1704     $tmp .= "uninstall-nls:\n";
1705     $tmp .= "\tfor base in $files; do \\\n";
1706     $tmp .= "\t  rm -f \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/\$\$base ;\\\n";
1707     $tmp .= "\tdone\n\n";
1708     appendLines ($tmp);
1709
1710     $target_adds{"distdir"} .= "distdir-nls ";
1711
1712     $tmp = "distdir-nls:\n";
1713     $tmp .= "\tfor file in $files; do \\\n";
1714     $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
1715     $tmp .= "\tdone\n";
1716
1717     appendLines ($tmp);
1718
1719     return 0;
1720
1721   nodocs:
1722     appendLines("docs-am:\n");
1723     return 1;
1724 }
1725
1726 #-----------------------------------------------------------------------------
1727 # Find headers in any of the source directories specified previously, that
1728 # are candidates for "moc-ing".
1729 sub findMocCandidates ()
1730 {
1731     foreach $dir (@headerdirs)
1732     {
1733         my @list = ();
1734         opendir (SRCDIR, "$dir");
1735         @hFiles = grep { /.+\.$hExt$/o && !/^\./ } readdir(SRCDIR);
1736         closedir SRCDIR;
1737         foreach $hf (@hFiles)
1738         {
1739             next if ($hf =~ /^\.\#/);
1740             $hf =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1741             next if ($uiFiles{$1});
1742             open (HFIN, "$dir/$hf") || die "Could not open $dir/$hf: $!\n";
1743             my $hfsize = 0;
1744             seek(HFIN, 0, 2);
1745             $hfsize = tell(HFIN);
1746             seek(HFIN, 0, 0);
1747             read HFIN, $hfData, $hfsize;
1748             close HFIN;
1749             # push (@list, $hf) if(index($hfData, "Q_OBJECT") >= 0); ### fast but doesn't handle //Q_OBJECT
1750             if ( $hfData =~ /{([^}]*)Q_OBJECT/s ) {              ## handle " { friend class blah; Q_OBJECT "
1751                 push (@list, $hf) unless $1 =~ m://[^\n]*Q_OBJECT[^\n]*$:s;  ## handle "// Q_OBJECT"
1752             }
1753         }
1754         # The assoc array of root of headerfile and header filename
1755         foreach $hFile (@list)
1756         {
1757             $hFile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1758             if ($mocFiles{$1})
1759             {
1760               print STDERR "Warning: Multiple header files found for $1\n";
1761               next;                           # Use the first one
1762             }
1763             $mocFiles{$1} = "$dir\035$hFile";   # Add relative dir
1764         }
1765     }
1766
1767     return 0;
1768 }
1769
1770 #-----------------------------------------------------------------------------
1771
1772 # The programmer has specified a moc list. Prune out the moc candidates
1773 # list that we found based on looking at the header files. This generates
1774 # a warning if the programmer gets the list wrong, but this doesn't have
1775 # to be fatal here.
1776 sub pruneMocCandidates ($)
1777 {
1778     my %prunedMoc = ();
1779     local @mocList = split(' ', $_[0]);
1780
1781     foreach $mocname (@mocList)
1782     {
1783         $mocname =~ s/\.moc$//;
1784         if ($mocFiles{$mocname})
1785         {
1786             $prunedMoc{$mocname} = $mocFiles{$mocname};
1787         }
1788         else
1789         {
1790             my $print = $makefileDir;
1791             $print =~ s/^\Q$topdir\E\\//;
1792             # They specified a moc file but we can't find a header that
1793             # will generate this moc file. That's possible fatal!
1794             print STDERR "Warning: No moc-able header file for $print/$mocname\n";
1795         }
1796     }
1797
1798     undef %mocFiles;
1799     %mocFiles = %prunedMoc;
1800 }
1801
1802 #-----------------------------------------------------------------------------
1803
1804 # Finds the cpp files (If they exist).
1805 # The cpp files get appended to the header file separated by \035
1806 sub checkMocCandidates ()
1807 {
1808     my @cppFiles;
1809     my $cpp2moc;  # which c++ file includes which .moc files
1810     my $moc2cpp;  # which moc file is included by which c++ files
1811
1812     return unless (keys %mocFiles);
1813     opendir(THISDIR, ".") || return;
1814     @cppFiles = grep { /.+\.$cppExt$/o  && !/.+\.moc\.$cppExt$/o
1815                          && !/.+\.all_$cppExt\.$cppExt$/o
1816                          && !/^\./  } readdir(THISDIR);
1817     closedir THISDIR;
1818     return unless (@cppFiles);
1819     my $files = join (" ", @cppFiles);
1820     $cpp2moc = {};
1821     $moc2cpp = {};
1822     foreach $cxxf (@cppFiles)
1823     {
1824       open (CXXFIN, $cxxf) || die "Could not open $cxxf: $!\n";
1825       seek(CXXFIN, 0, 2);
1826       my $cxxfsize = tell(CXXFIN);
1827       seek(CXXFIN, 0, 0);
1828       read CXXFIN, $cxxfData, $cxxfsize;
1829       close CXXFIN;
1830       while(($cxxfData =~ m/^[ \t]*\#include\s*[<\"](.*\.moc)[>\"]/gm)) {
1831         $cpp2moc->{$cxxf}->{$1} = 1;
1832         $moc2cpp->{$1}->{$cxxf} = 1;
1833       }
1834     }
1835     foreach my $mocFile (keys (%mocFiles))
1836     {
1837         @cppFiles = keys %{$moc2cpp->{"$mocFile.moc"}};
1838         if (@cppFiles == 1) {
1839             $mocFiles{$mocFile} .= "\035" . $cppFiles[0];
1840             push(@deped, $mocFile);
1841         } elsif (@cppFiles == 0) {
1842             push (@newObs, $mocFile);           # Produce new object file
1843             next    if ($haveAutomocTag);       # This is expected...
1844             # But this is an error we can deal with - let them know
1845             print STDERR
1846                 "Warning: No c++ file that includes $mocFile.moc\n";
1847         } else {
1848             # We can't decide which file to use, so it's fatal. Although as a
1849             # guess we could use the mocFile.cpp file if it's in the list???
1850             print STDERR
1851                 "Error: Multiple c++ files that include $mocFile.moc\n";
1852             print STDERR "\t",join ("\t", @cppFiles),"\n";
1853             $errorflag = 1;
1854             delete $mocFiles{$mocFile};
1855             # Let's continue and see what happens - They have been told!
1856         }
1857     }
1858 }
1859
1860 #-----------------------------------------------------------------------------
1861
1862 # Add the rules for generating moc source from header files
1863 # For Automoc output *.moc.cpp but normally we'll output *.moc
1864 # (We must compile *.moc.cpp separately. *.moc files are included
1865 # in the appropriate *.cpp file by the programmer)
1866 sub addMocRules ()
1867 {
1868     my $cppFile;
1869     my $hFile;
1870
1871     foreach $mocFile (keys (%mocFiles))
1872     {
1873         undef $cppFile;
1874         ($dir, $hFile, $cppFile) =  split ("\035", $mocFiles{$mocFile}, 3);
1875         $dir =~ s#^\.#\$(srcdir)#;
1876         if (defined ($cppFile))
1877         {
1878           $cppFile =~ s,\.[^.]*$,,;
1879           $target_adds{"$cppFile.o"} .= "$mocFile.moc ";
1880           $target_adds{"$cppFile.lo"} .= "$mocFile.moc ";
1881           appendLines ("$mocFile.moc: $dir/$hFile\n\t\$(MOC) $dir/$hFile -o $mocFile.moc\n");
1882           $cleanMoc .= " $mocFile.moc";
1883         }
1884         else
1885         {
1886             appendLines ("$mocFile$mocExt: $dir/$hFile\n\t\$(MOC) $dir/$hFile -o $mocFile$mocExt\n");
1887             $cleanMoc .= " $mocFile$mocExt";
1888         }
1889     }
1890 }
1891
1892 sub make_meta_classes ()
1893 {
1894     return if ($kdeopts{"qtonly"});
1895
1896     my $cppFile;
1897     my $hFile;
1898     my $moc_class_headers = "";
1899     foreach $program (@programs) {
1900         my $mocs = "";
1901         my @progsources = split(/[\s\034]+/, $sources{$program});
1902         my @depmocs = split(' ', $depedmocs{$program});
1903         my %shash = (), %mhash = ();
1904         @shash{@progsources} = 1;  # we are only interested in the existence
1905         @mhash{@depmocs} = 1;
1906
1907         print STDOUT "program=$program\n" if ($verbose);
1908         print STDOUT "psources=[".join(' ', keys %shash)."]\n" if ($verbose);
1909         print STDOUT "depmocs=[".join(' ', keys %mhash)."]\n" if ($verbose);
1910         print STDOUT "globalmocs=[".join(' ', keys(%globalmocs))."]\n" if ($verbose);
1911         foreach my $mocFile (keys (%globalmocs))
1912         {
1913             my ($dir, $hFile, $cppFile) = split ("\035", $globalmocs{$mocFile}, 3);
1914             if (defined ($cppFile))
1915             {
1916                 $mocs .= " $mocFile.moc" if exists $shash{$cppFile};
1917             }
1918             else
1919             {
1920                 # Bah. This is the case, if no C++ file includes the .moc
1921                 # file. We make a .moc.cpp file for that. Unfortunately this
1922                 # is not included in the %sources hash, but rather is mentioned
1923                 # in %depedmocs. If the user wants to use AUTO he can't just
1924                 # use an unspecific METAINCLUDES. Instead he must use
1925                 # program_METAINCLUDES. Anyway, it's not working real nicely.
1926                 # E.g. Its not clear what happens if user specifies two
1927                 # METAINCLUDES=AUTO in the same Makefile.am.
1928                 $mocs .= " $mocFile.moc.$cxxsuffix"
1929                     if exists $mhash{$mocFile.".moc.$cxxsuffix"};
1930             }
1931         }
1932         if ($mocs) {
1933             print STDOUT "==> mocs=[".$mocs."]\n" if ($verbose);
1934         }
1935         print STDOUT "\n" if $verbose;
1936     }
1937     if ($moc_class_headers) {
1938         appendLines ("$cleantarget-moc-classes:\n\t-rm -f $moc_class_headers\n");
1939         $target_adds{"$cleantarget-am"} .= "$cleantarget-moc-classes ";
1940     }
1941 }
1942
1943 #-----------------------------------------------------------------------------
1944
1945 sub updateMakefile ()
1946 {
1947     return if ($dryrun);
1948
1949     open (FILEOUT, "> $makefile")
1950                         || die "Could not create $makefile: $!\n";
1951
1952     $MakefileData =~ s/\034/\\\n/g;    # Restore continuation lines
1953     # Append our $progId line, _below_ the "generated by automake" line
1954     # because automake-1.6 relies on the first line to be his own.
1955     my $progIdLine = "\# $progId - " . '$Revision$ '."\n";
1956     if ( !( $MakefileData =~ s/^(.*generated .*by automake.*\n)/$1$progIdLine/ ) ) {
1957         warn "automake line not found in $makefile\n";
1958         # Fallback: first line
1959         print FILEOUT $progIdLine;
1960     };
1961     print FILEOUT $MakefileData;
1962     close FILEOUT;
1963 }
1964
1965 #-----------------------------------------------------------------------------
1966
1967 # The given line needs to be removed from the makefile
1968 # Do this by adding the special "removed line" comment at the line start.
1969 sub removeLine ($$)
1970 {
1971     my ($lookup, $old) = @_;
1972
1973     $old =~ s/\034/\\\n#>- /g;          # Fix continuation lines
1974     $MakefileData =~ s/\n$lookup/\n#>\- $old/;
1975 }
1976
1977 #-----------------------------------------------------------------------------
1978
1979 # Replaces the old line with the new line
1980 # old line(s) are retained but tagged as removed. The new line(s) have the
1981 # "added" tag placed before it.
1982 sub substituteLine ($$)
1983 {
1984     my ($lookup, $new) = @_;
1985
1986     if ($MakefileData =~ /\n($lookup)/) {
1987       $old = $1;
1988       $old =~ s/\034/\\\n#>\- /g;         # Fix continuation lines
1989       $new =~ s/\034/\\\n/g;
1990       my $newCount = ($new =~ tr/\n//) + 1;
1991       $MakefileData =~ s/\n$lookup/\n#>- $old\n#>\+ $newCount\n$new/;
1992     } else {
1993       print STDERR "Warning: substitution of \"$lookup\" in $printname failed\n";
1994     }
1995 }
1996
1997 #-----------------------------------------------------------------------------
1998
1999 # Slap new lines on the back of the file.
2000 sub appendLines ($)
2001 {
2002   my ($new) = @_;
2003   $new =~ s/\034/\\\n/g;        # Fix continuation lines
2004   my $newCount = ($new =~ tr/\n//) + 1;
2005   $MakefileData .= "\n#>\+ $newCount\n$new";
2006 }
2007
2008 #-----------------------------------------------------------------------------
2009
2010 # Restore the Makefile.in to the state it was before we fiddled with it
2011 sub restoreMakefile ()
2012 {
2013     $MakefileData =~ s/# $progId[^\n\034]*[\n\034]*//g;
2014     # Restore removed lines
2015     $MakefileData =~ s/([\n\034])#>\- /$1/g;
2016     # Remove added lines
2017     while ($MakefileData =~ /[\n\034]#>\+ ([^\n\034]*)/)
2018     {
2019         my $newCount = $1;
2020         my $removeLines = "";
2021         while ($newCount--) {
2022             $removeLines .= "[^\n\034]*([\n\034]|)";
2023         }
2024         $MakefileData =~ s/[\n\034]#>\+.*[\n\034]$removeLines/\n/;
2025     }
2026 }
2027
2028 #-----------------------------------------------------------------------------