Initial import of linux-libre deblob scripts from https://www.fsfla.org/svn/fsfla...
[librecmc/linux-deblob.git] / 3.3-rc7-gnu / deblob-main
1 #! /bin/sh
2
3 # Copyright (C) 2008-2012 Alexandre Oliva <lxoliva@fsfla.org>
4
5 # This program is part of GNU Linux-libre, a GNU project that
6 # publishes scripts to clean up Linux so as to make it suitable for
7 # use in the GNU Project and in Free System Distributions.
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 2 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, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # 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, write to the Free Software
21 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 # USA
23
24 # deblob-main - prepare a GNU Linux-libre tarball out of a non-libre
25 # Linux tarball.  It expects the Linux release (mver, say 3.0) as the
26 # first argument, the gnu sub-release (extra) as the second optional
27 # argument, and the patch release (sver, say .13) as an optional third
28 # argument.  mver and sver are pasted together to form kver.
29
30 # linux-$kver.tar.bz2 and deblob-$mver must exist in the current
31 # directory, and the line that sets kver and extra in deblob-$mver
32 # must match mver and extra.
33
34 # The resulting tarball is put in linux-libre-$kver-gnu$extra.tar.bz2.
35 # An uncompressed xdelta that produces linux-libre-$kver-gnu$extra.tar
36 # out of linux-$kver.tar is put in linux-libre-$kver-gnu$extra.xdelta.
37 # This xdelta can be distributed to enable third parties to easily
38 # reconstruct the binary tarball starting out of sources downloaded
39 # from kernel.org, but without distributing non-Free Software
40 # yourself, because xdelta (unlike patches) is not reversible: the
41 # removed bits are not present in it at all.
42
43 # xdelta version 3 uses different command line syntax, and it switched
44 # to the more standardized but less efficient vcdiff file format.
45 # This script will also produce a vcdiff file if xdelta3 is present,
46 # and it expects the xdelta program to use the version 1 syntax.
47
48 # To enable you to check the differences between the tarballs, a patch
49 # file is generated in linux-libre-$kver-gnu$extra.patch.  This patch
50 # file contains the non-Free blobs, even though in reversed form, so
51 # its distribution is discouraged.
52
53 # The tar files and binary deltas are finally compressed with bzip2,
54 # and optionally with lzip and xz too, if the compressors are
55 # available.
56
57 # At the end, the script attempts to generate a digital signature for
58 # the newly-created tarball.  This is the last thing the script does,
59 # so interrupting it at that point to skip the signing won't fail to
60 # do anything else.
61
62 # It is safe to interrupt the script at any other point.  When it gets
63 # a ^C (other than during signing), it starts cleaning up all of its
64 # temporary and output files.  If you insist, it may leave junk
65 # behind, and then it will refuse to run again before you clean it up
66 # by hand.  It takes extra care to avoid overwriting useful files.
67
68 # If deblob-$mver finds any unexpected situation, it will error out,
69 # and then deblob-main will quit.  Pass --force to deblob-main, before
70 # any other argument, for deblob-main to ignore any such situations.
71
72 case $1 in
73 --force) force=--force; shift;;
74 *) force=;;
75 esac
76
77 mver=$1 extra=$2 sver=$3
78 kver=$mver$sver gnu=gnu$extra
79 deblob= dir=`echo "$0" | sed 's,[^/]*$,,;s,^$,.,;s,/*$,,'`
80
81 if test ! -f linux-$kver.tar.bz2; then
82   echo linux-$kver.tar.bz2 does not exist >&2
83   exit 1
84 fi
85
86 if test -f deblob-$mver; then
87   deblob=deblob-$mver
88 elif test -f deblob; then
89   deblob=deblob
90 elif test -f $dir/deblob-$mver; then
91   cp $dir/deblob-$mver deblob
92   deblob=deblob
93 else
94   echo deblob does not exist >&2
95   exit 1
96 fi
97
98 x1="kver=$mver extra=$extra"
99 x2=`grep "^kver=[^ ]* extra=" $deblob`
100 if test "$x1" = "$x2"; then
101   :
102 else
103   echo deblob script does not match command-line arguments >&2
104   echo expected: $x1 >&2
105   echo found   : $x2 >&2
106   exit 1
107 fi
108
109 cleanup=
110
111 for f in \
112   linux-libre-$kver-$gnu.tar.bz2 \
113   linux-libre-$kver-$gnu.tar.bz2.asc \
114   linux-libre-$kver-$gnu.tar.bz2.sign \
115   linux-libre-$kver-$gnu.tar.xz \
116   linux-libre-$kver-$gnu.tar.xz.asc \
117   linux-libre-$kver-$gnu.tar.xz.sign \
118   linux-libre-$kver-$gnu.tar.lz \
119   linux-libre-$kver-$gnu.tar.lz.asc \
120   linux-libre-$kver-$gnu.tar.lz.sign \
121   linux-$kver.tar \
122   linux-libre-$kver-$gnu.tar \
123   linux-libre-$kver-$gnu.patch \
124   linux-libre-$kver-$gnu.log \
125   linux-libre-$kver-$gnu.vcdiff \
126   linux-libre-$kver-$gnu.vcdiff.bz2 \
127   linux-libre-$kver-$gnu.vcdiff.bz2.asc \
128   linux-libre-$kver-$gnu.vcdiff.bz2.sign \
129   linux-libre-$kver-$gnu.vcdiff.xz \
130   linux-libre-$kver-$gnu.vcdiff.xz.asc \
131   linux-libre-$kver-$gnu.vcdiff.xz.sign \
132   linux-libre-$kver-$gnu.vcdiff.lz \
133   linux-libre-$kver-$gnu.vcdiff.lz.asc \
134   linux-libre-$kver-$gnu.vcdiff.lz.sign \
135   linux-libre-$kver-$gnu.xdelta \
136   linux-libre-$kver-$gnu.xdelta.bz2 \
137   linux-libre-$kver-$gnu.xdelta.bz2.asc \
138   linux-libre-$kver-$gnu.xdelta.bz2.sign \
139   linux-libre-$kver-$gnu.xdelta.xz \
140   linux-libre-$kver-$gnu.xdelta.xz.asc \
141   linux-libre-$kver-$gnu.xdelta.xz.sign \
142   linux-libre-$kver-$gnu.xdelta.lz \
143   linux-libre-$kver-$gnu.xdelta.lz.asc \
144   linux-libre-$kver-$gnu.xdelta.lz.sign \
145 ; do
146   if test -f $f; then
147     echo $f already exists >&2
148     exit 1
149   fi
150   cleanup="$cleanup $f"
151 done
152
153 for d in \
154   linux-$kver \
155   linux-libre-$kver-$gnu \
156   orig-linux-$kver \
157 ; do
158   if test -d $d; then
159     echo $d already exists >&2
160     exit 1
161   fi
162   cleanup="$cleanup $d"
163 done
164
165 if test -f $dir/deblob-$kver; then
166   if cmp $dir/deblob-$kver $deblob; then
167     :
168   else
169     echo $dir/deblob-$kver and $deblob are different >&2
170     exit 1
171   fi
172 fi
173
174 if test ! -f deblob-check; then
175   if test -f $dir/deblob-check; then
176     cp $dir/deblob-check deblob-check
177   fi
178 else
179   if test -f $dir/deblob-check; then
180     if cmp $dir/deblob-check deblob-check; then
181       :
182     else
183       echo $dir/deblob-check and deblob-check are different >&2
184       exit 1
185     fi
186   fi
187 fi
188
189 trap "status=$?; echo cleaning up...; rm -rf $cleanup; (exit $status); exit" 0 1 2 15
190
191 set -e
192
193 echo Uncompressing linux-$kver.tar.bz2 into linux-$kver.tar
194 rm -rf linux-$kver linux-$kver.tar
195 bunzip2 < linux-$kver.tar.bz2 > linux-$kver.tar
196
197 echo Extracting linux-$kver.tar into linux-$kver
198 tar -xf linux-$kver.tar
199 rm -rf linux-libre-$kver-$gnu linux-libre-$kver-$gnu.tar
200
201 echo Copying linux-$kver to linux-libre-$kver-$gnu
202 cp linux-$kver.tar linux-libre-$kver-$gnu.tar
203 cp -lR linux-$kver/. linux-libre-$kver-$gnu
204
205 rm -f linux-libre-$kver-$gnu.log linux-libre-$kver-$gnu.log.tmp
206 echo Deblobbing within linux-libre-$kver-$gnu, saving output to linux-libre-$kver-$gnu.log
207 # We can't just pipe deblob into tee, for then we fail to detect
208 # error conditions.  Use file renaming to tell whether we succeeded.
209 if (cd linux-libre-$kver-$gnu && /bin/sh ../$deblob $force) 2>&1; then
210   mv linux-libre-$kver-$gnu.log.tmp linux-libre-$kver-$gnu.log
211 fi | tee linux-libre-$kver-$gnu.log.tmp
212 if test ! -f linux-libre-$kver-$gnu.log; then
213   mv linux-libre-$kver-$gnu.log.tmp linux-libre-$kver-$gnu.log
214   echo $deblob failed, aborting >&2
215   exit 1
216 fi
217 rm -f linux-libre-$kver-$gnu.patch
218
219 # Do not copy these scripts for now, deblob-check regards itself as a blob.
220 # cp -p $0 $deblob deblob-check linux-libre-$kver-$gnu
221
222 echo Generating linux-libre-$kver-$gnu.patch
223 diff -druN linux-$kver linux-libre-$kver-$gnu > linux-libre-$kver-$gnu.patch || :
224
225 echo Removing removed or modified files from linux-libre-$kver-$gnu.tar
226 diff -rq linux-$kver linux-libre-$kver-$gnu |
227 sed -n "
228   s,^Only in \\(linux-$kver\\(/.*\\)\\?\\): \\(.*\\),\1/\3,p;
229   s,^Files \\(linux-$kver\\)/\\(.*\\) and linux-libre-$kver-$gnu/\\2 differ,\\1/\\2,p;
230 " |
231 xargs tar --delete -f linux-libre-$kver-$gnu.tar
232
233 echo Adding modified or added files to linux-libre-$kver-$gnu.tar
234 rm -rf orig-linux-$kver
235 mv linux-$kver orig-linux-$kver
236 mv linux-libre-$kver-$gnu linux-$kver
237 diff -rq orig-linux-$kver linux-$kver |
238 sed -n "
239   s,^Files orig-\\(linux-$kver/.*\\) and \\1 differ,\\1,p;
240   s,^Only in \\(linux-$kver\\(/.*\\)\\?\\): \\(.*\\),\\1/\\3,p;
241 " |
242 xargs tar --append -f linux-libre-$kver-$gnu.tar
243
244 echo Wiping out extracted trees
245 rm -rf linux-$kver orig-linux-$kver
246
247 echo Creating vcdiff between linux-$kver.tar and linux-libre-$kver-$gnu.tar
248 xdelta3 -e -9 -S djw -s linux-$kver.tar linux-libre-$kver-$gnu.tar linux-libre-$kver-$gnu.vcdiff || : # don't fail if xdelta3 is not present
249
250 echo Creating xdelta between linux-$kver.tar and linux-libre-$kver-$gnu.tar
251 xdelta delta -0 linux-$kver.tar linux-libre-$kver-$gnu.tar linux-libre-$kver-$gnu.xdelta || : # xdelta returns nonzero on success
252
253 echo Compressing binary deltas and linux-libre-$kver-$gnu.tar
254 rm -f linux-$kver.tar
255 if test -f linux-libre-$kver-$gnu.vcdiff; then
256   bzip2 -k9 linux-libre-$kver-$gnu.vcdiff
257   xz -k9 linux-libre-$kver-$gnu.vcdiff || :
258   lzip -k9 linux-libre-$kver-$gnu.vcdiff || :
259   rm -f linux-libre-$kver-$gnu.vcdiff
260 fi
261 if test -f linux-libre-$kver-$gnu.xdelta; then
262   bzip2 -k9 linux-libre-$kver-$gnu.xdelta
263   xz -k9 linux-libre-$kver-$gnu.xdelta || :
264   lzip -k9 linux-libre-$kver-$gnu.xdelta || :
265   rm -f linux-libre-$kver-$gnu.xdelta
266 fi
267 bzip2 -k9 linux-libre-$kver-$gnu.tar
268 xz -k9 linux-libre-$kver-$gnu.tar || :
269 lzip -k9 linux-libre-$kver-$gnu.tar || :
270 rm -f linux-libre-$kver-$gnu.tar
271
272 trap "status=$?; (exit $status); exit" 0 1 2 15
273
274 echo Done except for signing, feel free to interrupt
275 for f in \
276   linux-libre-$kver-$gnu.tar.bz2 \
277   linux-libre-$kver-$gnu.tar.xz \
278   linux-libre-$kver-$gnu.tar.lz \
279   linux-libre-$kver-$gnu.vcdiff.bz2 \
280   linux-libre-$kver-$gnu.vcdiff.xz \
281   linux-libre-$kver-$gnu.vcdiff.lz \
282   linux-libre-$kver-$gnu.xdelta.bz2 \
283   linux-libre-$kver-$gnu.xdelta.xz \
284   linux-libre-$kver-$gnu.xdelta.lz \
285 ; do
286   if test -f $f; then
287     gpg -a --detach-sign $f
288     mv $f.asc $f.sign
289   fi
290 done
291
292 echo All set, please review linux-libre-$kver-$gnu.patch
293
294 exit 0