-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
466 lines (417 loc) · 11 KB
/
run.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env bash
# Change current directory to the script's directory
cd "$(dirname "$0")" || exit "$?"
# Define constants
readonly BUILD_ENV_PATH="./build.env"
readonly SCRIPT_PATH="./build.sh"
readonly OUTPUT_DIR="./output"
readonly CONFIG_DIR="./config"
readonly DOCKER_BUILD_PATH="/home/build/wrt"
readonly OUTPUT_VOLUME="$OUTPUT_DIR:$DOCKER_BUILD_PATH/output_dir"
readonly BUILD_SCRIPT_VOLUME="$SCRIPT_PATH:$DOCKER_BUILD_PATH/$SCRIPT_PATH"
# Define variables
SELECTED_DEVICE=""
SELECTED_FIRMWARE=""
SELECTED_FIRMWARE_REPO=""
SELECTED_FIRMWARE_VERSION=""
SELECTED_FIRMWARE_DEPS=""
DOCKER_TAG=""
CACHE_VOLUME=""
CONFIG_VOLUME=""
SET_CLEAN_LEVEL=""
PRINT_CLEAN_LEVEL=""
SET_VERBOSE_STATUS=""
PRINT_VERBOSE_STATUS=""
# Text format variables
readonly NORMAL='\033[0m'
readonly BOLD='\033[1m'
readonly BLUE='\033[1;34m'
readonly LIGHTBLUE='\033[1;94m'
readonly LIGHTRED='\033[0;91m'
source "$BUILD_ENV_PATH"
doNotRunAsRoot() {
if [[ $EUID == 0 ]]; then
echo "Don't run this script as root"
exit 1
fi
}
pressAnyKeyToContinue() {
read -n 1 -s -r -p "Press any key to continue"
echo
}
asciiLogo() {
cat <<'EOF'
_ _ _ _____ _____ _____ _ _ _
| | | || __ ||_ _| | __ | _ _ |_|| | _| | ___ ___
| | | || -| | | | __ -|| | || || || . || -_|| _|
|_____||__|__| |_| |_____||___||_||_||___||___||_|
EOF
}
printHeader() {
clear
echo -e "${BLUE}${BOLD}"
asciiLogo
echo -e "${NORMAL}"
echo -e "${LIGHTBLUE}Choose an option:${NORMAL}"
echo
}
menuItem() {
local number="$1"
local text="$2"
echo -e "${LIGHTBLUE}$number.${NORMAL} $text"
}
printUrgentText() {
local value="$1"
echo -e "${LIGHTRED}${BOLD}$value${NORMAL}"
}
printNonUrgentText() {
local value="$1"
echo -e "${BOLD}$value${NORMAL}"
}
# Get dependencies list based on firmware and version.
getDepsList() {
WRT_MAJOR_VERSION=$(echo "$SELECTED_FIRMWARE_VERSION" | sed -E 's/v([0-9]+).*/\1/')
case $SELECTED_FIRMWARE in
"openwrt")
if [[ "$SELECTED_FIRMWARE_VERSION" == "master" || "$WRT_MAJOR_VERSION" -ge 21 ]]; then
echo "Setting dependencies for OpenWRT 21.x.x and above..."
SELECTED_FIRMWARE_DEPS="$OPENWRT_CURRENT_DEPENDENCIES"
else
echo "Setting dependencies for OpenWRT 19.x.x and below..."
SELECTED_FIRMWARE_DEPS="$OPENWRT_OLD_DEPENDENCIES"
fi
;;
"librecmc")
echo "Setting dependencies for LibreCMC..."
SELECTED_FIRMWARE_DEPS="$LIBRECMC_CURRENT_DEPENDENCIES"
;;
esac
}
dockerBuild() {
docker build \
--build-arg USERNAME="$USER" \
--build-arg USERID="$UID" \
--build-arg WRT_DEPENDENCIES="$SELECTED_FIRMWARE_DEPS" \
--build-arg WRT_FIRMWARE_REPO="$SELECTED_FIRMWARE_REPO" \
--build-arg WRT_BRANCH="$SELECTED_FIRMWARE_VERSION" \
-t "$DOCKER_TAG" .
}
cockerRun() {
local dockerArg="$1"
CACHE_VOLUME="${SELECTED_FIRMWARE}_${SELECTED_FIRMWARE_VERSION}_cache_volume:$DOCKER_BUILD_PATH"
# Mount root of config dir when $SELECTED_DEVICE is empty is a feature (. ❛ ᴗ ❛.)
CONFIG_VOLUME="$CONFIG_DIR/$SELECTED_DEVICE/$SELECTED_FIRMWARE:$DOCKER_BUILD_PATH/device_config"
docker run -it \
-e GET_CLEAN_LEVEL="$SET_CLEAN_LEVEL" \
-e GET_VERBOSE_STATUS="$SET_VERBOSE_STATUS" \
-v "$BUILD_SCRIPT_VOLUME" \
-v "$CACHE_VOLUME" \
-v "$OUTPUT_VOLUME" \
-v "$CONFIG_VOLUME" \
"$DOCKER_TAG" "$dockerArg"
}
makeBuild() {
local dockerArg="$1"
getDepsList
dockerBuild
cockerRun "$dockerArg"
pressAnyKeyToContinue
}
firmwareMenu() {
# Select the directory containing the firmware configuration
selectDir() {
# Loop continues until a file named "versions" is found in the directory
while [ ! -f "$dirPath/versions" ]; do
dirList=("$dirPath"/*)
printHeader
# Display all directories as menu items
for i in "${!dirList[@]}"; do
menuItem "$((i + 1))" "$(basename "${dirList[i]}/")"
done
echo
menuItem "0" "Back"
echo
read -rp "> " select
# Validate selection is a number and within the available options.
if [[ "$select" =~ ^[0-9]+$ ]]; then
# If the selection is within the range of the directory list,
# update the directory path to the selected directory
if ((select >= 1)) && ((select <= ${#dirList[@]})); then
dirPath="${dirList[$((select - 1))]}"
# If the selection is 0 and the current directory is the config directory,
# exit the function with return code 2
elif ((select == 0)) && [ "$dirPath" == "$CONFIG_DIR" ]; then
return 2
# If the selection is 0 and the current directory is not the config directory,
# go up one level in the directory path
elif ((select == 0)); then
dirPath=$(dirname "$dirPath")
continue
fi
fi
done
}
local dirPath="$CONFIG_DIR"
local dirList
local selectedVersion
local versions=()
if ! selectDir; then
return "$?"
fi
# Read avaliable firmware version from file
local versionsFile="$dirPath/versions"
while IFS= read -r line; do
versions+=("$line")
done <"$versionsFile"
# Select avaliable firmware version
while :; do
printHeader
# Display all versions as menu items
for i in "${!versions[@]}"; do
menuItem "$((i + 1))" "${versions[i]}"
done
echo
menuItem "0" "Main Menu"
echo
read -rp "> " select
# Validate selection is a number and within the available options.
if [[ "$select" =~ ^[0-9]+$ ]]; then
# If the selection is within the range of the versions list,
# set the selected version and other related variables,
# then call the makeBuild function with the "preconfig" argument
if ((select >= 1)) && ((select <= ${#versions[@]})); then
selectedVersion="${versions[$((select - 1))]}"
source "$dirPath/firmware.env"
SELECTED_DEVICE="$DEVICE_NAME"
SELECTED_FIRMWARE="$FIRMWARE_NAME"
SELECTED_FIRMWARE_REPO="$FIRMWARE_REPO"
SELECTED_FIRMWARE_VERSION="$selectedVersion"
DOCKER_TAG="$(echo "$SELECTED_FIRMWARE" | tr '[:upper:]' '[:lower:]')_$(echo "$SELECTED_FIRMWARE_VERSION" | tr '[:upper:]' '[:lower:]')"
makeBuild preconfig
# If the selection is 0, return to the main menu
# Feature not a bug (´• ω •`)
elif ((select == 0)); then
return 0
fi
fi
done
}
manualConfigMenu() {
arg="$1"
# Fetch and select firmware version.
selectVersionAndRunBuild() {
OPENWRT_VERSIONS=()
# Read build.env file line by line and checks each line for pattern match.
while IFS= read -r line; do
if [[ $line =~ ^"${SELECTED_FIRMWARE}"_BRANCH_.* ]]; then
value=$(echo "$line" | cut -d'=' -f2 | tr -d '"')
OPENWRT_VERSIONS+=("$value")
fi
done <"$BUILD_ENV_PATH"
while :; do
printHeader
# List firmware versions.
for i in "${!OPENWRT_VERSIONS[@]}"; do
menuItem "$((i + 1))" "${OPENWRT_VERSIONS[$i]}"
done
echo
menuItem "0" "Back"
echo
read -rp "> " select
# Validate selection is a number and within the available options.
if [[ "$select" =~ ^[0-9]+$ ]]; then
if ((select >= 1)) && ((select <= ${#OPENWRT_VERSIONS[@]})); then
SELECTED_FIRMWARE_VERSION="${OPENWRT_VERSIONS[$((select - 1))]}" # Set the selected version.
makeBuild "$arg"
elif ((select == 0)); then
return 2
fi
fi
done
}
while :; do
printHeader
menuItem "1" "OpenWRT"
menuItem "2" "LibreCMC"
echo
menuItem "0" "Back"
echo
read -rp "> " select
case "$select" in
1)
SELECTED_FIRMWARE="OPENWRT"
SELECTED_FIRMWARE_REPO="$OPENWRT_REPO"
selectVersionAndRunBuild
;;
2)
SELECTED_FIRMWARE="LIBRECMC"
SELECTED_FIRMWARE_REPO="$LIBRECMC_REPO"
selectVersionAndRunBuild
;;
0)
break
;;
*)
continue
;;
esac
done
}
# Select the level of cleaning to be performed.
cleanMenu() {
while :; do
printHeader
menuItem "1" "clean"
menuItem "2" "targetclean"
menuItem "3" "dirclean"
menuItem "4" "config-clean"
menuItem "5" "distclean"
echo
menuItem "0" "none"
echo
echo "More info: https://openwrt.org/docs/guide-developer/toolchain/use-buildsystem#cleaning_up"
echo
read -rp "> " select
case "$select" in
1)
SET_CLEAN_LEVEL="clean"
PRINT_CLEAN_LEVEL="$(printUrgentText $SET_CLEAN_LEVEL)"
break
;;
2)
SET_CLEAN_LEVEL="targetclean"
PRINT_CLEAN_LEVEL="$(printUrgentText $SET_CLEAN_LEVEL)"
break
;;
3)
SET_CLEAN_LEVEL="dirclean"
PRINT_CLEAN_LEVEL="$(printUrgentText $SET_CLEAN_LEVEL)"
break
;;
4)
SET_CLEAN_LEVEL="config-clean"
PRINT_CLEAN_LEVEL="$(printUrgentText $SET_CLEAN_LEVEL)"
break
;;
5)
SET_CLEAN_LEVEL="distclean"
PRINT_CLEAN_LEVEL="$(printUrgentText $SET_CLEAN_LEVEL)"
break
;;
0)
SET_CLEAN_LEVEL="none"
PRINT_CLEAN_LEVEL="$(printNonUrgentText $SET_CLEAN_LEVEL)"
break
;;
*)
continue
;;
esac
done
}
verboseMode() {
if [[ "$SET_VERBOSE_STATUS" == "off" ]]; then
SET_VERBOSE_STATUS="on"
PRINT_VERBOSE_STATUS="$(printUrgentText $SET_VERBOSE_STATUS)"
elif [[ "$SET_VERBOSE_STATUS" == "on" ]]; then
SET_VERBOSE_STATUS="off"
PRINT_VERBOSE_STATUS="$(printNonUrgentText $SET_VERBOSE_STATUS)"
fi
}
main() {
doNotRunAsRoot
mkdir -p "$OUTPUT_DIR"
SET_CLEAN_LEVEL="none"
PRINT_CLEAN_LEVEL="$(printNonUrgentText $SET_CLEAN_LEVEL)"
SET_VERBOSE_STATUS="off"
PRINT_VERBOSE_STATUS="$(printNonUrgentText $SET_VERBOSE_STATUS)"
# Check if arguments are provided.
if [ $# -gt 0 ]; then
# Loop to process command line arguments.
while (("$#")); do
case "$1" in
-d | --device)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
SELECTED_DEVICE=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-f | --firmware)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
SELECTED_FIRMWARE=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-v | --version)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
SELECTED_FIRMWARE_VERSION=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--* | -*=) # Unsupported flags.
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # Preserve positional arguments.
PARAMS="$PARAMS $1"
shift
;;
esac
done
# Set positional arguments in their proper place.
eval set -- "$PARAMS"
# Check if all flags were provided.
if [ -z "$SELECTED_DEVICE" ] || [ -z "$SELECTED_FIRMWARE" ] || [ -z "$SELECTED_FIRMWARE_VERSION" ]; then
echo "Error: You must provide all flags (-d, -f, -v)" >&2
exit 1
fi
source "$CONFIG_DIR/$SELECTED_DEVICE/$SELECTED_FIRMWARE/firmware.env"
SELECTED_FIRMWARE_REPO="$FIRMWARE_REPO"
DOCKER_TAG="$(echo "$SELECTED_FIRMWARE" | tr '[:upper:]' '[:lower:]')_$(echo "$SELECTED_FIRMWARE_VERSION" | tr '[:upper:]' '[:lower:]')"
makeBuild preconfig
else
while :; do
printHeader
menuItem "1" "Select device config"
menuItem "2" "Manual config"
menuItem "3" "Enter container shell"
echo
menuItem "8" "Clean level: $PRINT_CLEAN_LEVEL"
menuItem "9" "Verbose mode: $PRINT_VERBOSE_STATUS"
echo
menuItem "0" "Quit"
echo
read -rp "> " select
case "$select" in
1)
firmwareMenu
;;
2)
manualConfigMenu manual
;;
3)
manualConfigMenu shell
;;
8)
cleanMenu
;;
9)
verboseMode
;;
0)
exit 0
;;
esac
done
fi
}
main "$@"