Add 64 bit Linux Client building
[working/Evergreen.git] / Open-ILS / xul / staff_client / external / make_updates.sh
1 #!/bin/bash
2
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 #
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
10 #
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
14 # License.
15 #
16 # The Original Code is Mozilla Update Packaging.
17 #
18 # The Initial Developer of the Original Code is
19 # Merrimack Valley Library Consortium.
20 # Portions created by the Initial Developer are Copyright (C) 2010
21 # the Initial Developer. All Rights Reserved.
22 #
23 # Contributor(s):
24 #  Thomas Berezansky <tsbere@mvlc.org>
25 #
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
37 #
38 # ***** END LICENSE BLOCK *****
39
40 # Portions of this code were based on code by Darin Fisher found here:
41 # http://mxr.mozilla.org/mozilla/source/tools/update-packaging/
42
43 prefix=${1:-/openils/var/updates}
44 BZIP2=${BZIP2:-bzip2}
45
46 GEN_UPDATES=0
47 WIN_UPDATES=0
48 LINUX32_UPDATES=0
49 LINUX64_UPDATES=0
50 EXT_UPDATES=0
51 CLIENTS=0
52 case "$2" in
53         generic-updates*)
54         echo "Building Generic Updates only"
55         GEN_UPDATES=1
56         ;;
57         win-updates*)
58         echo "Building Windows Updates only"
59         WIN_UPDATES=1
60         ;;
61         linux32-updates*)
62         echo "Building Linux (32 bit) Updates only"
63         LINUX32_UPDATES=1
64         ;;
65         linux64-updates*)
66         echo "Building Linux (64 bit) Updates only"
67         LINUX32_UPDATES=1
68         ;;
69         extension-updates*)
70         echo "Building Extension Updates only"
71         EXT_UPDATES=1
72         ;;
73         *)
74         echo "Building All Updates"
75         GEN_UPDATES=1
76         WIN_UPDATES=1
77         LINUX32_UPDATES=1
78         LINUX64_UPDATES=1
79         EXT_UPDATES=1
80         ;;
81 esac
82 case "$2" in
83         extension-updates*)
84         echo "Extension only - No client"
85         ;;
86         *-client)
87         echo "Building Client(s)"
88         CLIENTS=1
89         ;;
90         *)
91         echo "Not Building Client(s)"
92         ;;
93 esac
94
95 function unwrap_update
96 {
97         SOURCE="$1"
98         DEST="$2"
99         $MAR -C "$2" -x "$1"
100         find "$2" -type f -exec mv {} {}.bz2 \;
101         find "$2" -type f -name '*.bz2' -exec $BZIP2 -d {} \;
102 }
103
104 function prep_update
105 {
106         NEW="$1"
107         OLD="$2"
108         WORK="$3"
109         MANIFEST="$WORK/update.manifest"
110         ARCHIVEFILES="update.manifest"
111         rm -rf "$WORK"
112         mkdir -p "$WORK"
113         rm -f "$MANIFEST"
114         for FILE in `find "$NEW" -type f`; do
115                 check_file $FILE
116         done
117         for FILE in `find "$OLD" -type f`; do
118                 remove_file $FILE
119         done
120         $BZIP2 -z9 "$MANIFEST"
121         mv "$MANIFEST.bz2" "$MANIFEST"
122         rm -rf "$OLD"
123 }
124
125 function check_file
126 {
127         CHECK_FILE="${1#$NEW/}"
128         if [ $CHECK_FILE == "update.manifest" -o $CHECK_FILE == "defaults/preferences/developers.js" -o $CHECK_FILE == "defaults/preferences/aa_per_machine.js" ]; then
129         echo "Skipping $CHECK_FILE";
130                 return;
131         fi
132         DIR=$(dirname "$WORK/$CHECK_FILE")
133         if [ ! -f "$OLD/$CHECK_FILE" ]; then
134                 echo "add \"$CHECK_FILE\"" >> "$MANIFEST"
135                 mkdir -p "$DIR"
136                 $BZIP2 -cz9 "$NEW/$CHECK_FILE" > "$WORK/$CHECK_FILE"
137                 if [ -x "$NEW/$CHECK_FILE" ]; then
138                         chmod 0755 "$WORK/$CHECK_FILE"
139                 else
140                         chmod 0644 "$WORK/$CHECK_FILE"
141                 fi
142                 ARCHIVEFILES="$ARCHIVEFILES \"$CHECK_FILE\""
143                 return
144         elif ! diff "$OLD/$CHECK_FILE" "$NEW/$CHECK_FILE" > /dev/null; then
145                 mkdir -p "$DIR"
146                 $MBSDIFF "$OLD/$CHECK_FILE" "$NEW/$CHECK_FILE" "$WORK/$CHECK_FILE.patch"
147                 $BZIP2 -z9 "$WORK/$CHECK_FILE.patch"
148                 $BZIP2 -cz9 "$NEW/$CHECK_FILE" > "$WORK/$CHECK_FILE"
149                 PATCHSIZE=`du -b "$WORK/$CHECK_FILE.patch.bz2"`
150                 FULLSIZE=`du -b "$WORK/$CHECK_FILE"`
151                 PATCHSIZE="${PATCHSIZE%%        *}"
152                 FULLSIZE="${FULLSIZE%%  *}"
153                 if [ $PATCHSIZE -lt $FULLSIZE ]; then
154                         rm -f "$WORK/$CHECK_FILE"
155                         mv "$WORK/$CHECK_FILE.patch.bz2" "$WORK/$CHECK_FILE.patch"
156                         echo "patch \"$CHECK_FILE.patch\" \"$CHECK_FILE\"" >> "$MANIFEST"
157                         ARCHIVEFILES="$ARCHIVEFILES \"$CHECK_FILE.patch\""
158                 else
159                         rm -f "$WORK/$CHECK_FILE.patch.bz2"
160                         if [ -x "$NEW/$CHECK_FILE" ]; then
161                                 chmod 0755 "$WORK/$CHECK_FILE"
162                         else
163                                 chmod 0644 "$WORK/$CHECK_FILE"
164                         fi
165                         echo "add \"$CHECK_FILE\"" >> "$MANIFEST"
166                         ARCHIVEFILES="$ARCHIVEFILES \"$CHECK_FILE\""
167                 fi
168         fi
169         rm -f "$OLD/$CHECK_FILE"
170 }
171
172 function remove_file
173 {
174         RM_FILE="${1#$OLD/}"
175         if [ $RM_FILE != "update.manifest" -a $RM_FILE != "defaults/preferences/developers.js" -a $RM_FILE != "defaults/preferences/aa_per_machine.js" -a $RM_FILE != "defaults/preferences/autoupdate.js" -a $RM_FILE != "defaults/preferences/autochannel.js" ]; then
176                 echo "remove \"$RM_FILE\"" >> "$MANIFEST"
177         fi
178 }
179
180 function build_update
181 {
182         eval "$MAR -C \"$WORK\" -c output.mar $ARCHIVEFILES"
183         mv "$WORK/output.mar" "$1"
184         rm -rf "$WORK"
185 }
186
187 function check_mar
188 {
189         if which mar; then
190                 MAR=${MAR:-mar}
191         fi
192         if which mbsdiff; then
193                 MBSDIFF=${MBSDIFF:-mbsdiff}
194         fi
195         if [ ! -x "$MAR" -o ! -x "$MBSDIFF" ]; then
196                 if [ ! -f "external/mar" -o ! -f "external/mbsdiff" ]; then
197                         wget ftp://ftp.mozilla.org/pub/mozilla.org/xulrunner/mar-generation-tools/mar-generation-tools-linux.zip
198                         unzip mar-generation-tools-linux.zip -d external
199                 fi
200                 MAR="$PWD/external/mar"
201                 MBSDIFF="$PWD/external/mbsdiff"
202         fi
203 }
204
205 function make_full_update
206 {
207         echo "Making full update"
208         rm -rf "oldclient"
209         mkdir -p "oldclient"
210         prep_update client oldclient client.working
211         build_update "full_update.mar"
212         mkdir -p "$PUBPATH"
213         mv full_update.mar "$PUBPATH/$VERSION.mar"
214         echo "Making full update patch def"
215         mkdir -p "$PATCHPATH"
216         HASH=$(sha512sum "$PUBPATH/$VERSION.mar")
217         SIZE=$(du -b "$PUBPATH/$VERSION.mar")
218         echo "<patch type=\"complete\" URL=\"$VERSION.mar\" hashFunction=\"sha512\" hashValue=\"${HASH%% *}\" size=\"${SIZE%%   *}\"/>" > "$PATCHPATH/$VERSION.patchline"
219 }
220
221 function make_partial_update
222 {
223         PREV_VERSION="${1%.mar}"
224         if [ "$VERSION" == "$PREV_VERSION" ]; then
225                 echo "Skipping partial update for same version"
226                 return
227         fi
228         echo "Making partial update from $PREV_VERSION"
229         rm -rf "oldclient"
230         mkdir -p "oldclient"
231         unwrap_update "$ARCHIVEPATH/$1" oldclient
232         prep_update client oldclient client.working
233         build_update "partial_update.mar"
234         mv partial_update.mar "$PUBPATH/$PREV_VERSION-$VERSION.mar"
235         echo "Making partial update patch def"
236         mkdir -p "$PATCHPATH"
237         HASH=$(sha512sum "$PUBPATH/$PREV_VERSION-$VERSION.mar")
238         SIZE=$(du -b "$PUBPATH/$PREV_VERSION-$VERSION.mar")
239         echo "<patch type=\"partial\" URL=\"$PREV_VERSION-$VERSION.mar\" hashFunction=\"sha512\" hashValue=\"${HASH%% *}\" size=\"${SIZE%%      *}\"/>" > "$PATCHPATH/$PREV_VERSION-$VERSION.patchline"
240 }
241
242 function make_partial_updates
243 {
244         echo "Checking for partial update source files"
245         if [ -d "$ARCHIVEPATH" ]; then
246                 for OLDVER in `find "$ARCHIVEPATH" -maxdepth 1 -name '*.mar'`; do
247                         make_partial_update "${OLDVER##*/}"
248                 done
249         fi
250         mkdir -p "$ARCHIVEPATH"
251         echo "Copying full update to archive"
252         cp "$PUBPATH/$VERSION.mar" "$ARCHIVEPATH"
253         echo "Updating current version file"
254         echo "$VERSION" > "$PATCHPATH/VERSION"
255 }
256
257 function cleanup_files
258 {
259         echo "Cleaning up previous update mar files and update patch files"
260         find "$PUBPATH" -maxdepth 1 -name "*.mar" ! -name "*$VERSION.mar" -delete -print
261         find "$PATCHPATH" -maxdepth 1 -name "*.patch" ! -name "*$VERSION.patch" -delete -print
262 }
263
264 # First, do we have the mar and mbsdiff tools?
265 check_mar
266 VERSION=`cat build/VERSION`
267
268 # Generic Updates - No XULRunner packaged, channel of "release"
269 # NOTE: Generic updates CAN update Windows/Linux builds, and will do so if you don't build platform specific ones
270 if [ $GEN_UPDATES -eq 1 ]; then
271         PATCHPATH="$prefix/patch"
272         PUBPATH="$prefix/pub"
273         ARCHIVEPATH="$prefix/archives"
274         if [ $CLIENTS -eq 1 ]; then
275                 make generic-client
276                 mkdir -p "$prefix/pub/clients/"
277                 find "$prefix/pub/clients/" -name '*_client.xpi' -delete
278                 mv evergreen_staff_client.xpi "$prefix/pub/clients/${VERSION}_client.xpi"
279         else
280                 make client_app
281         fi
282         make_full_update
283         make_partial_updates
284         cleanup_files
285 fi
286
287 # Windows Updates - Windows XULRunner, update channel of "win"
288 if [ $WIN_UPDATES -eq 1 ]; then
289         PATCHPATH="$prefix/patch/win"
290         PUBPATH="$prefix/pub/win"
291         ARCHIVEPATH="$prefix/archives/win"
292         if [ $CLIENTS -eq 1 ]; then
293                 make win-client
294                 mkdir -p "$prefix/pub/clients/"
295                 find "$prefix/pub/clients/" -name '*_setup.exe' -delete
296                 mv evergreen_staff_client_setup.exe "$prefix/pub/clients/${VERSION}_setup.exe"
297         else
298                 make win-xulrunner
299         fi
300         make_full_update
301         make_partial_updates
302         cleanup_files
303 fi
304
305 # Linux 32 bit Updates - Linux XULRunner, update channel of "lin"
306 if [ $LINUX32_UPDATES -eq 1 ]; then
307         PATCHPATH="$prefix/patch/lin"
308         PUBPATH="$prefix/pub/lin"
309         ARCHIVEPATH="$prefix/archives/lin"
310         if [ $CLIENTS -eq 1 ]; then
311                 make linux32-client
312                 mkdir -p "$prefix/pub/clients/"
313                 find "$prefix/pub/clients/" -name '*_i686.tar.bz2' -delete
314                 mv evergreen_staff_client_i686.tar.bz2 "$prefix/pub/clients/${VERSION}_i686.tar.bz2"
315         else
316                 make linux32-xulrunner
317         fi
318         make_full_update
319         make_partial_updates
320         cleanup_files
321 fi
322
323 # Linux 64 bit Updates - Linux XULRunner, update channel of "lin64"
324 if [ $LINUX64_UPDATES -eq 1 ]; then
325         PATCHPATH="$prefix/patch/lin64"
326         PUBPATH="$prefix/pub/lin64"
327         ARCHIVEPATH="$prefix/archives/lin64"
328         if [ $CLIENTS -eq 1 ]; then
329                 make linux64-client
330                 mkdir -p "$prefix/pub/clients/"
331                 find "$prefix/pub/clients/" -name '*_x86_64.tar.bz2' -delete
332                 mv evergreen_staff_client_x86_64.tar.bz2 "$prefix/pub/clients/${VERSION}_x86_64.tar.bz2"
333         else
334                 make linux64-xulrunner
335         fi
336         make_full_update
337         make_partial_updates
338         cleanup_files
339 fi
340
341 # Extension Updates
342 # Not really "Updates" so much as "Update", plural for consistency in command.
343 # Extensions don't do partial updates. Or at least not that I found docs for.
344 if [ $EXT_UPDATES -eq 1 ]; then
345         make extension
346         mkdir -p "$prefix/pub/"
347         find "$prefix/pub/" -maxdepth 1 -name '*_extension.xpi' -delete
348         mv evergreen.xpi "$prefix/pub/${VERSION}_extension.xpi" 
349         SHA512=$(sha512sum "$prefix/pub/${VERSION}_extension.xpi")
350         SHA512=${SHA512%% *}
351         sed -e "s|<em:version>.*</em:version>|<em:version>$VERSION</em:version>|" -e "s|<em:updateLink>.*</em:updateLink>|<em:updateLink>https://::HOSTNAME::/updates/${VERSION}_extension.xpi</em:updateLink>|" -e "s|<em:updateHash>.*</em:updateHash>|<em:updateHash>sha512:$SHA512</em:updateHash>|" update.rdf > "$prefix/patch/update.rdf"
352 fi