-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·252 lines (222 loc) · 8.88 KB
/
setup.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
#!/bin/bash
setup-dotfiles() {
local GREEN RED YELLOW RESET
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
RESET='\033[0m'
local GIT_PUSH_URL GIT_DIR
GIT_PUSH_URL='[email protected]:xpac1985/dotfiles.git'
GIT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) # stolen from https://stackoverflow.com/a/246128/2177148
### .bashrc
if [ ! -f ~/.bashrc ]
then
# file doesn't exist
ln -s ~/dotfiles/bashrc ~/.bashrc && echo -e "${GREEN}.bashrc link created${RESET}"
else
if [ -L ~/.bashrc ] && [ "$(readlink ~/.bashrc)" = ~/dotfiles/bashrc ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.bashrc already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
if grep -q 'source ~/dotfiles/bashrc' ~/.bashrc
then
# file has a source statement for dotfiles
echo -e "${GREEN}.bashrc already exists, and it sources dotfiles/bashrc${RESET}"
else
# file does not have a source statement for dotfiles
printf '\nsource ~/dotfiles/bashrc\n' >> ~/.bashrc
if [ $? -eq 0 ]
then
# successfully added source statement to file
echo -e "${YELLOW}created 'source' statement in ~/.bashrc to dotfiles${RESET}"
else
# failed to add source statement to file
echo -e "${RED}failed to create 'source' statement in ~/.bashrc to dotfiles${RESET}"
fi
fi
fi
fi
### .nanorc
if [ ! -f ~/.nanorc ]
then
# file doesn't exist
ln -s ~/dotfiles/nanorc ~/.nanorc && echo -e "${GREEN}.nanorc link created${RESET}"
else
if [ -L ~/.nanorc ] && [ "$(readlink ~/.nanorc)" = ~/dotfiles/nanorc ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.nanorc already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.nanorc ~/.nanorc.bak && ln -s ~/dotfiles/nanorc ~/.nanorc
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.nanorc to ~/.nanorc.bak, then linked .nanorc to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.nanorc to ~/.nanorc.bak or create symlink${RESET}"
fi
fi
fi
### .tmux.conf
if [ ! -f ~/.tmux.conf ]
then
# file doesn't exist
ln -s ~/dotfiles/tmux.conf ~/.tmux.conf && echo -e "${GREEN}.tmux.conf link created${RESET}"
else
if [ -L ~/.tmux.conf ] && [ "$(readlink ~/.tmux.conf)" = ~/dotfiles/tmux.conf ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.tmux.conf already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.tmux.conf ~/.tmux.conf.bak && ln -s ~/dotfiles/tmux.conf ~/.tmux.conf
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.tmux.conf to ~/.tmux.conf.bak, then linked .tmux.conf to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.tmux.conf to ~/.tmux.conf.bak or create symlink${RESET}"
fi
fi
fi
### .profile
if [ ! -f ~/.profile ]
then
# file doesn't exist
ln -s ~/dotfiles/profile ~/.profile && echo -e "${GREEN}.profile link created${RESET}"
else
if [ -L ~/.profile ] && [ "$(readlink ~/.profile)" = ~/dotfiles/profile ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.profile already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.profile ~/.profile.bak && ln -s ~/dotfiles/profile ~/.profile
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.profile to ~/.profile.bak, then linked .profile to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.profile to ~/.profile.bak or create symlink${RESET}"
fi
fi
fi
### .vimrc
if [ ! -f ~/.vimrc ]
then
# file doesn't exist
ln -s ~/dotfiles/vimrc ~/.vimrc && echo -e "${GREEN}.vimrc link created${RESET}"
else
if [ -L ~/.vimrc ] && [ "$(readlink ~/.vimrc)" = ~/dotfiles/vimrc ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.vimrc already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.vimrc ~/.vimrc.bak && ln -s ~/dotfiles/vimrc ~/.vimrc
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.vimrc to ~/.vimrc.bak, then linked .vimrc to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.vimrc to ~/.vimrc.bak or create symlink${RESET}"
fi
fi
fi
### init.vim
# symlink from vimrc
if [ ! -f ~/.config/nvim/init.vim ]
then
# file doesn't exist
if [ ! -d ~/.config/nvim/ ]; then
# config directory doesn't exist
mkdir -p ~/.config/nvim/ && echo -e "${GREEN}nvim config directory created${RESET}"
fi
ln -s ~/dotfiles/vimrc ~/.config/nvim/init.vim && echo -e "${GREEN}vim.init link created${RESET}"
else
if [ -L ~/.config/nvim/init.vim ] && [ "$(readlink ~/.config/nvim/init.vim)" = ~/dotfiles/vimrc ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}vim.init already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.config/nvim/init.vim ~/.config/nvim/init.vim.bak && ln -s ~/dotfiles/vimrc ~/.config/nvim/init.vim
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.config/nvim/init.vim to ~/.config/nvim/init.vim.bak, then linked init.vim to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.config/nvim/init.vim to ~/.config/nvim/init.vim.bak or create symlink${RESET}"
fi
fi
fi
### .gitconfig
if [ ! -f ~/.gitconfig ]
then
# file doesn't exist
ln -s ~/dotfiles/gitconfig ~/.gitconfig && echo -e "${GREEN}.gitconfig link created${RESET}"
else
if [ -L ~/.gitconfig ] && [ "$(readlink ~/.gitconfig)" = ~/dotfiles/gitconfig ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.gitconfig already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.gitconfig ~/.gitconfig.bak && ln -s ~/dotfiles/gitconfig ~/.gitconfig
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.gitconfig to ~/.gitconfig.bak, then linked .gitconfig to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.gitconfig to ~/.gitconfig.bak or create symlink${RESET}"
fi
fi
fi
### .ssh/config
if [ ! -f ~/.ssh/config ]
then
# file doesn't exist
ln -s ~/dotfiles/sshconfig ~/.ssh/config && echo -e "${GREEN}.ssh/config link created${RESET}"
else
if [ -L ~/.ssh/config ] && [ "$(readlink ~/.ssh/config)" = ~/dotfiles/sshconfig ]
then
# file exists and is valid symlink to dotfiles
echo -e "${GREEN}.ssh/config already linked to dotfiles${RESET}"
else
# file exists, but is not a valid symlink to dotfiles
mv ~/.ssh/config ~/.ssh/config.bak && ln -s ~/dotfiles/sshconfig ~/.ssh/config
if [ $? -eq 0 ]
then
# successfully added moved old file and linked to dotfiles
echo -e "${YELLOW}Moved existing ~/.ssh/config to ~/.ssh/config.bak, then linked .ssh/config to dotfiles${RESET}"
else
# failed to either move old file or link to dotfiles
echo -e "${RED}Failed to either move ~/.ssh/config to ~/.ssh/config.bak or create symlink${RESET}"
fi
fi
fi
### Change git repo push URL
git --git-dir="${GIT_DIR}/.git" remote -v | grep -q "${GIT_PUSH_URL}"
if [ $? -eq 0 ]
then
echo -e "${GREEN}Git repo push URL already properly set to use SSH${RESET}"
else
git --git-dir="${GIT_DIR}/.git" remote set-url --push origin ${GIT_PUSH_URL}
git --git-dir="${GIT_DIR}/.git" remote -v | grep "(push)" | grep -q "${GIT_PUSH_URL}"
if [ $? -eq 0 ]; then
echo -e "${YELLOW}Git repo push URL now set to use SSH${RESET}"
else
echo -e "${RED}Failed to change Git repo push URL for reasons unknown to mankind${RESET}"
fi
fi
}
setup-dotfiles