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