-
Notifications
You must be signed in to change notification settings - Fork 0
/
archi_funcs.sh
131 lines (117 loc) · 3.77 KB
/
archi_funcs.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
#!/bin/bash
set -eu
shopt -s extglob
next() {
clear
printf "
_ _ _ _
/ \ _ __ ___| |__ (_) ___| |__
/ _ \ | '__/ __| '_ \| | / __| '_ \
/ ___ \| | | (__| | | | |_\__ \ | | |
/_/ \_\_| \___|_| |_|_(_)___/_| |_|
--------------------------------------
"
}
# arguments: '-s': don't show input (useful for passwords); '-e': allow empty input.
# returns result in the 'ret' variable
read_input() {
_secret=0
_allow_empty=0
_prompt=""
for arg in "$@"; do
case $arg in
-s ) _secret=1;;
-e ) _allow_empty=1;;
* ) [ -z "$_prompt" ] && _prompt=$arg || _prompt="$_prompt $arg";;
esac
done
while true; do
# shellcheck disable=SC2059
printf "$_prompt\n"
if [ $_secret -eq 1 ]; then
read -ersp "> " ret
else
read -erp "> " ret
fi
case $ret in
"" )
if [ $_allow_empty -eq 1 ]; then
return
fi;;
*\ * ) ;;
* ) return;;
esac
printf "Invalid input.\n\n> "
done
}
# first argument is the prompt, second argument is 'Y/n' (default is yes), 'y/N' (default is no) or 'y/n' (no default)
# returns 'y' or 'n' in the 'ret' variable
read_input_yn() {
while true; do
printf "$1 [%s]\n" "$2"
read -erp "> " ret
case ${ret,,} in # lowercase ret
"" )
case $2 in
"Y/n" ) ret=y; return;;
"y/N" ) ret=n; return;;
esac;;
y|yes ) ret=y return;;
n|no ) ret=n return;;
esac
printf "Invalid input.\n\n"
done
}
# first argument is the prompt, second argument is the newline ('\n') separated choices, first space ends the
# match area (example format: 'matchable_choice1 non_matchable_suffix1\nmatchable_choice2')
# returns result in the 'ret' variable
choose() {
mapfile -t choices < <(echo -e "$2")
to_show=$(echo -e "$2" | nl -s ') ')
[ ${#choices[@]} -eq 0 ] && return 1
[ ${#choices[@]} -eq 1 ] && ret=${choices[0]} && return
while true; do
printf "$1 (leave blank for '%s'):\n%s" "${choices[0]% *}" "$to_show"
read_input -e
case $ret in
"" ) ret=${choices[0]% *}; return;;
+([0-9]) )
ret=$((ret -= 1))
if ((ret >= 0 && ret < ${#choices[@]})); then
ret=${choices[$ret]% *}
return
fi;;
* )
for elem in "${choices[@]}"; do
if [ "${elem% *}" = "$ret" ]; then
ret=${ret% *}
return
fi
done;;
esac
printf "Invalid input\n\n"
done
}
# finds (and prompt for choice if necessary) the video driver to install
# returns the video driver package name or 'none' in the 'ret' variable
detect_vdriver() {
vga=$(lspci | grep VGA | tr "[:upper:]" "[:lower:]")
if [ "$(systemd-detect-virt --vm)" != "none" ]; then
ret="none"
elif [[ $vga == *"nvidia"* || -f /sys/kernel/debug/dri/0/vbios.rom ]]; then
printf "Nvidia GPU detected\n"
ret="nvidia"
elif [[ $vga == *"advanced micro devices"* || -f /sys/kernel/debug/dri/0/radeon_pm_info || -f /sys/kernel/debug/dri/0/radeon_sa_info ]]; then
printf "AMD GPU detected\n"
choose "Select the video driver to install" "xf86-video-amdgpu (NEW)\nxf86-video-ati (OLD)"
elif [[ $vga == *"intel corporation"* || -f /sys/kernel/debug/dri/0/i915_capabilities ]]; then
printf "Intel GPU detected\n"
ret="xf86-video-intel"
else
ret="none"
fi
}
update_pkg_db() {
printf "Updating packages database\n"
sudo pacman -Syy --noconfirm
}