Merge in a change from some other incarnation of this file (gzip?)
[platform/upstream/coreutils.git] / bootstrap
1 #! /bin/sh
2
3 # Bootstrap this package from CVS.
4
5 # Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 # Written by Paul Eggert.
23
24 nl='
25 '
26
27 # Ensure file names are sorted consistently across platforms.
28 # Also, ensure diagnostics are in English, e.g., "wget --help" below.
29 LC_ALL=C
30 export LC_ALL
31
32 # Temporary directory names.
33 bt='._bootmp'
34 bt_regex=`echo "$bt"| sed 's/\./[.]/g'`
35 bt2=${bt}2
36
37 usage() {
38   echo >&2 "\
39 Usage: $0 [OPTION]...
40 Bootstrap this package from the checked-out sources.
41
42 Options:
43  --gnulib-srcdir=DIRNAME  Specify the local directory where gnulib
44                           sources reside.  Use this if you already
45                           have gnulib sources on your machine, and
46                           do not want to waste your bandwidth downloading
47                           them again.
48  --copy                   Copy files instead of creating symbolic links.
49  --force                  Attempt to bootstrap even if the sources seem
50                           not to have been checked out.
51  --skip-po                Do not download po files.
52  --cvs-user=USERNAME      Set the CVS username to be used when accessing
53                           the gnulib repository.
54
55 If the file .bootstrap.conf exists in the current working directory, its
56 contents are read as shell variables to configure the bootstrap.
57
58 Running without arguments will suffice in most cases.
59 "
60 }
61
62 # Configuration.
63
64 # List of gnulib modules needed.
65 gnulib_modules=
66
67 # Any gnulib files needed that are not in modules.
68 gnulib_files=
69
70 # Translation Project URL, for the registry of all projects
71 # and for the translation-team master directory.
72 TP_URL='http://www.iro.umontreal.ca/translation/registry.cgi?domain='
73 TP_PO_URL='http://www.iro.umontreal.ca/translation/teams/PO/'
74
75 extract_package_name='
76   /^AC_INIT(/{
77      /.*,.*,.*,/{
78        s///
79        s/[][]//g
80        p
81        q
82      }
83      s/AC_INIT(\[*//
84      s/]*,.*//
85      s/^GNU //
86      y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
87      s/[^A-Za-z0-9_]/-/g
88      p
89   }
90 '
91 package=`sed -n "$extract_package_name" configure.ac` || exit
92
93 # Extra files from gnulib, which override files from other sources.
94 gnulib_extra_files='
95         build-aux/install-sh
96         build-aux/missing
97         build-aux/mdate-sh
98         build-aux/texinfo.tex
99         build-aux/depcomp
100         build-aux/config.guess
101         build-aux/config.sub
102         doc/INSTALL
103 '
104
105 # Other locale categories that need message catalogs.
106 EXTRA_LOCALE_CATEGORIES=
107
108 # Additional xgettext options to use.  Use "\\\newline" to break lines.
109 XGETTEXT_OPTIONS='\\\
110  --flag=_:1:pass-c-format\\\
111  --flag=N_:1:pass-c-format\\\
112  --flag=error:3:c-format --flag=error_at_line:5:c-format\\\
113 '
114
115 # Files we don't want to import.
116 excluded_files=
117
118 # File that should exist in the top directory of a checked out hierarchy,
119 # but not in a distribution tarball.
120 CVS_only_file=README-hacking
121
122 # Whether to use copies instead of symlinks.
123 copy=false
124
125 # Override the default configuration, if necessary.
126 test -r bootstrap.conf && . ./bootstrap.conf
127
128 # Translate configuration into internal form.
129
130 # Parse options.
131
132 for option
133 do
134   case $option in
135   --help)
136     usage
137     exit;;
138   --gnulib-srcdir=*)
139     GNULIB_SRCDIR=`expr "$option" : '--gnulib-srcdir=\(.*\)'`;;
140   --cvs-user=*)
141     CVS_USER=`expr "$option" : '--cvs-user=\(.*\)'`;;
142   --skip-po)
143     SKIP_PO=t;;
144   --force)
145     CVS_only_file=;;
146   --copy)
147     copy=true;;
148   *)
149     echo >&2 "$0: $option: unknown option"
150     exit 1;;
151   esac
152 done
153
154 if test -n "$CVS_only_file" && test ! -r "$CVS_only_file"; then
155   echo "$0: Bootstrapping from a non-checked-out distribution is risky." >&2
156   exit 1
157 fi
158
159 echo "$0: Bootstrapping CVS $package..."
160
161 cleanup_gnulib() {
162   status=$?
163   rm -fr gnulib
164   exit $status
165 }
166
167 # Get gnulib files.
168
169 case ${GNULIB_SRCDIR--} in
170 -)
171   if [ ! -d gnulib ]; then
172     echo "$0: getting gnulib files..."
173
174     case ${CVS_AUTH-pserver} in
175     pserver)
176       CVS_PREFIX=':pserver:anonymous@';;
177     ssh)
178       CVS_PREFIX="$CVS_USER${CVS_USER+@}";;
179     *)
180       echo "$0: $CVS_AUTH: Unknown CVS access method" >&2
181       exit 1;;
182     esac
183
184     case $CVS_RSH in
185     '') CVS_RSH=ssh; export CVS_RSH;;
186     esac
187
188     trap cleanup_gnulib 1 2 13 15
189
190     cvs -z3 -q -d ${CVS_PREFIX}cvs.savannah.gnu.org:/cvsroot/gnulib co gnulib ||
191       cleanup_gnulib
192
193     trap - 1 2 13 15
194   fi
195   GNULIB_SRCDIR=gnulib
196 esac
197
198 gnulib_tool=$GNULIB_SRCDIR/gnulib-tool
199 <$gnulib_tool || exit
200
201 # Get translations.
202
203 get_translations() {
204   subdir=$1
205   domain=$2
206
207   case $WGET_COMMAND in
208   '')
209     echo "$0: wget not available; skipping translations";;
210   ?*)
211     echo "$0: getting translations into $subdir for $domain..." &&
212
213     (cd $subdir && rm -f dummy `ls | sed -n '/\.gmo$/p; /\.po/p'`) &&
214     $WGET_COMMAND -O "$subdir/$domain.html" "$TP_URL$domain" &&
215
216     sed -n 's|.*"http://[^"]*/translation/teams/PO/\([^/"]*\)/'"$domain"'-\([^/"]*\)\.[^."]*\.po".*|\1.\2|p' <"$subdir/$domain.html" |
217     sort -k 1,1 -k 2,2n -k2,2 -k3,3n -k3,3 -k4,4n -k4,4 -k5,5n -k5.5 |
218     awk -F. '
219       { if (lang && $1 != lang) print lang, ver }
220       { lang = $1; ver = substr($0, index($0, ".") + 1) }
221       END { if (lang) print lang, ver }
222     ' | awk -v domain="$domain" -v subdir="$subdir" '
223       {
224         lang = $1
225         ver = $2
226         urlfmt = ""
227         printf "{ $WGET_COMMAND -O %s/%s.po '\'"$TP_PO_URL"'/%s/%s-%s.%s.po'\'' &&\n", subdir, lang, lang, domain, ver, lang
228         printf "  msgfmt -c -o /dev/null %s/%s.po || {\n", subdir, lang
229         printf "    echo >&2 '\'"$0"': omitting translation for %s'\''\n", lang
230         printf "    rm -f %s/%s.po; }; } &&\n", subdir, lang
231       }
232       END { print ":" }
233     ' | WGET_COMMAND="$WGET_COMMAND" sh;;
234   esac &&
235   ls "$subdir"/*.po 2>/dev/null |
236     sed 's|.*/||; s|\.po$||' >"$subdir/LINGUAS" &&
237   rm -f "$subdir/$domain.html"
238 }
239
240 case $SKIP_PO in
241 '')
242   case `wget --help` in
243   *'--no-cache'*)
244     WGET_COMMAND='wget -nv --no-cache';;
245   *'--cache=on/off'*)
246     WGET_COMMAND='wget -nv --cache=off';;
247   *'--non-verbose'*)
248     WGET_COMMAND='wget -nv';;
249   *)
250     WGET_COMMAND='';;
251   esac
252
253   if test -d po; then
254     get_translations po $package || exit
255   fi
256
257   if test -d runtime-po; then
258     get_translations runtime-po $package-runtime || exit
259   fi;;
260 esac
261
262 symlink_to_gnulib()
263 {
264   src=$GNULIB_SRCDIR/$1
265   dst=${2-$1}
266
267   test -f "$src" && {
268     if $copy; then
269       {
270         test ! -h "$dst" || {
271           echo "$0: rm -f $dst" &&
272           rm -f "$dst"
273         }
274       } &&
275       test -f "$dst" &&
276       cmp -s "$src" "$dst" || {
277         echo "$0: cp -fp $src $dst" &&
278         cp -fp "$src" "$dst"
279       }
280     else
281       test -h "$dst" &&
282       src_ls=`ls -diL "$src" 2>/dev/null` && set $src_ls && src_i=$1 &&
283       dst_ls=`ls -diL "$dst" 2>/dev/null` && set $dst_ls && dst_i=$1 &&
284       test "$src_i" = "$dst_i" || {
285         dot_dots=
286         case $src in
287         /*) ;;
288         *)
289           case /$dst/ in
290           *//* | */../* | */./* | /*/*/*/*/*/)
291              echo >&2 "$0: invalid symlink calculation: $src -> $dst"
292              exit 1;;
293           /*/*/*/*/)    dot_dots=../../../;;
294           /*/*/*/)      dot_dots=../../;;
295           /*/*/)        dot_dots=../;;
296           esac;;
297         esac
298
299         echo "$0: ln -fs $dot_dots$src $dst" &&
300         ln -fs "$dot_dots$src" "$dst"
301       }
302     fi
303   }
304 }
305
306 cp_mark_as_generated()
307 {
308   cp_src=$1
309   cp_dst=$2
310
311   if cmp -s "$cp_src" "$GNULIB_SRCDIR/$cp_dst"; then
312     symlink_to_gnulib "$cp_dst"
313   else
314     case $cp_dst in
315       *.[ch])             c1='/* '; c2=' */';;
316       *.texi)             c1='@c '; c2=     ;;
317       *.m4|*/Make*|Make*) c1='# ' ; c2=     ;;
318       *)                  c1=     ; c2=     ;;
319     esac
320
321     if test -z "$c1"; then
322       cmp -s "$cp_src" "$cp_dst" || {
323         echo "$0: cp -f $cp_src $cp_dst" &&
324         rm -f "$cp_dst" &&
325         sed "s!$bt_regex/!!g" "$cp_src" > "$cp_dst"
326       }
327     else
328       # Copy the file first to get proper permissions if it
329       # doesn't already exist.  Then overwrite the copy.
330       cp "$cp_src" "$cp_dst-t" &&
331       (
332         echo "$c1-*- buffer-read-only: t -*- vi: set ro:$c2" &&
333         echo "${c1}DO NOT EDIT! GENERATED AUTOMATICALLY!$c2" &&
334         sed "s!$bt_regex/!!g" "$cp_src"
335       ) > $cp_dst-t &&
336       if cmp -s "$cp_dst-t" "$cp_dst"; then
337         rm -f "$cp_dst-t"
338       else
339         echo "$0: cp $cp_src $cp_dst # with edits" &&
340         mv -f "$cp_dst-t" "$cp_dst"
341       fi
342     fi
343   fi
344 }
345
346 version_controlled_file() {
347   dir=$1
348   file=$2
349   found=no
350   if test -d CVS; then
351     grep -F "/$file/" $dir/CVS/Entries 2>/dev/null |
352              grep '^/[^/]*/[0-9]' > /dev/null && found=yes
353   elif test -d .git; then
354     git-rm -n "$dir/$file" > /dev/null 2>&1 && found=yes
355   else
356     echo "$0: no version control for $dir/$file?" >&2
357   fi
358   test $found = yes
359 }
360
361 # If $STR is not already on a line by itself in $FILE, insert it,
362 # sorting the new contents of the file and replacing $FILE with the result.
363 insert_sorted_if_absent() {
364   file=$1
365   str=$2
366   echo "$str" | sort -u - $file | cmp -s - $file \
367     || echo "$str" | sort -u - $file -o $file \
368     || exit
369 }
370
371 slurp() {
372   for dir in . `(cd $1 && find * -type d -print)`; do
373     copied=
374     sep=
375     for file in `ls $1/$dir`; do
376       test -d $1/$dir/$file && continue
377       for excluded_file in $excluded_files; do
378         test "$dir/$file" = "$excluded_file" && continue 2
379       done
380       if test $file = Makefile.am; then
381         copied=$copied${sep}gnulib.mk; sep=$nl
382         remove_intl='/^[^#].*\/intl/s/^/#/;'"s!$bt_regex/!!g"
383         sed "$remove_intl" $1/$dir/$file | cmp -s - $dir/gnulib.mk || {
384           echo "$0: Copying $1/$dir/$file to $dir/gnulib.mk ..." &&
385           rm -f $dir/gnulib.mk &&
386           sed "$remove_intl" $1/$dir/$file >$dir/gnulib.mk
387         }
388       elif { test "${2+set}" = set && test -r $2/$dir/$file; } ||
389            version_controlled_file $dir $file; then
390         echo "$0: $dir/$file overrides $1/$dir/$file"
391       else
392         copied=$copied$sep$file; sep=$nl
393         if test $file = gettext.m4; then
394           echo "$0: patching m4/gettext.m4 to remove need for intl/* ..."
395           rm -f $dir/$file
396           sed '
397             /^AC_DEFUN(\[AM_INTL_SUBDIR],/,/^]/c\
398               AC_DEFUN([AM_INTL_SUBDIR], [
399             /^AC_DEFUN(\[gt_INTL_SUBDIR_CORE],/,/^]/c\
400               AC_DEFUN([gt_INTL_SUBDIR_CORE], [])
401             $a\
402               AC_DEFUN([gl_LOCK_EARLY], [])
403           ' $1/$dir/$file >$dir/$file
404         else
405           cp_mark_as_generated $1/$dir/$file $dir/$file
406         fi
407       fi || exit
408     done
409
410     for dot_ig in .cvsignore .gitignore; do
411       ig=$dir/$dot_ig
412       if test -n "$copied" && test -f $ig; then
413         insert_sorted_if_absent $ig "$copied"
414         # If an ignored file name ends with _.h, then also add
415         # the name with just ".h".  Many gnulib headers are generated,
416         # e.g., stdint_.h -> stdint.h, dirent_.h ->..., etc.
417         f=`echo "$copied"|sed 's/_\.h$/.h/'`
418         insert_sorted_if_absent $ig "$f"
419       fi
420     done
421   done
422 }
423
424
425 # Create boot temporary directories to import from gnulib and gettext.
426 rm -fr $bt $bt2 &&
427 mkdir $bt $bt2 || exit
428
429 # Import from gnulib.
430
431 gnulib_tool_options="\
432  --import\
433  --no-changelog\
434  --aux-dir $bt/build-aux\
435  --doc-base $bt/doc\
436  --lib lib$package\
437  --m4-base $bt/m4/\
438  --source-base $bt/lib/\
439  --tests-base $bt/tests\
440  --local-dir gl\
441 "
442 echo "$0: $gnulib_tool $gnulib_tool_options --import ..."
443 $gnulib_tool $gnulib_tool_options --import $gnulib_modules &&
444 slurp $bt || exit
445
446 for file in $gnulib_files; do
447   symlink_to_gnulib $file || exit
448 done
449
450
451 # Import from gettext.
452
453 echo "$0: (cd $bt2; autopoint) ..."
454 cp configure.ac $bt2 &&
455 (cd $bt2 && autopoint && rm configure.ac) &&
456 slurp $bt2 $bt || exit
457
458 rm -fr $bt $bt2 || exit
459
460
461 # Reconfigure, getting other files.
462
463 for command in \
464   'aclocal --force -I m4' \
465   'autoconf --force' \
466   'autoheader --force' \
467   'automake --add-missing --copy --force-missing';
468 do
469   echo "$0: $command ..."
470   $command || exit
471 done
472
473
474 # Get some extra files from gnulib, overriding existing files.
475
476 for file in $gnulib_extra_files; do
477   case $file in
478   */INSTALL) dst=INSTALL;;
479   *) dst=$file;;
480   esac
481   symlink_to_gnulib $file $dst || exit
482 done
483
484
485 # Create gettext configuration.
486 echo "$0: Creating po/Makevars from po/Makevars.template ..."
487 rm -f po/Makevars
488 sed '
489   /^EXTRA_LOCALE_CATEGORIES *=/s/=.*/= '"$EXTRA_LOCALE_CATEGORIES"'/
490   /^MSGID_BUGS_ADDRESS *=/s/=.*/= bug-'"$package"'@gnu.org/
491   /^XGETTEXT_OPTIONS *=/{
492     s/$/ \\/
493     a\
494         '"$XGETTEXT_OPTIONS"' $${end_of_xgettext_options+}
495   }
496 ' po/Makevars.template >po/Makevars
497
498 if test -d runtime-po; then
499   # Similarly for runtime-po/Makevars, but not quite the same.
500   rm -f runtime-po/Makevars
501   sed '
502     /^DOMAIN *=.*/s/=.*/= '"$package"'-runtime/
503     /^subdir *=.*/s/=.*/= runtime-po/
504     /^MSGID_BUGS_ADDRESS *=/s/=.*/= bug-'"$package"'@gnu.org/
505     /^XGETTEXT_OPTIONS *=/{
506       s/$/ \\/
507       a\
508           '"$XGETTEXT_OPTIONS_RUNTIME"' $${end_of_xgettext_options+}
509     }
510   ' <po/Makevars.template >runtime-po/Makevars
511
512   # Copy identical files from po to runtime-po.
513   (cd po && cp -p Makefile.in.in *-quot *.header *.sed *.sin ../runtime-po)
514 fi
515
516 echo "$0: done.  Now you can run './configure'."