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