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