-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
354 lines (295 loc) · 9.17 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
#!/bin/bash
#
# 2019/07/18 StarryLeo
############################ SETUP PARAMETERS
app_name='starry-vim'
app_path="$HOME/.starry-vim"
repo_url='https://github.com/StarryLeo/starry-vim.git'
repo_branch='master'
plug_url='https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
debug_mode='0'
fork_maintainer='0'
backup_dir="$HOME/.cache/.starry-vim_backup"
backup_time_s='5184000'
backup_time_v='20736000'
############################ BASIC SETUP TOOLS
red="\33[31m"
green="\33[32m"
blue="\33[34m"
none="\33[0m"
msg() {
printf '%b\n' "$1" >&2
}
success() {
if [ "$ret" -eq '0' ]; then
msg "${green}[✔]${none} ${1}${2}"
fi
}
error() {
msg "${red}[✘]${none} ${1}${2}"
exit 1
}
debug() {
if [ "$debug_mode" -eq '1' ] && [ "$ret" -gt '1' ]; then
msg "An error occurred in function \"${red}${FUNCNAME[$i+1]}${none}\" on line ${red}${BASH_LINENO[$i+1]}${none}, we're sorry for that."
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
# throw error on non-zero return value
if [ "$?" -ne 0 ]; then
error "You must have '${red}${1}${none}' installed to continue."
fi
}
variable_set() {
if [ -z "$1" ]; then
error "You must have your ${red}HOME${none} environmental variable set to continue."
fi
}
lnif() {
if [ -e "$1" ]; then
ln -sf "$1" "$2"
fi
ret="$?"
debug
}
successful() {
if [ "$ret" -eq '0' ]; then
msg "${green}${1}${2}${none} "
fi
}
check_update() {
if [ -e "$1" ]; then
update_mode="update"
else
update_mode=""
fi
}
############################ SETUP FUNCTIONS
do_backup() {
if [ -e "$1" ] || [ -e "$2" ] || [ -e "$3" ]; then
today=`date +%Y%m%d_%s`
if [ "${update_mode}" = "update" ]; then
today_s=${today##*_}
if [ -f "$7" ]; then
last_update=`sed -n '$p' $7`
last_update_day_s=${last_update##*_}
time_u=`expr $today_s - $last_update_day_s`
msg "Last update $4 was ${green}${time_u}${none} seconds ago."
if [ -e "$6" ]; then
for filename in `ls -a $6`
do
last_backup=$filename
done
last_backup_day_s=${last_backup##*_}
time_b=`expr $today_s - $last_backup_day_s`
if [ "$time_b" -gt "$8" ]; then
msg "Last backup $4 was ${green}${time_b}${none} seconds ago."
msg "Starting backup $4..."
echo "update_backup_$today" >> "$7"
cp -a "$5" "$6/.$4.$today"
cp -a "$1" "$6/.$4.vim.$today"
ret="$?"
success "$4 has been backed up."
elif [ "$time_b" -gt "$9" ]; then
msg "Last backup $4 was ${green}${time_b}${none} seconds ago."
msg "Starting backup $4..."
echo "update_backup_$today" >> "$7"
cp -a "$5" "$6/.$4.$today"
ret="$?"
success "$4 has been backed up."
else
echo "update_$today" >> "$7"
fi
fi
else
msg "Starting backup $4..."
if [ ! -e "$6" ]; then
mkdir -p "$6"
fi
if [ ! -e "$6/.history" ]; then
mkdir -p "$6/.history"
fi
touch "$6/.history/update.history"
echo "update_backup_$today" >> "$7"
cp -a "$5" "$6/.$4.$today"
ret="$?"
success "$4 has been backed up."
fi
else
msg "Attempting to back up your original vim configuration."
msg "Starting backup..."
for i in "$1" "$2" "$3"; do
[ -e "$i" ] && [ ! -L "$i" ] && mv -v "$i" "$i.$today";
done
echo ""
ret="$?"
success "Your original vim configuration has been backed up."
fi
else
msg "Do not need to back up your original vim configuration."
fi
debug
}
sync_repo() {
local repo_path="$1"
local repo_url="$2"
local repo_branch="$3"
local repo_name="$4"
if [ ! -e "$repo_path" ]; then
msg "Trying to clone $repo_name..."
mkdir -p "$repo_path"
git clone -b "$repo_branch" "$repo_url" "$repo_path"
ret="$?"
success "Successfully cloned $repo_name."
else
msg "Trying to update $repo_name..."
cd "$repo_path" && git pull origin "$repo_branch"
ret="$?"
success "Successfully updated $repo_name."
fi
debug
}
create_symlinks() {
local source_path="$1"
local target_path="$2"
if [ ! -e "$2/.vim" ]; then
mkdir -p "$2/.vim"
fi
lnif "$source_path/.vimrc" "$target_path/.vimrc"
if program_exists "nvim"; then
if [ ! -e "$2/.config" ]; then
mkdir -p "$2/.config"
else
today=`date +%Y%m%d_%s`
for i in "$2/.config/nvim" "$2/.config/nvim/init.vim"; do
[ -e "$i" ] && [ ! -L "$i" ] && mv -v "$i" "$i.$today";
done
fi
lnif "$2/.vim" "$target_path/.config/nvim"
lnif "$source_path/.vimrc" "$target_path/.config/nvim/init.vim"
[ -L "$2/.vim/.vim" ] && rm "$2/.vim/.vim"
fi
ret="$?"
success "Setting up vim symlinks."
debug
}
generate_dot_starry() {
if [ ! -e "$1" ]; then
mkdir -p "$1"
cp "$2" "$1/init.vim"
touch "$1/config.vim"
touch "$1/packages.vim"
touch "$1/README.md"
ret="$?"
success "Successfully generate .starry in your HOME directory."
elif [ ! -f "$1/init.vim" ]; then
cp "$2" "$1/init.vim"
ret="$?"
success "Successfully generate .starry/init.vim in your HOME directory."
fi
debug
}
setup_fork_mode() {
if [ "$1" -eq '1' ]; then
if [ ! -e "$2/fork" ]; then
mkdir -p "$2/fork"
touch "$2/fork/config.vim"
touch "$2/fork/packages.vim"
touch "$2/fork/README.md"
ret="$?"
success "Created fork maintainer files."
debug
fi
fi
}
setup_plug() {
local system_shell="$SHELL"
export SHELL='/bin/sh'
if [ "${update_mode}" = "update" ]; then
msg "Starting updating plugins..."
vim \
"+set nomore" \
"+PlugUpdate" \
"+qall"
export SHELL="$system_shell"
ret="$?"
success "Now updating plugins using vim-plug."
else
msg "Starting installing plugins..."
vim \
"+set nomore" \
"+PlugInstall" \
"+qall"
export SHELL="$system_shell"
ret="$?"
success "Now installing plugins using vim-plug."
fi
debug
}
install_vim_plug() {
curl -fLo "$1/plug.vim" --create-dirs "$2"
if [ "${update_mode}" = "update" ]; then
ret="$?"
success "Successfully updated vim-plug for starry-vim."
else
ret="$?"
success "Successfully installed vim-plug for starry-vim."
fi
debug
}
############################ MAIN()
variable_set "$HOME"
program_must_exist "vim"
program_must_exist "git"
program_must_exist "curl"
check_update "$app_path"
do_backup "$HOME/.vim" \
"$HOME/.vimrc" \
"$HOME/.gvimrc" \
"$app_name" \
"$app_path" \
"$backup_dir" \
"$backup_dir/.history/update.history" \
"$backup_time_v" \
"$backup_time_s"
sync_repo "$app_path" \
"$repo_url" \
"$repo_branch" \
"$app_name"
create_symlinks "$app_path" \
"$HOME"
generate_dot_starry "$HOME/.starry" \
"$app_path/init.vim"
setup_fork_mode "$fork_maintainer" \
"$app_path"
install_vim_plug "$HOME/.vim/autoload" \
"$plug_url"
setup_plug
ret="$?"
successful " _ _ "
successful " ___ | |_ __ _ _ __ _ __ _ _ __ __(_) _ __ ___ "
successful " / __|| __|/ _\` || '__|| '__|| | | | _____\ \ / /| || '_ \` _ \ "
successful " \__ \| |_| (_| || | | | | |_| ||_____|\ V / | || | | | | |"
successful " |___/ \__|\__,_||_| |_| \__, | \_/ |_||_| |_| |_|"
successful " |___/ "
successful ""
if [ "${update_mode}" = "update" ]; then
successful "...is now updated!"
else
successful "...is now installed!"
fi
successful ""
msg "${blue}Thanks for installing $app_name.${none}"
msg "${blue}© `date +%Y` https://github.com/StarryLeo/starry-vim${none}"
debug
exit