Rewrite all file names at once.
[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 13:03:14 $'
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 # - tmp             pathname of a temporary directory
927 # Output:
928 # - modules         list of modules, including dependencies
929 func_modules_transitive_closure ()
930 {
931   # In order to process every module only once (for speed), process an "input
932   # list" of modules, producing an "output list" of modules. During each round,
933   # more modules can be queued in the input list. Once a module on the input
934   # list has been processed, it is added to the "handled list", so we can avoid
935   # to process it again.
936   handledmodules=
937   inmodules="$modules"
938   outmodules=
939   while test -n "$inmodules"; do
940     inmodules_this_round="$inmodules"
941     inmodules=                    # Accumulator, queue for next round
942     for module in $inmodules_this_round; do
943       func_verify_module
944       if test -n "$module"; then
945         if func_acceptable $module; then
946           outmodules="$outmodules $module"
947           deps=`func_get_dependencies $module`
948           # Duplicate dependencies are harmless, but Jim wants a warning.
949           duplicated_deps=`echo "$deps" | LC_ALL=C sort | LC_ALL=C uniq -d`
950           if test -n "$duplicated_deps"; then
951             echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
952           fi
953           inmodules="$inmodules $deps"
954           if test -n "$inctests"; then
955             testsmodule=`func_get_tests_module $module`
956             if test -n "$testsmodule"; then
957               inmodules="$inmodules $testsmodule"
958             fi
959           fi
960         fi
961       fi
962     done
963     handledmodules=`for m in $handledmodules $inmodules_this_round; do echo $m; done | LC_ALL=C sort -u`
964     # Remove $handledmodules from $inmodules.
965     for m in $inmodules; do echo $m; done | LC_ALL=C sort -u > "$tmp"/queued-modules
966     inmodules=`echo "$handledmodules" | LC_ALL=C join -v 2 - "$tmp"/queued-modules`
967   done
968   modules=`for m in $outmodules; do echo $m; done | LC_ALL=C sort -u`
969   rm -f "$tmp"/queued-modules
970 }
971
972 # func_modules_add_dummy
973 # Input:
974 # - local_gnulib_dir  from --local-dir
975 # - modules         list of modules, including dependencies
976 # Output:
977 # - modules         list of modules, including 'dummy' if needed
978 func_modules_add_dummy ()
979 {
980   have_lib_SOURCES=
981   sed_remove_backslash_newline=':a
982 /\\$/{
983 s/\\$//
984 N
985 s/\n//
986 ba
987 }'
988   for module in $modules; do
989     func_verify_nontests_module
990     if test -n "$module"; then
991       # Extract the value of "lib_SOURCES += ...".
992       for file in `func_get_automake_snippet "$module" | sed -e "$sed_remove_backslash_newline" | sed -n -e 's,^lib_SOURCES[     ]*+=\([^#]*\).*$,\1,p'`; do
993         # Ignore .h files since they are not compiled.
994         case "$file" in
995           *.h) ;;
996           *) have_lib_SOURCES=yes ;;
997         esac
998       done
999     fi
1000   done
1001   # Add the dummy module, to make sure the library will be non-empty.
1002   if test -z "$have_lib_SOURCES"; then
1003     modules="$modules dummy"
1004   fi
1005 }
1006
1007 # func_modules_to_filelist
1008 # Input:
1009 # - local_gnulib_dir  from --local-dir
1010 # - modules         list of modules, including dependencies
1011 # Output:
1012 # - files           list of files
1013 func_modules_to_filelist ()
1014 {
1015   files=
1016   for module in $modules; do
1017     func_verify_module
1018     if test -n "$module"; then
1019       files="$files "`func_get_filelist $module`
1020     fi
1021   done
1022   files=`for f in $files; do echo $f; done | LC_ALL=C sort -u`
1023 }
1024
1025 # func_emit_lib_Makefile_am
1026 # emits the contents of library makefile to standard output.
1027 # Input:
1028 # - local_gnulib_dir  from --local-dir
1029 # - modules         list of modules, including dependencies
1030 # - libname         library name
1031 # - makefile_name   from --makefile-name
1032 # - libtool         true if libtool will be used, false or blank otherwise
1033 # - actioncmd       (optional) command that will reproduce this invocation
1034 func_emit_lib_Makefile_am ()
1035 {
1036   # When creating an includable Makefile.am snippet, augment variables with
1037   # += instead of assigning them.
1038   if test -n "$makefile_name"; then
1039     assign='+='
1040   else
1041     assign='='
1042   fi
1043   if test "$libtool" = true; then
1044     libext=la
1045     perhapsLT=LT
1046     sed_eliminate_LDFLAGS=
1047   else
1048     libext=a
1049     perhapsLT=
1050     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[       ]*+=/d'
1051   fi
1052   echo "## Process this file with automake to produce Makefile.in."
1053   func_emit_copyright_notice
1054   if test -n "$actioncmd"; then
1055     echo "# Reproduce by: $actioncmd"
1056   fi
1057   echo
1058   if test -z "$makefile_name"; then
1059     # No need to generate dependencies since the sources are in gnulib, not here.
1060     echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
1061   fi
1062   echo
1063   (
1064     for module in $modules; do
1065       func_verify_nontests_module
1066       if test -n "$module"; then
1067         {
1068           func_get_automake_snippet "$module" |
1069             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1070                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1071             sed -e "$sed_eliminate_LDFLAGS" |
1072             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1073             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1074                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
1075           if test "$module" = 'alloca'; then
1076             echo "${libname}_${libext}_LIBADD += @${perhapsLT}ALLOCA@"
1077           fi
1078         } > amsnippet.tmp
1079         # Skip the contents if its entirely empty.
1080         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1081           echo "## begin gnulib module $module"
1082           echo
1083           cat amsnippet.tmp
1084           echo "## end   gnulib module $module"
1085           echo
1086         fi
1087         rm -f amsnippet.tmp
1088       fi
1089     done
1090   ) > allsnippets.tmp
1091   if grep "^[a-zA-Z0-9_]*_${perhapsLT}LIBRARIES *= *$libname\\.$libext\$" allsnippets.tmp > /dev/null; then
1092     # One of the snippets already specifies an installation location for the
1093     # library. Don't confuse automake by saying it should not be installed.
1094     :
1095   else
1096     # By default, the generated library should not be installed.
1097     echo "noinst_${perhapsLT}LIBRARIES $assign $libname.$libext"
1098   fi
1099   echo
1100   echo "${libname}_${libext}_SOURCES ="
1101   # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. However,
1102   # automake during its analyses looks for $(LIBOBJS), not for @LIBOBJS@.
1103   echo "${libname}_${libext}_LIBADD = \$(${perhapsLT}LIBOBJS)"
1104   if test "$libtool" = true; then
1105     echo "${libname}_${libext}_LDFLAGS = \$(AM_LDFLAGS)"
1106   fi
1107   if test -z "$makefile_name"; then
1108     echo "noinst_HEADERS ="
1109     # Automake versions < 1.9b create an empty pkgdatadir at installation time
1110     # if you specify pkgdata_DATA to empty. This is a workaround.
1111     if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1112       echo "pkgdata_DATA ="
1113     fi
1114     echo "EXTRA_DIST ="
1115     echo "BUILT_SOURCES ="
1116     echo "SUFFIXES ="
1117   fi
1118   echo "MOSTLYCLEANFILES $assign core *.stackdump"
1119   if test -z "$makefile_name"; then
1120     echo "MOSTLYCLEANDIRS ="
1121     echo "CLEANFILES ="
1122     echo "DISTCLEANFILES ="
1123     echo "MAINTAINERCLEANFILES ="
1124     echo
1125     echo "AM_CPPFLAGS ="
1126   fi
1127   echo
1128   cat allsnippets.tmp
1129   echo
1130   echo "mostlyclean-local: mostlyclean-generic"
1131   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
1132   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
1133   echo "            if test -d \$\$dir; then \\"
1134   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1135   echo "            fi; \\"
1136   echo "          done"
1137   rm -f allsnippets.tmp
1138 }
1139
1140 # func_emit_tests_Makefile_am
1141 # emits the contents of tests makefile to standard output.
1142 # Input:
1143 # - local_gnulib_dir  from --local-dir
1144 # - modules         list of modules, including dependencies
1145 # - libname         library name
1146 # - makefile_name   from --makefile-name
1147 # - libtool         true if libtool will be used, false or blank otherwise
1148 # - sourcebase      relative directory containing lib source code
1149 # - m4base          relative directory containing autoconf macros
1150 # - testsbase       relative directory containing unit test code
1151 func_emit_tests_Makefile_am ()
1152 {
1153   if test "$libtool" = true; then
1154     libext=la
1155   else
1156     libext=a
1157   fi
1158   if test "$libtool" = true; then
1159     libext=la
1160     sed_eliminate_LDFLAGS=
1161   else
1162     libext=a
1163     sed_eliminate_LDFLAGS='/^lib_LDFLAGS[       ]*+=/d'
1164   fi
1165   testsbase_inverse=`echo "$testsbase" | sed -e 's,/$,,' | sed -e 's,[^/][^/]*,..,g'`
1166   echo "## Process this file with automake to produce Makefile.in."
1167   func_emit_copyright_notice
1168   echo
1169   # Generate dependencies here, since it eases the debugging of test failures.
1170   echo "AUTOMAKE_OPTIONS = 1.5 foreign"
1171   echo
1172   echo "ACLOCAL_AMFLAGS = -I ${testsbase_inverse}/${m4base}"
1173   echo
1174   (
1175     for module in $modules; do
1176       func_verify_tests_module
1177       if test -n "$module"; then
1178         {
1179           func_get_automake_snippet "$module" |
1180             sed -e 's,lib_LIBRARIES,lib%_LIBRARIES,g' \
1181                 -e 's,lib_LTLIBRARIES,lib%_LTLIBRARIES,g' |
1182             sed -e "$sed_eliminate_LDFLAGS" |
1183             sed -e 's,lib_\([A-Z][A-Z]*\),'"${libname}_${libext}"'_\1,g' |
1184             sed -e 's,lib%_LIBRARIES,lib_LIBRARIES,g' \
1185                 -e 's,lib%_LTLIBRARIES,lib_LTLIBRARIES,g'
1186         } > amsnippet.tmp
1187         # Skip the contents if its entirely empty.
1188         if grep '[^      ]' amsnippet.tmp > /dev/null ; then
1189           echo "## begin gnulib module $module"
1190           echo
1191           cat amsnippet.tmp
1192           echo "## end   gnulib module $module"
1193           echo
1194         fi
1195         rm -f amsnippet.tmp
1196       fi
1197     done
1198   ) > allsnippets.tmp
1199   # Nothing is being added to SUBDIRS; nevertheless the existence of this
1200   # variable is needed to avoid an error from automake:
1201   #   "AM_GNU_GETTEXT used but SUBDIRS not defined"
1202   echo "SUBDIRS ="
1203   echo "TESTS ="
1204   echo "TESTS_ENVIRONMENT ="
1205   echo "noinst_PROGRAMS ="
1206   echo "check_PROGRAMS ="
1207   echo "noinst_HEADERS ="
1208   if grep '^pkgdata_DATA *+=' allsnippets.tmp > /dev/null; then
1209     echo "pkgdata_DATA ="
1210   fi
1211   echo "EXTRA_DIST ="
1212   echo "BUILT_SOURCES ="
1213   echo "SUFFIXES ="
1214   echo "MOSTLYCLEANFILES = core *.stackdump"
1215   echo "MOSTLYCLEANDIRS ="
1216   echo "CLEANFILES ="
1217   echo "DISTCLEANFILES ="
1218   echo "MAINTAINERCLEANFILES ="
1219   echo
1220   echo "AM_CPPFLAGS = \\"
1221   echo "  -I. -I\$(srcdir) \\"
1222   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
1223   echo "  -I${testsbase_inverse}/${sourcebase-lib} -I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
1224   echo
1225   echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
1226   echo
1227   cat allsnippets.tmp
1228   echo "# Clean up after Solaris cc."
1229   echo "clean-local:"
1230   echo "        rm -rf SunWS_cache"
1231   echo
1232   echo "mostlyclean-local: mostlyclean-generic"
1233   echo "        @test -z \"\$(MOSTLYCLEANDIRS)\" || \\"
1234   echo "          for dir in \$(MOSTLYCLEANDIRS); do \\"
1235   echo "            if test -d \$\$dir; then \\"
1236   echo "              echo \"rmdir \$\$dir\"; rmdir \$\$dir; \\"
1237   echo "            fi; \\"
1238   echo "          done"
1239   rm -f allsnippets.tmp
1240 }
1241
1242 # func_import modules
1243 # Uses also the variables
1244 # - destdir         target directory
1245 # - local_gnulib_dir  from --local-dir
1246 # - libname         library name
1247 # - sourcebase      directory relative to destdir where to place source code
1248 # - m4base          directory relative to destdir where to place *.m4 macros
1249 # - docbase         directory relative to destdir where to place doc files
1250 # - testsbase       directory relative to destdir where to place unit test code
1251 # - auxdir          directory relative to destdir where to place build aux files
1252 # - inctests        true if --with-tests was given, blank otherwise
1253 # - avoidlist       list of modules to avoid, from --avoid
1254 # - lgpl            true if library's license shall be LGPL, blank otherwise
1255 # - makefile_name   from --makefile-name
1256 # - libtool         true if --libtool was given, false if --no-libtool was
1257 #                   given, blank otherwise
1258 # - guessed_libtool true if the configure.ac file uses libtool, false otherwise
1259 # - macro_prefix    prefix of gl_EARLY, gl_INIT macros to use
1260 # - autoconf_minversion  minimum supported autoconf version
1261 # - doit            : if actions shall be executed, false if only to be printed
1262 # - symbolic        true if files should be symlinked, copied otherwise
1263 func_import ()
1264 {
1265   # Get the cached settings.
1266   cached_local_gnulib_dir=
1267   cached_specified_modules=
1268   cached_avoidlist=
1269   cached_sourcebase=
1270   cached_m4base=
1271   cached_docbase=
1272   cached_testsbase=
1273   cached_libname=
1274   cached_lgpl=
1275   cached_makefile_name=
1276   cached_libtool=
1277   cached_macro_prefix=
1278   cached_files=
1279   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1280     cached_libtool=false
1281     my_sed_traces='
1282       s,#.*$,,
1283       s,^dnl .*$,,
1284       s, dnl .*$,,
1285       /gl_LOCAL_DIR(/ {
1286         s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
1287       }
1288       /gl_MODULES(/ {
1289         s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
1290       }
1291       /gl_AVOID(/ {
1292         s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
1293       }
1294       /gl_SOURCE_BASE(/ {
1295         s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
1296       }
1297       /gl_M4_BASE(/ {
1298         s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
1299       }
1300       /gl_DOC_BASE(/ {
1301         s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
1302       }
1303       /gl_TESTS_BASE(/ {
1304         s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
1305       }
1306       /gl_LIB(/ {
1307         s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
1308       }
1309       /gl_LGPL/ {
1310         s,^.*$,cached_lgpl=true,p
1311       }
1312       /gl_MAKEFILE_NAME(/ {
1313         s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
1314       }
1315       /gl_LIBTOOL/ {
1316         s,^.*$,cached_libtool=true,p
1317       }
1318       /gl_MACRO_PREFIX(/ {
1319         s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
1320       }'
1321     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
1322     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1323       my_sed_traces='
1324         s,#.*$,,
1325         s,^dnl .*$,,
1326         s, dnl .*$,,
1327         /AC_DEFUN(\['"${cached_macro_prefix}"'_FILE_LIST\], \[/ {
1328           s,^.*$,cached_files=",p
1329           n
1330           ta
1331           :a
1332           s,^\]).*$,",
1333           tb
1334           p
1335           n
1336           ba
1337           :b
1338           p
1339         }'
1340       eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-comp.m4`
1341     fi
1342   fi
1343
1344   # Merge the cached settings with the specified ones.
1345   # The m4base must be the same as expected from the pathname.
1346   if test -n "$cached_m4base" && test "$cached_m4base" != "$m4base"; then
1347     func_fatal_error "$m4base/gnulib-cache.m4 is expected to contain gl_M4_BASE([$m4base])"
1348   fi
1349   # The local_gnulib_dir defaults to the cached one. Recall that the cached one
1350   # is relative to $destdir, whereas the one we use is relative to . or absolute.
1351   if test -z "$local_gnulib_dir"; then
1352     if test -n "$cached_local_gnulib_dir"; then
1353       case "$destdir" in
1354         /*)
1355           local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1356         *)
1357           case "$cached_local_gnulib_dir" in
1358             /*)
1359               local_gnulib_dir="$destdir/$cached_local_gnulib_dir" ;;
1360             *)
1361               func_relconcat "$destdir" "$cached_local_gnulib_dir"
1362               local_gnulib_dir="$relconcat" ;;
1363           esac ;;
1364       esac
1365     fi
1366   fi
1367   # Append the cached and the specified module names. So that
1368   # "gnulib-tool --import foo" means to add the module foo.
1369   specified_modules="$cached_specified_modules $1"
1370   # Append the cached and the specified avoidlist. This is probably better
1371   # than dropping the cached one when --avoid is specified at least once.
1372   avoidlist=`echo $cached_avoidlist $avoidlist`
1373   # The sourcebase defaults to the cached one.
1374   if test -z "$sourcebase"; then
1375     sourcebase="$cached_sourcebase"
1376     if test -z "$sourcebase"; then
1377       func_fatal_error "missing --source-base option"
1378     fi
1379   fi
1380   # The docbase defaults to the cached one.
1381   if test -z "$docbase"; then
1382     docbase="$cached_docbase"
1383     if test -z "$docbase"; then
1384       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."
1385     fi
1386   fi
1387   # The testsbase defaults to the cached one.
1388   if test -z "$testsbase"; then
1389     testsbase="$cached_testsbase"
1390     if test -z "$testsbase"; then
1391       func_fatal_error "missing --tests-base option"
1392     fi
1393   fi
1394   # The libname defaults to the cached one.
1395   if test -z "$supplied_libname"; then
1396     libname="$cached_libname"
1397     if test -z "$libname"; then
1398       func_fatal_error "missing --lib option"
1399     fi
1400   fi
1401   # Require LGPL if specified either way.
1402   if test -z "$lgpl"; then
1403     lgpl="$cached_lgpl"
1404   fi
1405   # The makefile_name defaults to the cached one.
1406   if test -z "$makefile_name"; then
1407     makefile_name="$cached_makefile_name"
1408   fi
1409   # Use libtool if specified either way, or if guessed.
1410   if test -z "$libtool"; then
1411     if test -n "$cached_m4base"; then
1412       libtool="$cached_libtool"
1413     else
1414       libtool="$guessed_libtool"
1415     fi
1416   fi
1417   # The macro_prefix defaults to the cached one.
1418   if test -z "$macro_prefix"; then
1419     macro_prefix="$cached_macro_prefix"
1420     if test -z "$macro_prefix"; then
1421       func_fatal_error "missing --macro-prefix option"
1422     fi
1423   fi
1424
1425   # Canonicalize the list of specified modules.
1426   specified_modules=`for m in $specified_modules; do echo $m; done | LC_ALL=C sort -u`
1427
1428   # Determine final module list.
1429   modules="$specified_modules"
1430   func_modules_transitive_closure
1431   echo "Module list with included dependencies:"
1432   echo "$modules" | sed -e 's/^/  /'
1433
1434   # Add the dummy module if needed.
1435   func_modules_add_dummy
1436
1437   # If --lgpl, verify that the licenses of modules are compatible.
1438   if test -n "$lgpl"; then
1439     for module in $modules; do
1440       license=`func_get_license $module`
1441       case $license in
1442         LGPL | 'GPLed build tool') ;;
1443         'public domain' | 'unlimited' | 'unmodifiable license text') ;;
1444         *) func_fatal_error "incompatible license on module $module: $license" ;;
1445       esac
1446     done
1447   fi
1448
1449   # Determine script to apply to imported library files.
1450   sed_transform_lib_file=
1451   for module in $modules; do
1452     if test $module = config-h; then
1453       # Assume config.h exists, and that -DHAVE_CONFIG_H is omitted.
1454       sed_transform_lib_file=$sed_transform_lib_file'
1455         s/^#ifdef[       ]*HAVE_CONFIG_H[        ]*$/#if 1/
1456       '
1457       break
1458     fi
1459   done
1460   if test -n "$lgpl"; then
1461     # Update license.
1462     sed_transform_lib_file=$sed_transform_lib_file'
1463       s/GNU General/GNU Lesser General/g
1464       s/version 2\([ ,]\)/version 2.1\1/g
1465     '
1466   fi
1467
1468   # Determine final file list.
1469   func_modules_to_filelist
1470   echo "File list:"
1471   echo "$files" | sed -e 's/^/  /'
1472
1473   test -n "$files" \
1474     || func_fatal_error "refusing to do nothing"
1475
1476   # Add m4/gnulib-tool.m4 to the file list. It is not part of any module.
1477   new_files="$files m4/gnulib-tool.m4"
1478   old_files="$cached_files"
1479   if test -f "$destdir"/$m4base/gnulib-tool.m4; then
1480     old_files="$old_files m4/gnulib-tool.m4"
1481   fi
1482
1483   # Create directories.
1484   if test ! -d "$destdir/$sourcebase"; then
1485     if $doit; then
1486       echo "Creating directory $destdir/$sourcebase"
1487       mkdir "$destdir/$sourcebase" || func_fatal_error "failed"
1488     else
1489       echo "Create directory $destdir/$sourcebase"
1490     fi
1491   fi
1492   if test ! -d "$destdir/$m4base"; then
1493     if $doit; then
1494       echo "Creating directory $destdir/$m4base"
1495       mkdir "$destdir/$m4base" || func_fatal_error "failed"
1496     else
1497       echo "Create directory $destdir/$m4base"
1498     fi
1499   fi
1500   docfiles=`echo "$files" | sed -n -e 's,^doc/,,p'`
1501   if test -n "$docfiles"; then
1502     if test ! -d "$destdir/$docbase"; then
1503       if $doit; then
1504         echo "Creating directory $destdir/$docbase"
1505         mkdir "$destdir/$docbase" || func_fatal_error "failed"
1506       else
1507         echo "Create directory $destdir/$docbase"
1508       fi
1509     fi
1510   fi
1511   if test -n "$inctests"; then
1512     if test ! -d "$destdir/$testsbase"; then
1513       if $doit; then
1514         echo "Creating directory $destdir/$testsbase"
1515         mkdir "$destdir/$testsbase" || func_fatal_error "failed"
1516       else
1517         echo "Create directory $destdir/$testsbase"
1518       fi
1519     fi
1520   fi
1521   if test ! -d "$destdir/$auxdir"; then
1522     if $doit; then
1523       echo "Creating directory $destdir/$auxdir"
1524       mkdir "$destdir/$auxdir" || func_fatal_error "failed"
1525     else
1526       echo "Create directory $destdir/$auxdir"
1527     fi
1528   fi
1529
1530   # func_dest_tmpfilename file
1531   # determines the name of a temporary file (file is relative to destdir).
1532   # Sets variable:
1533   #   - tmpfile       absolute filename of the temporary file
1534   func_dest_tmpfilename ()
1535   {
1536     if $doit; then
1537       # Put the new contents of $file in a file in the same directory (needed
1538       # to guarantee that an 'mv' to "$destdir/$file" works).
1539       tmpfile="$destdir/$1.tmp"
1540     else
1541       # Put the new contents of $file in a file in a temporary directory
1542       # (because the directory of "$file" might not exist).
1543       tmpfile="$tmp"/`basename "$1"`.tmp
1544     fi
1545   }
1546
1547   # Copy files or make symbolic links. Remove obsolete files.
1548   delimiter='   '
1549   sed_rewrite_old_files="\
1550     s,^build-aux/,$auxdir/,
1551     s,^doc/,$cached_docbase/,
1552     s,^lib/,$cached_sourcebase/,
1553     s,^m4/,$cached_m4base/,
1554     s,^tests/,$cached_testsbase/,"
1555   sed_rewrite_new_files="\
1556     s,^build-aux/,$auxdir/,
1557     s,^doc/,$docbase/,
1558     s,^lib/,$sourcebase/,
1559     s,^m4/,$m4base/,
1560     s,^tests/,$testsbase/,"
1561   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1562   # representing the files according to the last gnulib-tool invocation.
1563   for f in $old_files; do echo $f; done \
1564     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_old_files" \
1565     | LC_ALL=C sort \
1566     > "$tmp"/old-files
1567   # Construct a table with 2 columns: rewritten-file-name original-file-name,
1568   # representing the files after this gnulib-tool invocation.
1569   for f in $new_files; do echo $f; done \
1570     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" \
1571     | LC_ALL=C sort \
1572     > "$tmp"/new-files
1573   # First the files that are in old-files, but not in new-files:
1574   sed_take_first_column='s,'"$delimiter"'.*,,'
1575   for g in `LC_ALL=C join -t"$delimiter" -v1 "$tmp"/old-files "$tmp"/new-files | sed -e "$sed_take_first_column"`; do
1576     # Remove the file. Do nothing if the user already removed it.
1577     if test -f "$destdir/$g"; then
1578       if $doit; then
1579         echo "Removing file $g (backup in ${g}~)"
1580         mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1581       else
1582         echo "Remove file $g (backup in ${g}~)"
1583       fi
1584     fi
1585   done
1586   # func_add_or_update handles a file that ought to be present afterwards.
1587   # Uses parameters
1588   # - f             the original file name
1589   # - g             the rewritten file name
1590   # - already_present  nonempty if the file already exists, empty otherwise
1591   func_add_or_update ()
1592   {
1593     func_dest_tmpfilename "$g"
1594     func_lookup_file "$f"
1595     cp "$lookedup_file" "$tmpfile" || func_fatal_error "failed"
1596     if test -n "$sed_transform_lib_file"; then
1597       case "$f" in
1598         lib/*)
1599           sed -e "$sed_transform_lib_file" \
1600             < "$lookedup_file" > "$tmpfile" || func_fatal_error "failed"
1601           ;;
1602       esac
1603     fi
1604     if test -f "$destdir/$g"; then
1605       # The file already exists.
1606       if cmp "$destdir/$g" "$tmpfile" > /dev/null; then
1607         : # The file has not changed.
1608       else
1609         # Replace the file.
1610         if $doit; then
1611           if test -n "$already_present"; then
1612             echo "Updating file $g (backup in ${g}~)"
1613           else
1614             echo "Replacing file $g (non-gnulib code backuped in ${g}~) !!"
1615           fi
1616           mv -f "$destdir/$g" "$destdir/${g}~" || func_fatal_error "failed"
1617           if test -n "$symbolic" && test -z "$lookedup_tmp" \
1618              && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1619             func_ln_if_changed "$lookedup_file" "$destdir/$g"
1620           else
1621             mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1622           fi
1623         else
1624           if test -n "$already_present"; then
1625             echo "Update file $g (backup in ${g}~)"
1626           else
1627             echo "Replace file $g (non-gnulib code backuped in ${g}~) !!"
1628           fi
1629         fi
1630       fi
1631     else
1632       # Install the file.
1633       # Don't protest if the file should be there but isn't: it happens
1634       # frequently that developers don't put autogenerated files into CVS.
1635       if $doit; then
1636         echo "Copying file $g"
1637         if test -n "$symbolic" && test -z "$lookedup_tmp" \
1638            && cmp "$lookedup_file" "$tmpfile" > /dev/null; then
1639           func_ln_if_changed "$lookedup_file" "$destdir/$g"
1640         else
1641           mv -f "$tmpfile" "$destdir/${g}" || func_fatal_error "failed"
1642         fi
1643       else
1644         echo "Copy file $g"
1645       fi
1646     fi
1647     rm -f "$tmpfile"
1648   }
1649   # Then the files that are in new-files, but not in old-files:
1650   sed_take_last_column='s,^.*'"$delimiter"',,'
1651   already_present=
1652   LC_ALL=C join -t"$delimiter" -v2 "$tmp"/old-files "$tmp"/new-files \
1653     | sed -e "$sed_take_last_column" \
1654     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/added-files
1655   while read g f; do
1656     func_add_or_update
1657   done < "$tmp"/added-files
1658   # Then the files that are in new-files and in old-files:
1659   already_present=true
1660   LC_ALL=C join -t"$delimiter" "$tmp"/old-files "$tmp"/new-files \
1661     | sed -e "$sed_take_last_column" \
1662     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_new_files" > "$tmp"/kept-files
1663   while read g f; do
1664     func_add_or_update
1665   done < "$tmp"/kept-files
1666
1667   # Command-line invocation printed in a comment in generated gnulib-cache.m4.
1668   actioncmd="gnulib-tool --import"
1669   actioncmd="$actioncmd --dir=$destdir"
1670   if test -n "$local_gnulib_dir"; then
1671     actioncmd="$actioncmd --local-dir=$local_gnulib_dir"
1672   fi
1673   actioncmd="$actioncmd --lib=$libname"
1674   actioncmd="$actioncmd --source-base=$sourcebase"
1675   actioncmd="$actioncmd --m4-base=$m4base"
1676   actioncmd="$actioncmd --doc-base=$docbase"
1677   actioncmd="$actioncmd --aux-dir=$auxdir"
1678   for module in $avoidlist; do
1679     actioncmd="$actioncmd --avoid=$module"
1680   done
1681   if test -n "$lgpl"; then
1682     actioncmd="$actioncmd --lgpl"
1683   fi
1684   if test -n "$makefile_name"; then
1685     actioncmd="$actioncmd --makefile_name=$makefile_name"
1686   fi
1687   if test "$libtool" = true; then
1688     actioncmd="$actioncmd --libtool"
1689   else
1690     actioncmd="$actioncmd --no-libtool"
1691   fi
1692   actioncmd="$actioncmd --macro-prefix=$macro_prefix"
1693   actioncmd="$actioncmd `echo $specified_modules`"
1694
1695   # Default the makefile name to Makefile.am.
1696   if test -n "$makefile_name"; then
1697     makefile_am=$makefile_name
1698   else
1699     makefile_am=Makefile.am
1700   fi
1701
1702   # Create library makefile.
1703   func_dest_tmpfilename $sourcebase/$makefile_am
1704   func_emit_lib_Makefile_am > "$tmpfile"
1705   if test -f "$destdir"/$sourcebase/$makefile_am; then
1706     if cmp "$destdir"/$sourcebase/$makefile_am "$tmpfile" > /dev/null; then
1707       rm -f "$tmpfile"
1708     else
1709       if $doit; then
1710         echo "Updating $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1711         mv -f "$destdir"/$sourcebase/$makefile_am "$destdir"/$sourcebase/$makefile_am~
1712         mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1713       else
1714         echo "Update $sourcebase/$makefile_am (backup in $sourcebase/$makefile_am~)"
1715         rm -f "$tmpfile"
1716       fi
1717     fi
1718   else
1719     if $doit; then
1720       echo "Creating $sourcebase/$makefile_am"
1721       mv -f "$tmpfile" "$destdir"/$sourcebase/$makefile_am
1722     else
1723       echo "Create $sourcebase/$makefile_am"
1724       rm -f "$tmpfile"
1725     fi
1726   fi
1727
1728   # Create m4/gnulib-cache.m4.
1729   func_dest_tmpfilename $m4base/gnulib-cache.m4
1730   (
1731     func_emit_copyright_notice
1732     echo "#"
1733     echo "# This file represents the specification of how gnulib-tool is used."
1734     echo "# It acts as a cache: It is written and read by gnulib-tool."
1735     echo "# In projects using CVS, this file is meant to be stored in CVS,"
1736     echo "# like the configure.ac and various Makefile.am files."
1737     echo
1738     echo
1739     echo "# Specification in the form of a command-line invocation:"
1740     echo "#   $actioncmd"
1741     echo
1742     echo "# Specification in the form of a few gnulib-tool.m4 macro invocations:"
1743     # Store the local_gnulib_dir relative to destdir.
1744     case "$local_gnulib_dir" in
1745       "" | /*)
1746         relative_local_gnulib_dir="$local_gnulib_dir" ;;
1747       * )
1748         case "$destdir" in
1749           /*) relative_local_gnulib_dir="$local_gnulib_dir" ;;
1750           *)
1751             # destdir, local_gnulib_dir are both relative.
1752             func_relativize "$destdir" "$local_gnulib_dir"
1753             relative_local_gnulib_dir="$reldir" ;;
1754         esac ;;
1755     esac
1756     echo "gl_LOCAL_DIR([$relative_local_gnulib_dir])"
1757     echo "gl_MODULES(["`echo $specified_modules`"])"
1758     echo "gl_AVOID([$avoidlist])"
1759     echo "gl_SOURCE_BASE([$sourcebase])"
1760     echo "gl_M4_BASE([$m4base])"
1761     echo "gl_DOC_BASE([$docbase])"
1762     echo "gl_TESTS_BASE([$testsbase])"
1763     echo "gl_LIB([$libname])"
1764     test -z "$lgpl" || echo "gl_LGPL"
1765     echo "gl_MAKEFILE_NAME([$makefile_name])"
1766     if test "$libtool" = true; then
1767       echo "gl_LIBTOOL"
1768     fi
1769     echo "gl_MACRO_PREFIX([$macro_prefix])"
1770   ) > "$tmpfile"
1771   if test -f "$destdir"/$m4base/gnulib-cache.m4; then
1772     if cmp "$destdir"/$m4base/gnulib-cache.m4 "$tmpfile" > /dev/null; then
1773       rm -f "$tmpfile"
1774     else
1775       if $doit; then
1776         echo "Updating $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1777         mv -f "$destdir"/$m4base/gnulib-cache.m4 "$destdir"/$m4base/gnulib-cache.m4~
1778         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1779       else
1780         echo "Update $m4base/gnulib-cache.m4 (backup in $m4base/gnulib-cache.m4~)"
1781         if false; then
1782           cat "$tmpfile"
1783           echo
1784           echo "# gnulib-cache.m4 ends here"
1785         fi
1786         rm -f "$tmpfile"
1787       fi
1788     fi
1789   else
1790     if $doit; then
1791       echo "Creating $m4base/gnulib-cache.m4"
1792       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-cache.m4
1793     else
1794       echo "Create $m4base/gnulib-cache.m4"
1795       cat "$tmpfile"
1796       rm -f "$tmpfile"
1797     fi
1798   fi
1799
1800   # Create m4/gnulib-comp.m4.
1801   func_dest_tmpfilename $m4base/gnulib-comp.m4
1802   (
1803     func_emit_copyright_notice
1804     echo "#"
1805     echo "# This file represents the compiled summary of the specification in"
1806     echo "# gnulib-cache.m4. It lists the computed macro invocations that need"
1807     echo "# to be invoked from configure.ac."
1808     echo "# In projects using CVS, this file can be treated like other built files."
1809     echo
1810     echo
1811     echo "# This macro should be invoked from $configure_ac, in the section"
1812     echo "# \"Checks for programs\", right after AC_PROG_CC, and certainly before"
1813     echo "# any checks for libraries, header files, types and library functions."
1814     echo "AC_DEFUN([${macro_prefix}_EARLY],"
1815     echo "["
1816     echo "  m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
1817     echo "  m4_pattern_allow([^gl_ES$])dnl a valid locale name"
1818     echo "  AC_REQUIRE([AC_PROG_RANLIB])"
1819     if grep AC_GNU_SOURCE "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1820       echo "  AC_REQUIRE([AC_GNU_SOURCE])"
1821     fi
1822     if grep gl_USE_SYSTEM_EXTENSIONS "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1823       echo "  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])"
1824     fi
1825     if grep gl_LOCK "$destdir"/$m4base/*.m4 >/dev/null 2>/dev/null; then
1826       echo "  AC_REQUIRE([gl_LOCK_EARLY])"
1827     fi
1828     echo "])"
1829     echo
1830     echo "# This macro should be invoked from $configure_ac, in the section"
1831     echo "# \"Check for header files, types and library functions\"."
1832     echo "AC_DEFUN([${macro_prefix}_INIT],"
1833     echo "["
1834     if test "$libtool" = true; then
1835       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
1836       echo "  gl_cond_libtool=true"
1837     else
1838       echo "  AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
1839       echo "  gl_cond_libtool=false"
1840       echo "  gl_libdeps="
1841       echo "  gl_ltlibdeps="
1842     fi
1843     if test "$auxdir" != "build-aux"; then
1844       sed_replace_build_aux='
1845         :a
1846         /AC_CONFIG_FILES(.*:build-aux\/.*)/{
1847           s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
1848           ba
1849         }'
1850       sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
1851     else
1852       sed_replace_build_aux=
1853     fi
1854     for module in $modules; do
1855       func_verify_module
1856       if test -n "$module"; then
1857         func_get_autoconf_snippet "$module" \
1858           | sed -e '/^$/d;' -e 's/^/  /' \
1859                 -e 's/AM_GNU_GETTEXT(\[external\])/dnl you must add AM_GNU_GETTEXT([external]) or similar to configure.ac./' \
1860                 -e "$sed_replace_build_aux"
1861         if test "$module" = 'alloca' && test "$libtool" = true; then
1862           echo 'changequote(,)dnl'
1863           echo 'LTALLOCA=`echo "$ALLOCA" | sed '"'"'s/\.[^.]* /.lo /g;s/\.[^.]*$/.lo/'"'"'`'
1864           echo 'changequote([, ])dnl'
1865           echo 'AC_SUBST([LTALLOCA])'
1866         fi
1867       fi
1868     done
1869     # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
1870     # created using libtool, because libtool already handles the dependencies.
1871     if test "$libtool" != true; then
1872       libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
1873       echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
1874       echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
1875       echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
1876       echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
1877     fi
1878     echo "])"
1879     echo
1880     echo "# This macro records the list of files which have been installed by"
1881     echo "# gnulib-tool and may be removed by future gnulib-tool invocations."
1882     echo "AC_DEFUN([${macro_prefix}_FILE_LIST], ["
1883     echo "$files" | sed -e 's,^,  ,'
1884     echo "])"
1885   ) > "$tmpfile"
1886   if test -f "$destdir"/$m4base/gnulib-comp.m4; then
1887     if cmp "$destdir"/$m4base/gnulib-comp.m4 "$tmpfile" > /dev/null; then
1888       rm -f "$tmpfile"
1889     else
1890       if $doit; then
1891         echo "Updating $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1892         mv -f "$destdir"/$m4base/gnulib-comp.m4 "$destdir"/$m4base/gnulib-comp.m4~
1893         mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1894       else
1895         echo "Update $m4base/gnulib-comp.m4 (backup in $m4base/gnulib-comp.m4~)"
1896         if false; then
1897           cat "$tmpfile"
1898           echo
1899           echo "# gnulib-comp.m4 ends here"
1900         fi
1901         rm -f "$tmpfile"
1902       fi
1903     fi
1904   else
1905     if $doit; then
1906       echo "Creating $m4base/gnulib-comp.m4"
1907       mv -f "$tmpfile" "$destdir"/$m4base/gnulib-comp.m4
1908     else
1909       echo "Create $m4base/gnulib-comp.m4"
1910       cat "$tmpfile"
1911       rm -f "$tmpfile"
1912     fi
1913   fi
1914
1915   if test -n "$inctests"; then
1916     # Create tests makefile.
1917     func_dest_tmpfilename $testsbase/$makefile_am
1918     func_emit_tests_Makefile_am > "$tmpfile"
1919     if test -f "$destdir"/$testsbase/$makefile_am; then
1920       if cmp "$destdir"/$testsbase/$makefile_am "$tmpfile" > /dev/null; then
1921         rm -f "$tmpfile"
1922       else
1923         if $doit; then
1924           echo "Updating $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
1925           mv -f "$destdir"/$testsbase/$makefile_am "$destdir"/$testsbase/$makefile_am~
1926           mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
1927         else
1928           echo "Update $testsbase/$makefile_am (backup in $testsbase/$makefile_am~)"
1929           rm -f "$tmpfile"
1930         fi
1931       fi
1932     else
1933       if $doit; then
1934         echo "Creating $testsbase/$makefile_am"
1935         mv -f "$tmpfile" "$destdir"/$testsbase/$makefile_am
1936       else
1937         echo "Create $testsbase/$makefile_am"
1938         rm -f "$tmpfile"
1939       fi
1940     fi
1941   fi
1942
1943   echo "Finished."
1944   echo
1945   echo "You may need to add #include directives for the following .h files."
1946   (
1947    # First the #include <...> directives without #ifs, sorted for convenience.
1948    for module in $modules; do
1949      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1950        :
1951      else
1952        func_get_include_directive "$module" | grep -v 'include "'
1953      fi
1954    done | LC_ALL=C sort -u
1955    # Then the #include "..." directives without #ifs, sorted for convenience.
1956    for module in $modules; do
1957      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1958        :
1959      else
1960        func_get_include_directive "$module" | grep 'include "'
1961      fi
1962    done | LC_ALL=C sort -u
1963    # Then the #include directives that are surrounded by #ifs. Not sorted.
1964    for module in $modules; do
1965      if func_get_include_directive "$module" | grep '^#if' >/dev/null; then
1966        func_get_include_directive "$module"
1967      fi
1968    done
1969   ) | sed -e '/^$/d;' -e 's/^/  /'
1970   echo
1971   echo "Don't forget to"
1972   if test "$makefile_am" = Makefile.am; then
1973     echo "  - add \"$sourcebase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1974   else
1975     echo "  - \"include $makefile_name\" from within \"$sourcebase/Makefile.am\","
1976   fi
1977   if test -n "$inctests"; then
1978     if test "$makefile_am" = Makefile.am; then
1979       echo "  - add \"$testsbase/Makefile\" to AC_CONFIG_FILES in $configure_ac,"
1980     else
1981       echo "  - \"include $makefile_name\" from within \"$testsbase/Makefile.am\","
1982     fi
1983   fi
1984   if test "$makefile_am" = Makefile.am; then
1985     sourcebase_dir=`echo "$sourcebase" | sed -n -e 's,/[^/]*$,/,p'`
1986     sourcebase_base=`basename "$sourcebase"`
1987     echo "  - mention \"${sourcebase_base}\" in SUBDIRS in ${sourcebase_dir}Makefile.am,"
1988   fi
1989   if test -n "$inctests"; then
1990     if test "$makefile_am" = Makefile.am; then
1991       testsbase_dir=`echo "$testsbase" | sed -n -e 's,/[^/]*$,/,p'`
1992       testsbase_base=`basename "$testsbase"`
1993       echo "  - mention \"${testsbase_base}\" in SUBDIRS in ${testsbase_dir}Makefile.am,"
1994     fi
1995   fi
1996   echo "  - mention \"-I ${m4base}\" in ACLOCAL_AMFLAGS in Makefile.am,"
1997   echo "  - invoke ${macro_prefix}_EARLY in $configure_ac, right after AC_PROG_CC,"
1998   echo "  - invoke ${macro_prefix}_INIT in $configure_ac."
1999 }
2000
2001 # func_create_testdir testdir modules
2002 # Input:
2003 # - local_gnulib_dir  from --local-dir
2004 # - auxdir          directory relative to destdir where to place build aux files
2005 func_create_testdir ()
2006 {
2007   testdir="$1"
2008   modules="$2"
2009   modules=`for m in $modules; do echo $m; done | LC_ALL=C sort -u`
2010
2011   # Subdirectory names.
2012   sourcebase=gllib
2013   m4base=glm4
2014   docbase=gldoc
2015   testsbase=gltests
2016
2017   # Determine final module list.
2018   func_modules_transitive_closure
2019   echo "Module list with included dependencies:"
2020   echo "$modules" | sed -e 's/^/  /'
2021
2022   # Add the dummy module if needed.
2023   func_modules_add_dummy
2024
2025   # Determine final file list.
2026   func_modules_to_filelist
2027   echo "File list:"
2028   echo "$files" | sed -e 's/^/  /'
2029
2030   # Create directories.
2031   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
2032     case "$d" in
2033       build-aux) mkdir -p "$testdir/$auxdir" ;;
2034       doc)       mkdir -p "$testdir/$docbase" ;;
2035       lib)       mkdir -p "$testdir/$sourcebase" ;;
2036       m4)        mkdir -p "$testdir/$m4base" ;;
2037       tests)     mkdir -p "$testdir/$testsbase" ;;
2038       *)         mkdir -p "$testdir/$d" ;;
2039     esac
2040   done
2041
2042   # Copy files or make symbolic links.
2043   delimiter='   '
2044   sed_rewrite_files="\
2045     s,^build-aux/,$auxdir/,
2046     s,^doc/,$docbase/,
2047     s,^lib/,$sourcebase/,
2048     s,^m4/,$m4base/,
2049     s,^tests/,$testsbase/,"
2050   for f in $files; do echo $f; done \
2051     | sed -e "s,^.*\$,&$delimiter&," -e "$sed_rewrite_files" \
2052     | LC_ALL=C sort \
2053     > "$tmp"/files
2054   while read g f; do
2055     func_lookup_file "$f"
2056     if test -n "$lookedup_tmp"; then
2057       cp -p "$lookedup_file" "$testdir/$g"
2058     else
2059       ln "$lookedup_file" "$testdir/$g" 2>/dev/null ||
2060       if test -z "$symbolic"; then
2061         cp -p "$lookedup_file" "$testdir/$g"
2062       else
2063         ln -s "$lookedup_file" "$testdir/$g"
2064       fi
2065     fi
2066   done < "$tmp"/files
2067
2068   # Create $sourcebase/Makefile.am.
2069   mkdir -p "$testdir/$sourcebase"
2070   func_emit_lib_Makefile_am > "$testdir/$sourcebase/Makefile.am"
2071
2072   # Create $m4base/Makefile.am.
2073   mkdir -p "$testdir/$m4base"
2074   (echo "## Process this file with automake to produce Makefile.in."
2075    echo
2076    echo "EXTRA_DIST ="
2077    for f in $files; do
2078      case "$f" in
2079        m4/* )
2080          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
2081      esac
2082    done
2083   ) > "$testdir/$m4base/Makefile.am"
2084
2085   subdirs="$sourcebase $m4base"
2086   subdirs_with_configure_ac=""
2087
2088   if test -f "$testdir"/$m4base/gettext.m4; then
2089     # Avoid stupid error message from automake:
2090     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
2091     mkdir -p "$testdir/po"
2092     (echo "## Process this file with automake to produce Makefile.in."
2093     ) > "$testdir/po/Makefile.am"
2094     subdirs="$subdirs po"
2095   fi
2096
2097   if test -n "$inctests"; then
2098     test -d "$testdir/$testsbase" || mkdir "$testdir/$testsbase"
2099     # Create $testsbase/Makefile.am.
2100     func_emit_tests_Makefile_am > "$testdir/$testsbase/Makefile.am"
2101     # Create $testsbase/configure.ac.
2102     (echo "# Process this file with autoconf to produce a configure script."
2103      echo "AC_INIT([dummy], [0])"
2104      echo "AC_CONFIG_AUX_DIR([../$auxdir])"
2105      echo "AM_INIT_AUTOMAKE"
2106      echo
2107      echo "AM_CONFIG_HEADER([config.h])"
2108      echo
2109      echo "AC_PROG_CC"
2110      echo "AC_PROG_INSTALL"
2111      echo "AC_PROG_MAKE_SET"
2112      echo "AC_PROG_RANLIB"
2113      echo
2114      if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2115        echo "AC_GNU_SOURCE"
2116        echo
2117      fi
2118      if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2119        echo "gl_USE_SYSTEM_EXTENSIONS"
2120        echo
2121      fi
2122      if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2123        echo "gl_LOCK_EARLY"
2124        echo
2125      fi
2126      if test "$libtool" = true; then
2127        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2128        echo "gl_cond_libtool=true"
2129      else
2130        echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2131        echo "gl_cond_libtool=false"
2132        echo "gl_libdeps="
2133        echo "gl_ltlibdeps="
2134      fi
2135      # Wrap the set of autoconf snippets into an autoconf macro that is then
2136      # invoked. This is needed because autoconf does not support AC_REQUIRE
2137      # at the top level:
2138      #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2139      # but we want the AC_REQUIRE to have its normal meaning (provide one
2140      # expansion of the required macro before the current point, and only one
2141      # expansion total).
2142      echo "AC_DEFUN([gl_INIT], ["
2143      if test "$auxdir" != "build-aux"; then
2144        sed_replace_build_aux='
2145          :a
2146          /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2147            s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:../'"$auxdir"'/\2)|
2148            ba
2149          }'
2150        sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2151      else
2152        sed_replace_build_aux=
2153      fi
2154      # We don't have explicit ordering constraints between the various
2155      # autoconf snippets. It's cleanest to put those of the library before
2156      # those of the tests.
2157      for module in $modules; do
2158        func_verify_nontests_module
2159        if test -n "$module"; then
2160          func_get_autoconf_snippet "$module" \
2161            | sed -e "$sed_replace_build_aux"
2162        fi
2163      done
2164      for module in $modules; do
2165        func_verify_tests_module
2166        if test -n "$module"; then
2167          func_get_autoconf_snippet "$module" \
2168            | sed -e "$sed_replace_build_aux"
2169        fi
2170      done
2171      # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2172      # created using libtool, because libtool already handles the dependencies.
2173      if test "$libtool" != true; then
2174        libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2175        echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2176        echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2177        echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2178        echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2179      fi
2180      echo "])"
2181      echo "gl_INIT"
2182      echo
2183      # Usually $testsbase/config.h will be a superset of config.h. Verify this
2184      # by "merging" config.h into $testsbase/config.h; look out for gcc warnings.
2185      echo "AH_TOP([#include \"../config.h\"])"
2186      echo
2187      echo "AC_OUTPUT([Makefile])"
2188     ) > "$testdir/$testsbase/configure.ac"
2189     subdirs="$subdirs $testsbase"
2190     subdirs_with_configure_ac="$subdirs_with_configure_ac $testsbase"
2191   fi
2192
2193   # Create Makefile.am.
2194   (echo "## Process this file with automake to produce Makefile.in."
2195    echo
2196    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2197    echo
2198    echo "SUBDIRS = $subdirs"
2199    echo
2200    echo "ACLOCAL_AMFLAGS = -I $m4base"
2201   ) > "$testdir/Makefile.am"
2202
2203   # Create configure.ac.
2204   (echo "# Process this file with autoconf to produce a configure script."
2205    echo "AC_INIT([dummy], [0])"
2206    if test "$auxdir" != "."; then
2207      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2208    fi
2209    echo "AM_INIT_AUTOMAKE"
2210    echo
2211    echo "AM_CONFIG_HEADER([config.h])"
2212    echo
2213    echo "AC_PROG_CC"
2214    echo "AC_PROG_INSTALL"
2215    echo "AC_PROG_MAKE_SET"
2216    echo
2217    echo "m4_pattern_forbid([^gl_[A-Z]])dnl the gnulib macro namespace"
2218    echo "m4_pattern_allow([^gl_ES$])dnl a valid locale name"
2219    echo
2220    echo "AC_PROG_RANLIB"
2221    echo
2222    if grep AC_GNU_SOURCE "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2223      echo "AC_GNU_SOURCE"
2224      echo
2225    fi
2226    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2227      echo "gl_USE_SYSTEM_EXTENSIONS"
2228      echo
2229    fi
2230    if grep gl_LOCK "$testdir/$m4base"/*.m4 >/dev/null 2>/dev/null; then
2231      echo "gl_LOCK_EARLY"
2232      echo
2233    fi
2234    if test "$libtool" = true; then
2235      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [true])"
2236      echo "gl_cond_libtool=true"
2237    else
2238      echo "AM_CONDITIONAL([GL_COND_LIBTOOL], [false])"
2239      echo "gl_cond_libtool=false"
2240      echo "gl_libdeps="
2241      echo "gl_ltlibdeps="
2242    fi
2243    # Wrap the set of autoconf snippets into an autoconf macro that is then
2244    # invoked. This is needed because autoconf does not support AC_REQUIRE
2245    # at the top level:
2246    #   error: AC_REQUIRE(gt_CSHARPCOMP): cannot be used outside of an AC_DEFUN'd macro
2247    # but we want the AC_REQUIRE to have its normal meaning (provide one
2248    # expansion of the required macro before the current point, and only one
2249    # expansion total).
2250    echo "AC_DEFUN([gl_INIT], ["
2251    if test "$auxdir" != "build-aux"; then
2252      sed_replace_build_aux='
2253        :a
2254        /AC_CONFIG_FILES(.*:build-aux\/.*)/{
2255          s|AC_CONFIG_FILES(\(.*\):build-aux/\(.*\))|AC_CONFIG_FILES(\1:'"$auxdir"'/\2)|
2256          ba
2257        }'
2258      sed_replace_build_aux=`echo "$sed_replace_build_aux" | sed -e 1d -e 's/^ *//'`
2259    else
2260      sed_replace_build_aux=
2261    fi
2262    for module in $modules; do
2263      func_verify_nontests_module
2264      if test -n "$module"; then
2265        func_get_autoconf_snippet "$module" \
2266          | sed -e "$sed_replace_build_aux"
2267      fi
2268    done
2269    # _LIBDEPS and _LTLIBDEPS variables are not needed if this library is
2270    # created using libtool, because libtool already handles the dependencies.
2271    if test "$libtool" != true; then
2272      libname_upper=`echo "$libname" | tr 'a-z' 'A-Z'`
2273      echo "  ${libname_upper}_LIBDEPS=\"\$gl_libdeps\""
2274      echo "  AC_SUBST([${libname_upper}_LIBDEPS])"
2275      echo "  ${libname_upper}_LTLIBDEPS=\"\$gl_ltlibdeps\""
2276      echo "  AC_SUBST([${libname_upper}_LTLIBDEPS])"
2277    fi
2278    echo "])"
2279    echo "gl_INIT"
2280    echo
2281    if test -n "$subdirs_with_configure_ac"; then
2282      echo "AC_CONFIG_SUBDIRS(["`echo $subdirs_with_configure_ac`"])"
2283    fi
2284    makefiles="Makefile"
2285    for d in $subdirs; do
2286      # For subdirs that have a configure.ac by their own, it's the subdir's
2287      # configure.ac which creates the subdir's Makefile.am, not this one.
2288      case " $subdirs_with_configure_ac " in
2289        *" $d "*) ;;
2290        *) makefiles="$makefiles $d/Makefile" ;;
2291      esac
2292    done
2293    echo "AC_OUTPUT([$makefiles])"
2294   ) > "$testdir/configure.ac"
2295
2296   # Create autogenerated files.
2297   (cd "$testdir"
2298    echo "executing ${AUTORECONF} --force --install"
2299    ${AUTORECONF} --force --install
2300   )
2301   if grep '^BUILT_SOURCES *+=' "$testdir/$sourcebase/Makefile.am" > /dev/null; then
2302     (cd "$testdir"
2303      ./configure
2304        cd "$sourcebase"
2305        echo 'built_sources: $(BUILT_SOURCES)' >> Makefile
2306        make built_sources
2307        cd ..
2308      make distclean
2309     )
2310   fi
2311 }
2312
2313 # func_create_megatestdir megatestdir allmodules
2314 # Input:
2315 # - local_gnulib_dir  from --local-dir
2316 # - auxdir          directory relative to destdir where to place build aux files
2317 func_create_megatestdir ()
2318 {
2319   megatestdir="$1"
2320   allmodules="$2"
2321   if test -z "$allmodules"; then
2322     allmodules=`func_all_modules`
2323   fi
2324
2325   megasubdirs=
2326   # First, all modules one by one.
2327   for onemodule in $allmodules; do
2328     func_create_testdir "$megatestdir/$onemodule" $onemodule
2329     megasubdirs="${megasubdirs}$onemodule "
2330   done
2331   # Then, all modules all together.
2332   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
2333   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
2334   func_create_testdir "$megatestdir/ALL" "$allmodules"
2335   megasubdirs="${megasubdirs}ALL"
2336
2337   # Create Makefile.am.
2338   (echo "## Process this file with automake to produce Makefile.in."
2339    echo
2340    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
2341    echo
2342    echo "SUBDIRS = $megasubdirs"
2343   ) > "$megatestdir/Makefile.am"
2344
2345   # Create configure.ac.
2346   (echo "# Process this file with autoconf to produce a configure script."
2347    echo "AC_INIT([dummy], [0])"
2348    if test "$auxdir" != "."; then
2349      echo "AC_CONFIG_AUX_DIR([$auxdir])"
2350    fi
2351    echo "AM_INIT_AUTOMAKE"
2352    echo
2353    echo "AC_PROG_MAKE_SET"
2354    echo
2355    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
2356    echo "AC_OUTPUT([Makefile])"
2357   ) > "$megatestdir/configure.ac"
2358
2359   # Create autogenerated files.
2360   (cd "$megatestdir"
2361    # Do not use "${AUTORECONF} --install", because autoreconf operates
2362    # recursively, but the subdirectories are already finished, therefore
2363    # calling autoreconf here would only waste lots of CPU time.
2364    echo "executing ${ACLOCAL}"
2365    ${ACLOCAL}
2366    echo "executing mkdir build-aux"
2367    mkdir build-aux
2368    echo "executing ${AUTOCONF}"
2369    ${AUTOCONF}
2370    echo "executing ${AUTOMAKE} --add-missing --copy"
2371    ${AUTOMAKE} --add-missing --copy
2372   )
2373 }
2374
2375 case $mode in
2376   "" )
2377     func_fatal_error "no mode specified" ;;
2378
2379   list )
2380     func_all_modules
2381     ;;
2382
2383   import | update )
2384
2385     # Where to import.
2386     if test -z "$destdir"; then
2387       destdir=.
2388     fi
2389     test -d "$destdir" \
2390       || func_fatal_error "destination directory does not exist: $destdir"
2391
2392     # Prefer configure.ac to configure.in.
2393     if test -f "$destdir"/configure.ac; then
2394       configure_ac="$destdir/configure.ac"
2395     else
2396       if test -f "$destdir"/configure.in; then
2397         configure_ac="$destdir/configure.in"
2398       else
2399         func_fatal_error "cannot find $destdir/configure.ac"
2400       fi
2401     fi
2402
2403     # Analyze configure.ac.
2404     guessed_auxdir="."
2405     guessed_libtool=false
2406     my_sed_traces='
2407       s,#.*$,,
2408       s,^dnl .*$,,
2409       s, dnl .*$,,
2410       /AC_CONFIG_AUX_DIR/ {
2411         s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
2412       }
2413       /A[CM]_PROG_LIBTOOL/ {
2414         s,^.*$,guessed_libtool=true,p
2415       }'
2416     eval `sed -n -e "$my_sed_traces" < "$configure_ac"`
2417
2418     if test -z "$auxdir"; then
2419       auxdir="$guessed_auxdir"
2420     fi
2421
2422     # Determine where to apply func_import.
2423     if test -n "$m4base"; then
2424       # Apply func_import to a particular gnulib directory.
2425       # Any number of additional modules can be given.
2426       if test ! -f "$destdir/$m4base"/gnulib-cache.m4; then
2427         # First use of gnulib in the given m4base.
2428         test -n "$supplied_libname" || supplied_libname=true
2429         test -n "$sourcebase" || sourcebase="lib"
2430         test -n "$docbase" || docbase="doc"
2431         test -n "$testsbase" || testsbase="tests"
2432         test -n "$macro_prefix" || macro_prefix="gl"
2433       fi
2434       func_import "$*"
2435     else
2436       # Apply func_import to all gnulib directories.
2437       # To get this list of directories, look at Makefile.am. (Not at
2438       # configure, because it may be omitted from CVS. Also, don't run
2439       # "find $destdir -name gnulib-cache.m4", as it might be too expensive.)
2440       m4dirs=
2441       m4dirs_count=0
2442       if test -f "$destdir"/Makefile.am; then
2443         aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[         ]*=' "$destdir"/Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[       ]*=\(.*\)$/\1/'`
2444         m4dir_is_next=
2445         for arg in $aclocal_amflags; do
2446           if test -n "$m4dir_is_next"; then
2447             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
2448             case "$arg" in
2449               /*) ;;
2450               *)
2451                 if test -f "$destdir/$arg"/gnulib-cache.m4; then
2452                   m4dirs="$m4dirs $arg"
2453                   m4dirs_count=`expr $m4dirs_count + 1`
2454                 fi
2455                 ;;
2456             esac
2457           else
2458             if test "X$arg" = "X-I"; then
2459               m4dir_is_next=yes
2460             else
2461               m4dir_is_next=
2462             fi
2463           fi
2464         done
2465       else
2466         # No Makefile.am! Oh well. Look at the last generated aclocal.m4.
2467         if test -f "$destdir"/aclocal.m4; then
2468           sedexpr1='s,^m4_include(\[\(.*\)])$,\1,p'
2469           sedexpr2='s,^[^/]*$,.,'
2470           sedexpr3='s,/[^/]*$,,'
2471           m4dirs=`sed -n -e "$sedexpr1" aclocal.m4 | sed -e "$sedexpr2" -e "$sedexpr3" | LC_ALL=C sort -u`
2472           m4dirs_count=`echo "$m4dirs" | wc -l`
2473         fi
2474       fi
2475       if test $m4dirs_count = 0; then
2476         # First use of gnulib in a package.
2477         # Any number of additional modules can be given.
2478         test -n "$supplied_libname" || supplied_libname=true
2479         test -n "$sourcebase" || sourcebase="lib"
2480         m4base="m4"
2481         test -n "$docbase" || docbase="doc"
2482         test -n "$testsbase" || testsbase="tests"
2483         test -n "$macro_prefix" || macro_prefix="gl"
2484         func_import "$*"
2485       else
2486         if test $m4dirs_count = 1; then
2487           # There's only one use of gnulib here. Assume the user means it.
2488           # Any number of additional modules can be given.
2489           for m4base in $m4dirs; do
2490             func_import "$*"
2491           done
2492         else
2493           # Ambiguous - guess what the user meant.
2494           if test $# = 0; then
2495             # No further arguments. Guess the user wants to update all of them.
2496             for m4base in $m4dirs; do
2497               # Perform func_import in a subshell, so that variable values
2498               # such as
2499               #   local_gnulib_dir, avoidlist, sourcebase, m4base, docbase,
2500               #   testsbase, libname, lgpl, makefile_name, libtool, macro_prefix
2501               # don't propagate from one directory to another.
2502               (func_import) || exit 1
2503             done
2504           else
2505             # Really ambiguous.
2506             func_fatal_error "Ambiguity: to which directory should the modules be added? Please specify at least --m4-base=..."
2507           fi
2508         fi
2509       fi
2510     fi
2511     ;;
2512
2513   create-testdir )
2514     if test -z "$destdir"; then
2515       func_fatal_error "please specify --dir option"
2516     fi
2517     mkdir "$destdir"
2518     test -d "$destdir" \
2519       || func_fatal_error "could not create destination directory"
2520     test -n "$auxdir" || auxdir="build-aux"
2521     func_create_testdir "$destdir" "$*"
2522     ;;
2523
2524   create-megatestdir )
2525     if test -z "$destdir"; then
2526       func_fatal_error "please specify --dir option"
2527     fi
2528     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2529     test -n "$auxdir" || auxdir="build-aux"
2530     func_create_megatestdir "$destdir" "$*"
2531     ;;
2532
2533   test )
2534     test -n "$destdir" || destdir=testdir$$
2535     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2536     test -n "$auxdir" || auxdir="build-aux"
2537     func_create_testdir "$destdir" "$*"
2538     cd "$destdir"
2539       mkdir build
2540       cd build
2541         ../configure
2542         make
2543         make check
2544         make distclean
2545         remaining=`find . -type f -print`
2546         if test -n "$remaining"; then
2547           echo "Remaining files:" $remaining 1>&2
2548           echo "gnulib-tool: *** Stop." 1>&2
2549           exit 1
2550         fi
2551       cd ..
2552     cd ..
2553     rm -rf "$destdir"
2554     ;;
2555
2556   megatest )
2557     test -n "$destdir" || destdir=testdir$$
2558     mkdir "$destdir" || func_fatal_error "could not create destination directory"
2559     test -n "$auxdir" || auxdir="build-aux"
2560     func_create_megatestdir "$destdir" "$*"
2561     cd "$destdir"
2562       mkdir build
2563       cd build
2564         ../configure
2565         make
2566         make check
2567         make distclean
2568         remaining=`find . -type f -print`
2569         if test -n "$remaining"; then
2570           echo "Remaining files:" $remaining 1>&2
2571           echo "gnulib-tool: *** Stop." 1>&2
2572           exit 1
2573         fi
2574       cd ..
2575     cd ..
2576     rm -rf "$destdir"
2577     ;;
2578
2579   extract-description )
2580     for module
2581     do
2582       func_verify_module
2583       if test -n "$module"; then
2584         func_get_description "$module"
2585       fi
2586     done
2587     ;;
2588
2589   extract-filelist )
2590     for module
2591     do
2592       func_verify_module
2593       if test -n "$module"; then
2594         func_get_filelist "$module"
2595       fi
2596     done
2597     ;;
2598
2599   extract-dependencies )
2600     for module
2601     do
2602       func_verify_module
2603       if test -n "$module"; then
2604         func_get_dependencies "$module"
2605       fi
2606     done
2607     ;;
2608
2609   extract-autoconf-snippet )
2610     for module
2611     do
2612       func_verify_module
2613       if test -n "$module"; then
2614         func_get_autoconf_snippet "$module"
2615       fi
2616     done
2617     ;;
2618
2619   extract-automake-snippet )
2620     for module
2621     do
2622       func_verify_module
2623       if test -n "$module"; then
2624         func_get_automake_snippet "$module"
2625       fi
2626     done
2627     ;;
2628
2629   extract-include-directive )
2630     for module
2631     do
2632       func_verify_module
2633       if test -n "$module"; then
2634         func_get_include_directive "$module"
2635       fi
2636     done
2637     ;;
2638
2639   extract-license )
2640     for module
2641     do
2642       func_verify_module
2643       if test -n "$module"; then
2644         func_get_license "$module"
2645       fi
2646     done
2647     ;;
2648
2649   extract-maintainer )
2650     for module
2651     do
2652       func_verify_module
2653       if test -n "$module"; then
2654         func_get_maintainer "$module"
2655       fi
2656     done
2657     ;;
2658
2659   extract-tests-module )
2660     for module
2661     do
2662       func_verify_module
2663       if test -n "$module"; then
2664         func_get_tests_module "$module"
2665       fi
2666     done
2667     ;;
2668
2669   * )
2670     func_fatal_error "unknown operation mode --$mode" ;;
2671 esac
2672
2673 rm -rf "$tmp"
2674 # Undo the effect of the previous 'trap' command. Some shellology:
2675 # We cannot use "trap - 0 1 2 3 15", because Solaris sh would attempt to
2676 # execute the command "-". "trap '' ..." is fine only for signal 0 (= normal
2677 # exit); for the others we need to call 'exit' explicitly. The value of $? is
2678 # 128 + signal number and is set before the trap-registered command is run.
2679 trap '' 0
2680 trap 'exit $?' 1 2 3 15
2681
2682 exit 0
2683
2684 # Local Variables:
2685 # indent-tabs-mode: nil
2686 # whitespace-check-buffer-indent: nil
2687 # End: