Use "sort -u" where it makes sense.
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2006 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19
20 # This program is meant for authors or maintainers which want to import
21 # modules from gnulib into their packages.
22
23 progname=$0
24 package=gnulib
25 cvsdatestamp='$Date: 2006-09-18 12:54:13 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28
29 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
30 AUTOCONFPATH=
31 #case $USER in
32 #  bruno )
33 #    AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
34 #    AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
35 #    ;;
36 #esac
37
38 # You can set AUTOMAKEPATH to empty if automake 1.9.x is already in your PATH.
39 AUTOMAKEPATH=
40
41 # If you didn't set AUTOCONFPATH and AUTOMAKEPATH, you can also set the
42 # variables AUTOCONF, ACLOCAL, AUTOMAKE, AUTORECONF individually.
43 if test -z "${AUTOCONF}" || test -n "${AUTOCONFPATH}"; then
44   AUTOCONF="${AUTOCONFPATH}autoconf"
45 fi
46 if test -z "${ACLOCAL}" || test -n "${AUTOMAKEPATH}"; then
47   ACLOCAL="${AUTOMAKEPATH}aclocal"
48 fi
49 if test -z "${AUTOMAKE}" || test -n "${AUTOMAKEPATH}"; then
50   AUTOMAKE="${AUTOMAKEPATH}automake"
51 fi
52 if test -z "${AUTORECONF}" || test -n "${AUTOCONFPATH}"; then
53   AUTORECONF="${AUTOCONFPATH}autoreconf"
54 fi
55
56 # GNU sort is needed. Set SORT to its location (not needed if it's called
57 # 'sort' and already in the PATH).
58 if test -z "$SORT"; then
59   SORT=sort
60 fi
61
62 # func_usage
63 # outputs to stdout the --help usage message.
64 func_usage ()
65 {
66   echo "\
67 Usage: gnulib-tool --list
68        gnulib-tool --import [module1 ... moduleN]
69        gnulib-tool --update
70        gnulib-tool --create-testdir --dir=directory module1 ... moduleN
71        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
72        gnulib-tool --test --dir=directory module1 ... moduleN
73        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
74        gnulib-tool --extract-description module
75        gnulib-tool --extract-filelist module
76        gnulib-tool --extract-dependencies module
77        gnulib-tool --extract-autoconf-snippet module
78        gnulib-tool --extract-automake-snippet module
79        gnulib-tool --extract-include-directive module
80        gnulib-tool --extract-license module
81        gnulib-tool --extract-maintainer module
82        gnulib-tool --extract-tests-module module
83
84 Operation modes:
85       --list                print the available module names
86       --import              import the given modules into the current package;
87                             if no modules are specified, update the current
88                             package from the current gnulib
89       --update              update the current package, restore files omitted
90                             from CVS
91       --create-testdir      create a scratch package with the given modules
92       --create-megatestdir  create a mega scratch package with the given modules
93                             one by one and all together
94       --test                test the combination of the given modules
95                             (recommended to use CC=\"gcc -Wall\" here)
96       --megatest            test the given modules one by one and all together
97                             (recommended to use CC=\"gcc -Wall\" here)
98       --extract-description        extract the description
99       --extract-filelist           extract the list of files
100       --extract-dependencies       extract the dependencies
101       --extract-autoconf-snippet   extract the snippet for configure.ac
102       --extract-automake-snippet   extract the snippet for library makefile
103       --extract-include-directive  extract the #include directive
104       --extract-license            report the license terms of the source files
105                                    under lib/
106       --extract-maintainer         report the maintainer(s) inside gnulib
107       --extract-tests-module       report the unit test module, if it exists
108
109 General options:
110       --dir=DIRECTORY       Specify the target directory.
111                             For --import, this specifies where your
112                             configure.ac can be found.  Defaults to current
113                             directory.
114       --local-dir=DIRECTORY  Specify a local override directory where to look
115                             up files before looking in gnulib's directory.
116
117 Options for --import:
118       --lib=LIBRARY         Specify the library name.  Defaults to 'libgnu'.
119       --source-base=DIRECTORY
120                             Directory relative to --dir where source code is
121                             placed (default \"lib\").
122       --m4-base=DIRECTORY   Directory relative to --dir where *.m4 macros are
123                             placed (default \"m4\").
124       --doc-base=DIRECTORY  Directory relative to --dir where doc files are
125                             placed (default \"doc\").
126       --tests-base=DIRECTORY
127                             Directory relative to --dir where unit tests are
128                             placed (default \"tests\").
129       --aux-dir=DIRECTORY   Directory relative to --dir where auxiliary build
130                             tools are placed (default \"build-aux\").
131       --with-tests          Include unit tests for the included modules.
132       --avoid=MODULE        Avoid including the given MODULE. Useful if you
133                             have code that provides equivalent functionality.
134                             This option can be repeated.
135       --lgpl                Abort if modules aren't available under the LGPL.
136                             Also modify license template from GPL to LGPL.
137       --makefile-name=NAME  Name of makefile in automake syntax in the
138                             source-base and tests-base directories
139                             (default \"Makefile.am\").
140       --libtool             Use libtool rules.
141       --no-libtool          Don't use libtool rules.
142       --macro-prefix=PREFIX  Specify the prefix of the macros 'gl_EARLY' and
143                             'gl_INIT'. Default is 'gl'.
144       --no-changelog        don't update or create ChangeLog files
145
146 Options for --import and --update:
147       --dry-run             For --import, only print what would have been done.
148   -s, --symbolic, --symlink Make symbolic links instead of copying files.
149
150 Report bugs to <bug-gnulib@gnu.org>."
151 }
152
153 # func_version
154 # outputs to stdout the --version message.
155 func_version ()
156 {
157   year=`echo "$last_checkin_date" | sed -e 's,/.*$,,'`
158   echo "$progname (GNU $package) $version"
159   echo "Copyright (C) $year Free Software Foundation, Inc.
160 This is free software; see the source for copying conditions.  There is NO
161 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
162   echo "Written by" "Bruno Haible" "and" "Simon Josefsson"
163 }
164
165 # func_emit_copyright_notice
166 # outputs to stdout a header for a generated file.
167 func_emit_copyright_notice ()
168 {
169   echo "# Copyright (C) 2004-2006 Free Software Foundation, Inc."
170   echo "#"
171   echo "# This file is free software, distributed under the terms of the GNU"
172   echo "# General Public License.  As a special exception to the GNU General"
173   echo "# Public License, this file may be distributed as part of a program"
174   echo "# that contains a configuration script generated by Autoconf, under"
175   echo "# the same distribution terms as the rest of that program."
176   echo "#"
177   echo "# Generated by gnulib-tool."
178 }
179
180 # func_tmpdir
181 # creates a temporary directory.
182 # Sets variable
183 # - tmp             pathname of freshly created temporary directory
184 func_tmpdir ()
185 {
186   # Use the environment variable TMPDIR, falling back to /tmp. This allows
187   # users to specify a different temporary directory, for example, if their
188   # /tmp is filled up or too small.
189   : ${TMPDIR=/tmp}
190   {
191     # Use the mktemp program if available. If not available, hide the error
192     # message.
193     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
194     test -n "$tmp" && test -d "$tmp"
195   } ||
196   {
197     # Use a simple mkdir command. It is guaranteed to fail if the directory
198     # already exists.  $RANDOM is bash specific and expands to empty in shells
199     # other than bash, ksh and zsh.  Its use does not increase security;
200     # rather, it minimizes the probability of failure in a very cluttered /tmp
201     # directory.
202     tmp=$TMPDIR/gl$$-$RANDOM
203     (umask 077 && mkdir "$tmp")
204   } ||
205   {
206     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
207     { (exit 1); exit 1; }
208   }
209 }
210
211 # func_fatal_error message
212 # outputs to stderr a fatal error message, and terminates the program.
213 func_fatal_error ()
214 {
215   echo "gnulib-tool: *** $1" 1>&2
216   echo "gnulib-tool: *** Stop." 1>&2
217   exit 1
218 }
219
220 # func_readlink SYMLINK
221 # outputs the target of the given symlink.
222 if (type -p readlink) > /dev/null 2>&1; then
223   func_readlink ()
224   {
225     # Use the readlink program from GNU coreutils.
226     readlink "$1"
227   }
228 else
229   func_readlink ()
230   {
231     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
232     # would do the wrong thing if the link target contains " -> ".
233     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
234   }
235 fi
236
237 # func_ln_if_changed SRC DEST
238 # Like ln -s, but avoids munging timestamps if the link is correct.
239 func_ln_if_changed ()
240 {
241   if test $# -ne 2; then
242     echo "usage: func_ln_if_changed SRC DEST" >&2
243   fi
244   if test -L "$2" && test "$1" = "`func_readlink "$2"`"; then
245     :
246   else
247     rm -f "$2"
248     ln -s "$1" "$2"
249   fi
250 }
251
252 # func_relativize DIR1 DIR2
253 # computes a relative pathname RELDIR such that DIR1/RELDIR = DIR2.
254 # Input:
255 # - DIR1            relative pathname, relative to the current directory
256 # - DIR2            relative pathname, relative to the current directory
257 # Output:
258 # - reldir          relative pathname of DIR2, relative to DIR1
259 func_relativize ()
260 {
261   dir0=`pwd`
262   dir1="$1"
263   dir2="$2"
264   sed_first='s,^\([^/]*\)/.*$,\1,'
265   sed_rest='s,^[^/]*/*,,'
266   sed_last='s,^.*/\([^/]*\)$,\1,'
267   sed_butlast='s,/*[^/]*$,,'
268   while test -n "$dir1"; do
269     first=`echo "$dir1" | sed -e "$sed_first"`
270     if test "$first" != "."; then
271       if test "$first" = ".."; then
272         dir2=`echo "$dir0" | sed -e "$sed_last"`/"$dir2"
273         dir0=`echo "$dir0" | sed -e "$sed_butlast"`
274       else
275         first2=`echo "$dir2" | sed -e "$sed_first"`
276         if test "$first2" = "$first"; then
277           dir2=`echo "$dir2" | sed -e "$sed_rest"`
278         else
279           dir2="../$dir2"
280         fi
281         dir0="$dir0"/"$first"
282       fi
283     fi
284     dir1=`echo "$dir1" | sed -e "$sed_rest"`
285   done
286   reldir="$dir2"
287 }
288
289 # func_relconcat DIR1 DIR2
290 # computes a relative pathname DIR1/DIR2, with obvious simplifications.
291 # Input:
292 # - DIR1            relative pathname, relative to the current directory
293 # - DIR2            relative pathname, relative to DIR1
294 # Output:
295 # - relconcat       DIR1/DIR2, relative to the current directory
296 func_relconcat ()
297 {
298   dir1="$1"
299   dir2="$2"
300   sed_first='s,^\([^/]*\)/.*$,\1,'
301   sed_rest='s,^[^/]*/*,,'
302   sed_last='s,^.*/\([^/]*\)$,\1,'
303   sed_butlast='s,/*[^/]*$,,'
304   while true; do
305     first=`echo "$dir2" | sed -e "$sed_first"`
306     if test "$first" = "."; then
307       dir2=`echo "$dir2" | sed -e "$sed_rest"`
308       if test -z "$dir2"; then
309         relconcat="$dir1"
310         break
311       fi
312     else
313       last=`echo "$dir1" | sed -e "$sed_last"`
314       while test "$last" = "."; do
315         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
316         last=`echo "$dir1" | sed -e "$sed_last"`
317       done
318       if test -z "$dir1"; then
319         relconcat="$dir2"
320         break
321       fi
322       if test "$first" = ".."; then
323         if test "$last" = ".."; then
324           relconcat="$dir1/$dir2"
325           break
326         fi
327         dir1=`echo "$dir1" | sed -e "$sed_butlast"`
328         dir2=`echo "$dir2" | sed -e "$sed_rest"`
329         if test -z "$dir1"; then
330           relconcat="$dir2"
331           break
332         fi
333         if test -z "$dir2"; then
334           relconcat="$dir1"
335           break
336         fi
337       else
338         relconcat="$dir1/$dir2"
339         break
340       fi
341     fi
342   done
343 }
344
345 # Command-line option processing.
346 # Removes the OPTIONS from the arguments. Sets the variables:
347 # - mode            list or import or create-testdir or create-megatestdir
348 # - destdir         from --dir
349 # - local_gnulib_dir  from --local-dir
350 # - libname, supplied_libname  from --lib
351 # - sourcebase      from --source-base
352 # - m4base          from --m4-base
353 # - docbase         from --doc-base
354 # - testsbase       from --tests-base
355 # - auxdir          from --aux-dir
356 # - inctests        true if --with-tests was given, blank otherwise
357 # - avoidlist       list of modules to avoid, from --avoid
358 # - lgpl            true if --lgpl was given, blank otherwise
359 # - makefile_name   from --makefile-name
360 # - libtool         true if --libtool was given, false if --no-libtool was
361 #                   given, blank otherwise
362 # - macro_prefix    from --macro-prefix
363 # - autoconf_minversion  minimum supported autoconf version
364 # - do_changelog    false if --no-changelog was given, : otherwise
365 # - doit            : if actions shall be executed, false if only to be printed
366 # - symbolic        true if --symbolic was given, blank otherwise
367 {
368   mode=
369   destdir=
370   local_gnulib_dir=
371   libname=libgnu
372   supplied_libname=
373   sourcebase=
374   m4base=
375   docbase=
376   testsbase=
377   auxdir=
378   inctests=
379   avoidlist=
380   lgpl=
381   makefile_name=
382   libtool=
383   macro_prefix=
384   do_changelog=:
385   doit=:
386   symbolic=
387
388   supplied_opts="$@"
389
390   while test $# -gt 0; do
391     case "$1" in
392       --list | --lis )
393         mode=list
394         shift ;;
395       --import | --impor | --impo | --imp | --im | --i )
396         mode=import
397         shift ;;
398       --update | --updat | --upda | --upd | --up | --u )
399         mode=update
400         shift ;;
401       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
402         mode=create-testdir
403         shift ;;
404       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
405         mode=create-megatestdir
406         shift ;;
407       --test | --tes | --te | --t )
408         mode=test
409         shift ;;
410       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
411         mode=megatest
412         shift ;;
413       --extract-* )
414         mode=`echo "X$1" | sed -e 's/^X--//'`
415         shift ;;
416       --dir )
417         shift
418         if test $# = 0; then
419           func_fatal_error "missing argument for --dir"
420         fi
421         destdir=$1
422         shift ;;
423       --dir=* )
424         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
425         shift ;;
426       --local-dir )
427         shift
428         if test $# = 0; then
429           func_fatal_error "missing argument for --local-dir"
430         fi
431         local_gnulib_dir=$1
432         shift ;;
433       --local-dir=* )
434         local_gnulib_dir=`echo "X$1" | sed -e 's/^X--local-dir=//'`
435         shift ;;
436       --lib )
437         shift
438         if test $# = 0; then
439           func_fatal_error "missing argument for --lib"
440         fi
441         libname=$1
442         supplied_libname=true
443         shift ;;
444       --lib=* )
445         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
446         supplied_libname=true
447         shift ;;
448       --source-base )
449         shift
450         if test $# = 0; then
451           func_fatal_error "missing argument for --source-base"
452         fi
453         sourcebase=$1
454         shift ;;
455       --source-base=* )
456         sourcebase=`echo "X$1" | sed -e 's/^X--source-base=//'`
457         shift ;;
458       --m4-base )
459         shift
460         if test $# = 0; then
461           func_fatal_error "missing argument for --m4-base"
462         fi
463         m4base=$1
464         shift ;;
465       --m4-base=* )
466         m4base=`echo "X$1" | sed -e 's/^X--m4-base=//'`
467         shift ;;
468       --doc-base )
469         shift
470         if test $# = 0; then
471           func_fatal_error "missing argument for --doc-base"
472         fi
473         docbase=$1
474         shift ;;
475       --doc-base=* )
476         docbase=`echo "X$1" | sed -e 's/^X--doc-base=//'`
477         shift ;;
478       --tests-base )
479         shift
480         if test $# = 0; then
481           func_fatal_error "missing argument for --tests-base"
482         fi
483         testsbase=$1
484         shift ;;
485       --tests-base=* )
486         testsbase=`echo "X$1" | sed -e 's/^X--tests-base=//'`
487         shift ;;
488       --aux-dir )
489         shift
490         if test $# = 0; then
491           func_fatal_error "missing argument for --aux-dir"
492         fi
493         auxdir=$1
494         shift ;;
495       --aux-dir=* )
496         auxdir=`echo "X$1" | sed -e 's/^X--aux-dir=//'`
497         shift ;;
498       --with-tests )
499         inctests=true
500         shift ;;
501       --avoid )
502         shift
503         if test $# = 0; then
504           func_fatal_error "missing argument for --avoid"
505         fi
506         avoidlist="$avoidlist $1"
507         shift ;;
508       --avoid=* )
509         avoidlist="$avoidlist "`echo "X$1" | sed -e 's/^X--avoid=//'`
510         shift ;;
511       --lgpl )
512         lgpl=true
513         shift ;;
514       --makefile-name )
515         shift
516         if test $# = 0; then
517           func_fatal_error "missing argument for --makefile-name"
518         fi
519         makefile_name="$1"
520         shift ;;
521       --makefile-name=* )
522         makefile_name=`echo "X$1" | sed -e 's/^X--makefile-name=//'`
523         shift ;;
524       --libtool )
525         libtool=true
526         shift ;;
527       --no-libtool )
528         libtool=false
529         shift ;;
530       --macro-prefix )
531         shift
532         if test $# = 0; then
533           func_fatal_error "missing argument for --macro-prefix"
534         fi
535         macro_prefix="$1"
536         shift ;;
537       --macro-prefix=* )
538         macro_prefix=`echo "X$1" | sed -e 's/^X--macro-prefix=//'`
539         shift ;;
540       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
541         do_changelog=false
542         shift ;;
543       --dry-run )
544         doit=false
545         shift ;;
546       -s | --symbolic | --symboli | --symbol | --symbo | --symb | --symlink | --symlin | --symli | --syml | --sym | --sy )
547         symbolic=true
548         shift ;;
549       --help | --hel | --he | --h )
550         func_usage
551         exit 0 ;;
552       --version | --versio | --versi | --vers | --ver | --ve | --v )
553         func_version
554         exit 0 ;;
555       -- )
556         # Stop option processing
557         shift
558         break ;;
559       -* )
560         echo "gnulib-tool: unknown option $1" 1>&2
561         echo "Try 'gnulib-tool --help' for more information." 1>&2
562         exit 1 ;;
563       * )
564         break ;;
565     esac
566   done
567
568   if test "$mode" = update; then
569     if test $# != 0; then
570       echo "gnulib-tool: too many arguments in 'update' mode" 1>&2
571       echo "Try 'gnulib-tool --help' for more information." 1>&2
572       echo "If you really want to modify the gnulib configuration of your project," 1>&2
573       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
574       exit 1
575     fi
576     if test -n "$local_gnulib_dir" || test -n "$supplied_libname" \
577        || test -n "$sourcebase" || test -n "$m4base" \
578        || test -n "$docbase" || test -n "$testsbase" || test -n "$auxdir" \
579        || test -n "$inctests" || test -n "$avoidlist" || test -n "$lgpl" \
580        || test -n "$makefile_name" || test -n "$macro_prefix"; then
581       echo "gnulib-tool: invalid options for 'update' mode" 1>&2
582       echo "Try 'gnulib-tool --help' for more information." 1>&2
583       echo "If you really want to modify the gnulib configuration of your project," 1>&2
584       echo "you need to use 'gnulib --import' - at your own risk!" 1>&2
585       exit 1
586     fi
587     do_changelog=false
588   fi
589
590   # Determine the minimum supported autoconf version from the project's
591   # configure.ac.
592   DEFAULT_AUTOCONF_MINVERSION="2.59"
593   autoconf_minversion=
594   configure_ac=
595   if { test "$mode" = import || test "$mode" = update; } && test -n "$destdir"; then
596     if test -f "$destdir"/configure.ac; then
597       configure_ac="$destdir/configure.ac"
598     else
599       if test -f "$destdir"/configure.in; then
600         configure_ac="$destdir/configure.in"
601       fi
602     fi
603   else
604     if test -f configure.ac; then
605       configure_ac="configure.ac"
606     else
607       if test -f configure.in; then
608         configure_ac="configure.in"
609       fi
610     fi
611   fi
612   if test -n "$configure_ac"; then
613     # Use sed, not autoconf --trace, to look for the AC_PREREQ invocation,
614     # because when some m4 files are omitted from a CVS repository,
615     # "autoconf --trace=AC_PREREQ" fails with an error message like this:
616     #   m4: aclocal.m4:851: Cannot open m4/absolute-header.m4: No such file or directory
617     #   autom4te: m4 failed with exit status: 1
618     prereqs=
619     my_sed_traces='
620       s,#.*$,,
621       s,^dnl .*$,,
622       s, dnl .*$,,
623       /AC_PREREQ/ {
624         s,^.*AC_PREREQ([[ ]*\([^])]*\).*$,prereqs="$prereqs \1",p
625       }'
626     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
627     if test -n "$prereqs"; then
628       autoconf_minversion=`for version in $prereqs; do echo $version; done | $SORT -g -u | tail -1`
629     fi
630   fi
631   if test -z "$autoconf_minversion"; then
632     autoconf_minversion=$DEFAULT_AUTOCONF_MINVERSION
633   fi
634   case "$autoconf_minversion" in
635     1.* | 2.[0-4]* | 2.5[0-8]*)
636       func_fatal_error "minimum supported autoconf version is 2.59. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
637   esac
638
639   # Remove trailing slashes from the directory names. This is necessary for
640   # m4base (to avoid an error in func_import) and optional for the others.
641   sed_trimtrailingslashes='s,\([^/]\)//*$,\1,'
642   case "$local_gnulib_dir" in
643     */ ) sourcebase=`echo "$local_gnulib_dir" | sed -e "$sed_trimtrailingslashes"` ;;
644   esac
645   case "$sourcebase" in
646     */ ) sourcebase=`echo "$sourcebase" | sed -e "$sed_trimtrailingslashes"` ;;
647   esac
648   case "$m4base" in
649     */ ) m4base=`echo "$m4base" | sed -e "$sed_trimtrailingslashes"` ;;
650   esac
651   case "$docbase" in
652     */ ) docbase=`echo "$docbase" | sed -e "$sed_trimtrailingslashes"` ;;
653   esac
654   case "$testsbase" in
655     */ ) testsbase=`echo "$testsbase" | sed -e "$sed_trimtrailingslashes"` ;;
656   esac
657   case "$auxdir" in
658     */ ) auxdir=`echo "$auxdir" | sed -e "$sed_trimtrailingslashes"` ;;
659   esac
660 }
661
662 case "$0" in
663   /*) self_abspathname="$0" ;;
664   */*) self_abspathname=`pwd`/"$0" ;;
665   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
666        if test -x "$d/$0" && test ! -d "$d/$0"; then
667          self_abspathname="$d/$0"
668          break
669        fi
670      done
671      if test -z "$self_abspathname"; then
672        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
673      fi
674      ;;
675 esac
676 while test -h "$self_abspathname"; do
677   # Resolve symbolic link.
678   linkval=`func_readlink "$self_abspathname"`
679   test -n "$linkval" || break
680   case "$linkval" in
681     /* ) self_abspathname="$linkval" ;;
682     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
683   esac
684 done
685 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
686
687 func_tmpdir
688 trap 'rm -rf "$tmp"' 0 1 2 3 15
689
690 # func_lookup_file file
691 # looks up a file in $local_gnulib_dir or $gnulib_dir, or combines it through
692 # 'patch'.
693 # Input:
694 # - local_gnulib_dir  from --local-dir
695 # Output:
696 # - lookedup_file   name of the merged (combined) file
697 # - lookedup_tmp    true if it is located in the tmp directory, blank otherwise
698 func_lookup_file ()
699 {
700   lkfile="$1"
701   if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile"; then
702     lookedup_file="$local_gnulib_dir/$lkfile"
703     lookedup_tmp=
704   else
705     if test -f "$gnulib_dir/$lkfile"; then
706       if test -n "$local_gnulib_dir" && test -f "$local_gnulib_dir/$lkfile.diff"; then
707         lkbase=`echo "$lkfile" | sed -e 's,^.*/,,'`
708         rm -f "$tmp/$lkbase"
709         cp "$gnulib_dir/$lkfile" "$tmp/$lkbase"
710         patch -s "$tmp/$lkbase" < "$local_gnulib_dir/$lkfile.diff" \
711           || func_fatal_error "patch file $local_gnulib_dir/$lkfile.diff didn't apply cleanly"
712         lookedup_file="$tmp/$lkbase"
713         lookedup_tmp=true
714       else
715         lookedup_file="$gnulib_dir/$lkfile"
716         lookedup_tmp=
717       fi
718     else
719       func_fatal_error "file $gnulib_dir/$lkfile not found"
720     fi
721   fi
722 }
723
724 # func_all_modules
725 # Input:
726 # - local_gnulib_dir  from --local-dir
727 func_all_modules ()
728 {
729   # Filter out metainformation files like README, which are not modules.
730   # Filter out unit test modules; they can be retrieved through
731   # --extract-tests-module if desired.
732   {
733     (cd "$gnulib_dir/modules" && ls -1)
734     if test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules"; then
735       (cd "$local_gnulib_dir/modules" && ls -1 | sed -e 's,\.diff$,,')
736     fi
737   } \
738       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^COPYING$/d' -e '/^README$/d' -e '/^TEMPLATE$/d' -e '/^TEMPLATE-TESTS$/d' -e '/~$/d' \
739       | sed -e '/-tests$/d' \
740       | LC_ALL=C sort -u
741 }
742
743 # func_verify_module
744 # verifies a module name
745 # Input:
746 # - local_gnulib_dir  from --local-dir
747 # - module          module name argument
748 func_verify_module ()
749 {
750   if { test -f "$gnulib_dir/modules/$module" \
751        || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
752             && test -f "$local_gnulib_dir/modules/$module"; }; } \
753      && test "CVS" != "$module" \
754      && test "ChangeLog" != "$module" \
755      && test "COPYING" != "$module" \
756      && test "README" != "$module" \
757      && test "TEMPLATE" != "$module" \
758      && test "TEMPLATE-TESTS" != "$module"; then
759     # OK, $module is a correct module name.
760     # Verify that building the module description with 'patch' succeeds.
761     func_lookup_file "modules/$module"
762   else
763     echo "gnulib-tool: module $module doesn't exist" 1>&2
764     module=
765   fi
766 }
767
768 # func_verify_nontests_module
769 # verifies a module name, excluding tests modules
770 # Input:
771 # - local_gnulib_dir  from --local-dir
772 # - module          module name argument
773 func_verify_nontests_module ()
774 {
775   case "$module" in
776     *-tests ) module= ;;
777     * ) func_verify_module ;;
778   esac
779 }
780
781 # func_verify_tests_module
782 # verifies a module name, considering only tests modules
783 # Input:
784 # - local_gnulib_dir  from --local-dir
785 # - module          module name argument
786 func_verify_tests_module ()
787 {
788   case "$module" in
789     *-tests ) func_verify_module ;;
790     * ) module= ;;
791   esac
792 }
793
794 sed_extract_prog=':[     ]*$/ {
795   :a
796     n
797     s/^Description:[     ]*$//
798     s/^Files:[   ]*$//
799     s/^Depends-on:[      ]*$//
800     s/^configure\.ac:[   ]*$//
801     s/^Makefile\.am:[    ]*$//
802     s/^Include:[         ]*$//
803     s/^License:[         ]*$//
804     s/^Maintainer:[      ]*$//
805     tb
806     p
807     ba
808   :b
809 }'
810
811 # func_get_description module
812 # Input:
813 # - local_gnulib_dir  from --local-dir
814 func_get_description ()
815 {
816   func_lookup_file "modules/$1"
817   sed -n -e "/^Description$sed_extract_prog" < "$lookedup_file"
818 }
819
820 # func_get_filelist module
821 # Input:
822 # - local_gnulib_dir  from --local-dir
823 func_get_filelist ()
824 {
825   func_lookup_file "modules/$1"
826   sed -n -e "/^Files$sed_extract_prog" < "$lookedup_file"
827   case "$autoconf_minversion" in
828     2.59)
829       #echo m4/onceonly.m4
830       echo m4/onceonly_2_57.m4
831       ;;
832   esac
833 }
834
835 # func_get_dependencies module
836 # Input:
837 # - local_gnulib_dir  from --local-dir
838 func_get_dependencies ()
839 {
840   # ${module}-tests always implicitly depends on ${module}.
841   echo "$1" | sed -n -e 's/-tests//p'
842   # Then the explicit dependencies listed in the module description.
843   func_lookup_file "modules/$1"
844   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
845 }
846
847 # func_get_autoconf_snippet module
848 # Input:
849 # - local_gnulib_dir  from --local-dir
850 func_get_autoconf_snippet ()
851 {
852   func_lookup_file "modules/$1"
853   sed -n -e "/^configure\.ac$sed_extract_prog" < "$lookedup_file"
854 }
855
856 # func_get_automake_snippet module
857 # Input:
858 # - local_gnulib_dir  from --local-dir
859 func_get_automake_snippet ()
860 {
861   func_lookup_file "modules/$1"
862   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$lookedup_file"
863 }
864
865 # func_get_include_directive module
866 # Input:
867 # - local_gnulib_dir  from --local-dir
868 func_get_include_directive ()
869 {
870   func_lookup_file "modules/$1"
871   sed -n -e "/^Include$sed_extract_prog" < "$lookedup_file" | \
872   sed -e 's/^\(["<]\)/#include \1/'
873 }
874
875 # func_get_license module
876 # Input:
877 # - local_gnulib_dir  from --local-dir
878 func_get_license ()
879 {
880   func_lookup_file "modules/$1"
881   sed -n -e "/^License$sed_extract_prog" < "$lookedup_file"
882 }
883
884 # func_get_maintainer module
885 # Input:
886 # - local_gnulib_dir  from --local-dir
887 func_get_maintainer ()
888 {
889   func_lookup_file "modules/$1"
890   sed -n -e "/^Maintainer$sed_extract_prog" < "$lookedup_file"
891 }
892
893 # func_get_tests_module module
894 # Input:
895 # - local_gnulib_dir  from --local-dir
896 func_get_tests_module ()
897 {
898   # The naming convention for tests modules is hardwired: ${module}-tests.
899   if test -f "$gnulib_dir/modules/$1"-tests \
900      || { test -n "$local_gnulib_dir" && test -d "$local_gnulib_dir/modules" \
901           && test -f "$local_gnulib_dir/modules/$1"-tests; }; then
902     echo "$1"-tests
903   fi
904 }
905
906 # func_acceptable module
907 # tests whether a module is acceptable.
908 # Input:
909 # - avoidlist       list of modules to avoid
910 func_acceptable ()
911 {
912   for avoid in $avoidlist; do
913     if test "$avoid" = "$1"; then
914       return 1
915     fi
916   done
917   return 0
918 }
919
920 # func_modules_transitive_closure
921 # Input:
922 # - local_gnulib_dir  from --local-dir
923 # - modules         list of specified modules
924 # - inctests        true if tests should be included, blank otherwise
925 # - avoidlist       list of modules to avoid
926 # Output:
927 # - modules         list of modules, including dependencies
928 func_modules_transitive_closure ()
929 {
930   while true; do
931     xmodules=
932     for module in $modules; do
933       func_verify_module
934       if test -n "$module"; then
935         # Duplicate dependencies are harmless, but Jim wants a warning.
936         duplicated_deps=`func_get_dependencies $module | LC_ALL=C sort | LC_ALL=C uniq -d`
937         if test -n "$duplicated_deps"; then
938           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
939         fi
940         if func_acceptable $module; then
941           xmodules="$xmodules $module"
942           for depmodule in `func_get_dependencies $module`; do
943             if func_acceptable $depmodule; then
944               xmodules="$xmodules $depmodule"
945             fi
946           done
947           if test -n "$inctests"; then
948             testsmodule=`func_get_tests_module $module`
949             if test -n "$testsmodule"; then
950               if func_acceptable $testsmodule; then
951                 xmodules="$xmodules $testsmodule"
952                 for depmodule in `func_get_dependencies $testsmodule`; do
953                   if func_acceptable $depmodule; then
954                     xmodules="$xmodules $depmodule"
955                   fi
956                 done
957               fi
958             fi
959           fi
960         fi
961       fi
962     done
963     xmodules=`for m in $xmodules; do echo $m; done | LC_ALL=C sort | LC_ALL=C uniq`
964     if test "$xmodules" = "$modules"; then
965       break
966     fi
967     modules="$xmodules"
968   done
969 }
970
971 # func_modules_add_dummy
972 # Input:
973 # - local_gnulib_dir  from --local-dir
974 # - modules         list of modules, including dependencies
975 # Output:
976 # - modules         list of modules, including 'dummy' if needed
977 func_modules_add_dummy ()
978 {
979   have_lib_SOURCES=
980   sed_remove_backslash_newline=':a
981 /\\$/{
982 s/\\$//
983 N
984 s/\n//
985 ba
986 }'
987   for module in $modules; do
988     func_verify_nontests_module
989     if test -n "$module"; then
990       # Extract the value of "lib_SOURCES += ...".
991       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
992         # Ignore .h files since they are not compiled.
993         case "$file" in
994           *.h) ;;
995           *) have_lib_SOURCES=yes ;;
996         esac
997       done
998     fi
999   done
1000   # Add the dummy module, to make sure the library will be non-empty.
1001   if test -z "$have_lib_SOURCES"; then
1002     modules="$modules dummy"
1003   fi
1004 }
1005
1006 # func_modules_to_filelist
1007 # Input:
1008 # - local_gnulib_dir  from --local-dir
1009 # - modules         list of modules, including dependencies
1010 # Output:
1011 # - files           list of files
1012 func_modules_to_filelist ()
1013 {
1014   files=
1015   for module in $modules; do
1016     func_verify_module
1017     if test -n "$module"; then
1018       files="$files "`func_get_filelist $module`
1019     fi
1020   done
1021   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1022 }
1023
1024 # func_emit_lib_Makefile_am
1025 # emits the contents of library makefile to standard output.
1026 # Input:
1027 # - local_gnulib_dir  from --local-dir
1028 # - modules         list of modules, including dependencies
1029 # - libname         library name
1030 # - makefile_name   from --makefile-name
1031 # - libtool         true if libtool will be used, false or blank otherwise
1032 # - actioncmd       (optional) command that will reproduce this invocation
1033 func_emit_lib_Makefile_am ()
1034 {
1035   # When creating an includable Makefile.am snippet, augment variables with
1036   # += instead of assigning them.
1037   if test -n "$makefile_name"; then
1038     assign='+='
1039   else
1040     assign='='
1041   fi
1042   if test "$libtool" = true; then
1043     libext=la
1044     perhapsLT=LT
1045     sed_eliminate_LDFLAGS=
1046   else
1047     libext=a
1048     perhapsLT=
1049     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[       ]*+=/d'
1050   fi
1051   echo "## Process this file with automake to produce Makefile.in."
1052   func_emit_copyright_notice
1053   if test -n "$actioncmd"; then
1054     echo "# Reproduce by: $actioncmd"
1055   fi
1056   echo
1057   if test -z "$makefile_name"; then
1058     # No need to generate dependencies since the sources are in gnulib, not here.
1059     echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
1060   fi
1061   echo
1062   (
1063     for module in $modules; do
1064       func_verify_nontests_module
1065       if test -n "$module"; then
1066         {
1067           func_get_automake_snippet "$module" |
1068             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1069                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1070             sed -e "$sed_eliminate_LDFLAGS" |
1071             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1072             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1073                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
1074           if test "$module" = 'alloca'; then
1075             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
1076           fi
1077         } > amsnippet.tmp
1078         # Skip the contents if its entirely empty.
1079         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1080           echo "## begin gnulib module $module"
1081           echo
1082           cat amsnippet.tmp
1083           echo "## end   gnulib module $module"
1084           echo
1085         fi
1086         rm -f amsnippet.tmp
1087       fi
1088     done
1089   ) > allsnippets.tmp
1090   if grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
1091     # One of the snippets already specifies an installation location for the
1092     # library. Don't confuse automake by saying it should not be installed.
1093     :
1094   else
1095     # By default, the generated library should not be installed.
1096     echo "noinst_${perhapsLT}LIBRARIES $assign $libname.$libext"
1097   fi
1098   echo
1099   echo "${libname}_${libext}_SOURCES ="
1100   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
1101   # automake during its analyses looks for $(LIBOBJS), not for @LIBOBJS@.
1102   echo "${libname}_${libext}_LIBADD = \$(${perhapsLT}LIBOBJS)"
1103   if test "$libtool" = true; then
1104     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
1105   fi
1106   if test -z "$makefile_name"; then
1107     echo "noinst_HEADERS ="
1108     # Automake versions < 1.9b create an empty pkgdatadir at installation time
1109     # if you specify pkgdata_DATA to empty. This is a workaround.
1110     if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1111       echo "pkgdata_DATA ="
1112     fi
1113     echo "EXTRA_DIST ="
1114     echo "BUILT_SOURCES ="
1115     echo "SUFFIXES ="
1116   fi
1117   echo "MOSTLYCLEANFILES $assign core *.stackdump"
1118   if test -z "$makefile_name"; then
1119     echo "MOSTLYCLEANDIRS ="
1120     echo "CLEANFILES ="
1121     echo "DISTCLEANFILES ="
1122     echo "MAINTAINERCLEANFILES ="
1123     echo
1124     echo "AM_CPPFLAGS ="
1125   fi
1126   echo
1127   cat allsnippets.tmp
1128   echo
1129   echo "mostlyclean-local: mostlyclean-generic"
1130   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
1131   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
1132   echo "            if test -d \$\$dir; then \\"
1133   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1134   echo "            fi; \\"
1135   echo "          done"
1136   rm -f allsnippets.tmp
1137 }
1138
1139 # func_emit_tests_Makefile_am
1140 # emits the contents of tests makefile to standard output.
1141 # Input:
1142 # - local_gnulib_dir  from --local-dir
1143 # - modules         list of modules, including dependencies
1144 # - libname         library name
1145 # - makefile_name   from --makefile-name
1146 # - libtool         true if libtool will be used, false or blank otherwise
1147 # - sourcebase      relative directory containing lib source code
1148 # - m4base          relative directory containing autoconf macros
1149 # - testsbase       relative directory containing unit test code
1150 func_emit_tests_Makefile_am ()
1151 {
1152   if test "$libtool" = true; then
1153     libext=la
1154   else
1155     libext=a
1156   fi
1157   if test "$libtool" = true; then
1158     libext=la
1159     sed_eliminate_LDFLAGS=
1160   else
1161     libext=a
1162     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[       ]*+=/d'
1163   fi
1164   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
1165   echo "## Process this file with automake to produce Makefile.in."
1166   func_emit_copyright_notice
1167   echo
1168   # Generate dependencies here, since it eases the debugging of test failures.
1169   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1170   echo
1171   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
1172   echo
1173   (
1174     for module in $modules; do
1175       func_verify_tests_module
1176       if test -n "$module"; then
1177         {
1178           func_get_automake_snippet "$module" |
1179             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1180                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1181             sed -e "$sed_eliminate_LDFLAGS" |
1182             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1183             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1184                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
1185         } > amsnippet.tmp
1186         # Skip the contents if its entirely empty.
1187         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1188           echo "## begin gnulib module $module"
1189           echo
1190           cat amsnippet.tmp
1191           echo "## end   gnulib module $module"
1192           echo
1193         fi
1194         rm -f amsnippet.tmp
1195       fi
1196     done
1197   ) > allsnippets.tmp
1198   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1199   # variable is needed to avoid an error from automake:
1200   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1201   echo "SUBDIRS ="
1202   echo "TESTS ="
1203   echo "TESTS_ENVIRONMENT ="
1204   echo "noinst_PROGRAMS ="
1205   echo "check_PROGRAMS ="
1206   echo "noinst_HEADERS ="
1207   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1208     echo "pkgdata_DATA ="
1209   fi
1210   echo "EXTRA_DIST ="
1211   echo "BUILT_SOURCES ="
1212   echo "SUFFIXES ="
1213   echo "MOSTLYCLEANFILES = core *.stackdump"
1214   echo "MOSTLYCLEANDIRS ="
1215   echo "CLEANFILES ="
1216   echo "DISTCLEANFILES ="
1217   echo "MAINTAINERCLEANFILES ="
1218   echo
1219   echo "AM_CPPFLAGS = \\"
1220   echo "  -I. -I\$(srcdir) \\"
1221   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1222   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1223   echo
1224   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1225   echo
1226   cat allsnippets.tmp
1227   echo "# Clean up after Solaris cc."
1228   echo "clean-local:"
1229   echo "        rm -rf SunWS_cache"
1230   echo
1231   echo "mostlyclean-local: mostlyclean-generic"
1232   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
1233   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
1234   echo "            if test -d \$\$dir; then \\"
1235   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1236   echo "            fi; \\"
1237   echo "          done"
1238   rm -f allsnippets.tmp
1239 }
1240
1241 # func_import modules
1242 # Uses also the variables
1243 # - destdir         target directory
1244 # - local_gnulib_dir  from --local-dir
1245 # - libname         library name
1246 # - sourcebase      directory relative to destdir where to place source code
1247 # - m4base          directory relative to destdir where to place *.m4 macros
1248 # - docbase         directory relative to destdir where to place doc files
1249 # - testsbase       directory relative to destdir where to place unit test code
1250 # - auxdir          directory relative to destdir where to place build aux files
1251 # - inctests        true if --with-tests was given, blank otherwise
1252 # - avoidlist       list of modules to avoid, from --avoid
1253 # - lgpl            true if library's license shall be LGPL, blank otherwise
1254 # - makefile_name   from --makefile-name
1255 # - libtool         true if --libtool was given, false if --no-libtool was
1256 #                   given, blank otherwise
1257 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1258 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1259 # - autoconf_minversion  minimum supported autoconf version
1260 # - doit            : if actions shall be executed, false if only to be printed
1261 # - symbolic        true if files should be symlinked, copied otherwise
1262 func_import ()
1263 {
1264   # Get the cached settings.
1265   cached_local_gnulib_dir=
1266   cached_specified_modules=
1267   cached_avoidlist=
1268   cached_sourcebase=
1269   cached_m4base=
1270   cached_docbase=
1271   cached_testsbase=
1272   cached_libname=
1273   cached_lgpl=
1274   cached_makefile_name=
1275   cached_libtool=
1276   cached_macro_prefix=
1277   cached_files=
1278   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1279     cached_libtool=false
1280     my_sed_traces='
1281       s,#.*$,,
1282       s,^dnl .*$,,
1283       s, dnl .*$,,
1284       /gl_LOCAL_DIR(/ {
1285         s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
1286       }
1287       /gl_MODULES(/ {
1288         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1289       }
1290       /gl_AVOID(/ {
1291         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1292       }
1293       /gl_SOURCE_BASE(/ {
1294         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1295       }
1296       /gl_M4_BASE(/ {
1297         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1298       }
1299       /gl_DOC_BASE(/ {
1300         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1301       }
1302       /gl_TESTS_BASE(/ {
1303         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1304       }
1305       /gl_LIB(/ {
1306         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1307       }
1308       /gl_LGPL/ {
1309         s,^.*$,cached_lgpl=true,p
1310       }
1311       /gl_MAKEFILE_NAME(/ {
1312         s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
1313       }
1314       /gl_LIBTOOL/ {
1315         s,^.*$,cached_libtool=true,p
1316       }
1317       /gl_MACRO_PREFIX(/ {
1318         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1319       }'
1320     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1321     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1322       my_sed_traces='
1323         s,#.*$,,
1324         s,^dnl .*$,,
1325         s, dnl .*$,,
1326         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1327           s,^.*$,cached_files=",p
1328           n
1329           ta
1330           :a
1331           s,^\]).*$,",
1332           tb
1333           p
1334           n
1335           ba
1336           :b
1337           p
1338         }'
1339       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1340     fi
1341   fi
1342
1343   # Merge the cached settings with the specified ones.
1344   # The m4base must be the same as expected from the pathname.
1345   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1346     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1347   fi
1348   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
1349   # is relative to $destdir, whereas the one we use is relative to . or absolute.
1350   if test -z "$local_gnulib_dir"; then
1351     if test -n "$cached_local_gnulib_dir"; then
1352       case "$destdir" in
1353         /*)
1354           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1355         *)
1356           case "$cached_local_gnulib_dir" in
1357             /*)
1358               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1359             *)
1360               func_relconcat "$destdir" "$cached_local_gnulib_dir"
1361               local_gnulib_dir="$relconcat" ;;
1362           esac ;;
1363       esac
1364     fi
1365   fi
1366   # Append the cached and the specified module names. So that
1367   # "gnulib-tool --import foo" means to add the module foo.
1368   specified_modules="$cached_specified_modules $1"
1369   # Append the cached and the specified avoidlist. This is probably better
1370   # than dropping the cached one when --avoid is specified at least once.
1371   avoidlist=`echo $cached_avoidlist $avoidlist`
1372   # The sourcebase defaults to the cached one.
1373   if test -z "$sourcebase"; then
1374     sourcebase="$cached_sourcebase"
1375     if test -z "$sourcebase"; then
1376       func_fatal_error "missing --source-base option"
1377     fi
1378   fi
1379   # The docbase defaults to the cached one.
1380   if test -z "$docbase"; then
1381     docbase="$cached_docbase"
1382     if test -z "$docbase"; then
1383       func_fatal_error "missing --doc-base option. --doc-base has been introduced on 2006-07-11; if your last invocation of 'gnulib-tool --import' is before that date, you need to run 'gnulib-tool --import' once, with a --doc-base option."
1384     fi
1385   fi
1386   # The testsbase defaults to the cached one.
1387   if test -z "$testsbase"; then
1388     testsbase="$cached_testsbase"
1389     if test -z "$testsbase"; then
1390       func_fatal_error "missing --tests-base option"
1391     fi
1392   fi
1393   # The libname defaults to the cached one.
1394   if test -z "$supplied_libname"; then
1395     libname="$cached_libname"
1396     if test -z "$libname"; then
1397       func_fatal_error "missing --lib option"
1398     fi
1399   fi
1400   # Require LGPL if specified either way.
1401   if test -z "$lgpl"; then
1402     lgpl="$cached_lgpl"
1403   fi
1404   # The makefile_name defaults to the cached one.
1405   if test -z "$makefile_name"; then
1406     makefile_name="$cached_makefile_name"
1407   fi
1408   # Use libtool if specified either way, or if guessed.
1409   if test -z "$libtool"; then
1410     if test -n "$cached_m4base"; then
1411       libtool="$cached_libtool"
1412     else
1413       libtool="$guessed_libtool"
1414     fi
1415   fi
1416   # The macro_prefix defaults to the cached one.
1417   if test -z "$macro_prefix"; then
1418     macro_prefix="$cached_macro_prefix"
1419     if test -z "$macro_prefix"; then
1420       func_fatal_error "missing --macro-prefix option"
1421     fi
1422   fi
1423
1424   # Canonicalize the list of specified modules.
1425   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
1426
1427   # Determine final module list.
1428   modules="$specified_modules"
1429   func_modules_transitive_closure
1430   echo "Module list with included dependencies:"
1431   echo "$modules" | sed -e 's/^/  /'
1432
1433   # Add the dummy module if needed.
1434   func_modules_add_dummy
1435
1436   # If --lgpl, verify that the licenses of modules are compatible.
1437   if test -n "$lgpl"; then
1438     for module in $modules; do
1439       license=`func_get_license $module`
1440       case $license in
1441         LGPL | 'GPLed build tool') ;;
1442         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
1443         *) func_fatal_error "incompatible license on module $module: $license" ;;
1444       esac
1445     done
1446   fi
1447
1448   # Determine script to apply to imported library files.
1449   sed_transform_lib_file=
1450   for module in $modules; do
1451     if test $module = config-h; then
1452       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1453       sed_transform_lib_file=$sed_transform_lib_file'
1454         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1455       '
1456       break
1457     fi
1458   done
1459   if test -n "$lgpl"; then
1460     # Update license.
1461     sed_transform_lib_file=$sed_transform_lib_file'
1462       s/GNU General/GNU Lesser General/g
1463       s/version 2\([ ,]\)/version 2.1\1/g
1464     '
1465   fi
1466
1467   # Determine final file list.
1468   func_modules_to_filelist
1469   echo "File list:"
1470   echo "$files" | sed -e 's/^/  /'
1471
1472   test -n "$files" \
1473     || func_fatal_error "refusing to do nothing"
1474
1475   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1476   new_files="$files m4/gnulib-tool.m4"
1477   old_files="$cached_files"
1478   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1479     old_files="$old_files m4/gnulib-tool.m4"
1480   fi
1481
1482   # Create directories.
1483   if test ! -d "$destdir/$sourcebase"; then
1484     if $doit; then
1485       echo "Creating directory $destdir/$sourcebase"
1486       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1487     else
1488       echo "Create directory $destdir/$sourcebase"
1489     fi
1490   fi
1491   if test ! -d "$destdir/$m4base"; then
1492     if $doit; then
1493       echo "Creating directory $destdir/$m4base"
1494       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1495     else
1496       echo "Create directory $destdir/$m4base"
1497     fi
1498   fi
1499   docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1500   if test -n "$docfiles"; then
1501     if test ! -d "$destdir/$docbase"; then
1502       if $doit; then
1503         echo "Creating directory $destdir/$docbase"
1504         mkdir "$destdir/$docbase" || func_fatal_error "failed"
1505       else
1506         echo "Create directory $destdir/$docbase"
1507       fi
1508     fi
1509   fi
1510   if test -n "$inctests"; then
1511     if test ! -d "$destdir/$testsbase"; then
1512       if $doit; then
1513         echo "Creating directory $destdir/$testsbase"
1514         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1515       else
1516         echo "Create directory $destdir/$testsbase"
1517       fi
1518     fi
1519   fi
1520   if test ! -d "$destdir/$auxdir"; then
1521     if $doit; then
1522       echo "Creating directory $destdir/$auxdir"
1523       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1524     else
1525       echo "Create directory $destdir/$auxdir"
1526     fi
1527   fi
1528
1529   # func_dest_tmpfilename file
1530   # determines the name of a temporary file (file is relative to destdir).
1531   # Sets variable:
1532   #   - tmpfile       absolute filename of the temporary file
1533   func_dest_tmpfilename ()
1534   {
1535     if $doit; then
1536       # Put the new contents of $file in a file in the same directory (needed
1537       # to guarantee that an 'mv' to "$destdir/$file" works).
1538       tmpfile="$destdir/$1.tmp"
1539     else
1540       # Put the new contents of $file in a file in a temporary directory
1541       # (because the directory of "$file" might not exist).
1542       tmpfile="$tmp"/`basename "$1"`.tmp
1543     fi
1544   }
1545
1546   # Copy files or make symbolic links. Remove obsolete files.
1547   delimiter='   '
1548   for f in $old_files; do
1549     case "$f" in
1550       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1551       doc/*) g=`echo "$f" | sed -e "s,^doc/,$cached_docbase/,"` ;;
1552       lib/*) g=`echo "$f" | sed -e "s,^lib/,$cached_sourcebase/,"` ;;
1553       m4/*) g=`echo "$f" | sed -e "s,^m4/,$cached_m4base/,"` ;;
1554       tests/*) g=`echo "$f" | sed -e "s,^tests/,$cached_testsbase/,"` ;;
1555       *) g="$f" ;;
1556     esac
1557     echo "$g""$delimiter""$f"
1558   done | LC_ALL=C sort > "$tmp"/old-files
1559   for f in $new_files; do
1560     case "$f" in
1561       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1562       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1563       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1564       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1565       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1566       *) g="$f" ;;
1567     esac
1568     echo "$g""$delimiter""$f"
1569   done | LC_ALL=C sort > "$tmp"/new-files
1570   # First the files that are in old-files, but not in new-files:
1571   sed_take_first_column='s,'"$delimiter"'.*,,'
1572   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1573     # Remove the file. Do nothing if the user already removed it.
1574     if test -f "$destdir/$g"; then
1575       if $doit; then
1576         echo "Removing file $g (backup in ${g}~)"
1577         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1578       else
1579         echo "Remove file $g (backup in ${g}~)"
1580       fi
1581     fi
1582   done
1583   # func_add_or_update handles a file that ought to be present afterwards.
1584   # Uses parameters f, g, already_present.
1585   func_add_or_update ()
1586   {
1587     func_dest_tmpfilename "$g"
1588     func_lookup_file "$f"
1589     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1590     if test -n "$sed_transform_lib_file"; then
1591       case "$f" in
1592         lib/*)
1593           sed -e "$sed_transform_lib_file" \
1594             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1595           ;;
1596       esac
1597     fi
1598     if test -f "$destdir/$g"; then
1599       # The file already exists.
1600       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1601         : # The file has not changed.
1602       else
1603         # Replace the file.
1604         if $doit; then
1605           if test -n "$already_present"; then
1606             echo "Updating file $g (backup in ${g}~)"
1607           else
1608             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1609           fi
1610           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1611           if test -n "$symbolic" && test -z "$lookedup_tmp" \
1612              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1613             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1614           else
1615             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1616           fi
1617         else
1618           if test -n "$already_present"; then
1619             echo "Update file $g (backup in ${g}~)"
1620           else
1621             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1622           fi
1623         fi
1624       fi
1625     else
1626       # Install the file.
1627       # Don't protest if the file should be there but isn't: it happens
1628       # frequently that developers don't put autogenerated files into CVS.
1629       if $doit; then
1630         echo "Copying file $g"
1631         if test -n "$symbolic" && test -z "$lookedup_tmp" \
1632            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1633           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1634         else
1635           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1636         fi
1637       else
1638         echo "Copy file $g"
1639       fi
1640     fi
1641     rm -f "$tmpfile"
1642   }
1643   # Then the files that are in new-files, but not in old-files:
1644   sed_take_last_column='s,^.*'"$delimiter"',,'
1645   already_present=
1646   for f in `LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1647     case "$f" in
1648       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1649       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1650       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1651       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1652       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1653       *) g="$f" ;;
1654     esac
1655     func_add_or_update
1656   done
1657   # Then the files that are in new-files and in old-files:
1658   already_present=true
1659   for f in `LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_last_column"`; do
1660     case "$f" in
1661       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
1662       doc/*) g=`echo "$f" | sed -e "s,^doc/,$docbase/,"` ;;
1663       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
1664       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
1665       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
1666       *) g="$f" ;;
1667     esac
1668     func_add_or_update
1669   done
1670
1671   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1672   actioncmd="gnulib-tool --import"
1673   actioncmd="$actioncmd --dir=$destdir"
1674   if test -n "$local_gnulib_dir"; then
1675     actioncmd="$actioncmd --local-dir=$local_gnulib_dir"
1676   fi
1677   actioncmd="$actioncmd --lib=$libname"
1678   actioncmd="$actioncmd --source-base=$sourcebase"
1679   actioncmd="$actioncmd --m4-base=$m4base"
1680   actioncmd="$actioncmd --doc-base=$docbase"
1681   actioncmd="$actioncmd --aux-dir=$auxdir"
1682   for module in $avoidlist; do
1683     actioncmd="$actioncmd --avoid=$module"
1684   done
1685   if test -n "$lgpl"; then
1686     actioncmd="$actioncmd --lgpl"
1687   fi
1688   if test -n "$makefile_name"; then
1689     actioncmd="$actioncmd --makefile_name=$makefile_name"
1690   fi
1691   if test "$libtool" = true; then
1692     actioncmd="$actioncmd --libtool"
1693   else
1694     actioncmd="$actioncmd --no-libtool"
1695   fi
1696   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1697   actioncmd="$actioncmd `echo $specified_modules`"
1698
1699   # Default the makefile name to Makefile.am.
1700   if test -n "$makefile_name"; then
1701     makefile_am=$makefile_name
1702   else
1703     makefile_am=Makefile.am
1704   fi
1705
1706   # Create library makefile.
1707   func_dest_tmpfilename $sourcebase/$makefile_am
1708   func_emit_lib_Makefile_am > "$tmpfile"
1709   if test -f "$destdir"/$sourcebase/$makefile_am; then
1710     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
1711       rm -f "$tmpfile"
1712     else
1713       if $doit; then
1714         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1715         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
1716         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1717       else
1718         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1719         rm -f "$tmpfile"
1720       fi
1721     fi
1722   else
1723     if $doit; then
1724       echo "Creating $sourcebase/$makefile_am"
1725       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1726     else
1727       echo "Create $sourcebase/$makefile_am"
1728       rm -f "$tmpfile"
1729     fi
1730   fi
1731
1732   # Create m4/gnulib-cache.m4.
1733   func_dest_tmpfilename $m4base/gnulib-cache.m4
1734   (
1735     func_emit_copyright_notice
1736     echo "#"
1737     echo "# This file represents the specification of how gnulib-tool is used."
1738     echo "# It acts as a cache: It is written and read by gnulib-tool."
1739     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1740     echo "# like the configure.ac and various Makefile.am files."
1741     echo
1742     echo
1743     echo "# Specification in the form of a command-line invocation:"
1744     echo "#   $actioncmd"
1745     echo
1746     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1747     # Store the local_gnulib_dir relative to destdir.
1748     case "$local_gnulib_dir" in
1749       "" | /*)
1750         relative_local_gnulib_dir="$local_gnulib_dir" ;;
1751       * )
1752         case "$destdir" in
1753           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
1754           *)
1755             # destdir, local_gnulib_dir are both relative.
1756             func_relativize "$destdir" "$local_gnulib_dir"
1757             relative_local_gnulib_dir="$reldir" ;;
1758         esac ;;
1759     esac
1760     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
1761     echo "gl_MODULES(["`echo $specified_modules`"])"
1762     echo "gl_AVOID([$avoidlist])"
1763     echo "gl_SOURCE_BASE([$sourcebase])"
1764     echo "gl_M4_BASE([$m4base])"
1765     echo "gl_DOC_BASE([$docbase])"
1766     echo "gl_TESTS_BASE([$testsbase])"
1767     echo "gl_LIB([$libname])"
1768     test -z "$lgpl" || echo "gl_LGPL"
1769     echo "gl_MAKEFILE_NAME([$makefile_name])"
1770     if test "$libtool" = true; then
1771       echo "gl_LIBTOOL"
1772     fi
1773     echo "gl_MACRO_PREFIX([$macro_prefix])"
1774   ) > "$tmpfile"
1775   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1776     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1777       rm -f "$tmpfile"
1778     else
1779       if $doit; then
1780         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1781         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1782         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1783       else
1784         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1785         if false; then
1786           cat "$tmpfile"
1787           echo
1788           echo "# gnulib-cache.m4 ends here"
1789         fi
1790         rm -f "$tmpfile"
1791       fi
1792     fi
1793   else
1794     if $doit; then
1795       echo "Creating $m4base/gnulib-cache.m4"
1796       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1797     else
1798       echo "Create $m4base/gnulib-cache.m4"
1799       cat "$tmpfile"
1800       rm -f "$tmpfile"
1801     fi
1802   fi
1803
1804   # Create m4/gnulib-comp.m4.
1805   func_dest_tmpfilename $m4base/gnulib-comp.m4
1806   (
1807     func_emit_copyright_notice
1808     echo "#"
1809     echo "# This file represents the compiled summary of the specification in"
1810     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1811     echo "# to be invoked from configure.ac."
1812     echo "# In projects using CVS, this file can be treated like other built files."
1813     echo
1814     echo
1815     echo "# This macro should be invoked from $configure_ac, in the section"
1816     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1817     echo "# any checks for libraries, header files, types and library functions."
1818     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1819     echo "["
1820     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1821     echo "  m4_pattern_allow([^gl_ES$])dnl a valid locale name"
1822     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1823     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1824       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1825     fi
1826     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1827       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1828     fi
1829     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1830       echo "  AC_REQUIRE([gl_LOCK_EARLY])"
1831     fi
1832     echo "])"
1833     echo
1834     echo "# This macro should be invoked from $configure_ac, in the section"
1835     echo "# \"Check for header files, types and library functions\"."
1836     echo "AC_DEFUN([${macro_prefix}_INIT],"
1837     echo "["
1838     if test "$libtool" = true; then
1839       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1840       echo "  gl_cond_libtool=true"
1841     else
1842       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1843       echo "  gl_cond_libtool=false"
1844       echo "  gl_libdeps="
1845       echo "  gl_ltlibdeps="
1846     fi
1847     if test "$auxdir" != "build-aux"; then
1848       sed_replace_build_aux='
1849         :a
1850         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1851           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1852           ba
1853         }'
1854       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1855     else
1856       sed_replace_build_aux=
1857     fi
1858     for module in $modules; do
1859       func_verify_module
1860       if test -n "$module"; then
1861         func_get_autoconf_snippet "$module" \
1862           | sed -e '/^$/d;' -e 's/^/  /' \
1863                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1864                 -e "$sed_replace_build_aux"
1865         if test "$module" = 'alloca' && test "$libtool" = true; then
1866           echo 'changequote(,)dnl'
1867           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1868           echo 'changequote([, ])dnl'
1869           echo 'AC_SUBST([LTALLOCA])'
1870         fi
1871       fi
1872     done
1873     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
1874     # created using libtool, because libtool already handles the dependencies.
1875     if test "$libtool" != true; then
1876       libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
1877       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
1878       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
1879       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
1880       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
1881     fi
1882     echo "])"
1883     echo
1884     echo "# This macro records the list of files which have been installed by"
1885     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1886     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1887     echo "$files" | sed -e 's,^,  ,'
1888     echo "])"
1889   ) > "$tmpfile"
1890   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1891     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1892       rm -f "$tmpfile"
1893     else
1894       if $doit; then
1895         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1896         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1897         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1898       else
1899         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1900         if false; then
1901           cat "$tmpfile"
1902           echo
1903           echo "# gnulib-comp.m4 ends here"
1904         fi
1905         rm -f "$tmpfile"
1906       fi
1907     fi
1908   else
1909     if $doit; then
1910       echo "Creating $m4base/gnulib-comp.m4"
1911       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1912     else
1913       echo "Create $m4base/gnulib-comp.m4"
1914       cat "$tmpfile"
1915       rm -f "$tmpfile"
1916     fi
1917   fi
1918
1919   if test -n "$inctests"; then
1920     # Create tests makefile.
1921     func_dest_tmpfilename $testsbase/$makefile_am
1922     func_emit_tests_Makefile_am > "$tmpfile"
1923     if test -f "$destdir"/$testsbase/$makefile_am; then
1924       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
1925         rm -f "$tmpfile"
1926       else
1927         if $doit; then
1928           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
1929           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
1930           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
1931         else
1932           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
1933           rm -f "$tmpfile"
1934         fi
1935       fi
1936     else
1937       if $doit; then
1938         echo "Creating $testsbase/$makefile_am"
1939         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
1940       else
1941         echo "Create $testsbase/$makefile_am"
1942         rm -f "$tmpfile"
1943       fi
1944     fi
1945   fi
1946
1947   echo "Finished."
1948   echo
1949   echo "You may need to add #include directives for the following .h files."
1950   (
1951    # First the #include <...> directives without #ifs, sorted for convenience.
1952    for module in $modules; do
1953      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1954        :
1955      else
1956        func_get_include_directive "$module" | grep -v 'include "'
1957      fi
1958    done | LC_ALL=C sort -u
1959    # Then the #include "..." directives without #ifs, sorted for convenience.
1960    for module in $modules; do
1961      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1962        :
1963      else
1964        func_get_include_directive "$module" | grep 'include "'
1965      fi
1966    done | LC_ALL=C sort -u
1967    # Then the #include directives that are surrounded by #ifs. Not sorted.
1968    for module in $modules; do
1969      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1970        func_get_include_directive "$module"
1971      fi
1972    done
1973   ) | sed -e '/^$/d;' -e 's/^/  /'
1974   echo
1975   echo "Don't forget to"
1976   if test "$makefile_am" = Makefile.am; then
1977     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1978   else
1979     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
1980   fi
1981   if test -n "$inctests"; then
1982     if test "$makefile_am" = Makefile.am; then
1983       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1984     else
1985       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
1986     fi
1987   fi
1988   if test "$makefile_am" = Makefile.am; then
1989     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1990     sourcebase_base=`basename "$sourcebase"`
1991     echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1992   fi
1993   if test -n "$inctests"; then
1994     if test "$makefile_am" = Makefile.am; then
1995       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1996       testsbase_base=`basename "$testsbase"`
1997       echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1998     fi
1999   fi
2000   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
2001   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
2002   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
2003 }
2004
2005 # func_create_testdir testdir modules
2006 # Input:
2007 # - local_gnulib_dir  from --local-dir
2008 # - auxdir          directory relative to destdir where to place build aux files
2009 func_create_testdir ()
2010 {
2011   testdir="$1"
2012   modules="$2"
2013   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
2014
2015   # Subdirectory names.
2016   sourcebase=gllib
2017   m4base=glm4
2018   testsbase=gltests
2019
2020   # Determine final module list.
2021   func_modules_transitive_closure
2022   echo "Module list with included dependencies:"
2023   echo "$modules" | sed -e 's/^/  /'
2024
2025   # Add the dummy module if needed.
2026   func_modules_add_dummy
2027
2028   # Determine final file list.
2029   func_modules_to_filelist
2030   echo "File list:"
2031   echo "$files" | sed -e 's/^/  /'
2032
2033   # Create directories.
2034   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
2035     case "$d" in
2036       build-aux) mkdir -p "$testdir/$auxdir" ;;
2037       lib)       mkdir -p "$testdir/$sourcebase" ;;
2038       m4)        mkdir -p "$testdir/$m4base" ;;
2039       tests)     mkdir -p "$testdir/$testsbase" ;;
2040       *)         mkdir -p "$testdir/$d" ;;
2041     esac
2042   done
2043
2044   # Copy files or make symbolic links.
2045   for f in $files; do
2046     func_lookup_file "$f"
2047     case "$f" in
2048       build-aux/*) g=`echo "$f" | sed -e "s,^build-aux/,$auxdir/,"` ;;
2049       lib/*) g=`echo "$f" | sed -e "s,^lib/,$sourcebase/,"` ;;
2050       m4/*) g=`echo "$f" | sed -e "s,^m4/,$m4base/,"` ;;
2051       tests/*) g=`echo "$f" | sed -e "s,^tests/,$testsbase/,"` ;;
2052       *) g="$f" ;;
2053     esac
2054     if test -n "$lookedup_tmp"; then
2055       cp -p "$lookedup_file" "$testdir/$g"
2056     else
2057       ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
2058       if test -z "$symbolic"; then
2059         cp -p "$lookedup_file" "$testdir/$g"
2060       else
2061         ln -s "$lookedup_file" "$testdir/$g"
2062       fi
2063     fi
2064   done
2065
2066   # Create $sourcebase/Makefile.am.
2067   mkdir -p "$testdir/$sourcebase"
2068   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
2069
2070   # Create $m4base/Makefile.am.
2071   mkdir -p "$testdir/$m4base"
2072   (echo "## Process this file with automake to produce Makefile.in."
2073    echo
2074    echo "EXTRA_DIST ="
2075    for f in $files; do
2076      case "$f" in
2077        m4/* )
2078          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
2079      esac
2080    done
2081   ) > "$testdir/$m4base/Makefile.am"
2082
2083   subdirs="$sourcebase $m4base"
2084   subdirs_with_configure_ac=""
2085
2086   if test -f "$testdir"/$m4base/gettext.m4; then
2087     # Avoid stupid error message from automake:
2088     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
2089     mkdir -p "$testdir/po"
2090     (echo "## Process this file with automake to produce Makefile.in."
2091     ) > "$testdir/po/Makefile.am"
2092     subdirs="$subdirs po"
2093   fi
2094
2095   if test -n "$inctests"; then
2096     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
2097     # Create $testsbase/Makefile.am.
2098     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
2099     # Create $testsbase/configure.ac.
2100     (echo "# Process this file with autoconf to produce a configure script."
2101      echo "AC_INIT([dummy], [0])"
2102      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
2103      echo "AM_INIT_AUTOMAKE"
2104      echo
2105      echo "AM_CONFIG_HEADER([config.h])"
2106      echo
2107      echo "AC_PROG_CC"
2108      echo "AC_PROG_INSTALL"
2109      echo "AC_PROG_MAKE_SET"
2110      echo "AC_PROG_RANLIB"
2111      echo
2112      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2113        echo "AC_GNU_SOURCE"
2114        echo
2115      fi
2116      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2117        echo "gl_USE_SYSTEM_EXTENSIONS"
2118        echo
2119      fi
2120      if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2121        echo "gl_LOCK_EARLY"
2122        echo
2123      fi
2124      if test "$libtool" = true; then
2125        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2126        echo "gl_cond_libtool=true"
2127      else
2128        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2129        echo "gl_cond_libtool=false"
2130        echo "gl_libdeps="
2131        echo "gl_ltlibdeps="
2132      fi
2133      # Wrap the set of autoconf snippets into an autoconf macro that is then
2134      # invoked. This is needed because autoconf does not support AC_REQUIRE
2135      # at the top level:
2136      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2137      # but we want the AC_REQUIRE to have its normal meaning (provide one
2138      # expansion of the required macro before the current point, and only one
2139      # expansion total).
2140      echo "AC_DEFUN([gl_INIT], ["
2141      if test "$auxdir" != "build-aux"; then
2142        sed_replace_build_aux='
2143          :a
2144          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2145            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
2146            ba
2147          }'
2148        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2149      else
2150        sed_replace_build_aux=
2151      fi
2152      # We don't have explicit ordering constraints between the various
2153      # autoconf snippets. It's cleanest to put those of the library before
2154      # those of the tests.
2155      for module in $modules; do
2156        func_verify_nontests_module
2157        if test -n "$module"; then
2158          func_get_autoconf_snippet "$module" \
2159            | sed -e "$sed_replace_build_aux"
2160        fi
2161      done
2162      for module in $modules; do
2163        func_verify_tests_module
2164        if test -n "$module"; then
2165          func_get_autoconf_snippet "$module" \
2166            | sed -e "$sed_replace_build_aux"
2167        fi
2168      done
2169      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2170      # created using libtool, because libtool already handles the dependencies.
2171      if test "$libtool" != true; then
2172        libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2173        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2174        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2175        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2176        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2177      fi
2178      echo "])"
2179      echo "gl_INIT"
2180      echo
2181      # Usually $testsbase/config.h will be a superset of config.h. Verify this
2182      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
2183      echo "AH_TOP([#include \"../config.h\"])"
2184      echo
2185      echo "AC_OUTPUT([Makefile])"
2186     ) > "$testdir/$testsbase/configure.ac"
2187     subdirs="$subdirs $testsbase"
2188     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
2189   fi
2190
2191   # Create Makefile.am.
2192   (echo "## Process this file with automake to produce Makefile.in."
2193    echo
2194    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2195    echo
2196    echo "SUBDIRS = $subdirs"
2197    echo
2198    echo "ACLOCAL_AMFLAGS = -I $m4base"
2199   ) > "$testdir/Makefile.am"
2200
2201   # Create configure.ac.
2202   (echo "# Process this file with autoconf to produce a configure script."
2203    echo "AC_INIT([dummy], [0])"
2204    if test "$auxdir" != "."; then
2205      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2206    fi
2207    echo "AM_INIT_AUTOMAKE"
2208    echo
2209    echo "AM_CONFIG_HEADER([config.h])"
2210    echo
2211    echo "AC_PROG_CC"
2212    echo "AC_PROG_INSTALL"
2213    echo "AC_PROG_MAKE_SET"
2214    echo
2215    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2216    echo "m4_pattern_allow([^gl_ES$])dnl a valid locale name"
2217    echo
2218    echo "AC_PROG_RANLIB"
2219    echo
2220    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2221      echo "AC_GNU_SOURCE"
2222      echo
2223    fi
2224    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2225      echo "gl_USE_SYSTEM_EXTENSIONS"
2226      echo
2227    fi
2228    if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2229      echo "gl_LOCK_EARLY"
2230      echo
2231    fi
2232    if test "$libtool" = true; then
2233      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2234      echo "gl_cond_libtool=true"
2235    else
2236      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2237      echo "gl_cond_libtool=false"
2238      echo "gl_libdeps="
2239      echo "gl_ltlibdeps="
2240    fi
2241    # Wrap the set of autoconf snippets into an autoconf macro that is then
2242    # invoked. This is needed because autoconf does not support AC_REQUIRE
2243    # at the top level:
2244    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2245    # but we want the AC_REQUIRE to have its normal meaning (provide one
2246    # expansion of the required macro before the current point, and only one
2247    # expansion total).
2248    echo "AC_DEFUN([gl_INIT], ["
2249    if test "$auxdir" != "build-aux"; then
2250      sed_replace_build_aux='
2251        :a
2252        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2253          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2254          ba
2255        }'
2256      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2257    else
2258      sed_replace_build_aux=
2259    fi
2260    for module in $modules; do
2261      func_verify_nontests_module
2262      if test -n "$module"; then
2263        func_get_autoconf_snippet "$module" \
2264          | sed -e "$sed_replace_build_aux"
2265      fi
2266    done
2267    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2268    # created using libtool, because libtool already handles the dependencies.
2269    if test "$libtool" != true; then
2270      libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2271      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2272      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2273      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2274      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2275    fi
2276    echo "])"
2277    echo "gl_INIT"
2278    echo
2279    if test -n "$subdirs_with_configure_ac"; then
2280      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
2281    fi
2282    makefiles="Makefile"
2283    for d in $subdirs; do
2284      # For subdirs that have a configure.ac by their own, it's the subdir's
2285      # configure.ac which creates the subdir's Makefile.am, not this one.
2286      case " $subdirs_with_configure_ac " in
2287        *" $d "*) ;;
2288        *) makefiles="$makefiles $d/Makefile" ;;
2289      esac
2290    done
2291    echo "AC_OUTPUT([$makefiles])"
2292   ) > "$testdir/configure.ac"
2293
2294   # Create autogenerated files.
2295   (cd "$testdir"
2296    echo "executing ${AUTORECONF} --force --install"
2297    ${AUTORECONF} --force --install
2298   )
2299   if grep '^BUILT_SOURCES *+=' "$testdir/$sourcebase/Makefile.am" > /dev/null; then
2300     (cd "$testdir"
2301      ./configure
2302        cd "$sourcebase"
2303        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2304        make built_sources
2305        cd ..
2306      make distclean
2307     )
2308   fi
2309 }
2310
2311 # func_create_megatestdir megatestdir allmodules
2312 # Input:
2313 # - local_gnulib_dir  from --local-dir
2314 # - auxdir          directory relative to destdir where to place build aux files
2315 func_create_megatestdir ()
2316 {
2317   megatestdir="$1"
2318   allmodules="$2"
2319   if test -z "$allmodules"; then
2320     allmodules=`func_all_modules`
2321   fi
2322
2323   megasubdirs=
2324   # First, all modules one by one.
2325   for onemodule in $allmodules; do
2326     func_create_testdir "$megatestdir/$onemodule" $onemodule
2327     megasubdirs="${megasubdirs}$onemodule "
2328   done
2329   # Then, all modules all together.
2330   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2331   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
2332   func_create_testdir "$megatestdir/ALL" "$allmodules"
2333   megasubdirs="${megasubdirs}ALL"
2334
2335   # Create Makefile.am.
2336   (echo "## Process this file with automake to produce Makefile.in."
2337    echo
2338    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2339    echo
2340    echo "SUBDIRS = $megasubdirs"
2341   ) > "$megatestdir/Makefile.am"
2342
2343   # Create configure.ac.
2344   (echo "# Process this file with autoconf to produce a configure script."
2345    echo "AC_INIT([dummy], [0])"
2346    if test "$auxdir" != "."; then
2347      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2348    fi
2349    echo "AM_INIT_AUTOMAKE"
2350    echo
2351    echo "AC_PROG_MAKE_SET"
2352    echo
2353    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2354    echo "AC_OUTPUT([Makefile])"
2355   ) > "$megatestdir/configure.ac"
2356
2357   # Create autogenerated files.
2358   (cd "$megatestdir"
2359    # Do not use "${AUTORECONF} --install", because autoreconf operates
2360    # recursively, but the subdirectories are already finished, therefore
2361    # calling autoreconf here would only waste lots of CPU time.
2362    echo "executing ${ACLOCAL}"
2363    ${ACLOCAL}
2364    echo "executing mkdir build-aux"
2365    mkdir build-aux
2366    echo "executing ${AUTOCONF}"
2367    ${AUTOCONF}
2368    echo "executing ${AUTOMAKE} --add-missing --copy"
2369    ${AUTOMAKE} --add-missing --copy
2370   )
2371 }
2372
2373 case $mode in
2374   "" )
2375     func_fatal_error "no mode specified" ;;
2376
2377   list )
2378     func_all_modules
2379     ;;
2380
2381   import | update )
2382
2383     # Where to import.
2384     if test -z "$destdir"; then
2385       destdir=.
2386     fi
2387     test -d "$destdir" \
2388       || func_fatal_error "destination directory does not exist: $destdir"
2389
2390     # Prefer configure.ac to configure.in.
2391     if test -f "$destdir"/configure.ac; then
2392       configure_ac="$destdir/configure.ac"
2393     else
2394       if test -f "$destdir"/configure.in; then
2395         configure_ac="$destdir/configure.in"
2396       else
2397         func_fatal_error "cannot find $destdir/configure.ac"
2398       fi
2399     fi
2400
2401     # Analyze configure.ac.
2402     guessed_auxdir="."
2403     guessed_libtool=false
2404     my_sed_traces='
2405       s,#.*$,,
2406       s,^dnl .*$,,
2407       s, dnl .*$,,
2408       /AC_CONFIG_AUX_DIR/ {
2409         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2410       }
2411       /A[CM]_PROG_LIBTOOL/ {
2412         s,^.*$,guessed_libtool=true,p
2413       }'
2414     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2415
2416     if test -z "$auxdir"; then
2417       auxdir="$guessed_auxdir"
2418     fi
2419
2420     # Determine where to apply func_import.
2421     if test -n "$m4base"; then
2422       # Apply func_import to a particular gnulib directory.
2423       # Any number of additional modules can be given.
2424       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2425         # First use of gnulib in the given m4base.
2426         test -n "$supplied_libname" || supplied_libname=true
2427         test -n "$sourcebase" || sourcebase="lib"
2428         test -n "$docbase" || docbase="doc"
2429         test -n "$testsbase" || testsbase="tests"
2430         test -n "$macro_prefix" || macro_prefix="gl"
2431       fi
2432       func_import "$*"
2433     else
2434       # Apply func_import to all gnulib directories.
2435       # To get this list of directories, look at Makefile.am. (Not at
2436       # configure, because it may be omitted from CVS. Also, don't run
2437       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2438       m4dirs=
2439       m4dirs_count=0
2440       if test -f "$destdir"/Makefile.am; then
2441         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2442         m4dir_is_next=
2443         for arg in $aclocal_amflags; do
2444           if test -n "$m4dir_is_next"; then
2445             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2446             case "$arg" in
2447               /*) ;;
2448               *)
2449                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2450                   m4dirs="$m4dirs $arg"
2451                   m4dirs_count=`expr $m4dirs_count + 1`
2452                 fi
2453                 ;;
2454             esac
2455           else
2456             if test "X$arg" = "X-I"; then
2457               m4dir_is_next=yes
2458             else
2459               m4dir_is_next=
2460             fi
2461           fi
2462         done
2463       else
2464         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2465         if test -f "$destdir"/aclocal.m4; then
2466           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2467           sedexpr2='s,^[^/]*$,.,'
2468           sedexpr3='s,/[^/]*$,,'
2469           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
2470           m4dirs_count=`echo "$m4dirs" | wc -l`
2471         fi
2472       fi
2473       if test $m4dirs_count = 0; then
2474         # First use of gnulib in a package.
2475         # Any number of additional modules can be given.
2476         test -n "$supplied_libname" || supplied_libname=true
2477         test -n "$sourcebase" || sourcebase="lib"
2478         m4base="m4"
2479         test -n "$docbase" || docbase="doc"
2480         test -n "$testsbase" || testsbase="tests"
2481         test -n "$macro_prefix" || macro_prefix="gl"
2482         func_import "$*"
2483       else
2484         if test $m4dirs_count = 1; then
2485           # There's only one use of gnulib here. Assume the user means it.
2486           # Any number of additional modules can be given.
2487           for m4base in $m4dirs; do
2488             func_import "$*"
2489           done
2490         else
2491           # Ambiguous - guess what the user meant.
2492           if test $# = 0; then
2493             # No further arguments. Guess the user wants to update all of them.
2494             for m4base in $m4dirs; do
2495               # Perform func_import in a subshell, so that variable values
2496               # such as
2497               #   local_gnulib_dir, avoidlist, sourcebase, m4base, docbase,
2498               #   testsbase, libname, lgpl, makefile_name, libtool, macro_prefix
2499               # don't propagate from one directory to another.
2500               (func_import) || exit 1
2501             done
2502           else
2503             # Really ambiguous.
2504             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2505           fi
2506         fi
2507       fi
2508     fi
2509     ;;
2510
2511   create-testdir )
2512     if test -z "$destdir"; then
2513       func_fatal_error "please specify --dir option"
2514     fi
2515     mkdir "$destdir"
2516     test -d "$destdir" \
2517       || func_fatal_error "could not create destination directory"
2518     test -n "$auxdir" || auxdir="build-aux"
2519     func_create_testdir "$destdir" "$*"
2520     ;;
2521
2522   create-megatestdir )
2523     if test -z "$destdir"; then
2524       func_fatal_error "please specify --dir option"
2525     fi
2526     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2527     test -n "$auxdir" || auxdir="build-aux"
2528     func_create_megatestdir "$destdir" "$*"
2529     ;;
2530
2531   test )
2532     test -n "$destdir" || destdir=testdir$$
2533     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2534     test -n "$auxdir" || auxdir="build-aux"
2535     func_create_testdir "$destdir" "$*"
2536     cd "$destdir"
2537       mkdir build
2538       cd build
2539         ../configure
2540         make
2541         make check
2542         make distclean
2543         remaining=`find . -type f -print`
2544         if test -n "$remaining"; then
2545           echo "Remaining files:" $remaining 1>&2
2546           echo "gnulib-tool: *** Stop." 1>&2
2547           exit 1
2548         fi
2549       cd ..
2550     cd ..
2551     rm -rf "$destdir"
2552     ;;
2553
2554   megatest )
2555     test -n "$destdir" || destdir=testdir$$
2556     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2557     test -n "$auxdir" || auxdir="build-aux"
2558     func_create_megatestdir "$destdir" "$*"
2559     cd "$destdir"
2560       mkdir build
2561       cd build
2562         ../configure
2563         make
2564         make check
2565         make distclean
2566         remaining=`find . -type f -print`
2567         if test -n "$remaining"; then
2568           echo "Remaining files:" $remaining 1>&2
2569           echo "gnulib-tool: *** Stop." 1>&2
2570           exit 1
2571         fi
2572       cd ..
2573     cd ..
2574     rm -rf "$destdir"
2575     ;;
2576
2577   extract-description )
2578     for module
2579     do
2580       func_verify_module
2581       if test -n "$module"; then
2582         func_get_description "$module"
2583       fi
2584     done
2585     ;;
2586
2587   extract-filelist )
2588     for module
2589     do
2590       func_verify_module
2591       if test -n "$module"; then
2592         func_get_filelist "$module"
2593       fi
2594     done
2595     ;;
2596
2597   extract-dependencies )
2598     for module
2599     do
2600       func_verify_module
2601       if test -n "$module"; then
2602         func_get_dependencies "$module"
2603       fi
2604     done
2605     ;;
2606
2607   extract-autoconf-snippet )
2608     for module
2609     do
2610       func_verify_module
2611       if test -n "$module"; then
2612         func_get_autoconf_snippet "$module"
2613       fi
2614     done
2615     ;;
2616
2617   extract-automake-snippet )
2618     for module
2619     do
2620       func_verify_module
2621       if test -n "$module"; then
2622         func_get_automake_snippet "$module"
2623       fi
2624     done
2625     ;;
2626
2627   extract-include-directive )
2628     for module
2629     do
2630       func_verify_module
2631       if test -n "$module"; then
2632         func_get_include_directive "$module"
2633       fi
2634     done
2635     ;;
2636
2637   extract-license )
2638     for module
2639     do
2640       func_verify_module
2641       if test -n "$module"; then
2642         func_get_license "$module"
2643       fi
2644     done
2645     ;;
2646
2647   extract-maintainer )
2648     for module
2649     do
2650       func_verify_module
2651       if test -n "$module"; then
2652         func_get_maintainer "$module"
2653       fi
2654     done
2655     ;;
2656
2657   extract-tests-module )
2658     for module
2659     do
2660       func_verify_module
2661       if test -n "$module"; then
2662         func_get_tests_module "$module"
2663       fi
2664     done
2665     ;;
2666
2667   * )
2668     func_fatal_error "unknown operation mode --$mode" ;;
2669 esac
2670
2671 rm -rf "$tmp"
2672 # Undo the effect of the previous 'trap' command. Some shellology:
2673 # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
2674 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
2675 # exit); for the others we need to call 'exit' explicitly. The value of $? is
2676 # 128 + signal number and is set before the trap-registered command is run.
2677 trap '' 0
2678 trap 'exit $?' 1 2 3 15
2679
2680 exit 0
2681
2682 # Local Variables:
2683 # indent-tabs-mode: nil
2684 # whitespace-check-buffer-indent: nil
2685 # End: