-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.sh
executable file
·548 lines (496 loc) · 20 KB
/
install.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/bin/bash
#Checks whether argument is a program argument or data
checkArg() {
for validArg in "${validArgList[@]}"; do
if [[ "$1" == "$validArg" ]]; then return 1; fi
done
}
checkCommand() {
command -v "$1" > /dev/null
}
checkRoot() {
if [[ "$UID" != "0" ]]; then return 1; fi
}
#Output messages with colours based off of categories - $1: Mode, $2: Message content, $3: Newline toggle
output() {
extraContent="\n"
if [[ "$3" == "noNewline" ]]; then extraContent=""; fi
resetCol="\033[0m"
case $1 in
"success") echo -en "\033[1;32m${2}${resetCol}${extraContent}";;
"list"|"minor") echo -en "\033[1;36m${2}${resetCol}${extraContent}";;
"warning") echo -en "\033[1;33m${2}${resetCol}${extraContent}";;
"error") echo -en "\033[1;31m${2}${resetCol}${extraContent}";;
"normal"|*) echo -en "\033[1;37m${2}${resetCol}${extraContent}";;
esac
}
#Processes the background argument (listing and validating)
getBackground() {
background="$1"
checkArg "$background" || return 1
if [[ "$1" == "" ]] || [[ "$1" == "list" ]]; then
output "list" "Available backgrounds:"
for availableBackground in "backgrounds/4k/"*; do
availableBackground="${availableBackground##*/}"
availableBackground="${availableBackground^}"
output "success" " ${availableBackground/.png}"
done
output "normal" "Or specify a file (e.g. './install.sh -i -b background.png')"; exit
fi
background="${background/.png}"
if [[ ! -f "$background.png" ]]; then
if [[ ! -f "backgrounds/4k/${background,,}.png" ]]; then
output "error" "Invalid background, use './install.sh -b' to view available backgrounds"; exit 1
else
background="${background,,}"
fi
fi
background="$background.png"
}
#Processes the custom background argument (validating)
getCustomBackground() {
backgroundColour="${1}"
#Check $background_colour is valid (not another argument)
if ! checkArg "$backgroundColour" || [[ "$backgroundColour" == "" ]]; then
#Give error message if it failed
output "warning" "Invalid or no background colour found, ignoring"
backgroundColour=""
return 1
fi
}
#Display all valid resolutions
printResolutions() {
output "normal" "Supported resolutions: ('custom' will allow GRUB to guess settings)"
for listResolution in "backgrounds/"*; do
if [[ "$listResolution" != "backgrounds/import.py" ]]; then
output "normal" " - ${listResolution//backgrounds\/}"
fi
done
output "normal" " - [WIDTH]x[HEIGHT]"
}
#Processes the resolutiom argument (listing, validating)
getResolution() {
resolution="$1"
if checkArg "$resolution"; then
#Check if it's a valid resolution, otherwise use default
case "$resolution" in
4k|4K|3480x2160|2160p) resolution="4k"; gfxmode="GRUB_GFXMODE=3840x2160,auto";;
2k|2K|2560x1440|1440p) resolution="2k"; gfxmode="GRUB_GFXMODE=2560x1440,auto";;
1920x1080|1080p) resolution="1080p"; gfxmode="GRUB_GFXMODE=1920x1080,auto";;
3840x2400) resolution="3840x2400"; gfxmode="GRUB_GFXMODE=3840x2400,auto";;
2240x1400) resolution="2240x1400"; gfxmode="GRUB_GFXMODE=2240x1400,auto";;
1920x1200) resolution="1920x1200"; gfxmode="GRUB_GFXMODE=1920x1200,auto";;
*x*) gfxmode="GRUB_GFXMODE=$resolution,auto"; echo "Custom resolution found, using \"$resolution\"";;
"custom") resolution="custom"; gfxmode="GRUB_GFXMODE=auto";;
""|"list") printResolutions; exit;;
*) output "error" "Invalid resolution, using default"; resolution="1080p";;
esac
else
#Use default resolution
output "warning" "No resolution specified, using 1080p"
resolution="1080p"
gfxmode="GRUB_GFXMODE=1920x1080,auto"
return 1
fi
}
#Processes the font colour argument (validating)
getFontColour() {
font_colour="${1%,*}"
selected_font_colour="${1#*,}"
#Check $font_colour is valid (not another argument)
if ! checkArg "$font_colour" || ! checkArg "$selected_font_colour" || [[ "$font_colour" == "" ]] || [[ "$selected_font_colour" == "" ]]; then
#Give error message if it failed
output "warning" "Invalid or no font colour found, ignoring"
font_colour=""
return 1
fi
}
#Processes the font size argument (validating)
getFontSize() {
fontsize="${1/"px"}"
if checkArg "$fontsize"; then
if [[ ! "$fontsize" =~ ^[0-9]+$ ]]; then
output "warning" "Font size must be an integer, ignoring"
fontsize=""
fi
else
#Reset fontsize
output "warning" "Incorrect usage of --fontsize, ignoring"
fontsize=""
return 1
fi
}
#Processes the font argument (listing, validating)
getFontFile() {
fontfile="$1"
checkArg "$fontfile" || return 1
if [[ "$1" == "" ]] || [[ "$1" == "list" ]]; then
output "list" "Available fonts:"
for availableFont in "fonts/"*".ttf"; do
availableFont="${availableFont##*/}"
output "success" " $availableFont"
done
output "normal" "Or specify a file (e.g. './install.sh -i -f Font.ttf')"; exit
fi
if [[ ! -f "$fontfile" ]]; then
if [[ -f "fonts/${fontfile}.ttf" ]]; then
fontfile="$fontfile".ttf
elif [[ -f "fonts/${fontfile}.otf" ]]; then
fontfile="$fontfile".otf
elif [[ ! -f "fonts/${fontfile}" ]]; then
output "error" "Invalid fontfile, use './install.sh -f' to view available fonts"; exit 1
fi
fi
}
#Processes the icon type argument (validating)
getIconType() {
iconType="${1}"
if [[ "$iconType" == "colorless" ]]; then
iconType="colourless"
fi
if checkArg "$iconType"; then
if [[ "$iconType" != "coloured" ]] && [[ "$iconType" != "colourless" ]]; then
output "warning" "Icon type must be either coloured or colourless, ignoring"
iconType=""
fi
else
output "warning" "Incorrect usage of --icons, ignoring"
return 1
fi
}
updateGrub() {
output "success" "Updating grub..."
if checkCommand update-grub; then
update-grub
elif checkCommand grub-mkconfig; then
grub-mkconfig -o /boot/grub/grub.cfg
elif checkCommand zypper; then
grub2-mkconfig -o /boot/grub2/grub.cfg
elif checkCommand dnf; then
grub2-mkconfig -o /boot/grub2/grub.cfg || grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
fi
}
installCore() {
generateFont() {
#"input" "output" "size" "font family"
if checkCommand grub-mkfont; then
mkfontCommand="grub-mkfont"
elif checkCommand grub2-mkfont; then
mkfontCommand="grub2-mkfont"
else
output "error" "Neither grub-mkfont nor grub2-mkfont could be found, exiting"; exit 1
fi
extraFlag=""
if [[ "$forceBoldFont" == "true" ]] || [[ "$5" == "-b" ]]; then
extraFlag="-b"
fi
$mkfontCommand "$1" -o "$2" -s "$3" -n "$4" "$extraFlag"
}
checkIconCached() { #$1: asset name, $2: resolution, $3: pretty name
#Decide if the assets have been cached
output "success" "Generating $3..." "noNewline"
if [[ ! -d "./build/$1/${2}px" ]]; then
if ! message=$(./icon_builder.py "--custom" "$1" "$2" "build" "$iconType" 2>&1 >/dev/null); then
output "error" " failed"
else
output "success" " done"
fi
#Send any error output
if [[ "$message" != "" ]]; then
echo "$message"
fi
else
output "success" " found cached $3"
fi
}
generateThemeSizes() {
icon_size="$(($1 * 2))"
item_icon_space="$((icon_size / 2 + 2))"
item_height="$((icon_size / 6 + icon_size))"
item_padding="$((icon_size / 4))"
item_spacing="$((icon_size / 3))"
}
#Generate theme size values
generateThemeSizes "$fontsize"
if [[ "$iconType" == "colourless" ]]; then
iconColourDir="icons-colourless"
else
iconColourDir="icons"
fi
#Generate assets and set path
if [[ -d "./assets/$iconColourDir/${icon_size}px" ]]; then #Decide whether assets need to be generated
iconDir="./assets/$iconColourDir/${icon_size}px"
selectDir="./assets/select/${icon_size}px"
else
#Check if icons are cached and regenerate if not
checkIconCached "$iconColourDir" "$icon_size" "icons"
checkIconCached "select" "$item_height" "assets"
iconDir="./build/$iconColourDir/${icon_size}px"
selectDir="./build/select/${item_height}px"
fi
#Install theme components
output "success" "Installing theme assets..."
cp -r "$iconDir" "$installDir/icons"
cp "$selectDir/"*.png "$installDir/"
#Generate and install fonts
output "success" "Generating fonts..."
generateFont "fonts/$fontfile" "$installDir/${fontfile%.*}_$fontsize.pf2" "$fontsize" "Display"
generateFont "fonts/Terminus.ttf" "$installDir/Terminus_16.pf2" "16" "Console" "-b"
font_name="$(file "$installDir/${fontfile%.*}_$fontsize.pf2")"
font_name="${font_name#*": GRUB2 font "}"
font_name="${font_name//'"'}"
console_font_name="Console Bold 16"
#Fill out and install theme.txt
output "success" "Creating theme.txt..."
fileContent="$(cat assets/theme.txt)"
#Append the help label if enabled
if [[ "$helpLabel" == "true" ]]; then
fileContent+="$(echo -e "\n"; cat "assets/help-label.txt")"
fi
fileContent="${fileContent//"{icon_size_template}"/"$icon_size"}"
fileContent="${fileContent//"{item_icon_space_template}"/"$item_icon_space"}"
fileContent="${fileContent//"{item_height_template}"/"$item_height"}"
fileContent="${fileContent//"{item_padding_template}"/"$item_padding"}"
fileContent="${fileContent//"{item_spacing_template}"/"$item_spacing"}"
fileContent="${fileContent//"{font_name_template}"/"$font_name"}"
fileContent="${fileContent//"{console_font_name_template}"/"$console_font_name"}"
fileContent="${fileContent//"{font_colour_template}"/"$font_colour"}"
fileContent="${fileContent//"{selected_font_colour_template}"/"$selected_font_colour"}"
echo "$fileContent" > "$installDir/theme.txt"
#Install background
if [[ "$background" != "" ]]; then
if [[ ! -f "$background" ]]; then
if [[ -d "backgrounds/$resolution" ]]; then
background="backgrounds/$resolution/$background"
else
output "error" "Couldn't find \"backgrounds/$resolution/$background\", please report this issue, including a full log of the program's output"; exit 1
fi
fi
cp "$background" "$installDir/background.png"
else
#Set the resolution to generate an image for
if [[ "$resolution" == "1080p" ]]; then
customBackgroundRes="1920x1080"
elif [[ "$resolution" == "4k" ]]; then
customBackgroundRes="3840x2160"
elif [[ "$resolution" == "2k" ]]; then
customBackgroundRes="2560x1440"
else
customBackgroundRes="$resolution"
fi
output "success" "Generating custom background..."
convert -size "$customBackgroundRes" -depth 24 "xc:$backgroundColour" "PNG8:$installDir/background.png"
fi
}
installTheme() {
#Check user is root
if ! checkRoot; then output "error" "This script should be run as root"; exit 1; fi
#Remove theme if installed, and recreate directory
output "success" "Preparing theme directory ($installDir/)..."
if [[ -d "$installDir" ]]; then rm -rf "$installDir"; fi
mkdir -p "$installDir"
#Install files to $installDir
installCore
#Install splash screen
cp "$installDir/background.png" "$splashScreenPath"
#Create GRUB config directory, if it's missing
mkdir -p "/etc/default/grub.d/"
#Handle existing GRUB theme config
if [[ -f "/etc/default/grub.d/argon.cfg" ]]; then
output "success" "Replacing GRUB theme config..."
rm "/etc/default/grub.d/argon.cfg"
else
output "success" "Installing GRUB theme config..."
fi
#Add relevant config to file
echo "GRUB_THEME=\"$installDir/theme.txt\"" > "/etc/default/grub.d/argon.cfg"
echo "$gfxmode" >> "/etc/default/grub.d/argon.cfg"
#Update grub config
updateGrub
}
uninstallTheme() {
#Check user is root
if ! checkRoot; then
output "error" "This script should be run as root"; exit 1
fi
#Delete assets
if [[ -d "$installDir" ]]; then
rm -rf "$installDir"
else
output "warning" "Theme wasn't installed, exiting"; exit 1
fi
#Delete splash screen
if [[ -f "$splashScreenPath" ]]; then
rm -rf "$splashScreenPath"
fi
#Remove GRUB theme config
if [[ -f "/etc/default/grub.d/argon.cfg" ]]; then
output "success" "Removing GRUB theme config..."
rm "/etc/default/grub.d/argon.cfg"
else
output "warning" "GRUB theme config not found, '/etc/default/grub' may need configuring"
fi
#Update grub config
updateGrub
}
previewTheme() {
if ! checkCommand grub2-theme-preview; then
output "error" "No working copy of grub2-theme-preview found"
output "warning" "grub2-theme-preview: https://github.com/hartwork/grub2-theme-preview"
if checkRoot; then
output "warning" "If grub2-theme-preview was installed without the -g switch, it won't be available"
output "warning" "Running './install.sh' again without root may work"
fi
exit 1
fi
installDir="$(mktemp -d)"
#Install files to $installDir
installCore
output "success" "Installed to $installDir"
if ! grub2-theme-preview "$installDir"; then
output "error" "Failed to display preview, grub2-theme-preview may be broken"
fi
rm -rf "$installDir"
}
warnArgs() {
if [[ "$font_colour" == "" ]]; then
argWarnings+="$(output "minor" "No font colour specified, use -fc [VALUE] to set a font colour\n")"
font_colour="#cccccc"
selected_font_colour="#ffffff"
fi
if [[ "$resolution" == "" ]]; then
argWarnings+="$(output "warning" "No resolution specified, use -r [RESOLUTION] to set a resolution\n")"
argWarnings+="$(output "warning" " - Defaulting to 1080p\n")"
resolution="1080p"
gfxmode="GRUB_GFXMODE=1920x1080,auto"
fi
if [[ "$fontfile" == "" ]]; then
argWarnings+="$(output "warning" "No font specified, use -f [FONTFILE] to set a font\n")"
argWarnings+="$(output "warning" " - Defaulting to Terminus Bold\n")"
forceBoldFont="true"
fontfile="Terminus.ttf"
fi
if [[ "$fontsize" == "" ]]; then
argWarnings+="$(output "warning" "No font size specified, use -fs [VALUE] to set a font size\n")"
argWarnings+="$(output "warning" " - Defaulting to size 24\n")"
fontsize="24"
fi
if [[ "$background" == "" ]] && [[ "$backgroundColour" == "" ]]; then
argWarnings+="$(output "error" "No background or colour specified, use -b to list available backgrounds\n")"
argWarnings+="$(output "warning" " - Call the program with '-b [background]'\n")"
argsFailed="true"
fi
if [[ "$background" != "" ]] && [[ ! -f "$background" ]]; then
if [[ ! -d "backgrounds/$resolution" ]]; then
argWarnings+="$(output "error" "The default background can't be used with a custom resolution, other than \"custom\"\n")"
argsFailed="true"
fi
fi
if [[ "$background" != "" ]] && [[ "$backgroundColour" != "" ]]; then
argWarnings+="$(output "error" "Use either a background or a colour, not both\n")"
argsFailed="true"
fi
if [[ "$backgroundColour" != "" ]] && [[ "$resolution" == "custom" ]]; then
argWarnings+="$(output "error" "To use a custom colour background, a resolution must be used, other than \"custom\"\n")"
argsFailed="true"
fi
if [[ "$backgroundColour" != "" ]] && ! checkCommand convert; then
argWarnings+="$(output "error" "Imagemagick / convert is required to use a custom background colour\n")"
argsFailed="true"
fi
if [[ "$argWarnings" != "" ]]; then
echo -e "\n$argWarnings"
if [[ "$argsFailed" == "true" ]]; then exit 1; fi
fi
}
installDir="/usr/share/grub/themes/argon"
#Set bootPath for future reference, and to set splashScreenPath
if [[ -d "/boot/grub" ]]; then
bootPath="/boot/grub"
elif [[ -d "/boot/grub2" ]]; then
bootPath="/boot/grub2"
else
output "warning" "GRUB couldn't be found in /boot, this may cause issues"
fi
splashScreenPath="$bootPath/splash0.png"
if [[ "$#" == "0" ]]; then
output "error" "At least one argument is required, use './install.sh --help' to view options"; exit 1
fi
validArgList=("-h" "--help" "-i" "--install" "-u" "--uninstall" "-e" "--boot" "-p" "--preview" "-b" "--background" "-c" "--custom" "--custom-background" "-r" "--resolution" "-fc" "--fontcolour" "--font-colour" "-fs" "--fontsize" "--font-size" "-f" "--font" "-l" "--bold" "--icons" "-hl" "--helplabel" "--help-label" "--auto")
read -ra args <<< "${@}"; i=0
while [[ $i -le "$(($# - 1))" ]]; do
arg="${args[$i]}"
case $arg in
-h|--help) output "normal" "Usage: ./install.sh [-OPTION]";
output "normal" "Help:"
output "normal" "-h | --help : Display this page"
output "normal" "-i | --install : Install the theme (root)"
output "normal" "-u | --uninstall : Uninstall the theme (root)"
output "normal" "-e | --boot : Install the theme into '/boot/grub/themes'"
output "normal" "-p | --preview : Preview the theme (Works with other options, non-root)"
output "normal" "-b | --background : Specify which background to use (file)"
output "normal" " - Leave blank to view available backgrounds"
output "normal" "-c | --custom : Use a solid colour as a background"
output "normal" " - HTML colour value, must be quoted (\"#FFFFFF\")"
output "normal" "-r | --resolution : Use a specific resolution (Default: 1080p)"
output "normal" " - Leave blank to view available resolutions"
output "normal" "--icons : Choose whether to use 'coloured or 'colourless' icons"
output "normal" "-f | --font : Specify which font to use (file)"
output "normal" " - Leave blank to view available fonts"
output "normal" "-fc| --fontcolour : Use a specific font colour"
output "normal" " - HTML (must be quoted) and SVG 1.0 colours supported"
output "normal" " - Use the format: -fc \"textcolour,selectedcolour\""
output "normal" "-fs| --fontsize : Use a specific font size"
output "normal" "-l | --bold : Force font to be bold"
output "normal" "-hl| --help-label : Add a help label to the bottom of the theme"
output "normal" "\nRequired arguments: [--install + --background / --uninstall / --preview]"
output "success" "Program written by: Stuart Hayhurst"; exit;;
-i|--install) programOperation="install";;
-u|--uninstall) programOperation="uninstall";;
-e|--boot) installDir="$bootPath/themes/argon";;
-p|--preview) programOperation="preview";;
-b|--background) getBackground "${args["$((i + 1))"]}" && i="$((i + 1))";;
-c|--custom|--custom-background) getCustomBackground "${args["$((i + 1))"]}" && i="$((i + 1))";;
-r|--resolution) getResolution "${args["$((i + 1))"]}" && i="$((i + 1))";;
-fc|--fontcolour|--font-colour) getFontColour "${args["$((i + 1))"]}" && i="$((i + 1))";;
-fs|--fontsize|--font-size) getFontSize "${args["$((i + 1))"]}" && i="$((i + 1))";;
-f|--font) getFontFile "${args["$((i + 1))"]}" && i="$((i + 1))";;
-l|--bold) forceBoldFont="true";;
--icons) getIconType "${args["$((i + 1))"]}" && i="$((i + 1))";;
-hl|--helplabel|--help-label) helpLabel="true";;
--auto) auto="true";;
*) output "error" "Unknown parameter passed: $arg"; exit 1;;
esac
i=$((i + 1))
done
if [[ "$programOperation" == "install" ]] || [[ "$programOperation" == "preview" ]]; then
#Check all required arguments are present and set default values
warnArgs
output "success" "Using the following settings:"
output "list" "Resolution: ${resolution^}"
if [[ "$background" != "" ]]; then
output "list" "Background: ${background^}"
else
output "list" "Background: ${backgroundColour}"
fi
iconType="${iconType:-"coloured"}"; output "list" "Icon type: ${iconType^}"
output "list" "Font colour: ${font_colour^}"
output "list" "Selected font colour: ${selected_font_colour^}"
output "list" "Font size: $fontsize"
output "list" "Font file: $fontfile"
forceBoldFont="${forceBoldFont:-"false"}"; output "list" "Force bold: ${forceBoldFont^}"
output "list" "Install path: ${installDir}/"
if [[ "$auto" != "true" ]]; then
output "normal" "\nPress enter to continue..."; read -r
fi
fi
if [[ "$programOperation" == "install" ]]; then
installTheme
elif [[ "$programOperation" == "uninstall" ]]; then
uninstallTheme
elif [[ "$programOperation" == "preview" ]]; then
previewTheme
else
output "error" "One of '--install', '--uninstall' or '--preview' is required"
exit 1
fi