OSDN Git Service

fix typo
[instantos/instantARCH.git] / askutils.sh
1 #!/bin/bash
2
3 # User questions are seperated into functions to be reused in alternative installers
4 # like topinstall.sh
5
6 # check if the install session is GUI or cli
7 guimode() {
8     if [ -e /opt/noguimode ]; then
9         return 1
10     fi
11
12     if [ -n "$GUIMODE" ]; then
13         return 0
14     else
15         return 1
16     fi
17 }
18
19 # add installation info to summary
20 addsum() {
21     SUMMARY="$SUMMARY
22         $1: $(iroot $2)"
23 }
24
25 # set status wallpaper
26 wallstatus() {
27     guimode && feh --bg-scale /usr/share/liveutils/$1.jpg &
28 }
29
30 # ask for keyboard layout
31 asklayout() {
32     cd /root/instantARCH/data/lang/keyboard
33     while [ -z "$NEWKEY" ]; do
34         wallstatus worldmap
35         NEWKEY="$(ls | imenu -l 'Select keyboard layout ')"
36
37         # allow directly typing in layout name
38         if [ "$NEWKEY" = "other" ]; then
39             OTHERKEY="$(localectl list-x11-keymap-layouts | imenu -l 'select keyboard layout ')"
40
41             if [ -z "$OTHERKEY" ]; then
42                 unset NEWKEY
43             else
44                 # newline is intentional!!!
45                 echo "
46 $OTHERKEY" >/root/instantARCH/data/lang/keyboard/other
47             fi
48         fi
49     done
50
51     # option to cancel the installer
52     if [ "${NEWKEY}" = "forcequit" ]; then
53         exit 1
54     fi
55     iroot keyboard "$NEWKEY"
56 }
57
58 # ask for default locale
59 asklocale() {
60     cd /root/instantARCH/data/lang/locale
61     while [ -z "$NEWLOCALE" ]; do
62         NEWLOCALE="$(ls | imenu -l 'Select language> ')"
63     done
64     iroot locale "$NEWLOCALE"
65
66 }
67
68 # ask for region with region/city
69 askregion() {
70     cd /usr/share/zoneinfo
71     while [ -z "$REGION" ]; do
72         REGION=$(ls | imenu -l "select region ")
73     done
74
75     if [ -d "$REGION" ]; then
76         cd "$REGION"
77         while [ -z "$CITY" ]; do
78             CITY=$(ls | imenu -l "select the City nearest to you ")
79         done
80     fi
81
82     [ -n "$CITY" ] && iroot city "$CITY"
83
84 }
85
86 # choose between different nvidia drivers
87 askdrivers() {
88     if lspci | grep -iq 'nvidia'; then
89         echo "nvidia card detected"
90         while [ -z "$DRIVERCHOICE" ]; do
91             DRIVERCHOICE="$(echo 'nvidia proprietary (recommended)
92 nvidia-dkms (try if proprietary does not work)
93 nouveau open source
94 install without graphics drivers (not recommended)' | imenu -l 'select graphics drivers')"
95
96             if grep -q "without" <<<"$DRIVERCHOICE"; then
97                 if ! echo "are you sure you do not want to install graphics drivers?
98 This could prevent the system from booting" | imenu -C; then
99                     unset DRIVERCHOICE
100                 fi
101             fi
102
103         done
104
105         if grep -qi "dkms" <<<"$DRIVERCHOICE"; then
106             iroot graphics "dkms"
107         elif grep -qi "nvidia" <<<"$DRIVERCHOICE"; then
108             iroot graphics "nvidia"
109         elif grep -qi "open" <<<"$DRIVERCHOICE"; then
110             iroot graphics "open"
111         elif [ -z "$DRIVERCHOICE" ]; then
112             iroot graphics "nodriver"
113         fi
114
115     else
116         echo "no nvidia card detected"
117     fi
118
119 }
120
121 # offer to choose mirror country
122 askmirrors() {
123     curl -s 'https://www.archlinux.org/mirrorlist/' | grep -i '<option value' >/tmp/mirrors.html
124     grep -v '>All<' /tmp/mirrors.html | sed 's/.*<option value=".*">\(.*\)<\/option>.*/\1/g' |
125         sed -e "1iauto detect mirrors" |
126         imenu -l "choose mirror location" >/tmp/mirrorselect
127     if ! grep -q 'auto detect' </tmp/mirrorselect; then
128         cat /tmp/mirrors.html | grep ">$(cat /tmp/mirrorselect)<" | grep -o '".*"' | grep -o '[^"]*' >/tmp/countrycode
129         echo "fetching mirrors for $(cat /tmp/mirrorselect)"
130         curl -s "https://www.archlinux.org/mirrorlist/?country=$(cat /tmp/countrycode)&protocol=http&protocol=https&ip_version=4&use_mirror_status=on" |
131             grep -iE '(Server|generated)' |
132             sed 's/^#Server /Server /g' >/tmp/mirrorlist
133         cat /etc/pacman.d/mirrorlist >/tmp/oldmirrorlist
134
135         if echo "would you like to sort mirrors by speed? (recommended)" | imenu -C; then
136             touch /tmp/sortmirrors
137         fi
138
139         if [ -e /tmp/sortmirrors ]; then
140             cat /tmp/mirrorlist | head -20 >/tmp/mirrorlist2
141             rankmirrors -n 6 /tmp/mirrorlist2 >/tmp/topmirrors
142             touch /tmp/mirrorcontinue
143             pkill imenu
144         else
145             touch /tmp/mirrorcontinue
146         fi &
147
148         while ! [ -e /tmp/mirrorcontinue ]; do
149             imenu -m "sorting mirrors, please wait"
150         done
151
152         rm /tmp/mirrorcontinue
153         /tmp/sortmirrors
154
155         if [ -e /tmp/topmirrors ]; then
156             echo "" >/etc/pacman.d/mirrorlist
157         else
158             cat /tmp/topmirrors
159             sleep 2
160             cat /tmp/topmirrors >/etc/pacman.d/mirrorlist
161         fi
162
163         cat /tmp/mirrorlist >>/etc/pacman.d/mirrorlist
164         cat /tmp/oldmirrorlist >>/etc/pacman.d/mirrorlist
165     else
166         echo "ranking mirrors"
167         reflector --latest 40 --protocol http --protocol https --sort rate --save /etc/pacman.d/mirrorlist
168         iroot automirror 1
169     fi
170 }
171
172 # ask for user details
173 askuser() {
174     while [ -z $NEWUSER ]; do
175         wallstatus user
176         NEWUSER="$(imenu -i 'set username')"
177
178         # validate input as a unix name
179         if ! grep -Eq '^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$' <<<"$NEWUSER"; then
180             imenu -m "invalid username"
181             unset NEWUSER
182         fi
183     done
184
185     while ! [ "$NEWPASS" = "$NEWPASS2" ] || [ -z "$NEWPASS" ]; do
186         NEWPASS="$(imenu -P 'set password')"
187         NEWPASS2="$(imenu -P 'confirm password')"
188     done
189
190     iroot user "$NEWUSER"
191     iroot password "$NEWPASS"
192
193 }
194
195 # ask about which hypervisor is used
196 askvm() {
197     if imvirt | grep -iq 'physical'; then
198         echo "system does not appear to be a virtual machine"
199         return
200     fi
201
202     while [ -z "$VIRTCONFIRM" ]; do
203         if ! imenu -c "is this system a virtual machine?"; then
204             if echo "Are you sure it's not?
205 giving the wrong answer here might greatly decrease performance. " | imenu -C; then
206                 return
207             fi
208         else
209             VIRTCONFIRM="true"
210         fi
211     done
212     iroot isvm 1
213
214     echo "virtualbox
215 kvm/qemu
216 other" | imenu -l "which hypervisor is being used?" >/tmp/vmtype
217
218     HYPERVISOR="$(cat /tmp/vmtype)"
219     case "$HYPERVISOR" in
220     kvm*)
221         if grep 'vendor' /proc/cpuinfo | grep -iq 'AMD'; then
222             echo "WARNING:
223         kvm/QEMU on AMD is not meant for desktop use and
224         is lacking some graphics features.
225         This installation will work, but some features will have to be disabled and
226         others might not perform well. 
227         It is highly recommended to use Virtualbox instead." | imenu -M
228             iroot kvm 1
229             if lshw -c video | grep -iq 'qxl'; then
230                 echo "WARNING:
231 QXL graphics detected
232 These may trigger a severe Xorg memory leak on kvm/QEMU on AMD,
233 leading to degraded video and input performance,
234 please switch your video card to either virtio or passthrough
235 until this is fixed" | imenu -M
236             fi
237         fi
238         ;;
239     virtualbox)
240         iroot virtualbox 1
241         ;;
242     other)
243         iroot othervm 1
244         echo "selecting other"
245         ;;
246     esac
247
248 }