OSDN Git Service

add askmirrors
[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" |
131             sed 's/^#Server /Server /g' >/tmp/mirrorlist
132         cat /etc/pacman.d/mirrorlist >/tmp/oldmirrorlist
133
134         cat /tmp/mirrorlist >/etc/pacman.d/mirrorlist
135         cat /tmp/oldmirrorlist >>/etc/pacman.d/mirrorlist
136     else
137         echo "ranking mirrors"
138         reflector --latest 40 --protocol http --protocol https --sort rate --save /etc/pacman.d/mirrorlist
139         iroot automirror 1
140     fi
141 }
142
143 # ask for user details
144 askuser() {
145     while [ -z $NEWUSER ]; do
146         wallstatus user
147         NEWUSER="$(imenu -i 'set username')"
148
149         # validate input as a unix name
150         if ! grep -Eq '^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$' <<<"$NEWUSER"; then
151             imenu -m "invalid username"
152             unset NEWUSER
153         fi
154     done
155
156     while ! [ "$NEWPASS" = "$NEWPASS2" ] || [ -z "$NEWPASS" ]; do
157         NEWPASS="$(imenu -P 'set password')"
158         NEWPASS2="$(imenu -P 'confirm password')"
159     done
160
161     iroot user "$NEWUSER"
162     iroot password "$NEWPASS"
163
164 }