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