forked from obbimi/Squore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange.R.package.sh
executable file
·268 lines (222 loc) · 9.35 KB
/
change.R.package.sh
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env bash
defaultTimeout=2
# derive allowed brands from existing Manifest files
allowedInput=$(ls -1 AndroidManifest*.xml | grep -v ALL | perl -ne 's~AndroidManifest(.+).xml~$1~; print' | sort )
function showHelp {
cat <<!
Usage:
$0 '($( echo ${allowedInput} | tr ' \n' '||'))'
Always change back to Squore (the default) before change to another 'Brand'
!
}
tobranded=${1:-Squore}
parentBrand="${2}"
if [[ "${tobranded}" = "-h" ]]; then
showHelp
exit
fi
brandMfFile=AndroidManifest${tobranded}.xml
if [[ ! -e ${brandMfFile} ]]; then
echo "\
ERROR : Unknown brand ${tobranded}
Allowed are: ${allowedInput}
" > /dev/stderr
exit
else
brandPkg=$(grep package= ${brandMfFile} | perl -ne 's~.*"([a-z\.]+)".*~$1~; print')
if [[ -z "$brandPkg" ]]; then
echo "Could not determine package from ${brandMfFile}"
exit
fi
if [[ $(echo "$brandPkg" | wc -l) -gt 1 ]]; then
echo "Invalid multilined package $brandPkg derived from ${brandMfFile}"
exit
fi
read -t $defaultTimeout -p "New package ${brandPkg}"
fi
if [[ -z "${parentBrand}" ]]; then
parentBrand="$(cat ${brandMfFile} | grep parentBrand | sed -e 's~parentBrand\s*=\s*~~' | tr -d ' ')"
read -t $defaultTimeout -p "Backup brand from ${brandMfFile} ==> '${parentBrand}'. Continuing in $defaultTimeout...."
echo
fi
function correctSportSpecificResource {
from=$1
to=$2
read -t $defaultTimeout -p "Correcting from ${from} to ${to}. Continue ?"
echo
echo -n > notcorrected.txt
echo -n > corrected.txt
let correctedCnt=0
let keptCnt=0
# list all renamed USED resource names and ...
for res in $(grep -E -r '@(array|bool|fraction|integer|integer-array|string|string-array|color).*__[A-Z][A-Za-z]+' * | perl -ne 's~.*@(array|bool|fraction|integer|integer-array|string|string-array|color)\/(\w+__\w+).*~$2~; print' | sort -u); do
# ... check that the appropriate res definition exists
if grep -E -q -r "name=.${res}." *; then
if echo ${res} | grep -q "__${from}"; then
nrOfOccurrencesAsExpr=$(grep -E -h -c -r "@.*${res}" * | grep -E -v '^0$' | xargs | sed -e 's/\ /+/g')
nrOfOccurrences=$((${nrOfOccurrencesAsExpr}))
#read -t $defaultTimeout -p "Nr of occurrences of valid ${res} : ${nrOfOccurrences}"
let keptCnt=keptCnt+nrOfOccurrences
printf "%2d (+%d) %-72s OK \n" ${keptCnt} ${nrOfOccurrences} ${res} >> notcorrected.txt
fi
else
#echo "XXXXXXXXXXXXXXXXXX Is NOT available : ${res}"
if [[ -n "${2}" ]]; then
newRes=$(echo "${res}" | sed -e "s~__${from}~__${to}~")
for f in $(grep -rl "$res" *); do
nrToCorrect=$(grep -c ${res} ${f})
#if [[ ${nrToCorrect} -gt 1 ]]; then
# echo "================>>>>>>>>>>>>>>>> ${nrToCorrect} corrections for ${res} to ${newRes} in ${f}"
#fi
# remember current modification time of file
oldFileTime=$(find ${f} -maxdepth 0 -printf "%Ty%Tm%Td%TH%TM.%.2TS")
sed -i "s~${res}~${newRes}~" ${f}
let correctedCnt=correctedCnt+nrToCorrect
# restore modification time of file
touch -t "${oldFileTime}" ${f}
printf "%2d (+%d) Corrected %-72s from %-32s to %-32s \n" ${correctedCnt} ${nrToCorrect} ${f} ${res} ${newRes} >> corrected.txt
done
fi
fi
done
cat notcorrected.txt
cat corrected.txt
if [[ ${correctedCnt} -gt 0 ]]; then
(
echo "=================================================="
echo "CORRECTED ${correctedCnt} resources from ${from} to ${to}"
echo "KEPT ${keptCnt} resources for ${from}"
echo "=================================================="
) | tee -a correctSportSpecificResource.txt
fi
}
cd src
####################################################
# Change Brand.java
####################################################
# check it exists
if ! grep -E -q "${tobranded}.*SportType" com/doubleyellow/scoreboard/Brand.java; then
echo "ERROR: Brand ${tobranded} not found in Brand.java" > /dev/stderr
grep -E -l "${tobranded}.*SportType" ../res/values/*.xml
exit 1
fi
# derive current package by looking for .R reference in ScoreBoard.java
pkgFrom=$(grep -E 'import[ ]+.*\.R;' com/doubleyellow/scoreboard/main/ScoreBoard.java | perl -ne 's~import\s+([\w\.]+)\.R;~\1~; print')
read -t $defaultTimeout -p "From package ${pkgFrom}"
if [[ -z "${pkgFrom}" ]]; then
echo "Could not determine 'from' package"
exit 1
fi
####################################################
# Change Brand.java
####################################################
f=com/doubleyellow/scoreboard/Brand.java
oldFileTime=$(find ${f} -maxdepth 0 -printf "%Ty%Tm%Td%TH%TM.%.2TS")
if [[ "$tobranded" = "Squore" ]]; then
# comment out all brands ...
sed -i 's~^\(\s*\)\(\w\+\s*(\s*SportType\.\)~\1//\2~' ${f}
# ... and uncomment Squore (always required)
sed -i "s~^\(\s\+\)//\(Squore\s*(.*R.string.app_name\)~\1\2~" ${f}
fi
# ensure the chosen brand is uncommented in the Brands.java file (Squore brand should ALWAYS be uncommented)
if [[ "$tobranded" != "Squore" ]]; then
sed -i "s~^\(\s\+\)//\(${tobranded}\s*(.*R.string.app_name\)~\1\2~" ${f}
fi
# simply actually set the active brand like Brand.brand = Brand.Squore
sed -i "s~Brand.\w\+;~Brand.${tobranded};~" ${f}
#vi +/Brand. ${f}
touch -t "${oldFileTime}" ${f}
####################################################
# Change <other>.java
####################################################
read -t $defaultTimeout -p "Brand.java adapted. Continue?"
echo '=================================='
printf "Change to '${tobranded}'\n"
#tobrandedLC=$(echo ${tobranded} | tr 'ABCEDFGHIJKLMNOPQRSTUVWXYZ' 'abcedfghijklmnopqrstuvwxyz')
#if [[ "$tobranded" = "Squore" ]]; then
# tobrandedLC='scoreboard'
#fi
#for f in $(grep -E -irl 'com\.doubleyellow\.[a-z]+\.R[^a-z]' *); do
# cat ${f} | perl -ne "s~com\.doubleyellow\.(?!base.R)[a-z]+\.R([^A-Za-z'])~com.doubleyellow.${tobrandedLC}.R\$1~; print" > ${f}.1.txt
for f in $(grep -E -irl "${pkgFrom}\.R[^a-z]" *); do
oldFileTime=$(find ${f} -maxdepth 0 -printf "%Ty%Tm%Td%TH%TM.%.2TS")
cat ${f} | perl -ne "s~(import\s+|\()${pkgFrom}\.R([^A-Za-z'])~\$1${brandPkg}.R\$2~; print" > ${f}.1.txt
if [[ -n "$(diff ${f} ${f}.1.txt)" ]]; then
printf "File %-72s to %s \n" ${f} ${tobranded}
mv ${f}.1.txt ${f} #.2.txt
else
rm ${f}.1.txt
fi
touch -t "${oldFileTime}" ${f}
done
echo '=================================='
read -t $defaultTimeout -p ' Java files changed. Continue ... ? '
# change some defaults in xml files (in java is/should be taken care of by means of code)
cd ../res
####################################################
# Change menu/layout/prefences xml files
####################################################
echo '=================================='
fromSuffix="(Squash|Default)"
toSuffix=${tobranded}
if [[ "$tobranded" = "Squore" ]] ; then
fromSuffix="[A-Z][a-z][A-Za-z]+"
toSuffix=Squash
fi
for f in $(grep -E -rl "@(string|fraction|color).*__${fromSuffix}\""); do
oldFileTime=$(find ${f} -maxdepth 0 -printf "%Ty%Tm%Td%TH%TM.%.2TS")
cat ${f} | perl -ne "s~__${fromSuffix}\"~__${toSuffix}\"~; print" > ${f}.1.xml
if [[ -n "$(diff ${f} ${f}.1.xml)" ]]; then
printf "File %-30s to %s strings\n" ${f} ${tobranded}
#meld ${f} ${f}.1.xml
#rm -v ${f}.1.xml # TEMP
mv ${f}.1.xml ${f}
else
rm ${f}.1.xml
fi
#read -t $defaultTimeout -p ' Continue ? '
touch -t "${oldFileTime}" ${f}
done
echo '=================================='
#read -t $defaultTimeout -p 'menu/layout/preferences files processed. Continue ? '
if [[ -e correctSportSpecificResource.txt ]]; then
rm -v correctSportSpecificResource.txt
fi
if [[ -n "${parentBrand}" ]]; then
correctSportSpecificResource ${toSuffix} ${parentBrand}
fi
if [[ "${tobranded}" = "Padel" || "${parentBrand}" = "Padel" ]]; then
correctSportSpecificResource Padel TennisPadel
correctSportSpecificResource TennisPadel Default
fi
if [[ -e correctSportSpecificResource.txt && -n "$(grep 'to Default' correctSportSpecificResource.txt)" ]]; then
echo "To Default already happened"
else
correctSportSpecificResource ${toSuffix} Default
fi
if [[ -e correctSportSpecificResource.txt ]]; then
echo
echo '######## SUMMARY ############'
cat correctSportSpecificResource.txt
fi
####################################################
# Change build.gradle
####################################################
cd ..
f=build.gradle
oldFileTime=$(find ${f} -maxdepth 0 -printf "%Ty%Tm%Td%TH%TM.%.2TS")
# change manifest file name to one of brand we want to generate apk for
cat ${f} | perl -ne "s~(srcFile\s+')AndroidManifest[A-Z][a-z][A-Za-z]+.xml~\1${brandMfFile}~; print" > ${f}.tmp
if [[ -n "$(diff ${f}.tmp ${f})" ]]; then
mv ${f}.tmp ${f}
else
rm ${f}.tmp
fi
# change 'namespace "com.doubleyellow.tennispadel"'
cat ${f} | perl -ne "s~(namespace\s+')${pkgFrom}(')~\1${brandPkg}\2~; print" > ${f}.tmp
if [[ -n "$(diff ${f}.tmp ${f})" ]]; then
mv ${f}.tmp ${f}
else
rm ${f}.tmp
fi
touch -t "${oldFileTime}" ${f}