-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·435 lines (351 loc) · 8.56 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
#!/bin/bash
# shellcheck disable=SC2034
################################################################################
set -e
################################################################################
VERSION="2.4.9"
################################################################################
NORM=0
BOLD=1
UNLN=4
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAG=35
CYAN=36
GREY=37
DARK=90
CL_NORM="\e[0m"
CL_BOLD="\e[0;${BOLD};49m"
CL_UNLN="\e[0;${UNLN};49m"
CL_RED="\e[0;${RED};49m"
CL_GREEN="\e[0;${GREEN};49m"
CL_YELLOW="\e[0;${YELLOW};49m"
CL_BLUE="\e[0;${BLUE};49m"
CL_MAG="\e[0;${MAG};49m"
CL_CYAN="\e[0;${CYAN};49m"
CL_GREY="\e[0;${GREY};49m"
CL_DARK="\e[0;${DARK};49m"
CL_BL_RED="\e[1;${RED};49m"
CL_BL_GREEN="\e[1;${GREEN};49m"
CL_BL_YELLOW="\e[1;${YELLOW};49m"
CL_BL_BLUE="\e[1;${BLUE};49m"
CL_BL_MAG="\e[1;${MAG};49m"
CL_BL_CYAN="\e[1;${CYAN};49m"
CL_BL_GREY="\e[1;${GREY};49m"
################################################################################
GH_CONTENT="https://raw.githubusercontent.com"
REPOSITORY="$GH_CONTENT/andyone/dotfiles/master"
OMZ_INSTALL="$GH_CONTENT/robbyrussell/oh-my-zsh/master/tools/install.sh"
################################################################################
files=(".gitconfig" ".gitignore" ".dir_colors" ".rpmmacros" ".tigrc" ".tmux.conf" ".zshrc")
themes=("kaos-lite.zsh-theme" "kaos.zsh-theme")
################################################################################
pkg_manager=""
dist=""
################################################################################
# Main function
#
# Code: No
# Echo: No
main() {
banner
prepare "$@"
pushd "$HOME" &> /dev/null
doDepsInstall
doOMZInstall
doBackup
doInstall
popd &> /dev/null
show "\nAll done! Enjoy your dotfiles expirience!\n" $GREEN
}
# Print banner
#
# Code: No
# Echo: No
banner() {
show ""
show " ░░░██████╗░░█████╗░████████╗███████╗██╗██╗░░░░░███████╗░██████╗" $GREY
show " ░░░██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██║██║░░░░░██╔════╝██╔════╝" $GREY
show " ░░░██║░░██║██║░░██║░░░██║░░░█████╗░░██║██║░░░░░█████╗░░╚█████╗░" $GREY
show " ░░░██║░░██║██║░░██║░░░██║░░░██╔══╝░░██║██║░░░░░██╔══╝░░░╚═══██╗" $GREY
show " ██╗██████╔╝╚█████╔╝░░░██║░░░██║░░░░░██║███████╗███████╗██████╔╝" $GREY
show " ╚═╝╚═════╝░░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝╚══════╝╚══════╝╚═════╝░" $GREY
show " by @andyone | version: $VERSION" $DARK
show ""
sleep 3
}
# Prepare system for installing dotfiles
#
# Code: No
# Echo: No
prepare() {
local has_errors
dist=$(grep 'CPE_NAME' /etc/os-release | tr -d '"' | cut -d':' -f5)
case "$dist" in
"7") pkg_manager="/bin/yum" ;;
"8" | "9") pkg_manager="/bin/dnf" ;;
*) error "Unknown or unsupported OS"
has_errors=true ;;
esac
if [[ $has_errors ]] ; then
exit 1
fi
}
# Check installed dependencies
#
# Code: No
# Echo: No
checkDeps() {
if isCodespaces ; then
return
fi
local tmux_major
tmux_major=$(rpm -q --queryformat '%{version}' tmux | cut -b1)
if [[ "$tmux_major" != "3" ]] ; then
error "tmux ≥ 3.0 is required"
exit 1
fi
}
# Install dependencies
#
# Code: No
# Echo: No
doDepsInstall() {
local deps=""
if ! rpm -q kaos-repo &> /dev/null ; then
if ! isRoot ; then
sudo $pkg_manager install -y "https://yum.kaos.st/kaos-repo-latest.el${dist}.noarch.rpm"
else
$pkg_manager install -y "https://yum.kaos.st/kaos-repo-latest.el${dist}.noarch.rpm"
fi
fi
if ! hasApp "zsh" ; then
deps="zsh"
fi
if ! isCodespaces ; then
if ! hasApp "tmux" ; then
deps="$deps tmux"
fi
fi
if ! hasApp "git" ; then
deps="$deps git"
fi
if ! hasApp "bzip2" ; then
deps="$deps bzip2"
fi
if ! hasApp "curl" ; then
deps="$deps curl"
fi
if [[ -z "$deps" ]] ; then
return
fi
show "${CL_BL_CYAN}Installing deps…${CL_NORM}"
separator
if ! isRoot ; then
sudo $pkg_manager clean expire-cache &> /dev/null
# shellcheck disable=SC2086
sudo $pkg_manager -y install $deps
else
$pkg_manager clean expire-cache &> /dev/null
# shellcheck disable=SC2086
$pkg_manager -y install $deps
fi
# shellcheck disable=SC2181
if [[ $? -ne 0 ]] ; then
exit 1
fi
separator
}
# Install Oh My Zsh
#
# Code: No
# Echo: No
doOMZInstall() {
local current_user
if [[ -e "$HOME/.oh-my-zsh" ]] ; then
return
fi
show "${CL_BL_CYAN}Installing Oh My Zsh…${CL_NORM}"
separator
if ! sh -c "$(curl -fsSL "$OMZ_INSTALL") --unattended" ; then
exit 1
fi
current_user=$(id -u -n)
if ! getent passwd "$current_user" | grep -q '/bin/zsh' ; then
if ! isRoot ; then
sudo usermod -s "/bin/zsh" "$current_user"
else
usermod -s "/bin/zsh" "$current_user"
fi
fi
separator
}
# Create backup
#
# Code: No
# Echo: No
doBackup() {
if isCodespaces ; then
return
fi
local file_list ts output
file_list=$(getBackupFiles)
if [[ -z "$file_list" ]] ; then
return
fi
ts=$(date '+%Y%m%d%H%M%S')
output="$HOME/.andyone-dotfiles-${ts}.tar.bz2"
pushd "$HOME" &> /dev/null || return
# shellcheck disable=SC2086
tar cjf "$output" $file_list &> /dev/null
chmod 0600 "$output"
popd &> /dev/null || return
show "Backup created as $output" $DARK
show
}
# Install dotfiles
#
# Code: No
# Echo: No
doInstall() {
local file
showm "Installing " $BOLD
for file in "${themes[@]}" ; do
if download "themes/$file" "$HOME/.oh-my-zsh" ; then
showm "•" $GREEN
else
showm "•" $RED
show " ERROR\n" $RED
error "Can't download themes/$file"
exit 1
fi
done
for file in "${files[@]}" ; do
if download "$file" "$HOME" ; then
showm "•" $GREEN
else
showm "•" $RED
show " ERROR\n" $RED
error "Can't download $file"
exit 1
fi
done
show " ${CL_BL_GREEN}DONE${CL_NORM}"
}
# Collect list of files to backup
#
# Code: No
# Echo: List of files (String)
getBackupFiles() {
local file_list
for file in "${files[@]}" ; do
if [[ -e $HOME/$file ]] ; then
file_list+=("$file")
fi
done
echo "${file_list[*]}"
}
# Download file from GitHub
#
# 1: File name (String)
# 2: Target directory (String)
#
# Code: Yes
# Echo: No
download() {
local name="$1"
local dir="$2"
local rnd http_code
rnd=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w8 | head -n1)
http_code=$(curl -sL -m 10 -w "%{http_code}" -o "$dir/$name" "$REPOSITORY/${name}?r${rnd}")
if [[ "$http_code" != "200" ]] ; then
return 1
fi
return 0
}
# Check if required app is installed
#
# 1: Variable Description (Type)
#
# Code: Yes
# Echo: No
hasApp() {
if ! type -P "$1" &> /dev/null ; then
return 1
fi
return 0
}
# Return true if current user is root
#
# Code: Yes
# Echo: No
isRoot() {
if [[ $(id -u) == "0" ]] ; then
return 0
fi
return 1
}
# Return if we in codespaces
#
# Code: No
# Echo: No
isCodespaces() {
if [[ -n "$CODESPACES" ]] ; then
return 0
fi
return 1
}
################################################################################
# Print message
#
# 1: Message (String)
# 2: Color (Number) [Optional]
#
# Code: No
# Echo: No
show() {
if [[ -n "$2" ]] ; then
echo -e "\e[${2}m${1}\e[0m"
else
echo -e "$*"
fi
}
# Print message without newline symbol
#
# 1: Message (String)
# 2: Color (Number) [Optional]
#
# Code: No
# Echo: No
showm() {
if [[ -n "$2" ]] ; then
echo -e -n "\e[${2}m${1}\e[0m"
else
echo -e -n "$*"
fi
}
# Show separator
#
# Code: No
# Echo: No
separator() {
local i sep cols
cols=$(tput cols -T xterm-256color 2> /dev/null)
for i in $(seq 1 "${cols:-80}") ; do
sep="${sep}-"
done
show "\n$sep\n" $GREY
}
# Print error message
#
# 1: Error message (String)
#
# Code: No
# Echo: No
error() {
show "▲ $*" $RED 1>&2
}
################################################################################
main "$@"