-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·2666 lines (2245 loc) · 93.9 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
trap 'echo "SIG INT was received. This program will be terminated." && exit 1' INT
# The directory that dotfiles resources will be installed
DOTDIR=".dotfiles"
# Full path of the dotdir
FULL_DOTDIR_PATH="${HOME}/${DOTDIR}"
# The directory that dotfiles resources will be backuped
BACKUPDIR=".backup_of_dotfiles"
# The full directory that dotfiles resources will be backuped
FULL_BACKUPDIR_PATH="${HOME}/${BACKUPDIR}"
# Anchor for backup
BACKUP_ANCHOR_FILE=
# Status of create backup anchor file. Already created backup anchor file
STAT_ALREADY_CREATED_BACKUP_ANCHOR_FILE=255
# Status of create backup anchor file. Succeeded in creating backup anchor file
STAT_SUCCEEDED_IN_CREATING_BACKUP_ANCHOR_FILE=0
# Status of create backup anchor file. Failed to create backup anchor file
STAT_FAILED_TO_CREATE_BACKUP_ANCHOR_FILE=1
# Status of backup. Not started.
STAT_BACKUP_NOT_STARTED=255
# Status of backup. Finished.
STAT_BACKUP_FINISHED=0
# Status of backup. In progress.
STAT_BACKUP_IN_PROGRESS=1
# Git repository location over https
GIT_REPOSITORY_HTTPS="https://github.com/TsutomuNakamura/dotfiles.git"
# Git repository location over ssh
GIT_REPOSITORY_SSH="git@github.com:TsutomuNakamura/dotfiles.git"
# Raw git repository location over https
RAW_GIT_REPOSITORY_HTTPS="https://raw.github.com/TsutomuNakamura/dotfiles"
# Default XDG_CONFIG_HOME for Linux
DEFAULT_XDG_CONFIG_HOME_FOR_LINUX="${HOME}/.config"
# Default XDG_CONFIG_HOME for Mac
DEFAULT_XDG_CONFIG_HOME_FOR_MAC="${HOME}/Library/Preferences"
# Default XDG_DATA_HOME for Linux
DEFAULT_XDG_DATA_HOME_FOR_LINUX="${HOME}/.local/share"
# Default XDG_DATA_HOME for Mac
DEFAULT_XDG_DATA_HOME_FOR_MAC="${HOME}/Library"
# zsh dir
ZSH_DIR="${HOME}/.zsh"
# Temporary git user email from previous .gitconfig
GIT_USER_EMAIL_STORE_FILE="git_tmp_user_email"
# Full file path of temporary git user email from previous .gitconfig
GIT_USER_EMAIL_STORE_FILE_FULL_PATH="${FULL_BACKUPDIR_PATH}/${GIT_USER_EMAIL_STORE_FILE}"
# Temporary git user name from previous .gitconfig
GIT_USER_NAME_STORE_FILE="git_tmp_user_name"
# Full file path of temporary git user name from previous .gitconfig
GIT_USER_NAME_STORE_FILE_FULL_PATH="${FULL_BACKUPDIR_PATH}/${GIT_USER_NAME_STORE_FILE}"
# Temporary git user signingkey from previous .gitconfig
GIT_USER_SIGNINGKEY_STORE_FILE="git_tmp_user_signingkey"
# Full file path of temporary git user signingkey from previous .gitconfig
GIT_USER_SIGNINGKEY_STORE_FILE_FULL_PATH="${FULL_BACKUPDIR_PATH}/${GIT_USER_SIGNINGKEY_STORE_FILE}"
# Temporary git commit gpgsign from previous .gitconfig
GIT_COMMIT_GPGSIGN_STORE_FILE="git_tmp_commit_gpgsign"
# Full file path of temporary git commit gpgsign from previous .gitconfig
GIT_COMMIT_GPGSIGN_STORE_FILE_FULL_PATH="${FULL_BACKUPDIR_PATH}/${GIT_COMMIT_GPGSIGN_STORE_FILE}"
# Temporary git gpg program from previous .gitconfig
GIT_GPG_PROGRAM_STORE_FILE="git_tmp_gpg_program"
# Full file path of temporary git gpg program from previous .gitconfig
GIT_GPG_PROGRAM_STORE_FILE_FULL_PATH="${FULL_BACKUPDIR_PATH}/${GIT_GPG_PROGRAM_STORE_FILE}"
GLOBAL_DELIMITOR=','
declare -g -A GIT_PROPERTIES_TO_KEEP=(
# ['label']="${tmp_file_path},${name_of_variable},${command_to_restore}"
['email']="${GIT_USER_EMAIL_STORE_FILE_FULL_PATH}${GLOBAL_DELIMITOR}INI__user__email${GLOBAL_DELIMITOR}git config --global user.email \"\${__arg__}\""
['name']="${GIT_USER_NAME_STORE_FILE_FULL_PATH}${GLOBAL_DELIMITOR}INI__user__name${GLOBAL_DELIMITOR}git config --global user.name \"\${__arg__}\""
['signingkey_id']="${GIT_USER_SIGNINGKEY_STORE_FILE_FULL_PATH}${GLOBAL_DELIMITOR}INI__user__signingkey${GLOBAL_DELIMITOR}git config --global user.signingkey \"\${__arg__}\""
['gpgsign_flag']="${GIT_COMMIT_GPGSIGN_STORE_FILE_FULL_PATH}${GLOBAL_DELIMITOR}INI__commit__gpgsign${GLOBAL_DELIMITOR}git config --global commit.gpgsign \"\${__arg__}\""
['gpg_program']="${GIT_GPG_PROGRAM_STORE_FILE_FULL_PATH}${GLOBAL_DELIMITOR}INI__gpg__program${GLOBAL_DELIMITOR}git config --global gpg.program \"\${__arg__}\""
)
# Directories which may be required by brew of Mac
# These element will be added a prefix in front of them with $(brew --prefix)
declare -g -a DIRECTORIES_MAY_REQUIRED_BY_BREW_ON_MAC=(
"/sbin"
)
# Git user name to store .gitconfig
GIT_USER_NAME=
# Git user email to store .gitconfig
GIT_USER_EMAIL=
# Cache of absolute backup dir
CASH_ABSOLUTE_BACKUPDIR=
# Distribution of this environment
DISTRIBUTION=
# Post message list
declare -g -a POST_MESSAGES=()
PACKAGES_TO_INSTALL_ON_DEBIAN="git vim vim-gtk ctags tmux zsh unzip ranger ffmpeg cmake python3-dev libclang-dev xclip build-essential xbindkeys npm"
PACKAGES_TO_INSTALL_ON_DEBIAN_THAT_HAS_GUI="fonts-noto fonts-noto-mono fonts-noto-cjk"
PACKAGES_TO_INSTALL_ON_UBUNTU="git vim vim-gtk ctags tmux zsh unzip ranger ffmpeg cmake python3-dev libclang-dev build-essential xclip xbindkeys npm"
PACKAGES_TO_INSTALL_ON_UBUNTU+=" neovim python-dev python3-dev python3-pip"
PACKAGES_TO_INSTALL_ON_UBUNTU_THAT_HAS_GUI="fonts-noto fonts-noto-mono fonts-noto-cjk fonts-noto-cjk-extra"
PACKAGES_TO_INSTALL_ON_CENTOS="git vim-enhanced gvim ctags tmux zsh unzip gnome-terminal ffmpeg cmake gcc-c++ make python3-devel xclip npm"
PACKAGES_TO_INSTALL_ON_CENTOS_THAT_HAS_GUI="google-noto-sans-cjk-fonts.noarch google-noto-serif-fonts.noarch google-noto-sans-fonts.noarch"
PACKAGES_TO_INSTALL_ON_FEDORA="git vim-enhanced ctags tmux zsh unzip gnome-terminal ranger ffmpeg cmake gcc-c++ make python3-devel clang clang-devel xclip xbindkeys npm"
PACKAGES_TO_INSTALL_ON_FEDORA+=" neovim python2-neovim python3-neovim"
PACKAGES_TO_INSTALL_ON_FEDORA_THAT_HAS_GUI="google-noto-sans-fonts.noarch google-noto-serif-fonts.noarch google-noto-mono-fonts.noarch google-noto-cjk-fonts.noarch"
PACKAGES_TO_INSTALL_ON_ARCH="gvim git ctags tmux zsh unzip gnome-terminal ranger ffmpeg cmake gcc make python3 clang xclip xbindkeys npm"
PACKAGES_TO_INSTALL_ON_ARCH+=" neovim python-neovim"
PACKAGES_TO_INSTALL_ON_ARCH_THAT_HAS_GUI="noto-fonts noto-fonts-cjk"
# Packages will be installed on Mac
##PACKAGES_TO_INSTALL_ON_MAC="vim ctags tmux zsh unzip cmake python3 llvm"
##PACKAGES_TO_INSTALL_ON_MAC+=" neovim"
# URL of tmux-plugin
URL_OF_TMUX_PLUGIN="https://github.com/tmux-plugins/tpm.git"
# Symbolic link list of configuration of vim.
declare -g -a VIM_CONF_LINK_LIST=(
# "<link_dest>,<link_src>"
"../../../resources/etc/config/vim/bats.vim/after/syntax/sh.vim,${FULL_DOTDIR_PATH}/.vim/after/syntax"
"../../../resources/etc/config/vim/indent/after/syntax/yaml.vim,${FULL_DOTDIR_PATH}/.vim/after/syntax"
"../../resources/etc/config/vim/bats.vim/ftdetect/bats.vim,${FULL_DOTDIR_PATH}/.vim/ftdetect"
"../../resources/etc/config/vim/snipmate-snippets.git/snippets/bats.snippets,${FULL_DOTDIR_PATH}/.vim/snippets"
"../../resources/etc/config/vim/snipmate-snippets.git/snippets/chef.snippets,${FULL_DOTDIR_PATH}/.vim/snippets"
)
# Directories should be deep linked
declare -g -a DEEP_LINK_DIRECTORIES=(".config" "bin" ".local")
# Files should be copied on only Mac
declare -g -a FILES_SHOULD_BE_COPIED_ON_ONLY_MAC=("Inconsolata for Powerline.otf")
# Answer status for question() yes
ANSWER_OF_QUESTION_YES=0
# Answer status for question() no
ANSWER_OF_QUESTION_NO=1
# Answer status for question() aborted
ANSWER_OF_QUESTION_ABORTED=255
# Git update type: just clone
GIT_UPDATE_TYPE_JUST_CLONE=0
# Git update type: remote then clone due to not git repository
GIT_UPDATE_TYPE_REMOVE_THEN_CLONE_DUE_TO_NOT_GIT_REPOSITORY=1
# Git update type: remove then clone due to wrong remote
GIT_UPDATE_TYPE_REMOVE_THEN_CLONE_DUE_TO_WRONG_REMOTE=2
# Git update type: remove then clone due do un pushed yet
GIT_UPDATE_TYPE_REMOVE_THEN_CLONE_DUE_TO_UN_PUSHED_YET=3
# Git update type: reset then remove untracked files then pull
GIT_UPDATE_TYPE_RESET_THEN_REMOVE_UNTRACKED_THEN_PULL=4
# Git update type: just pull
GIT_UPDATE_TYPE_JUST_PULL=5
# Git update type: remove then clone due to the branch name that going to be installed is different from current branch name
GIT_UPDATE_TYPE_REMOVE_THEN_CLONE_DUE_TO_BRANCH_IS_DIFFERENT=6
# Git update type: can not get git-update-type due to some reason
GIT_UPDATE_TYPE_ABOARTED=255
# Color of font red
FONT_COLOR_GREEN='\033[0;32m'
# Color of font yello
FONT_COLOR_YELLOW='\033[0;33m'
# Color of font red
FONT_COLOR_RED='\033[0;31m'
# Color of font end
FONT_COLOR_END='\033[0m'
# Emojis
EMOJI_START_EYES="🤩"
function main() {
cd "${HOME}" || {
echo "ERROR: Your home directory \"${HOME}\" isn't exist or you don't have permission to access it." >&2
return 1
}
check_environment || return 1
local flag_init=0
local flag_deploy=0
local flag_only_install_packages=0
local flag_no_install_packages=0
local branch="master"
local flag_cleanup=0
local url_of_repo="$GIT_REPOSITORY_HTTPS"
local error_count=0
while getopts "idonb:cgh" opts; do
case $opts in
i)
flag_init=1;;
d)
flag_deploy=1;;
o)
flag_only_install_packages=1;;
n)
flag_no_install_packages=1;;
b)
branch="$OPTARG";;
c)
flag_cleanup=1;;
g)
url_of_repo="$GIT_REPOSITORY_SSH";;
h | \?)
usage && return 0 ;;
esac
done
if [ "$flag_only_install_packages" == "1" ] && [ "$flag_no_install_packages" == "1" ]; then
echo "Some contradictional options were found. (-o|--only-install-packages and -n|--no-install-packages)"
return 1
fi
if [ "$flag_only_install_packages" == "1" ]; then
if do_i_have_admin_privileges; then
install_packages "$branch" || (( error_count++ ))
else
echo "Sorry, you don't have privileges to install packages." >&2
(( error_count++ ))
fi
elif [ "$flag_cleanup" == "1" ]; then
backup_current_dotfiles || {
echo "ERROR: Cleaning up and backup current dotfiles are failed." >&2
(( error_count++ ))
}
elif [ "$flag_init" == "1" ]; then
init "$branch" "$url_of_repo" "$flag_no_install_packages" || {
echo "ERROR: init() has failed." >&2
(( error_count++ ))
}
elif [ "$flag_deploy" == "1" ]; then
deploy
elif [ "$flag_init" != "1" ] && [ "$flag_deploy" != "1" ]; then
# It's a default behavior.
init "$branch" "$url_of_repo" "$flag_no_install_packages" || {
echo "ERROR: init() has failed." >&2
(( error_count++ ))
}
if [[ "$error_count" -eq 0 ]]; then
deploy || {
echo "ERROR: deploy() has failed." >&2
(( error_count++ ))
}
fi
fi
do_post_instructions || (( error_count++ ))
return $error_count
}
function check_environment() {
is_customized_xdg_base_directories || {
logger_err "Sorry, this dotfiles requires XDG Base Directory as default or unset XDG_CONFIG_HOME and XDG_DATA_HOME environments."
logger_err "Current environment variables XDG_CONFIG_HOME and XDG_DATA_HOME is set like below."
if [[ -z "${XDG_CONFIG_HOME}" ]]; then
logger_err " XDG_CONFIG_HOME=(unset)"
else
logger_err " XDG_CONFIG_HOME=\"${XDG_CONFIG_HOME}\""
fi
logger_err " -> This must be set \"${HOME}/.config\" in Linux or \"${HOME}/Library/Preferences\" in Mac or unset."
if [[ -z "${XDG_DATA_HOME}" ]]; then
logger_err " XDG_DATA_HOME=(unset)"
else
logger_err " XDG_DATA_HOME=\"${XDG_DATA_HOME}\""
fi
logger_err " -> This must be set \"${HOME}/.local/share\" in Linux or \"${HOME}/Library\" in Mac or unset."
return 1
}
[[ -z "$BASH" ]] && {
logger_err "This script must run as bash script"
return 1
}
[[ -z "$BASH_VERSION" ]] && {
logger_err "This session does not have BASH_VERSION environment variable. Is this a proper bash session?"
return 1
}
local current_bash_version=$(grep -o -E '^[0-9](\.[0-9])+' <<< "$BASH_VERSION")
vercomp "4.0.0" "$current_bash_version"
local result="$?"
[[ "$result" -eq 1 ]] && {
logger_err "Version of bash have to greater than 4.0.0."
logger_err "Please update your bash greater than 4.0.0 then run this script again."
logger_err "If you use mac, you can change new version of bash by running commands like below..."
logger_err " $ brew install bash"
logger_err " $ grep -q '/usr/local/bin/bash' /etc/shells || echo /usr/local/bin/bash | sudo tee -a /etc/shells"
logger_err "...then relogin or restart your Mac"
return 1
}
if [[ "$(get_distribution_name)" == "mac" ]]; then
check_environment_of_mac || {
logger_err "Failed to pass checking the environment of Mac"
return 1
}
fi
return 0
}
# Check environment of Mac
function check_environment_of_mac() {
local dir
local prefix="$(brew --prefix)"
for dir in "${DIRECTORIES_MAY_REQUIRED_BY_BREW_ON_MAC[@]}"; do
dir="${prefix}${dir}"
if [[ ! -d "${dir}" ]]; then
local msg="Directory \"${dir}\" that may be required by brew does not exist.\n"
msg+=" Rerun this script after you created a directory \"${dir}\"\n"
msg+=" example)\n"
msg+=" sudo mkdir \"${dir}\"\n"
msg+=" sudo chown $(whoami) \"${dir}\""
logger_err "$msg"
return 1
fi
if ! has_permission_to_rw "$dir"; then
local msg="Directory \"${dir}\" not permitted to write and read by user $(whoami)."
msg+=" Please check your permission whether you have a permission to read/write to the directory \"${dir}\""
logger_err "$msg"
return 1
fi
done
return 0
}
function has_permission_to_rw() {
local dir="$1"
local user_name="$(whoami)"
local is_owner=0
local is_group=0
declare -a groups=($(id -G -n $user_name))
if [[ "$(stat -f '%Su' "$dir")" != "$user_name" ]]; then
is_owner=1
fi
local own_group="$(stat -f '%Sg' "$dir")"
if ! contains_element "$own_group" "${groups[@]}"; then
is_group=1
fi
if [[ "$is_owner" -eq 1 ]] && [[ "$is_group" -eq 1 ]]; then
# You don't have permission to read/writer to "$dir"
return 1
fi
return 0
}
# Run post instructions
function do_post_instructions() {
local result=0
clear_backup_anchor_file || {
logger_warn "Failed to delete backup anchor file \"${BACKUP_ANCHOR_FILE}\". You would delete it by your own, please."
# TODO: Need not detect as an error.
# (( ++result ))
}
print_post_message_list
return $result
}
# Create backup anchor file
# @return 0: Creating anchor file has succeeded
# @return 1: Anchor file is already existed
function create_backup_anchor_file() {
local backup_dir="$1"
local uuid="$(uuidgen)"
BACKUP_ANCHOR_FILE="${backup_dir}/${uuid}.backup_anchor"
[[ -f "$BACKUP_ANCHOR_FILE" ]] && return $STAT_ALREADY_CREATED_BACKUP_ANCHOR_FILE
echo -n "$STAT_BACKUP_IN_PROGRESS" > "$BACKUP_ANCHOR_FILE"
[[ ! -f "$BACKUP_ANCHOR_FILE" ]] && return $STAT_FAILED_TO_CREATE_BACKUP_ANCHOR_FILE
return $STAT_SUCCEEDED_IN_CREATING_BACKUP_ANCHOR_FILE
}
# Update status of anchor file
function update_backup_anchor_file() {
local status="$1"
echo -n "$status" > "$BACKUP_ANCHOR_FILE"
# TODO: testing
}
# Get status of backup
function get_backup_anchor_file_status() {
[[ ! -f "$BACKUP_ANCHOR_FILE" ]] && return $STAT_BACKUP_NOT_STARTED
local status=$(cat "$BACKUP_ANCHOR_FILE")
if [[ $status -eq $STAT_BACKUP_FINISHED ]]; then
return $STAT_BACKUP_FINISHED
elif [[ $status -eq $STAT_BACKUP_IN_PROGRESS ]]; then
return $STAT_BACKUP_IN_PROGRESS
fi
return $STAT_BACKUP_NOT_STARTED
}
# Clear backup anchor file
function clear_backup_anchor_file() {
[[ -z "${BACKUP_ANCHOR_FILE}" ]] && return 0
[[ ! -f "${BACKUP_ANCHOR_FILE}" ]] && return 0
rm -f "${BACKUP_ANCHOR_FILE}"
# Return the status of deleting the file was succeeded or not
[[ ! -f "${BACKUP_ANCHOR_FILE}" ]]
}
function print_post_message_list() {
[[ ${#POST_MESSAGES[@]} -ne 0 ]] && {
print_boarder " Summary of the instruction "
for line in "${POST_MESSAGES[@]}"; do
echo -e "* $line"
done
print_boarder
}
return 0
}
# Output the message to stdout then push it to info message list.
function logger_info() {
local message="$@"
message="${FONT_COLOR_GREEN}INFO${FONT_COLOR_END}: $message"
echo -e "$message"
push_post_message_list "$message"
}
# Output the message to errout then push it to warn message list.
function logger_warn() {
local message="$@"
local line_no="${BASH_LINENO[0]}"
message="${FONT_COLOR_YELLOW}WARN${FONT_COLOR_END}: line ${line_no}: ${FUNCNAME[1]}(): $message"
echo -e "$message" >&2
push_post_message_list "$message"
}
function logger_err() {
local message="$@"
local line_no
local func_name="${FUNCNAME[1]}"
if [[ "$func_name" == "pushd" ]] || [[ "$func_name" == "mmkdir" ]] || [[ "$func_name" == "lln" ]]; then
# If this method called from pushd, print the caller and its line of pushd for traceability.
func_name="${FUNCNAME[2]}"
line_no="${BASH_LINENO[1]}"
else
line_no="${BASH_LINENO[0]}"
fi
message="${FONT_COLOR_RED}ERROR${FONT_COLOR_END}: line ${line_no}: ${func_name}(): $message"
echo -e "$message" >&2
push_post_message_list "$message"
}
function push_post_message_list() {
local message="$1"
POST_MESSAGES+=("$message")
}
# Print boarder on console
function print_boarder() {
local subject="$1"
echo -n "==${subject}"
local width=$(( $(tput cols) - 2 - ${#subject} ))
printf '=%.0s' $(seq 1 ${width})
echo
}
function usage() {
echo "usage"
}
# Initialize dotfiles repo
function init() {
local branch=${1:-master}
local url_of_repo=${2:-$GIT_REPOSITORY_HTTPS}
local flag_no_install_packages=${3:-0}
local result=0
local answer_of_question
if [[ "$flag_no_install_packages" == 0 ]]; then
if do_i_have_admin_privileges; then
# Am I root? Or, am I in the sudoers?
install_packages "$branch" || {
local m="Failed to install dependency packages."
m+="\n If you want to continue following processes that after installing packages, you can specify the option \"-n (no-install-packages)\"."
m+="\n ex) "
m+="\n bash -- <(curl -o- https://raw.githubusercontent.com/TsutomuNakamura/dotfiles/master/install.sh) -n"
logger_err "$m"
return 1
}
else
echo "= NOTICE ========================================================"
echo "You don't have privileges to install packages."
echo "Process of installing packages will be skipped."
echo "================================================================="
question "Do you continue to install the dotfiles without dependency packages? [Y/n]: "
answer_of_question=$?
if [[ "$answer_of_question" -eq "$ANSWER_OF_QUESTION_NO" ]] || \
[[ "$answer_of_question" -eq "$ANSWER_OF_QUESTION_ABORTED" ]]; then
return 255
fi
fi
fi
backup_current_dotfiles || {
logger_err "Failed to backup .dotfiles data. Stop the instruction init()."
return 1
}
# Install patched fonts in your home environment
# Cloe the repository if it's not existed
init_repo "$url_of_repo" "$branch" || {
logger_err "Failed to initializing repository. Remaining install process will be aborted."
return 1
}
if has_desktop_env; then
install_fonts || {
logger_err "Failed to installing fonts. Remaining install process will be aborted."
return 1
}
else
logger_info "Installing fonts were skipped due to this environment doesn't have desktop components."
fi
init_vim_environment || {
logger_err "Failed to initializing vim environment. Remaining install process will be aborted."
return 1
}
install_bin_utils || {
logger_err "Failed to installing bin utils that will be installed in ~/bin. Remaining install process will be aborted."
return 1
}
prepare_vscode || {
logger_err "Failed to inizializing VSCode's environment. Remaining installation process will be aborted."
return 1
}
return 0
}
# Install packages.
# Fonts will be installed only when the machine have some desktop environments.
function install_packages() {
local branch="${1:-master}"
local result=0
local packages=
if [[ "$(get_distribution_name)" == "debian" ]]; then
packages="${PACKAGES_TO_INSTALL_ON_DEBIAN}"
has_desktop_env && packages+=" ${PACKAGES_TO_INSTALL_ON_DEBIAN_THAT_HAS_GUI}"
# TODO: add_yarn_repository_to_debian_like_systems will not be called because
# yarn will be installed to add vim-prettier and this installer for Debian does not support Neovim on Debian
install_packages_with_apt $packages || (( result++ ))
elif [[ "$(get_distribution_name)" == "ubuntu" ]]; then
packages="${PACKAGES_TO_INSTALL_ON_UBUNTU}"
has_desktop_env && packages+=" ${PACKAGES_TO_INSTALL_ON_UBUNTU_THAT_HAS_GUI}"
# TODO: add result++
add_additional_repositories_for_ubuntu
install_packages_with_apt $packages || (( result++ ))
elif [[ "$(get_distribution_name)" == "centos" ]]; then
packages="${PACKAGES_TO_INSTALL_ON_CENTOS}"
has_desktop_env && packages+=" ${PACKAGES_TO_INSTALL_ON_CENTOS_THAT_HAS_GUI}"
# TODO: ranger not supported in centos
# TODO: Are there google-noto-mono-(sans|serif) fonts?
install_packages_with_yum $packages \
&& logger_warn "Package \"ranger\" will not be installed on Cent OS. So please install it manually." \
|| (( result++ ))
elif [[ "$(get_distribution_name)" == "fedora" ]]; then
packages="${PACKAGES_TO_INSTALL_ON_FEDORA}"
has_desktop_env && packages+=" ${PACKAGES_TO_INSTALL_ON_FEDORA_THAT_HAS_GUI}"
install_packages_with_dnf $packages || (( result++ ))
elif [[ "$(get_distribution_name)" == "arch" ]]; then
packages="${PACKAGES_TO_INSTALL_ON_ARCH}"
has_desktop_env && packages+=" ${PACKAGES_TO_INSTALL_ON_ARCH_THAT_HAS_GUI}"
install_packages_with_pacman $packages || (( result++ ))
elif [[ "$(get_distribution_name)" == "mac" ]]; then
install_packages_with_homebrew "$branch" || (( result++ ))
else
logger_err "Failed to get OS distribution to install packages."
(( result++ ))
fi
return $result
}
# Add additional repositories for ubuntu.
# This function should call after do_i_have_admin_privileges() has passed
function add_additional_repositories_for_ubuntu() {
sudo apt-get update || {
logger_err "Some error has occured when updating packages with apt-get update."
return 1
}
command -v add-apt-repository || {
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common || {
logger_err "Failed to install software-properties-common"
return 1
}
}
local os_version=
os_version=$(get_linux_os_version)
local ret=$?
if [[ "$ret" -ne 0 ]]; then
logger_err "Failed to get os version for ubuntu at add_additional_repositories_for_ubuntu()"
return 1
fi
# Ubuntu greater or equal to 18.04 does not need to add ppa repository for neovim
local result_of_vercomp=0
vercomp "18.04" "$os_version" || { result_of_vercomp=$?; true; }
if [[ "$result_of_vercomp" -eq 1 ]]; then
# Added ppa:neovim-ppa/stable to install neovim
sudo add-apt-repository ppa:neovim-ppa/stable -y || {
logger_err "Failed to add repository ppa:neovim-ppa/stable"
return 1
}
logger_info "Added additional apt repositories. (ppa:neovim-ppa/stable)"
else
logger_info "No need to add a repository for Neovim to Ubuntu ${os_version}. Skipped it"
fi
#add_yarn_repository_to_debian_like_systems || return 1
return 0
}
# Add yarn repository to debian like system
function add_yarn_repository_to_debian_like_systems() {
wget -qO - https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - || {
logger_err "Failed to add yarn repository's gpg key from https://dl.yarnpkg.com/debian/pubkey.gpg"
return 1
}
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list || {
logger_err "Failed to add yarn repository to /etc/apt/sources.list.d/yarn.list"
return 1
}
# "sudo apt update" will be run from the another section
return 0
}
# Get the value of XDG_CONFIG_HOME for individual environments appropliately.
function get_xdg_config_home() {
if [[ -z "${XDG_CONFIG_HOME}" ]]; then
echo "${HOME}$(get_suffix_xdg_config_home)"
else
# Use eval to expand special variable like "~"
eval echo "${XDG_CONFIG_HOME}"
fi
}
# Get the value of suffix of XDG_CONFIG_HOME
function get_suffix_xdg_config_home() {
if [[ "$(get_distribution_name)" = "mac" ]]; then
echo "/Library/Preferences"
else
echo "/.config"
fi
}
# Get the value of suffix of XDG_DATA_HOME
function get_suffix_xdg_data_home() {
if [[ "$(get_distribution_name)" = "mac" ]]; then
echo "/Library"
else
echo "/.local/share"
fi
}
# Get the value of XDG_DATA_HOME for individual environments appropliately.
function get_xdg_data_home() {
if [[ -z "${XDG_DATA_HOME}" ]]; then
if [[ "$(get_distribution_name)" = "mac" ]]; then
echo "${HOME}/Library"
else
echo "${HOME}/.local/share"
fi
else
eval echo "${XDG_DATA_HOME}"
fi
}
function install_the_font() {
local install_cmd="$1"
local font_name="$2"
# Append to front "\n " if length of variable is greater than 0.
local extra_msg_on_already_installed="${3:+\n $3}"
local extra_msg_on_installed="${4:+\n $4}"
local extra_msg_on_failed="${5:+\n $5}"
local extra_msg_on_unknown_err="${6:+\n $6}"
eval "$install_cmd"
local ret=$?
# 0: The font has already installed.
# 1: Installing the font has successfully.
# 2: Failed to install the font.
# *: Unknown error
case "$ret" in
0 )
echo -e "INFO: ${font_name} has already installed.${extra_msg_on_already_installed}"
;;
1 )
logger_info "${font_name} has installed.${extra_msg_on_installed}"
;;
2 )
logger_err "Failed to install ${font_name}.${extra_msg_on_failed}"
;;
* )
logger_err "Unknown error was occured when installing ${font_name}.${extra_msg_on_unknown_err}"
;;
esac
# This function would be finished successfully if return code of install_cmd() is 0(already installed) or 1(install has successfully).
return $ret
}
# Installe font
function install_fonts() {
local result=0
local distribution_name="$(get_distribution_name)"
local flag_fc_cache=0
if [[ "$distribution_name" = "mac" ]]; then
local font_dir="$(get_xdg_data_home)/Fonts"
else
local font_dir="$(get_xdg_data_home)/fonts"
fi
mkdir -p $font_dir
pushd $font_dir || return 1
install_the_font "_install_font_inconsolata_nerd" \
"Inconsolata for Powerline Nerd Font" \
"" \
"For more information about the font, please see \"https://github.com/ryanoasis/nerd-fonts\"." \
"Please install it manually from \"https://github.com/ryanoasis/nerd-fonts\" if necessary." \
"Please install it manually from \"https://github.com/ryanoasis/nerd-fonts\" if necessary."
local ret_install_font_inconsolata_nerd=$?
[[ $ret_install_font_inconsolata_nerd -eq 1 ]] && (( flag_fc_cache++ ))
[[ $ret_install_font_inconsolata_nerd -gt 1 ]] && (( result++ ))
install_the_font "_install_font_migu1m" \
"Migu 1M Font" \
"" \
"For more information about the font, please see \"https://ja.osdn.net/projects/mix-mplus-ipa/\"." \
"The program will install IPA font alternatively." \
"The program will install IPA font alternatively."
local ret_install_font_migu1m=$?
[[ $ret_install_font_migu1m -eq 1 ]] && (( flag_fc_cache++ ))
[[ $ret_install_font_migu1m -gt 1 ]] && (( result++ ))
if [[ "$distribution_name" != "mac" ]]; then
# Installing the emoji font only on Linux because Mac has already supported it.
install_the_font "_install_font_noto_emoji" \
"NotoEmojiFont" \
"" \
"For more information about the font, please see \"https://github.com/googlei18n/noto-emoji\"." \
"Please install it manually from \"https://github.com/googlei18n/noto-emoji\" if necessary." \
"Please install it manually from \"https://github.com/googlei18n/noto-emoji\" if necessary."
local ret_install_font_noto_emoji=$?
[[ $ret_install_font_noto_emoji -eq 1 ]] && (( flag_fc_cache++ ))
[[ $ret_install_font_noto_emoji -gt 1 ]] && (( result++ ))
fi
if [[ "$distribution_name" == "mac" ]]; then
# "Inconsolata for Powerline.otf" is installed(linked) in deploy() method
logger_info "This dotfiles recommends you to use font that patched nerd fonts to show some icons on your terminal. If you don't have any fonts, \"Inconsolata for Powerline.otf\" has installed and try it ${EMOJI_START_EYES} ."
fi
popd
if [[ "$flag_fc_cache" -ne 0 ]]; then
echo "Building font information cache files with \"fc-cache -f ${font_dir}\""
fc-cache -f $font_dir && logger_info "Font cache was recreated."
fi
return $result
}
# Install font Inconsolata Nerd Font Complete
# Return codes are...
# 0: Already installed
# 1: Installed successfully
# 2: Failed to install
function _install_font_inconsolata_nerd() {
local result=0
# Inconsolata for Powerline Nerd Font
if [[ ! -e "Inconsolata Nerd Font Complete.otf" ]] || [[ "$(wc -c < 'Inconsolata Nerd Font Complete.otf')" == "0" ]]; then
rm -f "Inconsolata Nerd Font Complete.otf"
curl -fLo "Inconsolata Nerd Font Complete.otf" \
https://raw.githubusercontent.com/ryanoasis/nerd-fonts/v2.1.0/patched-fonts/Inconsolata/complete/Inconsolata%20Nerd%20Font%20Complete.otf
local ret_of_curl=$?
if [[ "$ret_of_curl" -eq 0 ]] && [[ -e "Inconsolata Nerd Font Complete.otf" ]] && [[ "$(wc -c < 'Inconsolata Nerd Font Complete.otf')" != "0" ]]; then
# Installing font has succeeded
result=1
else
# Installing font has failed
rm -f "Inconsolata Nerd Font Complete.otf"
result=2
fi
fi
# TODO: If you want to install old Powerline Nerd Font, please download it from old tag
# curl -fLo "Inconsolata for Powerline Nerd Font Complete.otf" \
# https://cdn.rawgit.com/ryanoasis/nerd-fonts/v1.0.0/patched-fonts/Inconsolata/complete/Inconsolata%20for%20Powerline%20Nerd%20Font%20Complete.otf
return $result
}
# Install font migu1m (for Japanese)
# Return codes are...
# 0: Already installed
# 1: Installed successfully
# 2: Failed to install
function _install_font_migu1m() {
local file_zipped_migu1m="migu-1m-20200307.zip"
local dir_migu1m="${file_zipped_migu1m%%.*}"
# Migu 1M has already been installed?
if [[ -e "migu-1m-bold.ttf" ]] && [[ "$(wc -c < migu-1m-bold.ttf)" != "0" ]] \
&& [[ -e "migu-1m-regular.ttf" ]] && [[ "$(wc -c < migu-1m-regular.ttf)" != 0 ]]; then
# Migu M1 has already installed
return 0
fi
# Migu 1M for Japanese font
curl -fLo "${file_zipped_migu1m}" \
https://raw.githubusercontent.com/TsutomuNakamura/dotfiles/master/resources/fonts/migu1m/${file_zipped_migu1m}
local ret_of_curl=$?
if [[ "$ret_of_curl" -eq 0 ]] && [[ -e "${file_zipped_migu1m}" ]] && [[ "$(wc -c < ${file_zipped_migu1m})" -ne 0 ]]; then
unzip ${file_zipped_migu1m}
pushd ${dir_migu1m} || return 1
mv ./*.ttf ../
popd
else
# Failed to install
rm -rf ${file_zipped_migu1m}
return 2
fi
if [[ -e "migu-1m-bold.ttf" ]] && [[ "$(wc -c < migu-1m-bold.ttf)" != "0" ]] \
&& [[ -e "migu-1m-regular.ttf" ]] && [[ "$(wc -c < migu-1m-regular.ttf)" != 0 ]]; then
# Downloading migu1m fonts has been successfully
rm -rf ${dir_migu1m} ${file_zipped_migu1m}
return 1
fi
rm -rf ${dir_migu1m} ${file_zipped_migu1m} migu-1m-bold.ttf migu-1m-regular.ttf
return 2
}
# Install font noto emoji (for emoji)
# Return codes are...
# 0: Already installed
# 1: Installed successfully
# 2: Failed to install
function _install_font_noto_emoji() {
if [[ -e "NotoColorEmoji.ttf" ]]&& [[ $(wc -c < "NotoColorEmoji.ttf") -ne 0 ]]; then
# Already installed
return 0
fi
rm -f "NotoColorEmoji.ttf"
local ret=0
curl -fLo "NotoColorEmoji.ttf" \
https://raw.githubusercontent.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf || {
logger_err "Failed to install NotoColorEmoji.ttf (from https://raw.githubusercontent.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf)"
(( ret++ ))
}
if [[ "$ret" -eq 0 ]] && \
[[ -e "NotoColorEmoji.ttf" ]] && [[ $(wc -c < "NotoColorEmoji.ttf") -ne 0 ]]; then
# Success
return 1
fi
rm -f "NotoColorEmoji.ttf"
return 2
}
# Installing packages with apt.
# Please call this function only after call do_i_have_admin_privileges() which the return-code is 0(true).
function install_packages_with_apt() {
declare -a packages=($@)
declare -a packages_will_be_installed
local output
sudo apt-get update || {
logger_err "Some error has occured when updating packages with apt-get update."
return 1
}
local pkg_cache=$(apt list --installed 2> /dev/null | grep -v -E 'Listing...' | cut -d '/' -f 1)
if [[ -z "$pkg_cache" ]]; then
logger_err "Failed to get installed packages with apt list --installed."
return 1
fi
local available_packages=
available_packages="$(sudo apt-cache pkgnames)"
if [[ -z "$available_packages" ]]; then
logger_err "Failed to get available package list with 'apt-cache pkgnames'"
return 1
fi
for (( i = 0; i < ${#packages[@]}; i++ )) {
local p="${packages[i]}"
if (grep -E "^${p}$" &> /dev/null <<< "$pkg_cache"); then
# Remove already installed packages
echo "${p} has already installed. Skipped."
continue
fi
if ! (grep -E "^${p}$" &> /dev/null <<< "$available_packages"); then
logger_warn "Package ${p} is not available. Installing ${p} was skipped."
continue
fi
packages_will_be_installed+=("${packages[i]}")
}
if [[ "${#packages_will_be_installed[@]}" -eq 0 ]]; then
echo "There are no packages to install"
return 0
fi
echo "Installing ${packages_will_be_installed[@]}..."
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y ${packages_will_be_installed[@]} || {
logger_err "Some error occured when installing ${packages_will_be_installed[@]} with apt-get install."
return 1
}
local installed_packages="${packages_will_be_installed[@]}"
logger_info "Packages ${installed_packages} have been installed."
return 0
}
function install_packages_with_yum() {
[[ "${#@}" -eq 0 ]] && {
echo "ERROR: Failed to find packages to install at install_packages_with_yum()" >&2
return 1
}
install_packages_on_redhat "yum" $@
}
function install_packages_with_dnf() {
[[ "${#@}" -eq 0 ]] && {
echo "ERROR: Failed to find packages to install at install_packages_with_dnf()" >&2
return 1
}