-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·2333 lines (2123 loc) · 82 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
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC1117
# Author: Mike 8a
# Description: Install all my basic configs and scripts
#
# -`
# ... .o+`
# .+++s+ .h`. `ooo/
# `+++%++ .h+++ `+oooo:
# +++o+++ .hhs++. `+oooooo:
# +s%%so%.hohhoo' 'oooooo+:
# `+ooohs+h+sh++`/: ++oooo+:
# hh+o+hoso+h+`/++++.+++++++:
# `+h+++h.+ `/++++++++++++++:
# `/+++ooooooooooooo/`
# ./ooosssso++osssssso+`
# .oossssso-````/osssss::`
# -osssssso. :ssss``to.
# :osssssss/ Mike osssl +
# /ossssssss/ 8a +sssslb
# `/ossssso+/:- -:/+ossss'.-
# `+sso+:-` `.-/+oso:
# `++:. `-/+/
# .` github.com/mike325/dotfiles `/
# TODO: create a install.ps1 to install
# - scoop and additional buckets versions, extras and nerd-fonts
# - chocolatey
ALL=1
COOL_FONTS=0
DOTCONFIGS=0
# VIM=0
NVIM=0
BIN=0
SHELL_SCRIPTS=0
# SHELL_FRAMEWORK=0
EMACS=0
DOTFILES=0
GIT=0
FORCE_INSTALL=0
BACKUP=0
BACKUP_DIR="$HOME/.local/backup_$(date '+%d.%b.%Y_%H-%M-%S')"
VERBOSE=0
PORTABLES=0
SYSTEMD=0
NOCOLOR=0
NOLOG=0
PYTHON=0
PKGS=0
TMP="${TMPDIR:-/tmp/}"
PKG_FILE=""
NEOVIM_DOTFILES=0
NEOVIM_DEV=0
CMD="ln -fns"
PYTHON_VERSION="all"
PROTOCOL="https"
GIT_USER="mike325"
GIT_HOST="github.com"
URL=""
# GIT_SSH=0
VERSION="0.5.0"
NAME="$0"
NAME="${NAME##*/}"
LOG="${NAME%%.*}.log"
WARN_COUNT=0
ERR_COUNT=0
SCRIPT_PATH="$0"
SCRIPT_PATH="${SCRIPT_PATH%/*}"
OS='unknown'
ARCH="$(uname -m)"
trap '{ exit_append; }' EXIT
trap '{ clean_up; }' SIGTERM SIGINT
if hash realpath 2>/dev/null; then
SCRIPT_PATH=$(realpath "$SCRIPT_PATH")
else
pushd "$SCRIPT_PATH" 1>/dev/null || exit 1
SCRIPT_PATH="$(pwd -P)"
popd 1>/dev/null || exit 1
fi
if [[ -n $ZSH_NAME ]]; then
CURRENT_SHELL="zsh"
elif [[ -n $BASH ]]; then
CURRENT_SHELL="bash"
else
# shellcheck disable=SC2009,SC2046
if [[ -z $CURRENT_SHELL ]]; then
CURRENT_SHELL="${SHELL##*/}"
fi
fi
if [ -z "$SHELL_PLATFORM" ]; then
if [[ -n $TRAVIS_OS_NAME ]]; then
export SHELL_PLATFORM="$TRAVIS_OS_NAME"
else
case "$OSTYPE" in
*'linux'*) export SHELL_PLATFORM='linux' ;;
*'darwin'*) export SHELL_PLATFORM='osx' ;;
*'msys'*) export SHELL_PLATFORM='msys' ;;
*'cygwin'*) export SHELL_PLATFORM='cygwin' ;;
*'windows'*) export SHELL_PLATFORM='windows' ;;
*'freebsd'*) export SHELL_PLATFORM='bsd' ;;
*) export SHELL_PLATFORM='unknown' ;;
esac
fi
fi
case "$SHELL_PLATFORM" in
# TODO: support more linux distros
linux)
if [[ -f /etc/arch-release ]]; then
OS='arch'
elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then
OS='ubuntu'
elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then
OS='redhat'
elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then
if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then
OS='raspbian'
else
OS='debian'
fi
fi
;;
cygwin | msys | windows)
OS='windows'
;;
osx)
OS='macos'
;;
bsd)
OS='bsd'
;;
*)
OS="$(uname -o)"
;;
esac
function is_windows() {
if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then
return 0
fi
return 1
}
# function is_wsl() {
# if [[ "$(uname -r)" =~ Microsoft ]]; then
# return 0
# fi
# return 1
# }
function is_osx() {
if [[ $SHELL_PLATFORM == 'osx' ]]; then
return 0
fi
return 1
}
# function is_linux() {
# if ! is_windows && ! is_wsl && ! is_osx; then
# return 0
# fi
# return 1
# }
# function is_root() {
# if ! is_windows && [[ $EUID -eq 0 ]]; then
# return 0
# fi
# return 1
# }
# function has_sudo() {
# if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then
# return 0
# fi
# return 1
# }
function is_arm() {
local arch
arch="$(uname -m)"
if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then
return 0
fi
return 1
}
function is_64bits() {
local arch
arch="$(uname -m)"
if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]] || [[ $arch == 'aarch64' ]] || [[ $arch == 'armv7' ]]; then
return 0
fi
return 1
}
function has_fetcher() {
if hash curl 2>/dev/null || hash wget 2>/dev/null; then
return 0
fi
return 1
}
if is_windows; then
# Windows does not support links we will use cp instead
CMD="cp -rf"
USER="$USERNAME"
fi
# colors
# shellcheck disable=SC2034
black="\033[0;30m"
# shellcheck disable=SC2034
red="\033[0;31m"
# shellcheck disable=SC2034
green="\033[0;32m"
# shellcheck disable=SC2034
yellow="\033[0;33m"
# shellcheck disable=SC2034
blue="\033[0;34m"
# shellcheck disable=SC2034
purple="\033[0;35m"
# shellcheck disable=SC2034
cyan="\033[0;36m"
# shellcheck disable=SC2034
white="\033[0;37;1m"
# shellcheck disable=SC2034
orange="\033[0;91m"
# shellcheck disable=SC2034
normal="\033[0m"
# shellcheck disable=SC2034
reset_color="\033[39m"
function help_user() {
cat <<EOF
Description
Simple installer of this dotfiles, by default install (link) all settings and configurations
if any flag is given, the script will install just want is being told to do.
Usage:
$NAME [OPTIONS]
Optional Flags
--url <URL> Provide full git url (ex. https://gitlab.com/mike325),
the new base user must have the following repos
- nvim
- .emacs.d
- dotfiles
--backup Backup all existing files into $HOME/.local/backup or the provided dir
NOTE: Backup will be auto activated if windows is running or '-c/--copy' flag is used
Default: off in linux on in windows
-f, --force Force installation, remove all previous conflict files before installing
This flag is always disable by default
--shell_scripts Install some bash/zsh shell scripts like:
- tmux tpm
- z.sh
- neofetch
Current shell: $CURRENT_SHELL
-c, --copy By default all dotfiles are linked using 'ln -s' command, this flag change
the command to 'cp -rf' this way you can remove the folder after installation
but you need to re-download the files each time you want to update the files
- Ignored option in Windows platform
- WARNING!!! if you use the option -f/--force all host Setting will be deleted!!!
-s, --shell Installs:
- Shell alias in $HOME/.config/shell
- Shell basic configurations \${SHELL}rc for bash, zsh, tcsh and csh
- Everything inside ./dotconfigs into $HOME
- Everything inside ./config/ into ${XDG_CONFIG_HOME:-$HOME/.config/} in unix or ~/AppData/Local/ in windows
- Python startup script in $HOME/.local/lib/
-d, --dotfiles Download my dotfiles repo in case, this options is meant to be used in case this
script is standalone executed
Default URL: $PROTOCOL://$GIT_HOST/$GIT_USER/dotfiles
-e, --emacs Download and install my evil Emacs dotfiles
Default URL: $PROTOCOL://$GIT_HOST/$GIT_USER/.emacs.d
-n, --neovim [TYPE] Download Neovim executable (portable in windows and linux) if it hasn't been Installed
Download and install my Vim dotfiles in Neovim's dir.
Check if vim dotfiles are already install and copy/link (depends of '-c/copy' flag) them,
otherwise download them from vim's dotfiles repo
Default URL: $PROTOCOL://$GIT_HOST/$GIT_USER/.vim
TYPE can be either'stable', 'dev' or dotfiles (no binary install)
-b, --bin Install shell functions and scripts in $HOME/.local/bin
-g, --git Install git configurations into $HOME/.config/git and $HOME/.gitconfig
-t, --portables Install isolated/portable programs into $HOME/.local/bin
- neovim - shellcheck
- texlab - fd
- stylua - lazygit
- bat - delta
- gh - shfmt
- ripgrep - pip2 and pip3
- fzf (Linux only) - jq (Linux only)
--fonts, --powerline Install the powerline patched fonts
- Since the patched fonts have different install method for Windows
they are just download
- This options is ignored if the install script is executed in a SSH session
--python If no version is given install python2 and python3 dependencies from:
- ./packages/${OS}/python2 - ./packages/${OS}/python3
--pkgs, --packages, --packages PKG_FILE [--only]
Install all .pkg files from ./packages/${OS}/
if the package file is given then force install packages from there
Additional flag --only cancel all other flags
-y, systemd Install user's systemd services (Just in Linux systems)
- Services are install in $HOME/.config/systemd/user
--nolog Disable log writing
--nocolor Disable color output
--verbose Output debug messages
--version Display the version and exit
-h, --help Display help, if you are seeing this, that means that you already know it (nice)
EOF
}
function warn_msg() {
local msg="$1"
if [[ $NOCOLOR -eq 0 ]]; then
printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg"
else
printf "[!] Warning:\t %s\n" "$msg"
fi
WARN_COUNT=$((WARN_COUNT + 1))
if [[ $NOLOG -eq 0 ]]; then
printf "[!] Warning:\t %s\n" "$msg" >>"${LOG}"
fi
return 0
}
function error_msg() {
local msg="$1"
if [[ $NOCOLOR -eq 0 ]]; then
printf "${red}[X] Error:${reset_color}\t %s\n" "$msg" 1>&2
else
printf "[X] Error:\t %s\n" "$msg" 1>&2
fi
ERR_COUNT=$((ERR_COUNT + 1))
if [[ $NOLOG -eq 0 ]]; then
printf "[X] Error:\t %s\n" "$msg" >>"${LOG}"
fi
return 0
}
function status_msg() {
local msg="$1"
if [[ $NOCOLOR -eq 0 ]]; then
printf "${green}[*] Info:${reset_color}\t %s\n" "$msg"
else
printf "[*] Info:\t %s\n" "$msg"
fi
if [[ $NOLOG -eq 0 ]]; then
printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}"
fi
return 0
}
function verbose_msg() {
local msg="$1"
if [[ $VERBOSE -eq 1 ]]; then
if [[ $NOCOLOR -eq 0 ]]; then
printf "${purple}[+] Debug:${reset_color}\t %s\n" "$msg"
else
printf "[+] Debug:\t %s\n" "$msg"
fi
fi
if [[ $NOLOG -eq 0 ]]; then
printf "[+] Debug:\t\t %s\n" "$msg" >>"${LOG}"
fi
return 0
}
function __parse_args() {
if [[ $# -lt 2 ]]; then
error_msg "Internal error in __parse_args function trying to parse $1"
exit 1
fi
local flag="$2"
local value="$1"
local pattern="^--${flag}=[a-zA-Z0-9.:@_/~-]+$"
if [[ -n $3 ]]; then
local pattern="^--${flag}=$3$"
fi
if [[ $value =~ $pattern ]]; then
local left_side="${value#*=}"
echo "${left_side/#\~/$HOME}"
else
echo "$value"
fi
}
function initlog() {
if [[ $NOLOG -eq 0 ]]; then
[[ -n $LOG ]] && rm -f "${LOG}" 2>/dev/null
if ! touch "${LOG}" &>/dev/null; then
error_msg "Fail to init log file"
NOLOG=1
return 1
fi
if [[ -f "${SCRIPT_PATH}/shell/banner" ]]; then
cat "${SCRIPT_PATH}/shell/banner" >"${LOG}"
fi
if ! is_osx; then
LOG=$(readlink -e "${LOG}")
fi
verbose_msg "Using log at ${LOG}"
fi
return 0
}
# shellcheck disable=SC2317
function exit_append() {
if [[ $NOLOG -eq 0 ]]; then
if [[ $WARN_COUNT -gt 0 ]] || [[ $ERR_COUNT -gt 0 ]]; then
printf "\n\n" >>"${LOG}"
fi
if [[ $WARN_COUNT -gt 0 ]]; then
printf "[*] Warnings:\t%s\n" "$WARN_COUNT" >>"${LOG}"
fi
if [[ $ERR_COUNT -gt 0 ]]; then
printf "[*] Errors:\t%s\n" "$ERR_COUNT" >>"${LOG}"
fi
fi
return 0
}
# shellcheck disable=SC2317
function clean_up() {
verbose_msg "Cleaning up by interrupt"
verbose_msg "Cleaning up rg ${TMP}/rg.*" && rm -rf "${TMP}/rg.*" 2>/dev/null
verbose_msg "Cleaning up rg $TMP/ripgrep-*" && rm -rf "$TMP/ripgrep-*" 2>/dev/null
verbose_msg "Cleaning up fd ${TMP}/fd.*" && rm -rf "${TMP}/fd.*" 2>/dev/null
verbose_msg "Cleaning up fd $TMP/fd-*" && rm -rf "$TMP/fd-*" 2>/dev/null
verbose_msg "Cleaning up pip $TMP/get-pip.py" && rm -rf "$TMP/get-pip.py" 2>/dev/null
verbose_msg "Cleaning up shellcheck $TMP/shellcheck*" && rm -rf "$TMP/shellcheck*" 2>/dev/null
verbose_msg "Cleaning up ctags $TMP/ctags*" && rm -rf "$TMP/ctags*" 2>/dev/null
verbose_msg "Cleaning up nvim $TMP/nvim" && rm -rf "$TMP/nvim" 2>/dev/null
exit_append
exit 1
}
function setup_config() {
local pre_cmd="$1"
local post_cmd="$2"
if [[ $BACKUP -eq 1 ]]; then
# We check if the target exist since we could be adding new
# scripts that may no be installed
if [[ -f $post_cmd ]] || [[ -d $post_cmd ]]; then
local name="${post_cmd##*/}"
# We want to copy all non symbolic links
if [[ ! -L $post_cmd ]]; then
verbose_msg "Backing up $post_cmd to ${BACKUP_DIR}/${name}"
cp -rf --backup=numbered "$post_cmd" "${BACKUP_DIR}/${name}"
elif [[ -d "$post_cmd/host" ]] && [[ $(ls -A "$post_cmd/host") ]]; then
# Check for host specific settings only if it's not empty
verbose_msg "Backing up $post_cmd/host to ${BACKUP_DIR}/${name}/host"
cp -rf --backup=numbered "$post_cmd/host" "${BACKUP_DIR}/${name}"
else
verbose_msg "Nothing to backup in $post_cmd"
fi
rm -rf "$post_cmd"
fi
elif [[ $FORCE_INSTALL -eq 1 ]]; then
verbose_msg "Removing $post_cmd"
rm -rf "$post_cmd"
elif [[ -f $post_cmd ]] || [[ -e $post_cmd ]] || [[ -d $post_cmd ]]; then
warn_msg "Skipping ${post_cmd##*/}, already exists in ${post_cmd%/*}"
return 1
fi
verbose_msg "Executing -> $CMD $pre_cmd $post_cmd"
if sh -c "$CMD $pre_cmd $post_cmd"; then
if [[ $BACKUP -eq 1 ]] && [[ $CMD == "cp -rf" ]]; then
local name="${post_cmd##*/}"
if [[ -d "${BACKUP_DIR}/${name}/host" ]] && [[ $(ls -A "${BACKUP_DIR}/${name}/host") ]]; then
status_msg "Restoring shell/host folder"
verbose_msg "Restoring shell host from ${BACKUP_DIR}/${name}/host"
cp -rf "${BACKUP_DIR}/${name}/host" "$post_cmd/host"
fi
fi
return 0
else
if [[ $CMD == "cp -rf" ]]; then
error_msg "Fail to copy $pre_cmd"
else
error_msg "Fail to link $pre_cmd"
fi
return 1
fi
}
function download_asset() {
if [[ $# -lt 2 ]]; then
error_msg "Not enough args"
return 1
fi
if ! has_fetcher; then
error_msg "This system has neither curl nor wget to download the asset $1"
return 2
fi
local asset="$1"
local url="$2"
local dest=""
if [[ -n $3 ]]; then
local dest="$3"
fi
local cmd=""
verbose_msg "Fetching $url"
if hash curl 2>/dev/null; then
cmd='curl -L '
if [[ $VERBOSE -eq 0 ]]; then
cmd="$cmd -s "
fi
cmd="$cmd $url"
if [[ -n $dest ]]; then
cmd="$cmd -o $dest"
fi
else # If not curl, wget is available since we checked with "has_fetcher"
cmd='wget '
if [[ $VERBOSE -eq 0 ]]; then
cmd="$cmd -q "
fi
if [[ -n $dest ]]; then
cmd="$cmd -O $dest"
fi
cmd="$cmd $url"
fi
if [[ $BACKUP -eq 1 ]]; then
if [[ -e $dest ]] || [[ -d $dest ]]; then
verbose_msg "Backing up $dest into $BACKUP_DIR"
mv --backup=numbered "$dest" "$BACKUP_DIR"
fi
elif [[ $FORCE_INSTALL -eq 1 ]] && [[ -f $dest ]]; then
verbose_msg "Removing $dest before re-download"
rm -rf "$dest"
elif [[ -e $dest ]] || [[ -d $dest ]]; then
warn_msg "Skipping $asset, already exists in ${dest%/*}"
return 4
fi
if [[ ! -d $dest ]] && [[ ! -f $dest ]]; then
verbose_msg "Downloading $asset"
if eval "$cmd"; then
return 0
else
error_msg "Failed to download $asset"
return 5
fi
else
warn_msg "$asset already exists in $dest, skipping download"
return 5
fi
}
function clone_repo() {
local repo="$1"
local dest="$2"
if hash git 2>/dev/null; then
if [[ $BACKUP -eq 1 ]]; then
if [[ -e $dest ]] || [[ -d $dest ]]; then
verbose_msg "Backing up $dest into $BACKUP_DIR"
mv --backup=numbered "$dest" "$BACKUP_DIR"
fi
elif [[ $FORCE_INSTALL -eq 1 ]]; then
verbose_msg "Removing $dest"
rm -rf "$dest"
elif [[ -e $dest ]] || [[ -d $dest ]]; then
warn_msg "Skipping ${repo##*/}, already exists in $dest"
return 1
fi
if [[ ! -d $dest ]] && [[ ! -f $dest ]]; then
verbose_msg "Cloning $repo into $dest"
# TODO: simplify this crap
if [[ $VERBOSE -eq 1 ]]; then
if git clone --recursive "$repo" "$dest"; then
return 0
fi
else
if git clone --quiet --recursive "$repo" "$dest" &>/dev/null; then
return 0
fi
fi
else
warn_msg "$dest already exists, skipping cloning"
return 3
fi
else
error_msg "Git command is not available"
return 2
fi
return 1
}
function setup_bin() {
status_msg "Getting shell functions and scripts"
for script in "${SCRIPT_PATH}"/bin/*; do
local scriptname="${script##*/}"
local file_basename="${scriptname%%.*}"
verbose_msg "Setup $script into $HOME/.local/bin/$file_basename"
setup_config "$script" "$HOME/.local/bin/$file_basename"
done
return 0
}
function setup_dotconfigs() {
local github="https://github.com"
local rst=0
status_msg "Getting python startup script"
setup_config "${SCRIPT_PATH}/scripts/pythonstartup.py" "$HOME/.local/lib/pythonstartup.py"
status_msg "Getting dotconfigs"
for script in "${SCRIPT_PATH}"/dotconfigs/*; do
local scriptname="${script##*/}"
# local file_basename="${scriptname%%.*}"
verbose_msg "Setup $script into $HOME/.${scriptname}"
setup_config "$script" "$HOME/.${scriptname}"
done
status_msg "Setting Config files"
local CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/"
# TODO: Add windows support
for config in "${SCRIPT_PATH}"/config/*; do
local config_name="${config##*/}"
if [[ ! -e "$CONFIG_DIR/$config_name" ]] || [[ $FORCE_INSTALL -eq 1 ]]; then
verbose_msg "Setup $config into ${CONFIG_DIR}"
setup_config "$config" "${CONFIG_DIR}/$config_name"
elif [[ -d $config ]]; then
for configfile in "$config"/*; do
local configfilename="${configfile##*/}"
if [[ ! -e "$CONFIG_DIR/$config_name/$configfilename" ]]; then
verbose_msg "Setting configfile $configfile into $CONFIG_DIR/$config_name/$configfilename"
setup_config "$configfile" "${CONFIG_DIR}/$config_name/configfilename"
else
warn_msg "Skipping ${configfilename}, already exists in ${CONFIG_DIR}/$config_name"
fi
done
elif [[ -f $config ]]; then
warn_msg "Skipping $config_name, already in $CONFIG_DIR"
else
error_msg "Weird, this config $config is not valid, since it is neither a directory or a file"
fi
done
# TODO: Add windows support
if [[ -f "$CONFIG_DIR/git/ignore" ]]; then
if [[ ! -e "${CONFIG_DIR}/fd/" ]] || [[ $FORCE_INSTALL -eq 1 ]]; then
verbose_msg "Setup fd global ignore based in global git ignore"
setup_config "$CONFIG_DIR/git/" "${CONFIG_DIR}/fd"
elif [[ ! -e "$CONFIG_DIR/fd/ignore" ]] || [[ $FORCE_INSTALL -eq 1 ]]; then
verbose_msg "Setup fd global ignore file based in global git ignore"
setup_config "$CONFIG_DIR/git/ignore" "${CONFIG_DIR}/fd/ignore"
else
warn_msg "Skipping fd global ignore, already installed"
fi
fi
local sh_shells=(bash zsh)
local csh_shells=(tcsh csh)
status_msg "Getting Shell init files"
for shell in "${sh_shells[@]}"; do
status_msg "Setting up ${shell}rc"
if [[ ! -f "$HOME/.${shell}rc" ]] || [[ $FORCE_INSTALL -eq 1 ]]; then
setup_config "${SCRIPT_PATH}/shell/init/shellrc.sh" "$HOME/.${shell}rc"
else
warn_msg "The file $HOME/.${shell}rc already exists, trying $HOME/.${shell}rc.$USER"
setup_config "${SCRIPT_PATH}/shell/init/shellrc.sh" "$HOME/.${shell}rc.$USER"
fi
done
setup_config "${SCRIPT_PATH}/shell/init/profile" "$HOME/.profile"
# setup_config "${SCRIPT_PATH}/shell/init/profile" "$HOME/.zprofile"
for shell in "${csh_shells[@]}"; do
status_msg "Setting up ${shell}rc"
if [[ ! -f "$HOME/.${shell}rc" ]]; then
setup_config "${SCRIPT_PATH}/shell/init/shellrc.csh" "$HOME/.${shell}rc"
else
warn_msg "The file $HOME/.${shell}rc already exists, trying $HOME/.${shell}rc.$USER"
setup_config "${SCRIPT_PATH}/shell/init/shellrc.csh" "$HOME/.${shell}rc.$USER"
fi
done
if is_windows; then
status_msg "Setting up Windows profile"
if [[ ! -f "$HOME/Documents/WindowsPowerShell/profile.ps1" ]]; then
[[ ! -d "$HOME/Documents/WindowsPowerShell/" ]] && mkdir -p "$HOME/Documents/WindowsPowerShell/"
setup_config "${SCRIPT_PATH}/shell/init/profile.ps1" "$HOME/Documents/WindowsPowerShell/profile.ps1"
fi
fi
setup_config "${SCRIPT_PATH}/shell/" "$HOME/.config/shell" || rst=1
if hash tmux 2>/dev/null; then
status_msg "Setting Tmux plugins"
[[ ! -d "$HOME/.tmux/plugins/tpm" ]] && mkdir -p "$HOME/.tmux/plugins/"
if [[ ! -d "$HOME/.tmux/plugins/tpm" ]]; then
if ! clone_repo "$github/tmux-plugins/tpm" "$HOME/.tmux/plugins/tpm"; then
error_msg "Failed to clone tmux plugin manager"
rst=1
fi
else
warn_msg "Skipping TPM, already install in $HOME/.tmux/plugins/tpm"
fi
else
warn_msg "Skipping tmux configs, tmux is not installed"
fi
return $rst
}
function setup_shell_scripts() {
local rst=0
if [[ $CURRENT_SHELL =~ (ba|z)?sh ]]; then
local github='https://raw.githubusercontent.com'
[[ ! -d "$HOME/.config/shell/scripts/" ]] && mkdir -p "$HOME/.config/shell/scripts/"
if [[ ! -f "$HOME/.config/shell/scripts/z.sh" ]]; then
status_msg 'Getting Z'
local z="${github}/rupa/z/master/z.sh"
if download_asset "Z script" "${z}" "$TMP/z.sh"; then
mv "$TMP/z.sh" "$HOME/.config/shell/scripts/z.sh"
[[ ! -f "$HOME/.z" ]] && touch "$HOME/.z"
else
error_msg 'Failed to download Z script'
rst=1
fi
else
warn_msg "Z script already install"
rst=1
fi
else
error_msg "Not compatible shell ${CURRENT_SHELL}"
rst=1
fi
if has_fetcher && { ! hash neofetch 2>/dev/null || [[ $FORCE_INSTALL -eq 1 ]]; }; then
[[ $FORCE_INSTALL -eq 1 ]] && status_msg 'Forcing neofetch install'
status_msg 'Getting neofetch'
local pkg='neofetch'
local url="https://raw.githubusercontent.com/dylanaraps/neofetch/master/neofetch"
if download_asset "neofetch script" "${url}" "$TMP/${pkg}"; then
mv "$TMP/${pkg}" "$HOME/.local/bin/"
chmod u+x "$HOME/.local/bin/${pkg}"
else
error_msg 'Failed to download neofetch script'
rst=1
fi
elif ! has_fetcher; then
error_msg "No curl neither wget to download neofetch"
rst=2
else
warn_msg "Skipping neofetch, already installed"
rst=2
fi
return $rst
}
function setup_git() {
status_msg "Installing Global Git settings"
setup_config "${SCRIPT_PATH}/git/gitconfig" "$HOME/.gitconfig"
status_msg "Installing Global Git templates and hooks"
setup_config "${SCRIPT_PATH}/git" "$HOME/.config/git"
status_msg "Setting up local git commands"
[[ ! -d "$HOME/.config/git/host" ]] && mkdir -p "$HOME/.config/git/host"
# Since we are initializing the system, we want to copy our own hooks in this repo
status_msg "Settings git hooks for the current dotfiles"
if [[ ! -d "${SCRIPT_PATH}/.git/hooks" ]]; then
setup_config "${SCRIPT_PATH}/git/templates/hooks/" "${SCRIPT_PATH}/.git/hooks"
else
[[ ! -d "${SCRIPT_PATH}/.git/hooks" ]] && mkdir -p "${SCRIPT_PATH}/.git/hooks"
for hooks in "${SCRIPT_PATH}"/git/templates/hooks/*; do
local scriptname="${script##*/}"
local file_basename="${scriptname%%.*}"
verbose_msg "Getting $hooks into ${SCRIPT_PATH}/.git/hooks/${hooks##*/}"
setup_config "$hooks" "${SCRIPT_PATH}/.git/hooks/${hooks##*/}"
done
fi
return 0
}
function get_nvim_dotfiles() {
status_msg "Setting up neovim"
if [[ $PORTABLES -eq 0 ]] && [[ $ALL -eq 0 ]] && [[ $NEOVIM_DOTFILES -eq 0 ]] && ! [[ $ARCH =~ ^arm ]]; then
local args="--portable"
[[ $FORCE_INSTALL -eq 1 ]] && args=" --force $args"
[[ $NOCOLOR -eq 1 ]] && args=" --nocolor $args"
[[ $VERBOSE -eq 1 ]] && args=" --verbose $args"
[[ $NEOVIM_DEV -eq 1 ]] && args=" --dev $args"
if ! hash nvim 2>/dev/null || [[ $FORCE_INSTALL -eq 1 ]]; then
if ! eval "${SCRIPT_PATH}/bin/get_nvim.sh ${args}"; then
error_msg ""
return 1
fi
fi
elif [[ $ARCH =~ ^arm ]]; then
warn_msg "Skipping neovim install, Portable not available for ARM systemas"
elif [[ $NEOVIM_DOTFILES -eq 0 ]]; then
verbose_msg "Skipping neovim install, already install with defaults or portables"
fi
if is_windows; then
# If we couldn't clone our repo, return
status_msg "Getting neovim in $HOME/AppData/Local/nvim"
if [[ -d "$HOME/.vim" ]]; then
setup_config "$HOME/.vim" "$HOME/AppData/Local/nvim"
setup_config "$HOME/.vim" "$HOME/.config/nvim"
elif [[ -d "$HOME/vimfiles" ]]; then
setup_config "$HOME/vimfiles" "$HOME/AppData/Local/nvim"
setup_config "$HOME/.vim" "$HOME/.config/nvim"
else
status_msg "Cloning neovim dotfiles in $HOME/AppData/Local/nvim"
if ! clone_repo "$URL/.vim" "$HOME/AppData/Local/nvim"; then
error_msg "Fail to clone dotvim files"
return 1
fi
setup_config "$HOME/AppData/Local/nvim" "$HOME/.config/nvim"
fi
else
# if the current command creates a symbolic link and we already have some vim
# settings, lets use them
status_msg "Checking existing vim dotfiles"
if [[ -d "$HOME/.vim" ]]; then
[[ $CMD == "ln -s" ]] && status_msg "Linking current vim dotfiles"
[[ $CMD == "cp -rf" ]] && status_msg "Copying current vim dotfiles"
if ! setup_config "$HOME/.vim" "$HOME/.config/nvim"; then
error_msg "Failed getting dotvim files"
return 1
fi
else
status_msg "Cloning neovim dotfiles in $HOME/.config/nvim"
if ! clone_repo "$URL/.vim" "$HOME/.config/nvim"; then
error_msg "Fail to clone dotvim files"
return 1
fi
fi
fi
return 0
}
function _windows_portables() {
local rst=0
local github='https://github.com'
if ! has_fetcher; then
error_msg "Missing curl and wget, aborting portables installation"
return 3
fi
if ! hash shellcheck 2>/dev/null || [[ $FORCE_INSTALL -eq 1 ]]; then
[[ $FORCE_INSTALL -eq 1 ]] && status_msg 'Forcing shellcheck install'
status_msg "Getting shellcheck"
local pkg='shellcheck-latest.zip'
local url="${github}/koalaman/shellcheck"
if download_asset "Shellcheck" "${url}/releases/download/latest/${pkg}" "$TMP/${pkg}"; then
[[ -d "$TMP/shellcheck-latest" ]] && rm -rf "$TMP/shellcheck-latest"
unzip -o "$TMP/${pkg}" -d "$TMP/shellcheck-latest"
chmod +x "$TMP/shellcheck-latest/shellcheck.exe"
mv "$TMP/shellcheck-latest/shellcheck.exe" "$HOME/.local/bin/shellcheck.exe"
verbose_msg "Cleaning up pkg ${TMP}/${pkg}" && rm -rf "${TMP:?}/${pkg}"
verbose_msg "Cleaning up data $TMP/shellcheck-latest" && rm -rf "$TMP/shellcheck-latest"
else
rst=1
fi
else
warn_msg "Skipping shellcheck, already installed"
rst=2
fi
if ! hash bat 2>/dev/null || [[ $FORCE_INSTALL -eq 1 ]]; then
[[ $FORCE_INSTALL -eq 1 ]] && status_msg 'Forcing bat install'
status_msg "Getting bat"
local pkg='bat.zip'
local url="${github}/sharkdp/bat"
if hash curl 2>/dev/null; then
# shellcheck disable=SC2155
local version="$(curl -Ls ${url}/tags | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+$' | sort -uh | head -n 1)"
else
# shellcheck disable=SC2155
local version="$(wget -qO- ${url}/tags | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+$' | sort -uh | head -n 1)"
fi
status_msg "Downloading bat version: ${version}"
local os_type='x86_64-pc-windows-msvc'
if download_asset "Bat" "${url}/releases/download/${version}/bat-${version}-${os_type}.zip" "$TMP/${pkg}"; then
pushd "$TMP" 1>/dev/null || return 1
verbose_msg "Extracting into $TMP/${pkg}"
if ! unzip -o "$TMP/${pkg}" -d "$TMP/bat-${version}-${os_type}/"; then
error_msg "An error occurred extracting zip file"
rst=1
else
chmod u+x "$TMP/bat-${version}-${os_type}/bat-${version}-${os_type}/bat.exe"
mv "$TMP/bat-${version}-${os_type}/bat-${version}-${os_type}/bat.exe" "$HOME/.local/bin/"
fi
verbose_msg "Cleaning up pkg ${TMP}/${pkg}" && rm -rf "${TMP:?}/${pkg}"
verbose_msg "Cleaning up data $TMP/bat-${version}-${os_type}" && rm -rf "$TMP/bat-${version}-${os_type}/"
popd 1>/dev/null || return 1
else
rst=1
fi
else
warn_msg "Skipping bat, already installed"
rst=2
fi
if ! hash delta 2>/dev/null || [[ $FORCE_INSTALL -eq 1 ]]; then
[[ $FORCE_INSTALL -eq 1 ]] && status_msg 'Forcing delta install'
status_msg "Getting delta"
local pkg='delta.zip'
local url="${github}/dandavison/delta"
if hash curl 2>/dev/null; then
# shellcheck disable=SC2155
local version="$(curl -Ls ${url}/tags | command grep -oE '0\.[1-8][0-9]\.[0-9]{1,2}' | sort -ruh | head -n 1)"
else
# shellcheck disable=SC2155
local version="$(wget -qO- ${url}/tags | command grep -oE '0\.[1-8][0-9]\.[0-9]{1,2}' | sort -ruh | head -n 1)"
fi
status_msg "Downloading delta version: ${version}"
local os_type='x86_64-pc-windows-msvc'
if download_asset "delta" "${url}/releases/download/${version}/delta-${version}-${os_type}.zip" "$TMP/${pkg}"; then
pushd "$TMP" 1>/dev/null || return 1
verbose_msg "Extracting into $TMP/${pkg}"
if ! unzip -o "$TMP/${pkg}"; then
error_msg "An error occurred extracting zip file"
rst=1
else
chmod u+x "$TMP/delta-${version}-${os_type}/delta.exe"