-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·192 lines (160 loc) · 5.57 KB
/
setup
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
#!/bin/bash
# raise error if this script is not executed in this directory
if [[ ${0} != "./setup" && ${0} != "setup" ]]; then
echo -e "FATAL: this script must be executed in dotfiles directory."
echo -e "You need to change directories so you can execure this script by"
echo -e "typing the following command."
echo -e "=== EXECUTE BELOW ==="
echo -e "$ ./setup"
echo -e "=== EXECUTE ABOVE ==="
echo -e "Aborted."
exit 1
fi
function link_file_or_directory() {
if [ -L "${2}" ]; then
if [ "$(readlink "$2")" != "$1" ]; then
echo "INFO: \"$2\" already links other file \"$(readlink "$2")\". Unlinked."
fi
unlink "$2"
elif [ -e "$2" ]; then
echo "INFO: \"$2\" already exists. Moved this file as \"$2.backup_$(date '+%Y%m%d_%H%M%S')\"."
mv "$2" "$2.backup_$(date '+%Y%m%d_%H%M%S')"
fi
ln -s "${1}" "${2}"
}
function install_vim_bundle() {
# install NeoBundle
curl -LsS https://raw.githubusercontent.com/Shougo/neobundle.vim/master/bin/install.sh > install.sh
sh ./install.sh 1>/dev/null 2>/dev/null
rm ./install.sh
# install vim-plug
if [ ! -e ~/.vim/autoload ]; then
mkdir -p ~/.vim/autoload
fi
curl -LsS https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim > ~/.vim/autoload/plug.vim
# install Vundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim 1>/dev/null 2>/dev/null
}
function install_vim_plugin() {
if [ ! -e ~/.is_installed_vim_plugin ]; then
vi
echo "true" > ~/.is_installed_vim_plugin
fi
}
function copy_gitconfig_local() {
if [ ! -f $PWD/core/gitconfig.local ]; then
cp $PWD/core/gitconfig.local.sample $PWD/core/gitconfig.local
fi
}
function check_git_version() {
# if Git version is 2.12.1, git_version is assigned to 2, and git_sub_version is assigned to 12.
git_version=`git --version | egrep -o '[0-9]+(\.[0-9]+)?' | head -1 | egrep -o '^[0-9]*' | head -1`
git_sub_version=`git --version | egrep -o '[0-9]+(\.[0-9]+)?' | head -1 | egrep -o '[0-9]*$' | head -1`
recommended_git_version=2
recommended_git_sub_version=8
# if Git version is less than recommended Git version
if [ "${git_version}" -lt "${recommended_git_version}" ] || ( [ "${git_version}" -eq "${recommended_git_version}" ] && [ "${git_sub_version}" -lt "${recommended_git_sub_version}" ] ); then
echo -e "WARNING: Your Git version is less than ${recommended_git_version}.${recommended_git_sub_version}."
echo -e "It is recommended to use Git ${recommended_git_version}.${recommended_git_sub_version} or higher.\n"
fi
}
function make_dotfiles_path() {
if [ ! -e $HOME/.dotpath ]; then
touch $HOME/.dotpath
fi
echo -e "DOTPATH=$PWD" > $HOME/.dotpath
}
function reload_profile() {
case "$(basename $(echo $SHELL))" in
bash)
source ~/.bash_profile
;;
zsh)
source ~/.zprofile
;;
esac
}
function install_brew() {
if [ "$(echo "$OSTYPE" | grep -c "^darwin")" -eq 1 ] && ! which brew 1>/dev/null 2>/dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
sudo chown -R $(whoami) /usr/local/Cellar
sudo chown -R $(whoami) /usr/local/Caskroom
fi
}
function install_packages() {
case "${OSTYPE}" in
darwin*)
: # brew bundle install --global --no-lock
;;
linux*)
package restore
;;
esac
}
function change_default_shell() {
if [[ "$(basename $(echo $SHELL))" != "zsh" ]]; then
if [[ "$(cat /etc/shells | grep -c $(which zsh))" = 0 ]]; then
echo $(which zsh) | sudo tee -a /etc/shells
fi
chsh -s $(which zsh)
fi
}
# copy gitconfig.local.sample to gitconfig.local
copy_gitconfig_local
# set DOTPATH
make_dotfiles_path
# link all files in core/ directory
for src_path in $PWD/core/*
do
if [ -f ${src_path} ]; then
dst_path=${HOME}/.`basename ${src_path}`
link_file_or_directory ${src_path} ${dst_path}
fi
done
# create .vim/ directory if not exists
if [ ! -e $HOME/.vim ]; then
mkdir $HOME/.vim
fi
# create VS Code preference directory if not exists
# TODO: this depends on macOS, so support for Linux
if [ ! -e $HOME/Library/Application\ Support/Code/User ]; then
mkdir -p $HOME/Library/Application\ Support/Code/User
fi
link_file_or_directory $PWD/vim $HOME/.vim/config
link_file_or_directory $PWD/git_template $HOME/.git_template
link_file_or_directory $PWD/bin/src $HOME/.bin
link_file_or_directory $PWD/config $HOME/.config
link_file_or_directory $PWD/zsh $HOME/.zsh
[ -d "$HOME/.bundle" ] || mkdir $HOME/.bundle
link_file_or_directory $PWD/bundle/config $HOME/.bundle/config
# link VS Code preferences
# TODO: this depends on macOS, so support for Linux
for src_path in ${PWD}/vscode/*
do
if [ -f ${src_path} ]; then
dst_path="$HOME/Library/Application Support/Code/User/`basename ${src_path}`"
link_file_or_directory "${src_path}" "${dst_path}"
fi
done
# Automator workflows
if [ ! -e $HOME/Library/Workflows ]; then
link_file_or_directory $PWD/macos/Workflows $HOME/Library/Workflows
elif [ ! -L $HOME/Library/Workflows ] && [[ ${OSTYPE} =~ ^darwin.+ ]]; then
echo "WARNING: Automator workflows already exist. Move them to another location."
fi
# unlink unnecessary files
unlink $HOME/.gitconfig.local.sample
# unlink if not Apple silicon Mac
if [ "$(uname -p)" != "arm" ] || [ "$(echo "$OSTYPE" | grep -c "^darwin")" -ne 1 ]; then
unlink $HOME/.gitconfig.arm_mac
fi
install_vim_bundle
check_git_version
reload_profile
touch .git/hooks/tmp && rm -r .git/hooks/* && git init
install_vim_plugin
install_brew
install_packages
change_default_shell
echo -e "Done! Check warnings if they exist."
exit 0