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