-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqalacs.sh
executable file
·430 lines (367 loc) · 10.3 KB
/
qalacs.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
#!/usr/bin/env bash
# Author: qaraluch - 01.2019 - MIT
# Part of the repo: arch-bootstrap
# Qaraluch's Arch Linux Auto Config Script (QALACS)
# Many thanks to LukeSmithxyz for inspiration!
set -e
readonly _pArgs="$@"
readonly _pName=$(basename $0)
########################## INSTALLATION PARAMS ##############################################################
# edit it before run!
readonly p_app_list='https://raw.githubusercontent.com/qaraluch/arch-bootstrap/master/qalacs-app-list.csv'
readonly p_AUR_helper='yay'
readonly p_exec_install_apps='Y'
readonly p_exec_setup_basic='Y'
#############################################################################################################
readonly tempDir='/tmp/qalacs'
readonly localAppListName='qalacs-app-list.csv'
# Calculated vars
readonly appListDownloadPath="${tempDir}/${localAppListName}"
# Main
main() {
local cmd
welcomeMsg
_isStringEmpty "$_pArgs" && printCommandsUsage
parseCommand "$_pArgs"
if _isStringEqual "$cmd" "download" ; then
execCmd_downloadAppList
_echoDone
elif _isStringEqual "$cmd" "show" ; then
execCmd_showAppList
_echoDone
elif _isStringEqual "$cmd" "run" ; then
checkIfAppListDownloaded
_switchYN $p_exec_setup_basic && execCmd_run_setupBasic # setup also root passwd
_switchYN $p_exec_setup_basic || _echoIt "${_pDel}" "Skipped basic setup" "$_ic"
_switchYN $p_exec_install_apps && execCmd_run_installApps
_switchYN $p_exec_install_apps || _echoIt "${_pDel}" "Skipped app installation" "$_ic"
execCmd_run_FinalTweak
_echoDone
fi
}
# CLI
parseCommand() {
while [[ $# -gt 0 ]]
do
command="$1"
case $command in
run)
cmd="$command"
shift
break
;;
show)
cmd="$command"
shift
break
;;
download)
cmd="$command"
shift
break
;;
run)
cmd="$command"
shift
break
;;
*)
shift
_echoIt "$_pDel" "Nothing to do ... :("
exit 1
;;
esac
done
}
# Msgs:
welcomeMsg() {
_echoIt
_echoIt "${_pDel}" "Welcome to: ${_cy}Qaraluch's Arch Linux Auto Config Script${_ce} (QALACS)"
_echoIt "${_pDel}" "Used variables:"
_echoIt "${_pDel}" " - app list to download: $p_app_list"
_echoIt "${_pDel}" "Subscript execution flags:"
_echoIt "${_pDel}" " - run install apps [Y]es/[N]o: ${_cy}${p_exec_install_apps}${_ce}"
_echoIt "${_pDel}" " - run basic setup [Y]es/[N]o: ${_cy}${p_exec_setup_basic}${_ce}"
_echoIt "${_pDel}" "Check above installation settings." "$_iw"
}
printCommandsUsage() {
_echoIt "${_pDel}" "Re-run this script with passed command argument to perform tasks:" "$_iw"
cat <<EOL
Usage:
${_pName} ${_cy}download${_ce} - download app list from external source.
${_pName} ${_cy}run${_ce} - run setup script.
${_pName} ${_cy}show${_ce} - show app list that will be installed.
EOL
}
# Command download:
execCmd_downloadAppList() {
_echoIt "$_pDel" "About to download app list..."
local source="${p_app_list}"
local destination="${appListDownloadPath}"
createTempDir
curlFile
}
createTempDir() {
_isDir "${tempDir}" || mkdir "${tempDir}"
[[ $? ]] && _echoIt "${_pDel}" " ... created temporary dir for download: "${tempDir}""
}
curlFile() {
curl -sL "${source}" > "${destination}"
[[ $? ]] && _echoIt "${_pDel}" "Download of the file: ${_cy}"${destination##*/}${_ce}" completed!" "$_it"
}
# Command show:
execCmd_showAppList() {
_echoIt "$_pDel" "List of apps that will be installed:"
local appListYesOnly="$(getAppListYesOnly)"
showAppList
}
getAppListYesOnly() {
echo "$(cat "${appListDownloadPath}" | sed -n '/^Y,/p')"
}
showAppList() {
echo "${appListYesOnly}" \
| sed -e "1d" \
| awk -F "\"*,\"*" '{printf " - \033[1;33m%-35s\033[0m - %s\n",$3,$4}'
}
# Command run:
checkIfAppListDownloaded() {
if _isNotFile ${appListDownloadPath}; then
_errorExit "${_pDel}" 'Need to download app list beforehand (qalacs.sh download). Abort...'
fi
}
execCmd_run_installApps() {
_isStringEmpty "${userName}" && getUserName # for su in case of standalone run
enableMoreCoresForCompilation
updateSystem
refreshKeyRing
configurePacman
installGit
installAURHelper
installApps
updateSystem
}
# Update system
enableMoreCoresForCompilation() {
local configFile='/etc/makepkg.conf'
local coreNr=$(nproc)
sed -i "s/-j2/-j${coreNr}/;s/^#MAKEFLAGS/MAKEFLAGS/" /etc/makepkg.conf
_echoIt "${_pDel}" "Enabled ${_cy}${coreNr}${_ce} cores for compilation"
}
updateSystem() {
_echoIt "${_pDel}" "About to update the system..."
pacman -Syu --noconfirm
}
refreshKeyRing() {
pacman --noconfirm -Sy archlinux-keyring >/dev/null 2>&1
_echoIt "${_pDel}" "Refreshed Arch keyring" "${_it}"
}
configurePacman() {
local configFile="/etc/pacman.conf"
sed -i "s/^#Color/Color/g" "${configFile}"
sed -i "/\[multilib\]/,/Include/"'s/^#//' "${configFile}"
updateSystem # need this cos rise error with multilib
_echoIt "${_pDel}" "Updated pacman config file" "${_it}"
}
installGit() {
local appName='git'
install_default "${appName}"
[[ $? ]] && _echoIt "${_pDel}" "Installed app: ${_cg}"${appName}"${_ce}" "${_it}"
}
installAURHelper() {
local helper="${p_AUR_helper}"
_echoIt "${_pDel}" "About to install AUR helper: ${_cy}${helper}${_ce}..."
cd /tmp
rm -rf /tmp/"${helper}"*
curl -sO https://aur.archlinux.org/cgit/aur.git/snapshot/"${helper}".tar.gz \
&& sudo -u "${userName}" tar -xvf "${helper}".tar.gz \
&& cd "${helper}" \
&& sudo -u "${userName}" makepkg --noconfirm -si \
&& [[ $? ]] && _echoIt "${_pDel}" "Installed AUR helper: ${_cg}"${helper}"${_ce}" "${_it}"
cd /tmp
}
# Install apps
installApps() {
_echoIt "${_pDel}" "About to install apps..."
local appListYesOnly="$(getAppListYesOnly)"
showAppList
_yesConfirmOrAbort "Continue or abort and edit installation list"
readAndInstallAppList
cd "${tempDir}"
_echoIt "${_pDel}" "${_cg}Installed all apps!${_ce}" "${_it}"
}
readAndInstallAppList() {
while IFS=, read -r switch repo name purpose ; do
n=$((n+1)) # omit title row
case "$repo" in
"d") install_default "$name" ;;
"g") install_gitAndMake "$name" ;;
"a") install_AUR "$name" ;;
esac
[[ $? ]] && _echoIt "${_pDel}" "Installed app: ${_cg}"${name}"${_ce}" "${_it}"
done <<< "${appListYesOnly}"
}
install_default() {
local name="$1"
pacman --noconfirm --needed -S "$1"
}
install_gitAndMake() {
local name="$1"
local dir=$(mktemp -d)
git clone --depth 1 "https://github.com/${name}" "$dir"
cd "$dir"
make
make install
cd /tmp
}
install_AUR() { \
local name="$1"
su - "$userName" -c "${p_AUR_helper} -S --noconfirm ${name}"
}
# Basic setup
execCmd_run_setupBasic() {
addRootPassword
addUser
setupSudoTemporary
}
# Add user
addRootPassword() {
_echoIt "${_pDel}" "Add ${_cr}root${_ce} password"
inputPassAndCheck
setupRootPass
}
setupRootPass() {
echo "root:$passwd1" | chpasswd
unset passwd1 passwd2
_echoIt "${_pDel}" "Root password set up" "${_it}"
}
addUser() {
_echoIt "${_pDel}" "It's time to add new ${_cg}user${_ce}..."
getUserName
inputPassAndCheck
setupUser
}
getUserName() {
userName=$(_readUserInput 'Enter user name')
}
inputPassAndCheck() {
typeInPass
while ! _isStringEqual "${passwd1}" "${passwd2}" ; do
_echoIt "${_pDel}" "Passwords do not match..." "${_iw}"
inputPassAndCheck
done
}
typeInPass() {
passwd1=$(_readUserInputSilent 'Enter password')
passwd2=$(_readUserInputSilent 'Retype password')
}
setupUser() {
groupadd "${userName}"
useradd -m -g "${userName}" -s /bin/zsh "$userName"
usermod -a -G wheel "${userName}"
echo "$userName:$passwd1" | chpasswd
unset passwd1 passwd2
_echoIt "${_pDel}" "User: ${_cy}${userName}${_ce} set up." "${_it}"
}
# Sudo
setupSudoTemporary() {
chSudo "%wheel ALL=(ALL) NOPASSWD: ALL"
_echoIt "${_pDel}" "Changed temporarily sudo config file" "${_iw}"
}
chSudo(){
local configFile="/etc/sudoers"
sed -i "/#QALACS/d" "${configFile}"
echo "$* #QALACS" >> "${configFile}"
}
# Final touch
execCmd_run_FinalTweak(){
servicesInit cronie
setupSudoFinal
}
setupSudoFinal() {
chSudo "%wheel ALL=(ALL) ALL"
_echoIt "${_pDel}" "Changed final sudo config file" "${_iw}"
}
servicesInit() {
for service in "$@"; do
systemctl enable "$service"
systemctl start "$service"
_echoIt "${_pDel}" "Enabled and started service: ${_cg}${service}${_ce}" "${_iw}"
done ;}
# Utils
readonly _pDel='[ QALACS ]'
export _cr=$'\033[0;31m' # color red
export _cg=$'\033[1;32m' # color green
export _cy=$'\033[1;33m' # color yellow
export _cb=$'\033[1;34m' # color blue
export _cm=$'\033[1;35m' # color magenta
export _cc=$'\033[1;36m' # color cyan
export _ce=$'\033[0m' # color end
export _it="[ ${_cg}✔${_ce} ]" # icon tick
export _iw="[ ${_cy}!${_ce} ]" # icon warn
export _ic="[ ${_cr}✖${_ce} ]" # icon cross
export _ia="[ ${_cy}?${_ce} ]" # icon ask
_echoIt() {
local delimiter=$1 ; local msg=$2 ; local icon=${3:-''} ; echo "${delimiter}${icon} $msg" >&2
}
_errorExit() {
local delimiter=$1 ; local msg=$2 ; local icon=${3:-"$_ic"} ; echo "${delimiter}${icon} ${msg}" 1>&2 ; exit 1
}
_yesConfirmOrAbort() {
local msg=${1:-'Continue'}
local msgDefaultAbort=${2:-'Abort script!'}
read -n 1 -s -r -p "${_pDel}${_ia} ${msg} [Y/n]?"
echo >&2
REPLY=${REPLY:-'Y'}
if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
_errorExit "${_pDel}" "${msgDefaultAbort}"
fi
}
_pressAnyKey() {
read -n 1 -s -r -p "${_pDel}${_ia} Press [any] key to continue. "
echo >&2
}
_isStringEqualY() {
local string=$1
[[ "$string" == "Y" ]]
}
_switchYN() {
local switch=$1
if _isStringEqualY $switch; then
return 0
else
return 1
fi
}
_echoDone() {
_echoIt "$_pDel" "DONE!" "$_it"
echo >&2
}
_isStringEmpty() {
local var=$1
[[ -z $var ]]
}
_isStringEqual(){
[[ "$1" == "$2" ]]
}
_isDir() {
local dir=$1
[[ -d $dir ]]
}
_isNotFile() {
local file=$1
[[ ! -f $file ]]
}
_readUserInput() {
local msg=${1:-'Enter here'}
read -r -p "${_pDel}${_ia} ${msg} ${_cb}>${_ce} "
echo >&2
echo ${REPLY}
}
_readUserInputSilent() {
local msg=${1:-'Enter here'}
read -r -s -p "${_pDel}${_ia} ${msg} ${_cb}>${_ce} "
echo >&2
echo ${REPLY}
}
# Main run!
main