-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexipatch-finalizer.sh
executable file
·336 lines (315 loc) · 8.04 KB
/
flexipatch-finalizer.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env bash
KEEP_FILES=0
ECHO_COMMANDS=0
RUN_SCRIPT=0
DIRECTORY=.
OUTPUT_DIRECTORY=
KEEP_GITFILES=0
DEBUG=0
if [[ $# = 0 ]]; then
set -- '-h'
fi
while (( $# )); do
case "$1" in
-d|--directory)
shift
DIRECTORY=$1 # source directory
shift
;;
-o|--output)
shift
OUTPUT_DIRECTORY=$1
shift
;;
--debug)
shift
DEBUG=1
;;
-r|--run)
shift
RUN_SCRIPT=1
;;
-e|--echo)
shift
ECHO_COMMANDS=1
;;
-k|--keep)
shift
KEEP_FILES=1
;;
-g|--git)
shift
KEEP_GITFILES=1
;;
-h|--help)
shift
fmt=" %-31s%s\n"
printf "%s" "Usage: $(basename ${BASH_SOURCE[0]}) [OPTION?]"
printf "\n"
printf "\nThis is a custom pre-processor designed to remove unused flexipatch patches and create a final build."
printf "\n\n"
printf "$fmt" "-r, --run" "include this flag to confirm that you really do want to run this script"
printf "\n"
printf "$fmt" "-d, --directory <dir>" "the flexipatch source directory to process (defaults to current directory)"
printf "$fmt" "-o, --output <dir>" "the output directory to store the processed files"
printf "$fmt" "-h, --help" "display this help section"
printf "$fmt" "-k, --keep" "keep temporary files and do not replace the original ones"
printf "$fmt" "-g, --git" "keep .git files"
printf "$fmt" "-e, --echo" "echo commands that will be run rather than running them"
printf "$fmt" " --debug" "prints additional debug information to stderr"
printf "\nWarning! This script alters and removes files within the source directory."
printf "\nWarning! This process is irreversible! Use with care. Do make a backup before running this."
printf "\n\n"
exit
;;
*)
echo "Ignoring unknown argument ($1)"
shift
;;
esac
done
if [[ $RUN_SCRIPT = 0 ]]; then
echo "Re-run this command with the --run option to confirm that you really want to run this script."
echo "The changes this script makes are irreversible."
exit 1
fi
if [[ -z ${OUTPUT_DIRECTORY} ]]; then
echo "Output directory not specified, see -o"
exit 1
fi
DIRECTORY=$(readlink -f "${DIRECTORY}")
OUTPUT_DIRECTORY=$(readlink -f "${OUTPUT_DIRECTORY}")
if [[ $DIRECTORY != $OUTPUT_DIRECTORY ]]; then
mkdir -p "${OUTPUT_DIRECTORY}"
cp -r -f "${DIRECTORY}/." -t "${OUTPUT_DIRECTORY}"
DIRECTORY=${OUTPUT_DIRECTORY}
fi
if [[ ! -e ${DIRECTORY}/patches.h ]]; then
printf "No patches.h file found. Make sure you run this script within a flexipatch source directory."
exit 1
fi
FILES_TO_DELETE=$(find $DIRECTORY -name "*.c" -o -name "*.h" | awk -v DEBUG="$DEBUG" -v DIRECTORY="$DIRECTORY" '
function istrue(f) {
ret = 0
for ( i = 2; i in f; i++ ) {
if ( f[i] == "||" ) {
if ( ret == -1 ) {
ret = 0
} else if ( ret == 1 ) {
break
}
continue
} else if ( f[i] == "&&" ) {
if ( ret == 0 ) {
ret = -1
}
continue
} else if ( ret == -1 ) {
continue
} else if ( f[i] !~ /_(PATCH|LAYOUT)$/ ) {
ret = 1
} else if ( f[i] ~ /^!/ ) {
ret = !patches[substr(f[i],2)]
} else {
ret = patches[f[i]]
}
}
if ( ret == -1 ) {
ret = 0
}
return ret
}
function schedule_delete(file) {
# Skip duplicates
for ( i = 1; i in files_to_delete; i++ ) {
if ( files_to_delete[i] == file) {
return
}
}
if (DEBUG) {
print "Scheduling file " file " for deletion." > "/dev/stderr"
}
files_to_delete[i] = file
}
function is_flexipatch(patch) {
return patch ~ /_(PATCH|LAYOUT)$/
}
BEGIN {
# Read patches.h and store patch settings in the patches associative array
if (DEBUG) {
print "Reading file " DIRECTORY "/patches.h" > "/dev/stderr"
}
while (( getline line < (DIRECTORY"/patches.h") ) > 0 ) {
split(line,f)
if ( f[1] ~ /^#define$/ ) {
if ( f[3] == 0 || f[3] == 1 ) {
patches[f[2]] = f[3]
} else {
patches[f[2]] = istrue(f)
}
if (DEBUG) {
print "Found " f[2] " = " patches[f[2]] > "/dev/stderr"
}
}
}
files_to_delete[0] = ""
}
{
level = 0
do_print[level] = 1
has_printed[level] = 0
condition[level] = ""
while (( getline line < $0) > 0 ) {
split(line,f)
if ( f[1] ~ /^#if$/ ) {
level++;
do_print[level] = do_print[level-1]
has_printed[level] = 0
condition[level] = f[2]
if ( do_print[level] ) {
if ( istrue(f) ) {
has_printed[level] = 1
do_print[level] = 1
} else {
do_print[level] = 0
}
}
if ( is_flexipatch(condition[level]) ) {
continue
}
} else if ( f[1] ~ /^#ifdef$/ || f[1] ~ /^#ifndef$/ ) {
level++;
do_print[level] = do_print[level-1]
has_printed[level] = 0
condition[level] = f[2]
if ( do_print[level] ) {
has_printed[level] = 1
do_print[level] = 1
}
if ( is_flexipatch(condition[level]) ) {
continue
}
} else if ( f[1] ~ /^#elif$/ ) {
if ( (!is_flexipatch(condition[level]) || has_printed[level] == 0) && do_print[level-1] == 1 ) {
if ( istrue(f) ) {
has_printed[level] = 1
do_print[level] = 1
} else {
do_print[level] = 0
}
} else {
do_print[level] = 0
}
if ( is_flexipatch(f[2]) ) {
continue
}
} else if ( f[1] ~ /^#else$/ ) {
if ( (!is_flexipatch(condition[level]) || has_printed[level] == 0) && do_print[level-1] == 1 ) {
has_printed[level] = 1
do_print[level] = 1
} else {
do_print[level] = 0
}
if ( is_flexipatch(condition[level]) ) {
continue
}
} else if ( f[1] ~ /^#include$/ && f[2] ~ /^"/ && (do_print[level] == 0 || f[2] == "\"patches.h\"") ) {
dir = ""
if ( $0 ~ /\// ) {
dir = $0
sub("/[^/]+$", "/", dir)
}
schedule_delete(dir substr(f[2], 2, length(f[2]) - 2))
continue
} else if ( f[1] ~ /^#endif$/ ) {
if ( is_flexipatch(condition[level]) ) {
level--
continue
}
level--
}
if ( do_print[level] ) {
print line > $0 ".~"
}
}
}
END {
for ( i = 1; i in files_to_delete; i++ ) {
print files_to_delete[i]
}
}
')
# Chmod and replace files
for FILE in $(find $DIRECTORY -name "*.~"); do
chmod --reference=${FILE%%.~} ${FILE}
if [[ $KEEP_FILES = 0 ]] || [[ $ECHO_COMMANDS = 1 ]]; then
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "mv ${FILE} ${FILE%%.~}"
else
mv ${FILE} ${FILE%%.~}
fi
fi
done
# Delete unnecessary files
if [[ $KEEP_FILES = 0 ]] || [[ $ECHO_COMMANDS = 1 ]]; then
# Remove dwmc shell script if patch not enabled
if [[ -f ${DIRECTORY}/patch/dwmc ]] && [[ $(grep -cE '^#define DWMC_PATCH +0 *$' ${DIRECTORY}/patches.h) > 0 ]]; then
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "rm ${DIRECTORY}/patch/dwmc"
echo "sed -r -i -e '/cp -f patch\/dwmc/d' \"${DIRECTORY}/Makefile\""
else
rm "${DIRECTORY}/patch/dwmc"
sed -r -i -e '/cp -f patch\/dwmc/d' "${DIRECTORY}/Makefile"
fi
fi
# Remove layoutmenu.sh shell script if patch not enabled
if [[ -f ${DIRECTORY}/patch/layoutmenu.sh ]] && [[ $(grep -cE '^#define BAR_LAYOUTMENU_PATCH +0 *$' ${DIRECTORY}/patches.h) > 0 ]]; then
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "rm ${DIRECTORY}/patch/layoutmenu.sh"
else
rm "${DIRECTORY}/patch/layoutmenu.sh"
fi
fi
for FILE in $FILES_TO_DELETE ${DIRECTORY}/patches.def.h; do
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "rm $FILE"
else
rm "$FILE"
fi
done
if [[ -f $DIRECTORY/README.md ]]; then
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "rm $DIRECTORY/README.md"
else
rm $DIRECTORY/README.md
fi
fi
if [[ $KEEP_GITFILES = 0 ]]; then
rm -rf $DIRECTORY/.git*
fi
# Remove empty include files
INCLUDE_RE='*patch/*include.[hc]'
if [[ $ECHO_COMMANDS = 1 ]]; then
INCLUDE_RE='*patch/*include.[hc][.]~'
fi
for FILE in $(find $DIRECTORY -path "$INCLUDE_RE"); do
if [[ $(grep -c "#include " $FILE) = 0 ]]; then
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "rm ${FILE%%.~}"
else
rm "$FILE"
fi
for LINE in $(grep -Ern "#include \"patch/$(basename ${FILE%%.~})\"" $DIRECTORY | grep -v '.~:' | awk -F":" '{print $1 ":" $2 }'); do
INCFILE=$(echo $LINE | cut -d":" -f1)
LINE_NO=$(echo $LINE | cut -d":" -f2)
if [[ $ECHO_COMMANDS = 1 ]]; then
echo "sed -i \"${LINE_NO}d\" ${INCFILE}"
else
sed -i "${LINE_NO}d" ${INCFILE}
fi
done
fi
done
fi
# Clean up the Makefile
sed -r -i -e 's/ patches.h$//' -e '/^patches.h:$/{N;N;d;}' "${DIRECTORY}/Makefile"