Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / misc / autopoint.in
1 #! /bin/sh
2 #
3 # Copyright (C) 2002-2015 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 3 of the License, or
8 # (at your option) 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, see <http://www.gnu.org/licenses/>.
17 #
18
19 # This file is meant for authors, maintainers, co-maintainers or installers
20 # of packages which are internationalized with the help of GNU gettext.  For
21 # further information how to use it consult the GNU gettext manual.
22
23 progname=$0
24 package=@PACKAGE@
25 version=@VERSION@
26 archive_version=@ARCHIVE_VERSION@
27
28 # Set variables
29 # - gettext_datadir     directory where the data files are stored.
30 prefix="@prefix@"
31 datarootdir="@datarootdir@"
32 : ${gettext_datadir="@datadir@/gettext"}
33 : ${AUTOM4TE=autom4te}
34
35 # func_tmpdir
36 # creates a temporary directory.
37 # Sets variable
38 # - tmp             pathname of freshly created temporary directory
39 func_tmpdir ()
40 {
41   # Use the environment variable TMPDIR, falling back to /tmp. This allows
42   # users to specify a different temporary directory, for example, if their
43   # /tmp is filled up or too small.
44   : ${TMPDIR=/tmp}
45   {
46     # Use the mktemp program if available. If not available, hide the error
47     # message.
48     tmp=`(umask 077 && mktemp -d "$TMPDIR/gtXXXXXX") 2>/dev/null` &&
49     test -n "$tmp" && test -d "$tmp"
50   } ||
51   {
52     # Use a simple mkdir command. It is guaranteed to fail if the directory
53     # already exists.  $RANDOM is bash specific and expands to empty in shells
54     # other than bash, ksh and zsh.  Its use does not increase security;
55     # rather, it minimizes the probability of failure in a very cluttered /tmp
56     # directory.
57     tmp=$TMPDIR/gt$$-$RANDOM
58     (umask 077 && mkdir "$tmp")
59   } ||
60   {
61     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
62     { (exit 1); exit 1; }
63   }
64 }
65
66 # Support for relocatability.
67 func_find_curr_installdir ()
68 {
69   # Determine curr_installdir, even taking into account symlinks.
70   curr_executable="$0"
71   case "$curr_executable" in
72     */* | *\\*) ;;
73     *) # Need to look in the PATH.
74       save_IFS="$IFS"; IFS="${PATH_SEPARATOR='@PATH_SEPARATOR@'}"
75       for dir in $PATH; do
76         IFS="$save_IFS"
77         test -z "$dir" && dir=.
78         for exec_ext in ''; do
79           if test -f "$dir/$curr_executable$exec_ext"; then
80             curr_executable="$dir/$curr_executable$exec_ext"
81             break 2
82           fi
83         done
84       done
85       IFS="$save_IFS"
86       ;;
87   esac
88   # Make absolute.
89   case "$curr_executable" in
90     /* | ?:/* | ?:\\*) ;;
91     *) curr_executable=`pwd`/"$curr_executable" ;;
92   esac
93   # Resolve symlinks.
94   sed_dirname='s,/[^/]*$,,'
95   sed_linkdest='s,^.* -> \(.*\),\1,p'
96   while : ; do
97     lsline=`LC_ALL=C ls -l "$curr_executable"`
98     case "$lsline" in
99       *" -> "*)
100         linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"`
101         case "$linkdest" in
102           /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;;
103           *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;;
104         esac ;;
105       *) break ;;
106     esac
107   done
108   curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'`
109   # Canonicalize.
110   curr_installdir=`cd "$curr_installdir" && pwd`
111 }
112 func_find_prefixes ()
113 {
114   # Compute the original/current installation prefixes by stripping the
115   # trailing directories off the original/current installation directories.
116   orig_installprefix="$orig_installdir"
117   curr_installprefix="$curr_installdir"
118   while true; do
119     orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
120     curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
121     if test -z "$orig_last" || test -z "$curr_last"; then
122       break
123     fi
124     if test "$orig_last" != "$curr_last"; then
125       break
126     fi
127     orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'`
128     curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'`
129   done
130 }
131 if test "@RELOCATABLE@" = yes; then
132   exec_prefix="@exec_prefix@"
133   bindir="@bindir@"
134   orig_installdir="$bindir" # see Makefile.am's *_SCRIPTS variables
135   func_find_curr_installdir # determine curr_installdir
136   func_find_prefixes
137   # Relocate the directory variables that we use.
138   gettext_datadir=`echo "$gettext_datadir/" | sed -e "s%^${orig_installprefix}/%${curr_installprefix}/%" | sed -e 's,/$,,'`
139 fi
140
141 # func_trace_autoconf macro configure.ac
142 # traces an Autoconf macro call and outputs the arguments to stdout,
143 # using autom4te.
144 func_trace_autoconf ()
145 {
146   echo '\
147 dnl replace macros which may abort autom4te with a no-op variant
148 m4_pushdef([m4_assert])
149 m4_pushdef([m4_fatal])
150 m4_pushdef([m4_warn])
151 m4_pushdef([m4_errprintn])
152 m4_pushdef([m4_exit])
153 m4_pushdef([m4_include])
154 m4_pushdef([m4_esyscmd])
155 ' \
156   | "$AUTOM4TE" --no-cache --language=Autoconf-without-aclocal-m4 \
157     --trace="$1":\$% - "$2" 2>/dev/null
158 }
159
160 # func_trace_sed macro configure.ac
161 # traces an Autoconf macro call and outputs the arguments to stdout,
162 # using sed.
163 func_trace_sed ()
164 {
165   sed_extract_arguments='
166 s,#.*$,,; s,^dnl .*$,,; s, dnl .*$,,;
167 /'"$1"'(/ {
168   ta
169   :a
170     s/)/)/
171     tb
172     s/\\$//
173     N
174     ba
175   :b
176   s,^.*'"$1"'([[ ]*\([^]"$`\\)]*\).*$,\1,p
177 }
178 d'
179   sed -e "$sed_extract_arguments" "$2"
180 }
181
182 # func_usage
183 # outputs to stdout the --help usage message.
184 func_usage ()
185 {
186   echo "\
187 Usage: autopoint [OPTION]...
188
189 Copies standard gettext infrastructure files into a source package.
190
191 Options:
192       --help           print this help and exit
193       --version        print version information and exit
194   -f, --force          force overwriting of files that already exist
195   -n, --dry-run        print modifications but don't perform them"
196 #  echo "\
197 #  -V version           copy the infrastructure of the specified gettext version
198 #                         (dangerous)"
199   echo "
200 Report bugs to <bug-gnu-gettext@gnu.org>."
201 }
202
203 # func_version
204 # outputs to stdout the --version message.
205 func_version ()
206 {
207   echo "$progname (GNU $package) $version"
208   echo "Uses a versions archive in @ARCHIVE_FORMAT@ format."
209   echo "Copyright (C) 2002-2013 Free Software Foundation, Inc.
210 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
211 This is free software: you are free to change and redistribute it.
212 There is NO WARRANTY, to the extent permitted by law."
213   echo "Written by" "Bruno Haible"
214 }
215
216 # func_fatal_error message
217 # outputs to stderr a fatal error message, and terminates the program.
218 func_fatal_error ()
219 {
220   echo "autopoint: *** $1" 1>&2
221   echo "autopoint: *** Stop." 1>&2
222   exit 1
223 }
224
225 # Nuisances.
226 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
227
228 # Unset more variables known to interfere with behavior of common tools.
229 CLICOLOR_FORCE= GREP_OPTIONS=
230 unset CLICOLOR_FORCE GREP_OPTIONS
231
232 # Command-line option processing.
233 # Removes the OPTIONS from the arguments. Sets the variables:
234 # - force           yes if --force was given, empty otherwise
235 # - ver             gettext version if -V was given, empty otherwise
236 # - doit            false if --dry-run was given, : otherwise
237 {
238   force=
239   ver=
240   doit=:
241
242   while test $# -gt 0; do
243     case "$1" in
244       -n | --dry-run | --dry-ru | --dry-r | --dry- | --dry | --dr | --d )
245         shift
246         doit=false ;;
247       -f | --force | --forc | --for | --fo | --f )
248         shift
249         force=yes ;;
250       --help | --hel | --he | --h )
251         func_usage; exit 0 ;;
252 #      -V ) # Some people put a space between -V and the version number.
253 #        shift
254 #        if test $# = 0; then
255 #          func_usage 1>&2
256 #          exit 1
257 #        fi
258 #        ver=$1;
259 #        shift ;;
260 #      -V*) # Some people omit the space between -V and the version number.
261 #        ver=`echo "X$1" | sed -e 's/^X-V//'`
262 #        shift ;;
263       --version | --versio | --versi | --vers | --ver | --ve | --v )
264         func_version
265         exit 0 ;;
266       -- ) # Stop option prcessing
267         shift; break ;;
268       -* )
269         echo "autopoint: unknown option $1" 1>&2
270         echo "Try 'autopoint --help' for more information." 1>&2
271         exit 1 ;;
272       * )
273         break ;;
274     esac
275   done
276 }
277
278 # Command-line argument processing.
279 # Analyzes the remaining arguments.
280 {
281   if test $# -gt 0; then
282     func_usage 1>&2
283     exit 1
284   fi
285 }
286
287 srcdir=`pwd`
288 # The current directory is now $srcdir.
289
290 # Check integrity of package: A configure.in/ac must be present. Sets variable
291 # - configure_in    name of configure.in/ac file.
292 if test -f configure.in; then
293   configure_in=configure.in
294 else
295   if test -f configure.ac; then
296     configure_in=configure.ac
297   else
298     # KDE specific convention: configure.in.in
299     if test -f configure.in.in; then
300       configure_in=configure.in.in
301     else
302       func_fatal_error "Missing configure.in or configure.ac, please cd to your package first."
303     fi
304   fi
305 fi
306
307 # Select the method for Autoconf macro tracing.  func_trace_autoconf
308 # is more accurate than func_trace_sed, but it only works with
309 # autoconf >= 2.69.
310 if echo "AC_PREREQ([2.69])" \
311   | "$AUTOM4TE" --no-cache --language=Autoconf-without-aclocal-m4 - 2>&1; then
312   func_trace=func_trace_autoconf
313 else
314   func_trace=func_trace_sed
315 fi
316
317 # func_version_prereq required_version version
318 # compares the required version and the latest archive version.
319 func_version_prereq ()
320 {
321   req="$1"
322   ver="$2"
323   echo "m4_if(m4_version_compare([$ver], [$req]), [-1], [m4_exit([1])])" \
324     | "$AUTOM4TE" --language=M4sugar 2>&1 >/dev/null
325 }
326
327 # If AM_GNU_GETTEXT_REQUIRE_VERSION is used and archive_version is newer than
328 # that, use archive_version.
329 xreq=`func_trace_sed AM_GNU_GETTEXT_REQUIRE_VERSION "$configure_in"`
330
331 # Need to use func_trace_sed instead of $func_trace, since
332 # AM_GNU_GETTEXT_VERSION is not a standard Autoconf trace.
333 xver=`func_trace_sed AM_GNU_GETTEXT_VERSION "$configure_in"`
334
335 # Prefer AM_GNU_GETTEXT_REQUIRE_VERSION over AM_GNU_GETTEXT_VERSION if both are
336 # specified.
337 if test -n "$xreq" && test -n "$xver"; then
338   echo "autopoint: using AM_GNU_GETTEXT_REQUIRE_VERSION instead of AM_GNU_GETTEXT_VERSION"
339 fi
340
341 if test -n "$xreq"; then
342   if func_version_prereq "$xreq" "$archive_version"; then
343     ver="$archive_version"
344   else
345     func_fatal_error "gettext version $xreq or newer is required"
346   fi
347 else
348   if test -z "$xver" && test -f intl/VERSION; then
349     xver=`cat intl/VERSION | LC_ALL=C sed -n -e 's/^.*gettext-\([-+_.0-9A-Za-z]*\).*$/\1/p'`
350   fi
351
352   # Check whether the -V option and the version number in configure.in match.
353   # At least one of the two must be given. If both are given, they must agree.
354
355   if test -n "$xver"; then
356     if test -n "$ver"; then
357       if test "X$ver" != "X$xver"; then
358         func_fatal_error "Version mismatch: specified -V $ver but the package uses gettext version $xver"
359       fi
360     else
361       ver="$xver"
362     fi
363   fi
364 fi
365
366 if test -z "$ver"; then
367   func_fatal_error "Missing version: please specify in $configure_in through a line 'AM_GNU_GETTEXT_VERSION(x.yy.zz)' the gettext version the package is using"
368 fi
369
370 # Check whether the version number is supported.
371 case "$ver" in
372   0.10.35 | 0.10.36 | 0.10.37 | 0.10.38 | 0.10.39 | 0.10.40 | \
373   0.11 | 0.11.1 | 0.11.2 | 0.11.3 | 0.11.4 | 0.11.5 | \
374   0.12 | 0.12.1 | \
375   0.13 | 0.13.1 | \
376   0.14 | 0.14.1 | 0.14.2 | 0.14.3 | 0.14.4 | 0.14.5 | 0.14.6 | \
377   0.15 | \
378   0.16 | 0.16.1 | \
379   0.17 | \
380   0.18 | 0.18.1 | 0.18.2 | 0.18.3 | \
381   0.19 | 0.19.1 | 0.19.2 | 0.19.3 | 0.19.4 | 0.19.5 | 0.19.6 | 0.19.7 )
382     ;;
383   *)
384     func_fatal_error "The AM_GNU_GETTEXT_VERSION declaration in your $configure_in
385                file requires the infrastructure from gettext-$ver but this version
386                is older. Please upgrade to gettext-$ver or newer."
387     ;;
388 esac
389
390 # Check in which directory config.rpath, mkinstalldirs etc. belong.
391 auxdir=`"$func_trace" AC_CONFIG_AUX_DIR "$configure_in"`
392 if test -n "$auxdir"; then
393   auxdir="$auxdir/"
394 fi
395
396 # Check in which directory the *.m4 macros belong.
397 macrodirs=`"$func_trace" AC_CONFIG_MACRO_DIR_TRACE "$configure_in"`
398 if test -z "$macrodirs"; then
399   macrodirs=`"$func_trace" AC_CONFIG_MACRO_DIR "$configure_in"`
400 fi
401 for arg in $macrodirs; do
402   m4dir="$arg"
403   break
404 done
405
406 if test -z "$m4dir" && test -f Makefile.am; then
407   # A package using automake.
408   # Extract the macro directory name from Makefile.am.
409   aclocal_amflags=`grep '^ACLOCAL_AMFLAGS[      ]*=' Makefile.am | sed -e 's/^ACLOCAL_AMFLAGS[  ]*=\(.*\)$/\1/'`
410   m4dir_is_next=
411   for arg in $aclocal_amflags; do
412     if test -n "$m4dir_is_next"; then
413       m4dir="$arg"
414       break
415     else
416       if test "X$arg" = "X-I"; then
417         m4dir_is_next=yes
418       else
419         m4dir_is_next=
420       fi
421     fi
422   done
423 fi
424
425 if test -z "$m4dir"; then
426   m4dir=m4
427 fi
428
429 # Check whether to omit the intl/ directory.
430 omitintl=
431 # Need to use func_trace_sed instead of $func_trace, since
432 # AM_GNU_GETTEXT is not a standard Autoconf trace.
433 xargs=`func_trace_sed AM_GNU_GETTEXT "$configure_in"`
434 save_IFS="$IFS"; IFS=:
435 for arg in $xargs; do
436   if test 'external' = "$arg"; then
437     omitintl=yes
438     break
439   fi
440 done
441 IFS="$save_IFS"
442
443 # Check in which directory or directories the po/* infrastructure belongs.
444 configfiles=`"$func_trace" AC_CONFIG_FILES "$configure_in"`
445 # PO directories have a Makefile.in generated from Makefile.in.in.
446 # Treat a directory as a PO directory if and only if it has a
447 # POTFILES.in file. This allows packages to have multiple PO
448 # directories under different names or in different locations.
449 sed_remove_Makefile_in='s,/Makefile\.in$,,'
450 podirs=`for f in $configfiles; do case "$f" in */Makefile.in) echo $f;; esac; done | sed -e "$sed_remove_Makefile_in"`
451 if test -z "$podirs"; then
452   # If we cannot get the list of PO directories from configure.ac, assume the
453   # common default.
454   podirs="po"
455 fi
456
457 # Set up a temporary checkout directory.
458 # Set variables
459 # - work_dir        directory containing the temporary checkout
460 work_dir=tmpwrk$$
461 mkdir "$work_dir" || {
462   if test -d "$work_dir"; then
463     func_fatal_error "directory $work_dir already exists"
464   else
465     func_fatal_error "cannot create directory $work_dir"
466   fi
467 }
468
469 # We support three archive formats.
470 #
471 # Format | Size (KiB) for gettext-0.17 | Extra tools needed |
472 # -------+-----------------------------+--------------------+
473 #  dir   |            3000             |         --         |
474 #  cvs   |             356             |         cvs        |
475 #  git   |             484             |         git        |
476 # -------+-----------------------------+--------------------+
477
478 case "@ARCHIVE_FORMAT@" in
479   dir*)
480     # The archive of different versions is very large (unless xz compression is
481     # used), but using it does not require special tools.
482     case "@ARCHIVE_FORMAT@" in
483       dirgz) gzip -d -c < "$gettext_datadir/archive.dir.tar.gz" ;;
484       dirbz2) bzip2 -d -c < "$gettext_datadir/archive.dir.tar.bz2" ;;
485       dirxz) xz -d -c < "$gettext_datadir/archive.dir.tar.xz" ;;
486     esac \
487       | (cd "$work_dir" && tar xf - "gettext-$ver")
488     if test `find "$work_dir" -type f -print | wc -l` = 0; then
489       rm -rf "$work_dir"
490       func_fatal_error "infrastructure files for version $ver not found; this is autopoint from GNU $package $version"
491     fi
492     mv "$work_dir/gettext-$ver" "$work_dir/archive"
493     ;;
494
495   cvs)
496     # We distributed the many different versions of the files in a CVS
497     # repository. This guaranteed a good compression rate:
498     #
499     #   Including version    size in KB of
500     #                       "du autopoint-files/archive"
501     #      0.10.35                  240
502     #      0.10.36                  428
503     #      0.10.37                  436
504     #      0.10.38                  488
505     #      0.10.39                  500
506     #      0.10.40                  528
507     #      0.11                     720
508     #      0.11.1                   740
509     #      0.11.2                   748
510     #      0.11.3                   804
511     #      0.11.4                   864
512     #      0.11.5                   880
513     #      0.12                    1032
514     #      0.12.1                  1032
515     #      0.13                    1220
516     #      0.13.1                  1236
517     #      0.14                    1296
518     #      0.14.1                  1300
519     #      0.14.2                  1420
520     #      0.14.3                  1428
521     #      0.14.4                  1464
522     #      0.14.5                  1508
523     #      0.14.6                  1580
524     #      0.15                    1760
525     #      0.16                    1808
526     #      0.16.1                  1812
527     #      0.17                    2128
528     #      0.18                    2656
529     #
530     # The requirement that the user must have the CVS program available is not
531     # a severe restrictions, because most of the people who use autopoint are
532     # users of CVS.
533     #
534     # But the CVS format is now deprecated, because "cvs init" does not work in
535     # all circumstances
536     # (see <http://lists.gnu.org/archive/html/bug-cvs/2010-05/msg00003.html>)
537     # and we are not allowed to distribute the cvs infrastructure files
538     # ourselves
539     # (see <http://lists.gnu.org/archive/html/bug-cvs/2010-06/msg00011.html>).
540     #
541     # Check availability of the CVS program.
542     (cvs -v) >/dev/null 2>/dev/null || func_fatal_error "cvs program not found"
543
544     # Set up a temporary CVS repository.
545     # We need the temporary CVS repository because any checkout needs write
546     # access to the CVSROOT/history file, so it cannot be under $gettext_datadir.
547     # We need the temporary checkout directory because when --force was not
548     # given, we need to compare the existing files with the checked out ones.
549     # Set variables
550     # - cvs_dir         directory containing the temporary repository
551     cvs_dir=tmpcvs$$
552     # Use an umask of 077, to avoid attacks that work by overwriting files in
553     # the "$CVSROOT"/CVSROOT directory.
554     (umask 077 && mkdir "$cvs_dir") || {
555       if test -d "$cvs_dir"; then
556         func_fatal_error "directory $cvs_dir already exists"
557       else
558         func_fatal_error "cannot create directory $cvs_dir"
559       fi
560     }
561     CVSROOT="$srcdir/$cvs_dir"
562     unset CVS_CLIENT_LOG
563     unset CVS_CLIENT_PORT
564     unset CVS_IGNORE_REMOTE_ROOT
565     unset CVS_LOCAL_BRANCH_NUM
566     unset CVS_NOBASES
567     unset CVS_PASSFILE
568     unset CVS_PASSWORD
569     unset CVS_PROXY_PORT
570     unset CVS_RCMD_PORT
571     unset CVS_RSH
572     unset CVS_SERVER
573     unset CVS_SERVER_SLEEP
574     CVS_SIGN_COMMITS=
575     export CVS_SIGN_COMMITS
576     unset CVS_SSH
577     unset CVS_VERIFY_CHECKOUTS
578     unset CVS_VERIFY_TEMPLATE
579     unset CVSIGNORE
580     unset CVSREAD
581     unset CVSREADONLYFS
582     unset CVSUMASK
583     unset CVSWRAPPERS
584
585     # Need to pass -d "$CVSROOT", because there may be a CVS directory in the
586     # current directory.
587     cvs -d "$CVSROOT" init
588     gzip -d -c < "$gettext_datadir/archive.cvs.tar.gz" | (cd "$cvs_dir" && tar xf -)
589
590     cd "$work_dir"
591     cvsver=gettext-`echo "$ver" | sed -e 's/\./_/g'`
592     (cvs -d "$CVSROOT" checkout -r"$cvsver" archive > /dev/null) 2>&1 | grep -v '^cvs checkout: Updating'
593     find archive -name CVS -type d -print | xargs rm -rf
594     cd ..
595     rm -rf "$cvs_dir"
596     # Check that really all CVS directories are gone, otherwise we would overwrite
597     # the contents of the user's CVS directories.
598     if test `find $work_dir/archive -name CVS -type d -print | wc -l` != 0; then
599       rm -rf "$work_dir"
600       func_fatal_error "failed to remove all CVS subdirectories"
601     fi
602     if test `find $work_dir/archive -type f -print | wc -l` = 0; then
603       rm -rf "$work_dir"
604       func_fatal_error "infrastructure files for version $ver not found; this is autopoint from GNU $package $version"
605     fi
606     ;;
607
608   git)
609     # Check availability of the git program.
610     (git --version) >/dev/null 2>/dev/null || func_fatal_error "git program not found"
611     mkdir "$work_dir/archive"
612     gzip -d -c < "$gettext_datadir/archive.git.tar.gz" | (cd "$work_dir/archive" && tar xf -)
613     (cd "$work_dir/archive" && git checkout -q "gettext-$ver") || {
614       rm -rf "$work_dir"
615       func_fatal_error "infrastructure files for version $ver not found; this is autopoint from GNU $package $version"
616     }
617     (cd "$work_dir/archive" && rm -rf .git .gitignore)
618     ;;
619 esac
620
621 # func_destfile file
622 # determines the destination file, relative to the package's top level
623 # directory, for a given file name, relative to archive.
624 # Sets variables
625 # - destfile        relative destination file name, or
626 #                   empty if the file shall be omitted
627 # - sharedowner     yes if the file is not only owned by GNU gettext but may
628 #                   be installed by automake or other tools, otherwise empty
629 # - allpodirs       yes if the file is to be installed in every dir in $podirs
630 func_destfile ()
631 {
632   # There are five categories of files:
633   # ABOUT_NLS -> top level directory
634   # config.rpath mkinstalldirs -> $auxdir
635   # m4/* -> $m4dir/
636   # intl/* -> intl/
637   # po/* -> 
638   sharedowner=
639   allpodirs=
640   case `echo "$1" | sed -e 's,[^/]*$,,'` in
641     "" )
642       case "$1" in
643         config.rpath ) destfile="$auxdir$1" ;;
644         mkinstalldirs ) destfile="$auxdir$1" sharedowner=yes ;;
645         * ) destfile="$1" ;;
646       esac
647       ;;
648     m4/ ) destfile=`echo "$1" | sed -e "s,^m4/,$m4dir/,"` ;;
649     intl/ ) if test -n "$omitintl"; then destfile=""; else destfile="$1"; fi ;;
650     po/ ) destfile=`echo "$1" | sed -e "s,^po/,,"` allpodirs=yes ;;
651     * ) destfile="$1" ;;
652   esac
653 }
654
655 # func_compare existingfile gettextfile
656 # compares the existing file and the file from gettext, and decides whether the
657 # existing file should be overwritten with the file from gettext. Returns 0 if
658 # it should be overwritten, or 1 if it should be skipped.
659 sed_extract_serial='s/^#.* serial \([^ ]*\).*/\1/p
660 1q'
661 func_compare ()
662 {
663   if cmp -s "$1" "$2"; then
664     false
665   else
666     case "$2" in
667       *.m4)
668         # For interoperability with gnulib. gnulib often has newer versions of
669         # the *.m4 files than the latest gettext release. Don't overwrite a
670         # newer version from gnulib with an older version from the gettext
671         # release. The version can be retrieved from the first line, which
672         # looks like this:   # file.m4 serial NN ...
673         existing_serial=`sed -n -e "$sed_extract_serial" < "$1"`
674         gettext_serial=`sed -n -e "$sed_extract_serial" < "$2"`
675         if test -n "$existing_serial" && test -n "$gettext_serial" \
676            && test "$existing_serial" -ge "$gettext_serial" 2> /dev/null; then
677           false
678         else
679           true
680         fi
681         ;;
682       *)
683         true
684         ;;
685     esac
686   fi
687 }
688
689 # If some files have been locally modified and we have not been requested
690 # to overwrite them, then bail out. This is better than leaving a source
691 # package around where half of the files are locally modified and half are
692 # original - too great risk of version mismatch.
693 if test -z "$force"; then
694   mismatch=
695   func_tmpdir
696   mismatchfile="$tmp"/autopoint.diff
697   for file in `find "$work_dir/archive" -type f -print | sed -e "s,^$work_dir/archive/,," | LC_ALL=C sort`; do
698     func_destfile "$file"
699     if test -n "$destfile"; then
700       func_compare_to_destfile ()
701       {
702         finaldestfile="$1"
703         if test -f "$finaldestfile"; then
704           if func_compare "$finaldestfile" "$work_dir/archive/$file"; then
705             if test -n "$sharedowner"; then
706               echo "autopoint: warning: File $finaldestfile has been locally modified." 1>&2
707             else
708               echo "autopoint: File $finaldestfile has been locally modified." 1>&2
709               mismatch=yes
710               diff -c "$work_dir/archive/$file" "$finaldestfile" | sed -e "1s,$work_dir/archive/,," >> "$mismatchfile"
711             fi
712           fi
713         fi
714       }
715       if test -n "$allpodirs"; then
716         for dir in $podirs; do
717           func_compare_to_destfile "$dir/$destfile"
718         done
719       else
720         func_compare_to_destfile "$destfile"
721       fi
722     fi
723   done
724   if test -n "$mismatch"; then
725     rm -rf "$work_dir"
726     func_fatal_error "Some files have been locally modified. Not overwriting them because --force has not been specified. For your convenience, you find the local modifications in the file '$mismatchfile'."
727   fi
728   rm -rf "$tmp"
729 fi
730
731 # func_mkdir_for to
732 # ensures the directory that would the given file exists.
733 # 'to' is a relative pathname, relative to the current directory.
734 func_mkdir_for ()
735 {
736   base=`echo "$1" | sed -e 's,/[^/]*$,,'`
737   if test "X$base" != "X$1" && test -n "$base"; then
738     func_mkdir_for "$base"
739     # Recompute base. It was clobbered by the recursive call.
740     base=`echo "$1" | sed -e 's,/[^/]*$,,'`
741     test -d "$base" || { echo "Creating directory $base"; mkdir "$base"; }
742   fi
743 }
744
745 # func_copy from to
746 # copies a file.
747 # 'from' is a relative pathname, relative to the current directory.
748 # 'to' is a relative pathname, relative to the current directory.
749 func_copy ()
750 {
751   if $doit; then
752     func_mkdir_for "$2"
753     rm -f "$2"
754     echo "Copying file $2"
755     cp "$1" "$2"
756   else
757     echo "Copy file $2"
758   fi
759 }
760
761 # func_backup to
762 # makes a backup of a file that is about to be overwritten or replaced.
763 # 'to' is a relative pathname, relative to the current directory.
764 func_backup ()
765 {
766   if $doit; then
767     if test -f "$1"; then
768       rm -f "$1~"
769       cp -p "$1" "$1~"
770     fi
771   fi
772 }
773
774 # Now copy the files.
775 for file in `find "$work_dir/archive" -type f -print | sed -e "s,^$work_dir/archive/,," | LC_ALL=C sort`; do
776   func_destfile "$file"
777   if test -n "$destfile"; then
778     func_copy_to_destfile ()
779     {
780       finaldestfile="$1"
781       mustcopy=
782       if test -f "$finaldestfile"; then
783         if func_compare "$finaldestfile" "$work_dir/archive/$file"; then
784           if test -n "$force"; then
785             # Overwrite locally modified file.
786             mustcopy=yes
787           fi
788           # If --force is not specified, don't overwrite locally modified files
789           # for which GNU gettext is a shared owner.
790         fi
791       else
792         mustcopy=yes
793       fi
794       if test -n "$mustcopy"; then
795         func_backup "$finaldestfile"
796         func_copy "$work_dir/archive/$file" "$finaldestfile"
797       fi
798     }
799     if test -n "$allpodirs"; then
800       for dir in $podirs; do
801         func_copy_to_destfile "$dir/$destfile"
802       done
803     else
804       func_copy_to_destfile "$destfile"
805     fi
806   fi
807 done
808
809 # That's it.
810 rm -rf "$work_dir"
811 exit 0