-
Notifications
You must be signed in to change notification settings - Fork 7
/
flashize-ebb
executable file
·162 lines (137 loc) · 5.07 KB
/
flashize-ebb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
#####################################################
# Flashize-EBB #
# Copyright 2016, Lanchon #
#####################################################
#####################################################
# Flashize is free software licensed under GNU's #
# General Public License (GPL) version 3 and any #
# later version. #
# ------------------------------------------------- #
# The Flashize Runtime is free software licensed #
# under GNU's Lesser General Public License (LGPL) #
# version 3 and any later version. #
#####################################################
set -e
set -o pipefail
script="$1"
inzip="$2"
zip="$3"
log="$4"
srcdir="$5"
destdir="$6"
extras=7
dir="$(dirname "$(readlink -f "$0")")"
version="$("$dir/flashize" "--version")"
if [ "$1" == "-v" ] || [ "$1" == "--version" ] && [ $# -eq 1 ]; then
echo "$version"
exit
fi
error() {
>&2 echo "$@"
}
fatal() {
error "$@"
exit 1
}
if [ "$inzip" == "-" ]; then inzip=""; fi
if [ "$zip" == "-" ]; then zip=""; fi
if [ "$log" == "-" ]; then log=""; fi
if [ "$srcdir" == "-" ]; then srcdir=""; fi
if [ "$destdir" == "-" ]; then destdir=""; fi
if [ -z "$script" ] || ([ "$script" == "-" ] && [ -z "$zip" ]) || [ -z "$destdir" ]; then
error "Flashize-EBB ($version)"
error
error "Converts a shell script to a flashable Android recovery zip. The resulting flashable zip"
error "can automatically extract resources bundled within the zipfile before invoking the script."
error "This tool is incompatible with Toybox due to shortcomings of its 'unzip' command, and"
error "requires Busybox instead. Thus the Toybox-based CyanogenMod 13 recovery is not supported."
error
error "Usage: <input-script> <input-zip> <output-zip> <runtime-logfile> <src-dir> <dest-dir> [<extra-src-spec>...]"
error
error "Reads the script from standard input if <input-script> is a dash (-)."
error
error "Names the output zipfile based on <input-script> if <output-zip> is null or a dash."
error
error "Can create a logfile on the device at runtime, according to the value of <runtime-logfile>:"
error " -The absolute path of the logfile to be created."
error " -A relative path or filename to be interpreted against the path of the zipfile being run."
error " -A colon (:) to use the pathname of the zipfile being run with a '.log' extension."
error " -Null or a dash to disable logging."
error
error "This setting can be overridden by creating a '/tmp/flashize-log' file on the target device:"
error " -If the file is empty then enable logging to '/tmp/flashize.log'."
error " -Otherwise override the value of <runtime-logfile> with the contents of the file."
error
error "Extracts files from the zip at runtime, according to the value of <src-dir>:"
error " -Null or a dash: changes to <dest-dir> and extracts files matching <extra-src-spec> there."
error " -A path within the zip: changes to <dest-dir>/<src-dir>, wipes its contents and extracts"
error " <src-dir> there. Also extracts files matching <extra-src-spec> to <dest-dir>."
error
error "Script debugging modes are enabled by creating dummy files on the target device:"
error " -Create '/tmp/flashize-ebb-debug' to trace the user-supplied script."
error " -Create '/tmp/flashize-debug' to trace resource extraction."
fatal
fi
if [ "$script" != "-" ] && [ ! -f "$script" ]; then
fatal "error: script not found"
fi
if [ -n "$inzip" ] && [ ! -f "$inzip" ]; then
fatal "error: input zip not found"
fi
if [ "${srcdir::1}" == "/" ]; then
fatal "error: source directory must be relative"
fi
if [ "${destdir::1}" != "/" ]; then
fatal "error: destination directory must be absolute"
fi
if [ -z "$zip" ]; then
zip="$(dirname "$script")/$(basename "$script" .sh).zip"
fi
srcdir="${srcdir%/}"
destdir="${destdir%/}/"
tmpzip="$zip.tmp"
rm -f "$tmpzip"
if [ -n "$inzip" ]; then
cp -T "$inzip" "$tmpzip"
fi
rm -f "$zip"
# BUG: variables should be shell-quoted in the following code:
(
cat <<EOF
#####################################################
# Flashize-EBB Runtime (${version}) #
# Copyright 2016, Lanchon #
#####################################################
export FLASHIZE_EBB_VERSION='$version'
EOF
if [ -z "$srcdir" ]; then
cat <<EOF
mkdir -p '$destdir' &&
cd '$destdir' &&
unzip -oq "\$1" -d '$destdir'\\
EOF
else
targetdir="${destdir}$srcdir"
cat <<EOF
rm -rf '$targetdir' &&
mkdir -p '$targetdir' &&
cd '$targetdir' &&
unzip -oq "\$1" -d '$destdir' '$srcdir/*'\\
EOF
fi
for extra in "${@:$extras}"; do
echo -n " \"$extra\""
done
echo
cat <<EOF
if [ \$? -ne 0 ]; then
>&2 echo "flashize-ebb: unable to extract package contents"
exit 1
fi
[ ! -f /tmp/flashize-ebb-debug ] && set +x || set -x
#####################################################
EOF
cat "$script"
) | "$dir/flashize" - "$tmpzip" "$log"
mv "$tmpzip" "$zip"