1#!/bin/bash
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6#
7# This tool generates full update packages for the update system.
8# Author: Darin Fisher
9#
10# In here to use the local common.sh to allow the full mars to have unfiltered files
11
12. $(dirname "$0")/common.sh
13
14# -----------------------------------------------------------------------------
15
16print_usage() {
17  notice "Usage: $(basename $0) [OPTIONS] ARCHIVE DIRECTORY"
18}
19
20if [ $# = 0 ]; then
21  print_usage
22  exit 1
23fi
24
25if [ $1 = -h ]; then
26  print_usage
27  notice ""
28  notice "The contents of DIRECTORY will be stored in ARCHIVE."
29  notice ""
30  notice "Options:"
31  notice "  -h  show this help text"
32  notice ""
33  exit 1
34fi
35
36# -----------------------------------------------------------------------------
37
38archive="$1"
39targetdir="$2"
40# Prevent the workdir from being inside the targetdir so it isn't included in
41# the update mar.
42if [ $(echo "$targetdir" | grep -c '\/$') = 1 ]; then
43  # Remove the /
44  targetdir=$(echo "$targetdir" | sed -e 's:\/$::')
45fi
46workdir="$targetdir.work"
47updatemanifestv2="$workdir/updatev2.manifest"
48updatemanifestv3="$workdir/updatev3.manifest"
49targetfiles="updatev2.manifest updatev3.manifest"
50
51mkdir -p "$workdir"
52
53# Generate a list of all files in the target directory.
54pushd "$targetdir"
55if test $? -ne 0 ; then
56  exit 1
57fi
58
59if [ ! -f "precomplete" ]; then
60  if [ ! -f "Contents/Resources/precomplete" ]; then
61    notice "precomplete file is missing!"
62    exit 1
63  fi
64fi
65
66list_files files
67
68popd
69
70# Add the type of update to the beginning of the update manifests.
71> $updatemanifestv2
72> $updatemanifestv3
73notice ""
74notice "Adding type instruction to update manifests"
75notice "       type complete"
76echo "type \"complete\"" >> $updatemanifestv2
77echo "type \"complete\"" >> $updatemanifestv3
78
79notice ""
80notice "Adding file add instructions to update manifests"
81num_files=${#files[*]}
82
83for ((i=0; $i<$num_files; i=$i+1)); do
84  f="${files[$i]}"
85
86  if check_for_add_if_not_update "$f"; then
87    make_add_if_not_instruction "$f" "$updatemanifestv3"
88    if check_for_add_to_manifestv2 "$f"; then
89      make_add_instruction "$f" "$updatemanifestv2" "" 1
90    fi
91  else
92    make_add_instruction "$f" "$updatemanifestv2" "$updatemanifestv3"
93  fi
94
95  dir=$(dirname "$f")
96  mkdir -p "$workdir/$dir"
97  $XZ --compress --x86 --lzma2 --format=xz --check=crc64 --force --stdout "$targetdir/$f" > "$workdir/$f"
98  copy_perm "$targetdir/$f" "$workdir/$f"
99
100  targetfiles="$targetfiles \"$f\""
101done
102
103# Append remove instructions for any dead files.
104notice ""
105notice "Adding file and directory remove instructions from file 'removed-files'"
106append_remove_instructions "$targetdir" "$updatemanifestv2" "$updatemanifestv3"
107
108$XZ --compress --x86 --lzma2 --format=xz --check=crc64 --force "$updatemanifestv2" && mv -f "$updatemanifestv2.xz" "$updatemanifestv2"
109$XZ --compress --x86 --lzma2 --format=xz --check=crc64 --force "$updatemanifestv3" && mv -f "$updatemanifestv3.xz" "$updatemanifestv3"
110
111eval "$MAR -C \"$workdir\" -c output.mar $targetfiles"
112mv -f "$workdir/output.mar" "$archive"
113
114# cleanup
115rm -fr "$workdir"
116
117notice ""
118notice "Finished"
119notice ""
120