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