-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.sh
executable file
·688 lines (591 loc) · 15.9 KB
/
bootstrap.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/bin/bash
DOTFILE_REPO="https://github.com/fr0zn/dotfiles.git"
DOTFILE_PATH="$HOME/.dotfiles"
DOTFILE_BACKUP="$HOME/.dotfiles-backup"
DOTFILE_SRC="$DOTFILE_PATH/src"
DOTFILE_BIN="$DOTFILE_PATH/bin"
DB_SYNC=0
OS_TYPE=""
STEPS="pre backup symlink install post"
msg() {
printf '%b\n' "$1" >&2
}
msg_info() {
if [[ "${2}" == "in" ]]; then
msg "\33[94m ->\33[0m ${1}"
else
msg "\33[94m==>\33[0m ${1}"
fi
}
msg_debug() {
if [[ "$DEBUG" == "1" ]]; then
if [[ "${2}" == "in" ]]; then
msg "\33[96m ->\33[0m ${1}"
else
msg "\33[96m==>\33[0m ${1}"
fi
fi
}
msg_ok() {
if [[ "${2}" == "in" ]]; then
msg "\33[92m ->\33[0m ${1}"
else
msg "\33[92m==>\33[0m ${1}"
fi
}
msg_error() {
if [[ "${2}" == "in" ]]; then
msg "\33[91m -> ERROR:\33[0m ${1}"
else
msg "\33[91m==> ERROR:\33[0m ${1}"
fi
}
die(){
msg_error "${@}"
exit 1
}
lnif() {
if [ -e "$1" ]; then
ln -sf "$1" "$2"
if [[ ! -L ${2} ]]; then
return 1
fi
return 0
fi
}
has_sudo() {
local prompt
prompt=$(sudo -n true 2>&1)
if [ $? -eq 0 ]; then
echo "has_sudo__pass_set"
elif echo $prompt | grep -q 'required'; then
echo "has_sudo__needs_pass"
else
echo "no_sudo"
fi
}
clean(){
if [[ "$DEBUG" != "1" ]]; then
$@ > /dev/null 2>&1
else
$@
fi
}
sudo_run(){
str="$@"
if [[ "$UID" == "0" ]]; then
sudo sh -c "${str}"
else
HAS_SUDO=$(has_sudo)
case "$HAS_SUDO" in
has_sudo__pass_set)
sudo sh -c "${str}"
;;
has_sudo__needs_pass)
msg_info "Please supply your user password for the following command: \"${str}\"" "in"
sudo sh -c "${str}"
;;
*)
msg_info "Please supply root password for the following command: \"${str}\"" "in"
su -c "${str}"
;;
esac
fi
}
sync_database() {
if [[ "$DB_SYNC" == "0" ]]; then
msg_info "Updating database packages"
case "${OS_TYPE}" in
"macos")
clean brew update
;;
"ubuntu" | "debian" | "rpi")
clean sudo_run 'apt-get update'
;;
"arch")
clean sudo_run 'pacman -Sy --noconfirm'
;;
esac
if [[ "$?" == "0" ]]; then
msg_ok "Database synced" "in"
DB_SYNC=1
else
die "Error syncing and updating packages" "in"
fi
fi
}
install_cask() {
sync_database
msg_info "Installing cask ${*} (${OS_TYPE})"
to_install_str=$(_get_packages_not_installed "${*}")
if [[ "$to_install_str" == "0" ]]; then
# All installed
return 0
fi
msg_info "Installing only casks '${to_install_str}' (${OS_TYPE})" "in"
case "${OS_TYPE}" in
"macos")
clean brew install --cask "${*}"
;;
*)
msg_error "brew cask not supported ${OS_TYPE}" "in"
return 1
esac
if [[ $? -ne 0 ]];then
msg_error "Error auto-installing '${*}', no permission, wrong package, or already installed" "in"
return 1
fi
return 0
}
_get_packages_not_installed(){
local is_installed
local packages=(${@})
local to_install=()
local already_installed=()
for i in "${!packages[@]}"; do
out=$(is_package_installed "${packages[$i]}")
is_installed="$?"
if [[ "$is_installed" == "1" ]]; then
to_install+=("${packages[$i]}")
else
already_installed+=("${packages[$i]}")
fi
done
to_install_str=$(IFS=":" echo "${to_install[*]}")
already_installed_str=$(IFS=":" echo "${already_installed[*]}")
if [[ -z "${to_install_str}" ]]; then
# Everything installed
msg_ok "All packages already installed, skipping" "in"
echo "0"
else
if [[ ! -z "${already_installed_str}" ]]; then
msg_ok "Packages already installed '${already_installed_str}'" "in"
fi
fi
echo "$to_install_str"
}
install_package() {
sync_database
msg_info "Installing packages '$*' (${OS_TYPE})"
to_install_str=$(_get_packages_not_installed "$*")
if [[ "$to_install_str" == "0" ]]; then
# All installed
return 0
fi
msg_info "Installing only packages '${to_install_str}' (${OS_TYPE})" "in"
case "${OS_TYPE}" in
"macos")
clean brew install "${to_install_str}"
;;
"ubuntu" | "debian" | "rpi")
clean sudo_run "apt-get -y install ${to_install_str}"
;;
"arch")
clean sudo_run "pacman -S --noconfirm ${to_install_str}"
;;
*)
msg_error "Auto-Installation not supported ${OS_TYPE}" "in"
return 1
esac
if [[ $? -ne 0 ]];then
msg_error "Error auto-installing '${to_install_str}', no permission, wrong package, or already installed" "in"
return 1
fi
return 0
}
y_n(){
read -p "$1 (y/n)?: " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
$"$2"
else
$"$3"
fi
}
is_package_installed(){
msg_debug "Checking if ${1} is installed"
case "${OS_TYPE}" in
"macos")
brew ls --versions ${1} > /dev/null 2>&1
if [[ "$?" == "1" ]]; then
brew cask ls --versions ${1} > /dev/null 2>&1
fi
;;
"ubuntu" | "debian" | "rpi")
dpkg -l | grep -w ${1} > /dev/null 2>&1
;;
"arch")
pacman -Qi ${1} > /dev/null 2>&1
;;
*)
msg_error "Check if package is installed not supported ${OS_TYPE}" "in"
return 1
esac
}
package_must_exist(){
is_package_installed $1
if [[ $? -ne 0 ]]; then
die "Not Found You must have '${1}' installed to continue." "in"
exit 1
fi
}
is_app_installed() {
if [[ "$OS_TYPE" == "macos" ]]; then
if [ -d "/Applications/${1}.app" ]; then
return 0
fi
return 1
else
msg_info "Not a macOS, can't check if ${1}.app is installed" "in"
return 1
fi
}
open_app(){
if [[ "$OS_TYPE" == "macos" ]]; then
if [ -d "/Applications/${1}.app" ]; then
bundle=$(mdls -name kMDItemCFBundleIdentifier -r /Applications/${1}.app)
/usr/bin/open -b "${bundle}"
else
msg_error "Application ${1}.app does not exist" "in"
fi
else
msg_info "Not a macOS, can't run app ${1}.app" "in"
return 1
fi
}
add_app_login(){
if [[ "$OS_TYPE" == "macos" ]]; then
if [ -d "/Applications/${1}.app" ]; then
tell="tell application \"System Events\" to make login item at end with properties {name: \"${1}\",path:\"/Applications/${1}.app\", hidden:false}"
/usr/bin/osascript -e "${tell}" >/dev/null
if [[ "$?" == "0" ]]; then
msg_ok "Application '${1}' set on startup" "in"
else
msg_error "Application '${1}' not set on startup" "in"
fi
else
msg_error "Application ${1}.app does not exist" "in"
fi
else
msg_info "Not a macOS, can't link app ${1}.app" "in"
return 1
fi
}
add_line(){
local file="${1}"
local line="${2}"
if [ ! -f "$file" ]; then
echo "$line" >> "$file"
else
grep -qF -- "$line" "$file" || echo "$line" >> "$file"
fi
}
program_exists() {
local ret='0'
command -v $1 >/dev/null 2>&1 || { local ret='1'; }
# fail on non-zero return value
if [ "$ret" -ne 0 ]; then
return 1
fi
return 0
}
program_must_exist() {
program_exists $1
if [[ $? -ne 0 ]]; then
die "Not Found You must have '$1' installed to continue." "in"
exit 1
fi
}
symlink_file(){
local path=$(dirname "${2}")
if [ ! -d "$path" ]; then
mkdir -p $path 2> /dev/null
fi
lnif "$DOTFILE_PATH/$1" "$2"
return $?
}
symlink_path(){
local path="${2}"
if [ -d "$path" ]; then
rm -rf "$path" 2> /dev/null
fi
lnif "$DOTFILE_PATH/$1" "$2"
return $?
}
function clone(){
msg_info "Retrieving sources..."
FROM=$1
WHERE=$2
if [ ! -e "$WHERE" ]; then
mkdir -p "$WHERE" 2> /dev/null
ERROR=$(git clone "$FROM" "$WHERE" 2>&1 > /dev/null)
if [[ $? -ne 0 ]]; then
msg_error "Error on clone $WHERE" "in"
return 1
else
msg_ok "Cloned $WHERE" "in"
return 0
fi
else
ERROR=$(cd "$WHERE" && git pull origin 2>&1 > /dev/null)
if [[ $? -ne 0 ]]; then
msg_error "Pull error: $WHERE" "in"
return 1
else
msg_ok "Pulled $WHERE" "in"
return 0
fi
fi
return
}
function clone_src(){
clone ${1} "$DOTFILE_SRC/${2}"
}
_function_exists() {
declare -f -F $1 > /dev/null
return $?
}
_remove_hidden() {
echo "${1}" | sed 's/^\.//g'
}
backup_file() {
msg_ok "Backing up files" "in"
mkdir -p $DOTFILE_BACKUP 2> /dev/null
local file_name
today=`date +%Y%m%d_%s`
for i in "$@"; do
file_name=$(_remove_hidden $(basename ${1}))
if [[ -e "$i" ]]; then
cp "$i" "${DOTFILE_BACKUP}/${file_name}.${today}" 2>/dev/null 2>&1;
if [[ ! -f "${DOTFILE_BACKUP}/${file_name}.${today}" ]]; then
msg_error "Backup file ${i}" "in"
exit 1
fi
fi
done
return 0
}
backup_path() {
if [ -d "$1" ]; then
msg_ok "Backing up folder" "in"
mkdir -p $DOTFILE_BACKUP 2> /dev/null
local file_name
file_name=$(_remove_hidden $(basename ${1}))
today=`date +%Y%m%d_%s`
cp -r "${1}" "${DOTFILE_BACKUP}/${file_name}.${today}" 2>/dev/null 2>&1;
if [[ ! -d "${DOTFILE_BACKUP}/${file_name}.${today}" ]]; then
msg_error "Backup path ${i}" "in"
exit 1
fi
fi
return 0
}
# Loads all install.sh script from the dotfiles folder
_load() {
list=$(find "$DOTFILE_PATH" -maxdepth 2 -name install.sh)
for element in $list; do
. $element
done
list=$(find "$DOTFILE_PATH/packages" -maxdepth 2 -name "*.sh")
for element in $list; do
. $element
done
}
_template() {
if _function_exists "${1}_${2}_$OS_TYPE"; then
msg_debug "${2}: ${1} ($OS_TYPE)" "in"
clean $"${1}_${2}_$OS_TYPE"
elif _function_exists "${1}_${2}"; then
msg_debug "${2}: ${1} (generic)" "in"
clean $"${1}_${2}"
else
msg_debug "${2}: Tried to run ${1}_${2}, but it doesn't exist" "in"
return 0
fi
return $?
}
install() {
local steps
steps=$STEPS
if [[ ! -z "$1" ]]; then
msg_info "Installing: '$1'"
if [[ ! -z "$2" ]]; then
msg_info "$1: Custom steps installation, steps: ${@:2}"
steps="${@:2}"
fi
for step in $steps; do
_template "$step" "$1"
if [[ $? -ne 0 ]]; then
msg_error "Error installing '$1' in step: $step"
return 1
fi
cd $HOME
done
msg_ok "Done: '$1'" "in"
return 0
fi
}
install_aur(){
sync_database
msg_info "Installing AUR package ${1} (${OS_TYPE})"
to_install_str=$(_get_packages_not_installed "$*")
if [[ "$to_install_str" == "0" ]]; then
# All installed
return 0
fi
case "${OS_TYPE}" in
"arch")
local path="${DOTFILE_SRC}/${1}"
clone https://aur.archlinux.org/${1}.git $path
cd $path
makepkg -sic --noconfirm
;;
*)
msg_error "AUR package not supported ${OS_TYPE}" "in"
return 1
esac
if [[ $? -ne 0 ]];then
msg_error "Error auto-installing '${*}', wrong package, failed build or missing dependencies" "in"
return 1
fi
return 0
}
install_brew_macos(){
program_exists "brew"
if [[ $? -ne 0 ]]; then
msg_info "Brew not found, installing ..."
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
program_exists "brew"
if [[ $? -ne 0 ]]; then
return 1
fi
return 0
}
ctrl_c() {
echo
echo
msg_error "Aborted by user! Exiting..."
exit 1
}
_pre_run() {
if [[ "$OS_TYPE" == "macos" ]]; then
if xcode-select --install 2>&1 | grep installed > /dev/null; then
msg_ok "Xcode CLI tools installed";
else
msg_error "Xcode CLI tools not installed Installing..."
fi
install "brew"
fi
program_exists "git"
if [[ $? -ne 0 ]]; then
install_package "git"
program_must_exist "git"
fi
clone $DOTFILE_REPO $DOTFILE_PATH
}
_get_os(){
uname_out=`uname`
if [[ "$uname_out" == "Darwin" ]]; then
OS_TYPE="macos"
elif [[ "$uname_out" == "Linux" ]]; then
OS_TYPE="linux"
if type lsb_release >/dev/null 2>&1 ; then
distro=$(lsb_release -i -s)
if [[ "$distro" == "Debian" ]]; then
OS_TYPE="debian"
elif [[ "$distro" == "Ubuntu" ]]; then
OS_TYPE="ubuntu"
elif [[ "$distro" == "Raspbian" ]]; then
OS_TYPE="rpi"
fi
fi
if [ -f "/etc/arch-release" ]; then
OS_TYPE="arch"
fi
fi
msg_info "Running installation for OS: ${OS_TYPE}"
}
_bootsrap_ask (){
## Collect the files in the array $files
files=$(find $DOTFILE_PATH/bootstrap -type f -name "${OS_TYPE}*" -not -name "common" | sed 's=.*/==;s/\.[^.]*$//')
## Enable extended globbing. This lets us use @(foo|bar) to
## match either 'foo' or 'bar'.
shopt -s extglob
## Start building the string to match against.
string="@("
## Add the rest of the files to the string
for file in $files; do
string+="|${file}"
done
## Close the parenthesis. $string is now @(file1|file2|...|fileN)
string+=")"
## Show the menu. This will list all files and the string "quit"
select file in $files "common"
do
case $file in
## If the choice is one of the files (if it matches $string)
$string)
echo "$file"
break;
;;
common)
echo "common"
break;
;;
*)
file=""
echo "Please choose a number from 1 to $((${#files[@]}+1))";;
esac
done
}
_run(){
# . "$DOTFILE_PATH/bootstrap"
bootstrap_file=$(_bootsrap_ask)
sync_database
if [ -f "$DOTFILE_PATH/bootstrap/$bootstrap_file.sh" ]; then
msg_info "Executing installation file of '$bootstrap_file'"
. "$DOTFILE_PATH/bootstrap/$bootstrap_file.sh"
fi
#msg_info "Executing common installation file"
#. "$DOTFILE_PATH/bootstrap/all.sh"
if [[ $? -eq 0 ]]; then
msg_ok "Done installing dotfiles!"
else
msg_error "Finished with some errors"
fi
}
_edit(){
bootstrap_file=$(_bootsrap_ask)
vi "$DOTFILE_PATH/bootstrap/${bootstrap_file}.sh"
y_n "Run installation now" _run return
}
_run_no(){
y_n "Edit installation" _edit return
}
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
_get_os
_pre_run
_load
if [ "$1" = "INTERACTIVE" ]; then
msg_info "Interactive shell"
# Interactive
while IFS= read -e -p "cmd: " cmd; do
history -s "$cmd"
$cmd
done
elif [ ! -z "$1" ]; then
# User is giving an install file
if [ ! -f "$1" ]; then
msg_error "File not found"
exit
fi
cmd_path=`realpath $1`
msg_info "Running commands from file '$cmd_path'"
. $cmd_path
else
y_n "Run installation" _run _run_no
fi
# vim: set sw=4 ts=4 sts=4 et tw=78 foldmarker={,} foldlevel=0 foldmethod=marker :