blob: 4b02629e2f1d7c6db2d7fde44bd302715e3b2170 [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001#!/bin/bash
2#
3# Copyright (C) 2007 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PROGNAME=`basename "$0"`
19
20INSTALL_SCRIPT_NAME=META-INF/com/android/update-script
21
22function cleantmp
23{
24 if [ ! -z "$TMPDIR" ]
25 then
26 rm -rf "$TMPDIR"
27 TMPDIR=
28 fi
29}
30
31function println
32{
33 if [ $# -gt 0 ]
34 then
35 echo "$PROGNAME: $@"
36 fi
37}
38
39function fail
40{
41 println "$@"
42 cleantmp
43 exit 1
44}
45
46function usage
47{
48 println "$@"
49 echo "Usage: $PROGNAME <command> [command-options] <ota-file>"
50 echo " Where <command> is one of:"
51 echo " --dump"
52 echo " Dump a description of the ota file"
53 echo " --dump-script"
54 echo " Dumps the install script to standard out"
55 echo " --append-script <file> -o|--output <outfile>"
56 echo " Append the contents of <file> to the install script"
57 echo " --replace-script <file> -o|--output <outfile>"
58 echo " Replace the install script with the contents of <file>"
59 fail
60}
61
62if [ $# -lt 2 ]
63then
64 usage
65fi
66CMD="$1"
67shift
68if [ "$CMD" = --dump ]
69then
70 CMD_DUMP=1
71 UNPACK_FILE=1
72elif [ "$CMD" = --dump-script ]
73then
74 CMD_DUMP_SCRIPT=1
75elif [ "$CMD" = --append-script ]
76then
77 CMD_APPEND_SCRIPT=1
78 SCRIPT_FILE=$1
79 shift
80 NEEDS_SCRIPT_FILE=1
81 NEEDS_OUTPUT=1
82elif [ "$CMD" = --replace-script ]
83then
84 CMD_REPLACE_SCRIPT=1
85 SCRIPT_FILE=$1
86 shift
87 NEEDS_SCRIPT_FILE=1
88 NEEDS_OUTPUT=1
89else
90 usage "Unknown command $CMD"
91fi
92
93if [ ! -z "$NEED_SCRIPT_FILE" ]
94then
95 if [ -z "$SCRIPT_FILE" -o ! -f "$SCRIPT_FILE" ]
96 then
97 usage "$CMD requires a valid script file"
98 fi
99fi
100
101if [ ! -z "$NEEDS_OUTPUT" ]
102then
103 if [ "x$1" != "x-o" -a "x$1" != "x--output" ]
104 then
105 usage "$CMD requires \"-o <file>\" or \"--output <file>\""
106 fi
107 shift
108
109 OUTFILE="$1"
110 shift
111 if [ -z "$OUTFILE" ]
112 then
113 usage "$CMD requires \"-o <file>\" or \"--output <file>\""
114 fi
115 if [ -d "$OUTFILE" ]
116 then
117 fail "Output file \"$OUTFILE\" is a directory"
118 fi
119fi
120
121FILE="$1"
122if [ ! -f "$FILE" ]
123then
124 fail "$FILE doesn't exist or isn't a file"
125fi
126if [ ! -z "$OUTFILE" -a "$FILE" -ef "$OUTFILE" ]
127then
128 fail "Refusing to use the input file as the output file"
129fi
130
131if [ $CMD_DUMP_SCRIPT ]
132then
133 unzip -p "$FILE" "$INSTALL_SCRIPT_NAME"
134 exit 0
135fi
136
137# Create a temporary directory for scratch files.
138#
139TMPDIR=`mktemp -d /tmp/$PROGNAME.XXXXXX`
140if [ $? -ne 0 ]
141then
142 TMPDIR=
143 fail "Can't create temporary directory"
144fi
145
146
147if [ $UNPACK_FILE ]
148then
149 ROOTDIR="$TMPDIR/root"
150 mkdir -p "$ROOTDIR"
151
152 println "Unpacking `basename $FILE`..."
153
154 unzip -q -d "$ROOTDIR" "$FILE"
155 if [ $? -ne 0 ]
156 then
157 fail "Couldn't unpack $FILE"
158 fi
159fi
160
161
162if [ $CMD_DUMP ]
163then
164 function dumpfile
165 {
166 echo "BEGIN `basename $1`"
167 cat "$1" | sed -e 's/^/ /'
168 echo "END `basename $1`"
169 }
170
171 echo Contents of root:
172 ls -1 "$ROOTDIR" | sed -e 's/^/ /'
173 echo
174 echo Contents of META-INF:
175 (cd "$ROOTDIR" && find META-INF -type f) | sed -e 's/^/ /'
176
177 echo
178 dumpfile "$ROOTDIR/META-INF/MANIFEST.MF"
179 echo
180 dumpfile "$ROOTDIR/$INSTALL_SCRIPT_NAME"
181 echo
182 dumpfile "$ROOTDIR/android-product.txt"
183 echo
184 dumpfile "$ROOTDIR/system/build.prop"
185fi
186
187if [ $CMD_APPEND_SCRIPT ]
188then
189 TMPSCRIPT="$TMPDIR/script"
190 NEWSCRIPT="$TMPDIR/$INSTALL_SCRIPT_NAME"
191 unzip -p "$FILE" "$INSTALL_SCRIPT_NAME" > "$TMPSCRIPT"
192 if [ $? -ne 0 ]
193 then
194 fail "Couldn't extract $INSTALL_SCRIPT_NAME from $FILE"
195 fi
196 mkdir -p `dirname "$NEWSCRIPT"`
197 cat "$TMPSCRIPT" "$SCRIPT_FILE" > "$NEWSCRIPT"
198
199 OVERWRITE_SCRIPT=1
200fi
201
202if [ $CMD_REPLACE_SCRIPT ]
203then
204 NEWSCRIPT="$TMPDIR/$INSTALL_SCRIPT_NAME"
205 mkdir -p `dirname "$NEWSCRIPT"`
206 cp "$SCRIPT_FILE" "$NEWSCRIPT"
207
208 OVERWRITE_SCRIPT=1
209fi
210
211if [ $OVERWRITE_SCRIPT ]
212then
213 cp "$FILE" "$TMPDIR/outfile.zip"
214 (cd "$TMPDIR" && zip -qu outfile.zip "$INSTALL_SCRIPT_NAME")
215 if [ $? -ne 0 ]
216 then
217 fail "Couldn't add new $INSTALL_SCRIPT_NAME to output file"
218 fi
219
220 rm -f "$OUTFILE"
221 mkdir -p `dirname "$OUTFILE"`
222 mv "$TMPDIR/outfile.zip" "$OUTFILE"
223fi
224
225cleantmp