build: let bootstrap resort to wget for downloading .po files
[gnulib.git] / build-aux / bootstrap
1 #! /bin/sh
2 # Print a version string.
3 scriptversion=2011-12-07.20; # UTC
4
5 # Bootstrap this package from checked-out sources.
6
7 # Copyright (C) 2003-2011 Free Software Foundation, Inc.
8
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 # Originally written by Paul Eggert.  The canonical version of this
23 # script is maintained as build-aux/bootstrap in gnulib, however, to
24 # be useful to your project, you should place a copy of it under
25 # version control in the top-level directory of your project.  The
26 # intent is that all customization can be done with a bootstrap.conf
27 # file also maintained in your version control; gnulib comes with a
28 # template build-aux/bootstrap.conf to get you started.
29
30 # Please report bugs or propose patches to bug-gnulib@gnu.org.
31
32 nl='
33 '
34
35 # Ensure file names are sorted consistently across platforms.
36 LC_ALL=C
37 export LC_ALL
38
39 local_gl_dir=gl
40
41 # Temporary directory names.
42 bt='._bootmp'
43 bt_regex=`echo "$bt"| sed 's/\./[.]/g'`
44 bt2=${bt}2
45 me=$0
46
47 usage() {
48   cat <<EOF
49 Usage: $me [OPTION]...
50 Bootstrap this package from the checked-out sources.
51
52 Options:
53  --gnulib-srcdir=DIRNAME  specify the local directory where gnulib
54                           sources reside.  Use this if you already
55                           have gnulib sources on your machine, and
56                           do not want to waste your bandwidth downloading
57                           them again.  Defaults to \$GNULIB_SRCDIR
58  --bootstrap-sync         if this bootstrap script is not identical to
59                           the version in the local gnulib sources,
60                           update this script, and then restart it with
61                           /bin/sh or the shell \$CONFIG_SHELL
62  --no-bootstrap-sync      do not check whether bootstrap is out of sync
63  --copy                   copy files instead of creating symbolic links
64  --force                  attempt to bootstrap even if the sources seem
65                           not to have been checked out
66  --no-git                 do not use git to update gnulib.  Requires that
67                           --gnulib-srcdir point to a correct gnulib snapshot
68  --skip-po                do not download po files
69
70 If the file $me.conf exists in the same directory as this script, its
71 contents are read as shell variables to configure the bootstrap.
72
73 For build prerequisites, environment variables like \$AUTOCONF and \$AMTAR
74 are honored.
75
76 Running without arguments will suffice in most cases.
77 EOF
78 }
79
80 # Configuration.
81
82 # Name of the Makefile.am
83 gnulib_mk=gnulib.mk
84
85 # List of gnulib modules needed.
86 gnulib_modules=
87
88 # Any gnulib files needed that are not in modules.
89 gnulib_files=
90
91 # A function to be called to edit gnulib.mk right after it's created.
92 # Override it via your own definition in bootstrap.conf.
93 gnulib_mk_hook() { :; }
94
95 # A function to be called after everything else in this script.
96 # Override it via your own definition in bootstrap.conf.
97 bootstrap_epilogue() { :; }
98
99 # The command to download all .po files for a specified domain into
100 # a specified directory.  Fill in the first %s is the domain name, and
101 # the second with the destination directory.  Use rsync's -L and -r
102 # options because the latest/%s directory and the .po files within are
103 # all symlinks.
104 po_download_command_format=\
105 "rsync --delete --exclude '*.s1' -Lrtvz \
106  'translationproject.org::tp/latest/%s/' '%s'"
107
108 # Fallback for downloading .po files (if rsync fails).
109 po_download_command_format2=\
110 "wget --mirror -nd -q -np -A.po -P '%s' \
111  http://translationproject.org/latest/%s/"
112
113 extract_package_name='
114   /^AC_INIT(/{
115      /.*,.*,.*, */{
116        s///
117        s/[][]//g
118        s/)$//
119        p
120        q
121      }
122      s/AC_INIT(\[*//
123      s/]*,.*//
124      s/^GNU //
125      y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
126      s/[^A-Za-z0-9_]/-/g
127      p
128   }
129 '
130 package=`sed -n "$extract_package_name" configure.ac` || exit
131 gnulib_name=lib$package
132
133 build_aux=build-aux
134 source_base=lib
135 m4_base=m4
136 doc_base=doc
137 tests_base=tests
138 gnulib_extra_files=''
139
140 # Additional gnulib-tool options to use.  Use "\newline" to break lines.
141 gnulib_tool_option_extras=
142
143 # Other locale categories that need message catalogs.
144 EXTRA_LOCALE_CATEGORIES=
145
146 # Additional xgettext options to use.  Use "\\\newline" to break lines.
147 XGETTEXT_OPTIONS='\\\
148  --flag=_:1:pass-c-format\\\
149  --flag=N_:1:pass-c-format\\\
150  --flag=error:3:c-format --flag=error_at_line:5:c-format\\\
151 '
152
153 # Package bug report address and copyright holder for gettext files
154 COPYRIGHT_HOLDER='Free Software Foundation, Inc.'
155 MSGID_BUGS_ADDRESS=bug-$package@gnu.org
156
157 # Files we don't want to import.
158 excluded_files=
159
160 # File that should exist in the top directory of a checked out hierarchy,
161 # but not in a distribution tarball.
162 checkout_only_file=README-hacking
163
164 # Whether to use copies instead of symlinks.
165 copy=false
166
167 # Set this to '.cvsignore .gitignore' in bootstrap.conf if you want
168 # those files to be generated in directories like lib/, m4/, and po/.
169 # Or set it to 'auto' to make this script select which to use based
170 # on which version control system (if any) is used in the source directory.
171 vc_ignore=auto
172
173 # Set this to true in bootstrap.conf to enable --bootstrap-sync by
174 # default.
175 bootstrap_sync=false
176
177 # Use git to update gnulib sources
178 use_git=true
179
180 # find_tool ENVVAR NAMES...
181 # -------------------------
182 # Search for a required program.  Use the value of ENVVAR, if set,
183 # otherwise find the first of the NAMES that can be run (i.e.,
184 # supports --version).  If found, set ENVVAR to the program name,
185 # die otherwise.
186 find_tool ()
187 {
188   find_tool_envvar=$1
189   shift
190   find_tool_names=$@
191   eval "find_tool_res=\$$find_tool_envvar"
192   if test x"$find_tool_res" = x; then
193     for i
194     do
195       if ($i --version </dev/null) >/dev/null 2>&1; then
196        find_tool_res=$i
197        break
198       fi
199     done
200   else
201     find_tool_error_prefix="\$$find_tool_envvar: "
202   fi
203   if test x"$find_tool_res" = x; then
204     echo >&2 "$me: one of these is required: $find_tool_names"
205     exit 1
206   fi
207   ($find_tool_res --version </dev/null) >/dev/null 2>&1 || {
208     echo >&2 "$me: ${find_tool_error_prefix}cannot run $find_tool_res --version"
209     exit 1
210   }
211   eval "$find_tool_envvar=\$find_tool_res"
212   eval "export $find_tool_envvar"
213 }
214
215 # Find sha1sum, named gsha1sum on MacPorts, and shasum on MacOS 10.6.
216 find_tool SHA1SUM sha1sum gsha1sum shasum
217
218 # Override the default configuration, if necessary.
219 # Make sure that bootstrap.conf is sourced from the current directory
220 # if we were invoked as "sh bootstrap".
221 case "$0" in
222   */*) test -r "$0.conf" && . "$0.conf" ;;
223   *) test -r "$0.conf" && . ./"$0.conf" ;;
224 esac
225
226 # Extra files from gnulib, which override files from other sources.
227 test -z "${gnulib_extra_files}" && \
228   gnulib_extra_files="
229         $build_aux/install-sh
230         $build_aux/missing
231         $build_aux/mdate-sh
232         $build_aux/texinfo.tex
233         $build_aux/depcomp
234         $build_aux/config.guess
235         $build_aux/config.sub
236         doc/INSTALL
237 "
238
239 if test "$vc_ignore" = auto; then
240   vc_ignore=
241   test -d .git && vc_ignore=.gitignore
242   test -d CVS && vc_ignore="$vc_ignore .cvsignore"
243 fi
244
245 # Translate configuration into internal form.
246
247 # Parse options.
248
249 for option
250 do
251   case $option in
252   --help)
253     usage
254     exit;;
255   --gnulib-srcdir=*)
256     GNULIB_SRCDIR=`expr "X$option" : 'X--gnulib-srcdir=\(.*\)'`;;
257   --skip-po)
258     SKIP_PO=t;;
259   --force)
260     checkout_only_file=;;
261   --copy)
262     copy=true;;
263   --bootstrap-sync)
264     bootstrap_sync=true;;
265   --no-bootstrap-sync)
266     bootstrap_sync=false;;
267   --no-git)
268     use_git=false;;
269   *)
270     echo >&2 "$0: $option: unknown option"
271     exit 1;;
272   esac
273 done
274
275 if $use_git || test -d "$GNULIB_SRCDIR"; then
276   :
277 else
278   echo "$0: Error: --no-git requires --gnulib-srcdir" >&2
279   exit 1
280 fi
281
282 if test -n "$checkout_only_file" && test ! -r "$checkout_only_file"; then
283   echo "$0: Bootstrapping from a non-checked-out distribution is risky." >&2
284   exit 1
285 fi
286
287 # Ensure that lines starting with ! sort last, per gitignore conventions
288 # for whitelisting exceptions after a more generic blacklist pattern.
289 sort_patterns() {
290   sort -u "$@" | sed '/^!/ {
291     H
292     d
293   }
294   $ {
295     P
296     x
297     s/^\n//
298   }' | sed '/^$/d'
299 }
300
301 # If $STR is not already on a line by itself in $FILE, insert it,
302 # sorting the new contents of the file and replacing $FILE with the result.
303 insert_sorted_if_absent() {
304   file=$1
305   str=$2
306   test -f $file || touch $file
307   echo "$str" | sort_patterns - $file | cmp - $file > /dev/null \
308     || { echo "$str" | sort_patterns - $file > $file.bak \
309       && mv $file.bak $file; } \
310     || exit 1
311 }
312
313 # Adjust $PATTERN for $VC_IGNORE_FILE and insert it with
314 # insert_sorted_if_absent.
315 insert_vc_ignore() {
316   vc_ignore_file="$1"
317   pattern="$2"
318   case $vc_ignore_file in
319   *.gitignore)
320     # A .gitignore entry that does not start with `/' applies
321     # recursively to subdirectories, so prepend `/' to every
322     # .gitignore entry.
323     pattern=`echo "$pattern" | sed s,^,/,`;;
324   esac
325   insert_sorted_if_absent "$vc_ignore_file" "$pattern"
326 }
327
328 # Die if there is no AC_CONFIG_AUX_DIR($build_aux) line in configure.ac.
329 found_aux_dir=no
330 grep '^[         ]*AC_CONFIG_AUX_DIR(\['"$build_aux"'\])' configure.ac \
331     >/dev/null && found_aux_dir=yes
332 grep '^[         ]*AC_CONFIG_AUX_DIR('"$build_aux"')' configure.ac \
333     >/dev/null && found_aux_dir=yes
334 if test $found_aux_dir = no; then
335   echo "$0: expected line not found in configure.ac. Add the following:" >&2
336   echo "  AC_CONFIG_AUX_DIR([$build_aux])" >&2
337   exit 1
338 fi
339
340 # If $build_aux doesn't exist, create it now, otherwise some bits
341 # below will malfunction.  If creating it, also mark it as ignored.
342 if test ! -d $build_aux; then
343   mkdir $build_aux
344   for dot_ig in x $vc_ignore; do
345     test $dot_ig = x && continue
346     insert_vc_ignore $dot_ig $build_aux
347   done
348 fi
349
350 # Note this deviates from the version comparison in automake
351 # in that it treats 1.5 < 1.5.0, and treats 1.4.4a < 1.4-p3a
352 # but this should suffice as we won't be specifying old
353 # version formats or redundant trailing .0 in bootstrap.conf.
354 # If we did want full compatibility then we should probably
355 # use m4_version_compare from autoconf.
356 sort_ver() { # sort -V is not generally available
357   ver1="$1"
358   ver2="$2"
359
360   # split on '.' and compare each component
361   i=1
362   while : ; do
363     p1=$(echo "$ver1" | cut -d. -f$i)
364     p2=$(echo "$ver2" | cut -d. -f$i)
365     if [ ! "$p1" ]; then
366       echo "$1 $2"
367       break
368     elif [ ! "$p2" ]; then
369       echo "$2 $1"
370       break
371     elif [ ! "$p1" = "$p2" ]; then
372       if [ "$p1" -gt "$p2" ] 2>/dev/null; then # numeric comparison
373         echo "$2 $1"
374       elif [ "$p2" -gt "$p1" ] 2>/dev/null; then # numeric comparison
375         echo "$1 $2"
376       else # numeric, then lexicographic comparison
377         lp=$(printf "$p1\n$p2\n" | LANG=C sort -n | tail -n1)
378         if [ "$lp" = "$p2" ]; then
379           echo "$1 $2"
380         else
381           echo "$2 $1"
382         fi
383       fi
384       break
385     fi
386     i=$(($i+1))
387   done
388 }
389
390 get_version() {
391   app=$1
392
393   $app --version >/dev/null 2>&1 || return 1
394
395   $app --version 2>&1 |
396   sed -n '# Move version to start of line.
397           s/.*[v ]\([0-9]\)/\1/
398
399           # Skip lines that do not start with version.
400           /^[0-9]/!d
401
402           # Remove characters after the version.
403           s/[^.a-z0-9-].*//
404
405           # The first component must be digits only.
406           s/^\([0-9]*\)[a-z-].*/\1/
407
408           #the following essentially does s/5.005/5.5/
409           s/\.0*\([1-9]\)/.\1/g
410           p
411           q'
412 }
413
414 check_versions() {
415   ret=0
416
417   while read app req_ver; do
418     # We only need libtoolize from the libtool package.
419     if test "$app" = libtool; then
420       app=libtoolize
421     fi
422     # Exempt git if --no-git is in effect.
423     if test "$app" = git; then
424       $use_git || continue
425     fi
426     # Honor $APP variables ($TAR, $AUTOCONF, etc.)
427     appvar=`echo $app | tr '[a-z]-' '[A-Z]_'`
428     test "$appvar" = TAR && appvar=AMTAR
429     case $appvar in
430         GZIP) ;; # Do not use $GZIP:  it contains gzip options.
431         *) eval "app=\${$appvar-$app}" ;;
432     esac
433     if [ "$req_ver" = "-" ]; then
434       # Merely require app to exist; not all prereq apps are well-behaved
435       # so we have to rely on $? rather than get_version.
436       $app --version >/dev/null 2>&1
437       if [ 126 -le $? ]; then
438         echo "$me: Error: '$app' not found" >&2
439         ret=1
440       fi
441     else
442       # Require app to produce a new enough version string.
443       inst_ver=$(get_version $app)
444       if [ ! "$inst_ver" ]; then
445         echo "$me: Error: '$app' not found" >&2
446         ret=1
447       else
448         latest_ver=$(sort_ver $req_ver $inst_ver | cut -d' ' -f2)
449         if [ ! "$latest_ver" = "$inst_ver" ]; then
450           echo "$me: Error: '$app' version == $inst_ver is too old" >&2
451           echo "       '$app' version >= $req_ver is required" >&2
452           ret=1
453         fi
454       fi
455     fi
456   done
457
458   return $ret
459 }
460
461 print_versions() {
462   echo "Program    Min_version"
463   echo "----------------------"
464   printf %s "$buildreq"
465   echo "----------------------"
466   # can't depend on column -t
467 }
468
469 use_libtool=0
470 # We'd like to use grep -E, to see if any of LT_INIT,
471 # AC_PROG_LIBTOOL, AM_PROG_LIBTOOL is used in configure.ac,
472 # but that's not portable enough (e.g., for Solaris).
473 grep '^[         ]*A[CM]_PROG_LIBTOOL' configure.ac >/dev/null \
474   && use_libtool=1
475 grep '^[         ]*LT_INIT' configure.ac >/dev/null \
476   && use_libtool=1
477 if test $use_libtool = 1; then
478   find_tool LIBTOOLIZE glibtoolize libtoolize
479 fi
480
481 case $buildreq in
482   automake) ;;
483   *) buildreq="automake 1.9
484 $buildreq" ;;
485 esac
486 case $buildreq in
487   autoconf) ;;
488   *) buildreq="autoconf 2.59
489 $buildreq" ;;
490 esac
491 if test ! -d "$local_gl_dir" \
492     || find "$local_gl_dir" -name '*.diff' -exec false {} +; then
493   :
494 else
495   case $buildreq in
496     patch) ;;
497     *) buildreq="patch -
498 $buildreq" ;;
499   esac
500 fi
501 if ! printf "$buildreq" | check_versions; then
502   echo >&2
503   if test -f README-prereq; then
504     echo "$0: See README-prereq for how to get the prerequisite programs" >&2
505   else
506     echo "$0: Please install the prerequisite programs" >&2
507   fi
508   exit 1
509 fi
510
511 echo "$0: Bootstrapping from checked-out $package sources..."
512
513 # See if we can use gnulib's git-merge-changelog merge driver.
514 if test -d .git && (git --version) >/dev/null 2>/dev/null ; then
515   if git config merge.merge-changelog.driver >/dev/null ; then
516     :
517   elif (git-merge-changelog --version) >/dev/null 2>/dev/null ; then
518     echo "$0: initializing git-merge-changelog driver"
519     git config merge.merge-changelog.name 'GNU-style ChangeLog merge driver'
520     git config merge.merge-changelog.driver 'git-merge-changelog %O %A %B'
521   else
522     echo "$0: consider installing git-merge-changelog from gnulib"
523   fi
524 fi
525
526
527 cleanup_gnulib() {
528   status=$?
529   rm -fr "$gnulib_path"
530   exit $status
531 }
532
533 git_modules_config () {
534   test -f .gitmodules && git config --file .gitmodules "$@"
535 }
536
537 gnulib_path=`git_modules_config submodule.gnulib.path`
538 test -z "$gnulib_path" && gnulib_path=gnulib
539
540 # Get gnulib files.
541
542 case ${GNULIB_SRCDIR--} in
543 -)
544   if git_modules_config submodule.gnulib.url >/dev/null; then
545     echo "$0: getting gnulib files..."
546     git submodule init || exit $?
547     git submodule update || exit $?
548
549   elif [ ! -d "$gnulib_path" ]; then
550     echo "$0: getting gnulib files..."
551
552     trap cleanup_gnulib 1 2 13 15
553
554     shallow=
555     git clone -h 2>&1 | grep -- --depth > /dev/null && shallow='--depth 2'
556     git clone $shallow git://git.sv.gnu.org/gnulib "$gnulib_path" ||
557       cleanup_gnulib
558
559     trap - 1 2 13 15
560   fi
561   GNULIB_SRCDIR=$gnulib_path
562   ;;
563 *)
564   # Use GNULIB_SRCDIR as a reference.
565   if test -d "$GNULIB_SRCDIR"/.git && \
566         git_modules_config submodule.gnulib.url >/dev/null; then
567     echo "$0: getting gnulib files..."
568     if git submodule -h|grep -- --reference > /dev/null; then
569       # Prefer the one-liner available in git 1.6.4 or newer.
570       git submodule update --init --reference "$GNULIB_SRCDIR" \
571         "$gnulib_path" || exit $?
572     else
573       # This fallback allows at least git 1.5.5.
574       if test -f "$gnulib_path"/gnulib-tool; then
575         # Since file already exists, assume submodule init already complete.
576         git submodule update || exit $?
577       else
578         # Older git can't clone into an empty directory.
579         rmdir "$gnulib_path" 2>/dev/null
580         git clone --reference "$GNULIB_SRCDIR" \
581           "$(git_modules_config submodule.gnulib.url)" "$gnulib_path" \
582           && git submodule init && git submodule update \
583           || exit $?
584       fi
585     fi
586     GNULIB_SRCDIR=$gnulib_path
587   fi
588   ;;
589 esac
590
591 if $bootstrap_sync; then
592   cmp -s "$0" "$GNULIB_SRCDIR/build-aux/bootstrap" || {
593     echo "$0: updating bootstrap and restarting..."
594     exec sh -c \
595       'cp "$1" "$2" && shift && exec "${CONFIG_SHELL-/bin/sh}" "$@"' \
596       -- "$GNULIB_SRCDIR/build-aux/bootstrap" \
597       "$0" "$@" --no-bootstrap-sync
598   }
599 fi
600
601 gnulib_tool=$GNULIB_SRCDIR/gnulib-tool
602 <$gnulib_tool || exit
603
604 # Get translations.
605
606 download_po_files() {
607   subdir=$1
608   domain=$2
609   echo "$me: getting translations into $subdir for $domain..."
610   cmd=`printf "$po_download_command_format" "$domain" "$subdir"`
611   eval "$cmd" && return
612   # Fallback to HTTP.
613   cmd=`printf "$po_download_command_format2" "$subdir" "$domain"`
614   eval "$cmd"
615 }
616
617 # Mirror .po files to $po_dir/.reference and copy only the new
618 # or modified ones into $po_dir.  Also update $po_dir/LINGUAS.
619 # Note po files that exist locally only are left in $po_dir but will
620 # not be included in LINGUAS and hence will not be distributed.
621 update_po_files() {
622   # Directory containing primary .po files.
623   # Overwrite them only when we're sure a .po file is new.
624   po_dir=$1
625   domain=$2
626
627   # Mirror *.po files into this dir.
628   # Usually contains *.s1 checksum files.
629   ref_po_dir="$po_dir/.reference"
630
631   test -d $ref_po_dir || mkdir $ref_po_dir || return
632   download_po_files $ref_po_dir $domain \
633     && ls "$ref_po_dir"/*.po 2>/dev/null |
634       sed 's|.*/||; s|\.po$||' > "$po_dir/LINGUAS" || return
635
636   langs=`cd $ref_po_dir && echo *.po|sed 's/\.po//g'`
637   test "$langs" = '*' && langs=x
638   for po in $langs; do
639     case $po in x) continue;; esac
640     new_po="$ref_po_dir/$po.po"
641     cksum_file="$ref_po_dir/$po.s1"
642     if ! test -f "$cksum_file" ||
643         ! test -f "$po_dir/$po.po" ||
644         ! $SHA1SUM -c --status "$cksum_file" \
645             < "$new_po" > /dev/null; then
646       echo "$me: updated $po_dir/$po.po..."
647       cp "$new_po" "$po_dir/$po.po" \
648           && $SHA1SUM < "$new_po" > "$cksum_file"
649     fi
650   done
651 }
652
653 case $SKIP_PO in
654 '')
655   if test -d po; then
656     update_po_files po $package || exit
657   fi
658
659   if test -d runtime-po; then
660     update_po_files runtime-po $package-runtime || exit
661   fi;;
662 esac
663
664 symlink_to_dir()
665 {
666   src=$1/$2
667   dst=${3-$2}
668
669   test -f "$src" && {
670
671     # If the destination directory doesn't exist, create it.
672     # This is required at least for "lib/uniwidth/cjk.h".
673     dst_dir=`dirname "$dst"`
674     if ! test -d "$dst_dir"; then
675       mkdir -p "$dst_dir"
676
677       # If we've just created a directory like lib/uniwidth,
678       # tell version control system(s) it's ignorable.
679       # FIXME: for now, this does only one level
680       parent=`dirname "$dst_dir"`
681       for dot_ig in x $vc_ignore; do
682         test $dot_ig = x && continue
683         ig=$parent/$dot_ig
684         insert_vc_ignore $ig `echo "$dst_dir"|sed 's,.*/,,'`
685       done
686     fi
687
688     if $copy; then
689       {
690         test ! -h "$dst" || {
691           echo "$me: rm -f $dst" &&
692           rm -f "$dst"
693         }
694       } &&
695       test -f "$dst" &&
696       cmp -s "$src" "$dst" || {
697         echo "$me: cp -fp $src $dst" &&
698         cp -fp "$src" "$dst"
699       }
700     else
701       # Leave any existing symlink alone, if it already points to the source,
702       # so that broken build tools that care about symlink times
703       # aren't confused into doing unnecessary builds.  Conversely, if the
704       # existing symlink's time stamp is older than the source, make it afresh,
705       # so that broken tools aren't confused into skipping needed builds.  See
706       # <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00326.html>.
707       test -h "$dst" &&
708       src_ls=`ls -diL "$src" 2>/dev/null` && set $src_ls && src_i=$1 &&
709       dst_ls=`ls -diL "$dst" 2>/dev/null` && set $dst_ls && dst_i=$1 &&
710       test "$src_i" = "$dst_i" &&
711       both_ls=`ls -dt "$src" "$dst"` &&
712       test "X$both_ls" = "X$dst$nl$src" || {
713         dot_dots=
714         case $src in
715         /*) ;;
716         *)
717           case /$dst/ in
718           *//* | */../* | */./* | /*/*/*/*/*/)
719              echo >&2 "$me: invalid symlink calculation: $src -> $dst"
720              exit 1;;
721           /*/*/*/*/)    dot_dots=../../../;;
722           /*/*/*/)      dot_dots=../../;;
723           /*/*/)        dot_dots=../;;
724           esac;;
725         esac
726
727         echo "$me: ln -fs $dot_dots$src $dst" &&
728         ln -fs "$dot_dots$src" "$dst"
729       }
730     fi
731   }
732 }
733
734 cp_mark_as_generated()
735 {
736   cp_src=$1
737   cp_dst=$2
738
739   if cmp -s "$cp_src" "$GNULIB_SRCDIR/$cp_dst"; then
740     symlink_to_dir "$GNULIB_SRCDIR" "$cp_dst"
741   elif cmp -s "$cp_src" "$local_gl_dir/$cp_dst"; then
742     symlink_to_dir $local_gl_dir "$cp_dst"
743   else
744     case $cp_dst in
745       *.[ch])             c1='/* '; c2=' */';;
746       *.texi)             c1='@c '; c2=     ;;
747       *.m4|*/Make*|Make*) c1='# ' ; c2=     ;;
748       *)                  c1=     ; c2=     ;;
749     esac
750
751     # If the destination directory doesn't exist, create it.
752     # This is required at least for "lib/uniwidth/cjk.h".
753     dst_dir=`dirname "$cp_dst"`
754     test -d "$dst_dir" || mkdir -p "$dst_dir"
755
756     if test -z "$c1"; then
757       cmp -s "$cp_src" "$cp_dst" || {
758         # Copy the file first to get proper permissions if it
759         # doesn't already exist.  Then overwrite the copy.
760         echo "$me: cp -f $cp_src $cp_dst" &&
761         rm -f "$cp_dst" &&
762         cp "$cp_src" "$cp_dst-t" &&
763         sed "s!$bt_regex/!!g" "$cp_src" > "$cp_dst-t" &&
764         mv -f "$cp_dst-t" "$cp_dst"
765       }
766     else
767       # Copy the file first to get proper permissions if it
768       # doesn't already exist.  Then overwrite the copy.
769       cp "$cp_src" "$cp_dst-t" &&
770       (
771         echo "$c1-*- buffer-read-only: t -*- vi: set ro:$c2" &&
772         echo "${c1}DO NOT EDIT! GENERATED AUTOMATICALLY!$c2" &&
773         sed "s!$bt_regex/!!g" "$cp_src"
774       ) > $cp_dst-t &&
775       if cmp -s "$cp_dst-t" "$cp_dst"; then
776         rm -f "$cp_dst-t"
777       else
778         echo "$me: cp $cp_src $cp_dst # with edits" &&
779         mv -f "$cp_dst-t" "$cp_dst"
780       fi
781     fi
782   fi
783 }
784
785 version_controlled_file() {
786   dir=$1
787   file=$2
788   found=no
789   if test -d CVS; then
790     grep -F "/$file/" $dir/CVS/Entries 2>/dev/null |
791              grep '^/[^/]*/[0-9]' > /dev/null && found=yes
792   elif test -d .git; then
793     git rm -n "$dir/$file" > /dev/null 2>&1 && found=yes
794   elif test -d .svn; then
795     svn log -r HEAD "$dir/$file" > /dev/null 2>&1 && found=yes
796   else
797     echo "$me: no version control for $dir/$file?" >&2
798   fi
799   test $found = yes
800 }
801
802 slurp() {
803   for dir in . `(cd $1 && find * -type d -print)`; do
804     copied=
805     sep=
806     for file in `ls -a $1/$dir`; do
807       case $file in
808       .|..) continue;;
809       # FIXME: should all file names starting with "." be ignored?
810       .*) continue;;
811       esac
812       test -d $1/$dir/$file && continue
813       for excluded_file in $excluded_files; do
814         test "$dir/$file" = "$excluded_file" && continue 2
815       done
816       if test $file = Makefile.am && test "X$gnulib_mk" != XMakefile.am; then
817         copied=$copied${sep}$gnulib_mk; sep=$nl
818         remove_intl='/^[^#].*\/intl/s/^/#/;'"s!$bt_regex/!!g"
819         sed "$remove_intl" $1/$dir/$file |
820         cmp - $dir/$gnulib_mk > /dev/null || {
821           echo "$me: Copying $1/$dir/$file to $dir/$gnulib_mk ..." &&
822           rm -f $dir/$gnulib_mk &&
823           sed "$remove_intl" $1/$dir/$file >$dir/$gnulib_mk &&
824           gnulib_mk_hook $dir/$gnulib_mk
825         }
826       elif { test "${2+set}" = set && test -r $2/$dir/$file; } ||
827            version_controlled_file $dir $file; then
828         echo "$me: $dir/$file overrides $1/$dir/$file"
829       else
830         copied=$copied$sep$file; sep=$nl
831         cp_mark_as_generated $1/$dir/$file $dir/$file
832       fi || exit
833     done
834
835     for dot_ig in x $vc_ignore; do
836       test $dot_ig = x && continue
837       ig=$dir/$dot_ig
838       if test -n "$copied"; then
839         insert_vc_ignore $ig "$copied"
840         # If an ignored file name ends with .in.h, then also add
841         # the name with just ".h".  Many gnulib headers are generated,
842         # e.g., stdint.in.h -> stdint.h, dirent.in.h ->..., etc.
843         # Likewise for .gperf -> .h, .y -> .c, and .sin -> .sed
844         f=`echo "$copied" |
845           sed '
846             s/\.in\.h$/.h/
847             s/\.sin$/.sed/
848             s/\.y$/.c/
849             s/\.gperf$/.h/
850           '
851         `
852         insert_vc_ignore $ig "$f"
853
854         # For files like sys_stat.in.h and sys_time.in.h, record as
855         # ignorable the directory we might eventually create: sys/.
856         f=`echo "$copied"|sed 's/sys_.*\.in\.h$/sys/'`
857         insert_vc_ignore $ig "$f"
858       fi
859     done
860   done
861 }
862
863
864 # Create boot temporary directories to import from gnulib and gettext.
865 rm -fr $bt $bt2 &&
866 mkdir $bt $bt2 || exit
867
868 # Import from gnulib.
869
870 gnulib_tool_options="\
871  --import\
872  --no-changelog\
873  --aux-dir $bt/$build_aux\
874  --doc-base $bt/$doc_base\
875  --lib $gnulib_name\
876  --m4-base $bt/$m4_base/\
877  --source-base $bt/$source_base/\
878  --tests-base $bt/$tests_base\
879  --local-dir $local_gl_dir\
880  $gnulib_tool_option_extras\
881 "
882 if test $use_libtool = 1; then
883   case "$gnulib_tool_options " in
884     *' --libtool '*) ;;
885     *) gnulib_tool_options="$gnulib_tool_options --libtool" ;;
886   esac
887 fi
888 echo "$0: $gnulib_tool $gnulib_tool_options --import ..."
889 $gnulib_tool $gnulib_tool_options --import $gnulib_modules &&
890 slurp $bt || exit
891
892 for file in $gnulib_files; do
893   symlink_to_dir "$GNULIB_SRCDIR" $file || exit
894 done
895
896
897 # Import from gettext.
898 with_gettext=yes
899 grep '^[         ]*AM_GNU_GETTEXT_VERSION(' configure.ac >/dev/null || \
900     with_gettext=no
901
902 if test $with_gettext = yes; then
903   echo "$0: (cd $bt2; ${AUTOPOINT-autopoint}) ..."
904   cp configure.ac $bt2 &&
905   (cd $bt2 && ${AUTOPOINT-autopoint} && rm configure.ac) &&
906   slurp $bt2 $bt || exit
907 fi
908 rm -fr $bt $bt2 || exit
909
910 # Remove any dangling symlink matching "*.m4" or "*.[ch]" in some
911 # gnulib-populated directories.  Such .m4 files would cause aclocal to fail.
912 # The following requires GNU find 4.2.3 or newer.  Considering the usual
913 # portability constraints of this script, that may seem a very demanding
914 # requirement, but it should be ok.  Ignore any failure, which is fine,
915 # since this is only a convenience to help developers avoid the relatively
916 # unusual case in which a symlinked-to .m4 file is git-removed from gnulib
917 # between successive runs of this script.
918 find "$m4_base" "$source_base" \
919   -depth \( -name '*.m4' -o -name '*.[ch]' \) \
920   -type l -xtype l -delete > /dev/null 2>&1
921
922 # Reconfigure, getting other files.
923
924 # Skip autoheader if it's not needed.
925 grep -E '^[      ]*AC_CONFIG_HEADERS?\>' configure.ac >/dev/null ||
926   AUTOHEADER=true
927
928 for command in \
929   libtool \
930   "${ACLOCAL-aclocal} --force -I '$m4_base' $ACLOCAL_FLAGS" \
931   "${AUTOCONF-autoconf} --force" \
932   "${AUTOHEADER-autoheader} --force" \
933   "${AUTOMAKE-automake} --add-missing --copy --force-missing"
934 do
935   if test "$command" = libtool; then
936     test $use_libtool = 0 \
937       && continue
938     command="${LIBTOOLIZE-libtoolize} -c -f"
939   fi
940   echo "$0: $command ..."
941   eval "$command" || exit
942 done
943
944
945 # Get some extra files from gnulib, overriding existing files.
946 for file in $gnulib_extra_files; do
947   case $file in
948   */INSTALL) dst=INSTALL;;
949   build-aux/*) dst=$build_aux/`expr "$file" : 'build-aux/\(.*\)'`;;
950   *) dst=$file;;
951   esac
952   symlink_to_dir "$GNULIB_SRCDIR" $file $dst || exit
953 done
954
955 if test $with_gettext = yes; then
956   # Create gettext configuration.
957   echo "$0: Creating po/Makevars from po/Makevars.template ..."
958   rm -f po/Makevars
959   sed '
960     /^EXTRA_LOCALE_CATEGORIES *=/s/=.*/= '"$EXTRA_LOCALE_CATEGORIES"'/
961     /^COPYRIGHT_HOLDER *=/s/=.*/= '"$COPYRIGHT_HOLDER"'/
962     /^MSGID_BUGS_ADDRESS *=/s|=.*|= '"$MSGID_BUGS_ADDRESS"'|
963     /^XGETTEXT_OPTIONS *=/{
964       s/$/ \\/
965       a\
966           '"$XGETTEXT_OPTIONS"' $${end_of_xgettext_options+}
967     }
968   ' po/Makevars.template >po/Makevars || exit 1
969
970   if test -d runtime-po; then
971     # Similarly for runtime-po/Makevars, but not quite the same.
972     rm -f runtime-po/Makevars
973     sed '
974       /^DOMAIN *=.*/s/=.*/= '"$package"'-runtime/
975       /^subdir *=.*/s/=.*/= runtime-po/
976       /^MSGID_BUGS_ADDRESS *=/s/=.*/= bug-'"$package"'@gnu.org/
977       /^XGETTEXT_OPTIONS *=/{
978         s/$/ \\/
979         a\
980             '"$XGETTEXT_OPTIONS_RUNTIME"' $${end_of_xgettext_options+}
981       }
982     ' po/Makevars.template >runtime-po/Makevars || exit 1
983
984     # Copy identical files from po to runtime-po.
985     (cd po && cp -p Makefile.in.in *-quot *.header *.sed *.sin ../runtime-po)
986   fi
987 fi
988
989 bootstrap_epilogue
990
991 echo "$0: done.  Now you can run './configure'."
992
993 # Local variables:
994 # eval: (add-hook 'write-file-hooks 'time-stamp)
995 # time-stamp-start: "scriptversion="
996 # time-stamp-format: "%:y-%02m-%02d.%02H"
997 # time-stamp-time-zone: "UTC"
998 # time-stamp-end: "; # UTC"
999 # End: