blob: 8d2626ffe90aa6dc48706e4088e8410e274e15bf [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
20function cleantmp
21{
22 if [ ! -z "$TMPDIR" ]
23 then
24 rm -rf "$TMPDIR"
25 TMPDIR=
26 fi
27}
28
29function println
30{
31 if [ $# -gt 0 ]
32 then
33 echo "$PROGNAME: $@"
34 fi
35}
36
37function fail
38{
39 println "$@"
40 cleantmp
41 exit 1
42}
43
44function usage
45{
46 println "$@"
47 echo "Usage: $PROGNAME <input file> <output file>"
48 fail
49}
50
51OTATOOL=`which otatool`
52if [ -z "$OTATOOL" ]
53then
54 OTATOOL="`dirname $0`/otatool"
55 if [ ! -x "$OTATOOL" ]
56 then
57 fail "Can't find otatool"
58 fi
59fi
60
61
62if [ $# -ne 2 ]
63then
64 usage
65fi
66
67INFILE="$1"
68OUTFILE="$2"
69
70if [ ! -f "$INFILE" ]
71then
72 fail "$INFILE doesn't exist or isn't a file"
73fi
74
75if [ -z "$OUTFILE" ]
76then
77 usage "Output file not specified"
78fi
79
80if [ -d "$OUTFILE" ]
81then
82 usage "Output file may not be a directory"
83fi
84
85if [ "$INFILE" -ef "$OUTFILE" ]
86then
87 fail "Refusing to use the input file as the output file"
88fi
89
90TMPDIR=`mktemp -d "/tmp/$PROGNAME.XXXXXX"`
91if [ $? -ne 0 ]
92then
93 TMPDIR=
94 fail "Can't create temporary directory"
95fi
96
97ORIGSCRIPT="$TMPDIR/orig"
98NEWSCRIPT="$TMPDIR/new"
99
100"$OTATOOL" --dump-script "$INFILE" |
101awk '
102 { print }
103 /^format SYSTEM:$/ {
104 print "delete_recursive DATA:"
105 }
106' > "$NEWSCRIPT"
107if [ $? -ne 0 ]
108then
109 fail "Couldn't modify script"
110fi
111
112"$OTATOOL" --replace-script "$NEWSCRIPT" -o "$OUTFILE" "$INFILE"
113if [ $? -ne 0 ]
114then
115 fail "Couldn't replace script"
116fi
117
118cleantmp