Fixed package group and set license
[platform/upstream/findutils.git] / import-gnulib.sh
1 #! /bin/sh
2 #
3 # import-gnulib.sh -- imports a copy of gnulib into findutils
4 # Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010,
5 #               2011 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 3 of the License, or
10 # (at your option) 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, see <http://www.gnu.org/licenses/>.
19 #
20 ##########################################################################
21 #
22 # This script is intended to populate the "gl" directory
23 # with a subset of the gnulib code, as provided by "gnulib-tool".
24 #
25 # To use it, just run this script with the top-level source directory
26 # as your working directory.
27
28 # If CDPATH is set, it will sometimes print the name of the directory
29 # to which you have moved.  Unsetting CDPATH prevents this, as does
30 # prefixing it with ".".
31 unset CDPATH
32
33 ## Defaults
34 configfile="./import-gnulib.config"
35 need_checkout=yes
36 gldest=gl
37
38 # If $GIT_CLONE_DEPTH is not set, apply a default.
39 : ${GIT_CLONE_DEPTH:=4}
40
41
42 # Remember arguments for comments we inject into output files
43 original_cmd_line_args="$@"
44
45 usage() {
46     cat >&2 <<EOF
47 usage: $0 [-d gnulib-directory] [-a]
48
49 The default behaviour is to check out the Gnulib code via anonymous
50 CVS (or update it if there is a version already checked out).  The
51 checkout or update is performed to the gnulib version indicated in
52 the configuration file $configfile.
53
54 If you wish to work with a different version of gnulib, use the -d option
55 to specify the directory containing the gnulib code.
56
57 The -a option skips the import of the gnulib code, and just generates
58 the output files (for example 'configure').
59 EOF
60 }
61
62 do_submodule () {
63     local sm_name="$1"
64     if test -f .gitmodules; then
65         if git config --file \
66             .gitmodules "submodule.${sm_name}.url" >/dev/null; then
67             # Submodule config in .gitmodules is already in place.
68             # Copy the submodule config into .git.
69             git submodule init || exit $?
70             # Update the gnulib module.
71             git submodule update || exit $?
72         else
73             # .gitmodules should include gnulib.
74             cat >&2 <<EOF
75 The .gitmodules file is present, but does not list ${sm_name}.
76 This version of findutils expects it to be there.
77 Please report this as a bug to bug-findutils@gnu.org.
78 The .gitmodules file contains this:
79 EOF
80             cat .gitmodules >&2
81             exit 1
82         fi
83     else
84         # findutils should have .gitmodules
85         cat >&2 <<EOF
86 The .gitmodules file is missing.  This version of findutils expects it
87 to be there.  Please report this as a bug to bug-findutils@gnu.org.
88 EOF
89         exit 1
90     fi
91 }
92
93 run_gnulib_tool() {
94     local tool="$1"
95     if test -f "$tool"
96     then
97         true
98     else
99         echo "$tool does not exist, did you specify the right directory?" >&2
100         exit 1
101     fi
102
103     if test -x "$tool"
104     then
105         true
106     else
107         echo "$tool is not executable" >&2
108         exit 1
109     fi
110
111
112     if [ -d "${gldest}" ]
113     then
114         echo "Warning: directory ${gldest} already exists." >&2
115     else
116         mkdir "${gldest}"
117     fi
118
119     set -x
120     if "$tool" --import --symlink --with-tests \
121         --dir=. --lib=libgnulib \
122         --source-base="${gldest}"/lib \
123         --m4-base="${gldest}"/m4 --local-dir=gnulib-local $modules
124     then
125         set +x
126     else
127         set +x
128         echo "$tool failed, exiting." >&2
129         exit 1
130     fi
131
132     # gnulib-tool does not remove broken symlinks leftover from previous runs;
133     # this assumes GNU find, but should be a safe no-op if it is not
134     find -L "${gldest}" -lname '*' -delete 2>/dev/null || :
135 }
136
137 rehack() {
138     printf "Updating the license of $1... "
139     # Use cp to get the permissions right first
140     cp -fp "$1" "$1".new
141     sed -e  \
142 's/Free Software Foundation\([;,]\) either ''version [2]/Free Software Foundation\1 either version 3/' < "$1" > "$1".new
143     if cmp "$1" "$1".new >/dev/null
144     then
145         echo "no change needed."
146         rm -f "$1".new
147     else
148         rm -f "$1" && mv "$1".new "$1"
149         echo "done."
150     fi
151 }
152
153
154
155 copyhack() {
156     src="$1"
157     dst="$2"
158     shift 2
159     if test -d "$dst"
160     then
161         dst="$dst"/"$(basename $src)"
162     fi
163     cp -fp "$src" "$dst" && rehack "$dst"
164 }
165
166
167 update_licenses() {
168     for f in $gpl3_update_files
169     do
170       rehack "$f" || exit
171     done
172 }
173
174
175
176 hack_gnulib_tool_output() {
177     local gnulibdir="${1}"
178     for file in $extra_files
179     do
180       case $file in
181         */mdate-sh | */texinfo.tex) dest=doc;;
182         *) dest=build-aux;;
183       esac
184       copyhack "${gnulibdir}"/"$file" "$dest" || exit
185     done
186
187     cat > gl/Makefile.am <<EOF
188 # Copyright (C) 2004, 2009 Free Software Foundation, Inc.
189 #
190 # This file is free software, distributed under the terms of the GNU
191 # General Public License.  As a special exception to the GNU General
192 # Public License, this file may be distributed as part of a program
193 # that contains a configuration script generated by Automake, under
194 # the same distribution terms as the rest of that program.
195 #
196 # This file was generated by $0 $original_cmd_line_args.
197 #
198 SUBDIRS = lib
199 EXTRA_DIST = m4/gnulib-cache.m4
200 EOF
201 }
202
203
204 refresh_output_files() {
205     autopoint -f &&
206     aclocal -I m4 -I gl/m4     &&
207     autoheader                     &&
208     autoconf                       &&
209     automake --add-missing --copy
210 }
211
212
213 update_version_file() {
214     local gnulib_git_dir="$1"
215     local ver
216     local outfile="lib/gnulib-version.c"
217     local gnulib_version="$( cd ${gnulib_git_dir} && git show-ref -s HEAD )"
218
219     if [ -z "$gnulib_version" ] ; then
220         ver="unknown (locally modified code; no version number available)"
221     else
222         ver="$gnulib_version"
223     fi
224
225
226     cat > "${outfile}".new <<EOF
227 /* This file is automatically generated by $0 and simply records which version of gnulib we used. */
228 const char * const gnulib_version = "$ver";
229 EOF
230     if test -f "$outfile" ; then
231         if diff "${outfile}".new "${outfile}" > /dev/null ; then
232             rm "${outfile}".new
233             return 0
234         fi
235     fi
236     mv "${outfile}".new "${outfile}"
237 }
238
239
240 check_merge_driver() {
241     local config_file=".git/config"
242     fixmsg="
243
244 We recommend that you use a git merge driver for the ChangeLog file.
245 This simplifies the task of merging branches.
246 Please see the README section in gnulib/gnulib/lib/git-merge-changelog.c
247
248 If you've read that and don't want to use it, just set the environment variable
249 DO_NOT_WANT_CHANGELOG_DRIVER.
250
251 Example:
252   git config merge.merge-changelog.name 'GNU-style ChangeLog merge driver'
253   git config merge.merge-changelog.driver '/usr/local/bin/git-merge-changelog  %O %A %B'
254   echo 'ChangeLog    merge=merge-changelog' >> .gitattributes
255 "
256     if [ -z "$DO_NOT_WANT_CHANGELOG_DRIVER" ] ; then
257         if git branch | egrep -q '\* *(master|rel-)'; then
258         # We are on the master branch or a release branch.
259         # Perhaps the user is simply building from git sources.
260         # Issue our message as a warning rather than an error.
261             fatal=false
262             label="Warning"
263         else
264             fatal=true
265             label="ERROR"
266         fi
267     else
268         fatal=false
269         label="Warning"
270     fi
271     if git config --get  merge.merge-changelog.name >/dev/null ; then
272         driver="$(git config --get merge.merge-changelog.driver |
273                   sed -e 's/[   ].*//')"
274         if [ $? -eq 0 ]; then
275             if ! [ -x "$driver" ]; then
276                 echo "ERROR: Merge driver $driver is not executable." >&2
277                 echo "ERROR: Please fix $config_file or install $driver" >&2
278                 # Always fatal - if configured, the merge driver should work.
279                 exit 1
280             else
281                 if [ -f .gitattributes ] ; then
282                     echo "The ChangeLog merge driver configuration seems OK."
283                 else
284                     echo "$label"': you have no .gitattributes file' >&2
285                     echo "$fixmsg" >&2
286                     if $fatal; then
287                         exit 1
288                     fi
289                 fi
290             fi
291         else
292             echo "$label"': There is no driver specified in [merge "merge-changelog"] in' "$config_file" >&2
293             echo "$fixmsg" >&2
294             if $fatal; then
295                 exit 1
296             fi
297         fi
298     else
299         echo "$label"': There is no name specified in [merge "merge-changelog"] in' "$config_file" >&2
300         echo "$fixmsg" >&2
301         if $fatal; then
302             exit 1
303         fi
304     fi
305 }
306
307
308 record_config_change() {
309     # $1: name of the import-gnulib.config file
310     # $2: name of the last.config file
311     echo "Recording current config from $1 in $2"
312     if ! cp  "$1" "$2"; then
313         rm -f "$2"
314         false
315     fi
316 }
317
318
319 check_old_gnulib_dir_layout() {
320     # We used to keep the gnulib git repo in ./gnulib-git/ and use
321     # ./gnulib/ as the destination directory into which we installed
322     # (part of) the gnulib code.  This changed and now ./gnulib/
323     # contains the gnulib submodule and the destination directory is
324     # ./gl/.  In other words, ./gnulib/ used to be the destination,
325     # but now it is the source.
326 fixmsg="\
327 Findutils now manages the gnulib source code as a git submodule.
328
329 If you are still using the directory layout in which the git tree for
330 gnulib lives in ./gnulib-git/, please fix this and re-run import-gnulib.sh.
331 The fix is very simple; please delete both ./gnulib/ and ./gnulib-git.
332
333 This wasn't done automatically for you just in case you had any local changes.
334 "
335
336     if test -d ./gl/; then
337         # Looks like we're already in the new directory layout.
338         true
339     elif test -d ./gnulib-git/; then
340         cat >&2 <<EOF
341 You still have a ./gnulib-git directory.
342
343 $fixmsg
344 EOF
345         false
346     else
347         # No ./gl/ and no ./gnulib-git/.   If ./gnulib/ exists, it might
348         # be either.   If there is no ./gnulib/ we are safe to proceed anyway.
349         if test -d ./gnulib/; then
350             if test -e ./gnulib/.git; then
351                 # Looks like it is the submodule.
352                 true
353             else
354         cat >&2 <<EOF
355 You have a ./gnulib directory which does not appear to be a submodule.
356
357 $fixmsg
358 EOF
359         false
360             fi
361         else
362             # No ./gl/, no ./gnulib/, no ./gnulib-git/.
363             # It is safe to proceed anyway.
364             true
365         fi
366     fi
367 }
368
369 main() {
370     ## Option parsing
371     local gnulibdir=/doesnotexist
372     while getopts "d:a" opt
373     do
374       case "$opt" in
375           d)  gnulibdir="$OPTARG" ; need_checkout=no ;;
376           a)  refresh_output_files && update_licenses ; exit $? ;;
377           **) usage; exit 1;;
378       esac
379     done
380
381     # We need the config file to tell us which modules
382     # to use, even if we don't want to know the gnulib version.
383     . $configfile || exit 1
384
385     ## If -d was not given, do update
386     if [ $need_checkout = yes ] ; then
387         if ! git version > /dev/null; then
388             cat >&2 <<EOF
389 You now need the tool 'git' in order to check out the correct version
390 of the gnulib code.  See http://git.or.cz/ for more information about git.
391 EOF
392             exit 1
393         fi
394
395         # Check for the old directory layout.
396         echo "Checking the submodule directory layout... "
397         check_old_gnulib_dir_layout || exit 1
398         echo "The submodule directory layout looks OK."
399
400         do_submodule gnulib
401         check_merge_driver
402         gnulibdir=gnulib
403     else
404         echo "Warning: using gnulib code which already exists in $gnulibdir" >&2
405     fi
406
407     local tool="${gnulibdir}"/gnulib-tool
408     if run_gnulib_tool "${tool}" &&
409         hack_gnulib_tool_output "${gnulibdir}" &&
410         refresh_output_files &&
411         update_licenses &&
412         update_version_file "${gnulibdir}"
413     then
414         echo Done.
415     else
416         echo FAILED >&2
417         exit 1
418     fi
419 }
420
421 main "$@"