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