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