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