-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.zsh
executable file
·407 lines (327 loc) · 8.91 KB
/
bootstrap.zsh
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
#!/usr/bin/env zsh
# ask for sudo permissions upfront
sudo -v
# keep permissions for the duration of the script
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
#
# variable declarations
#
platform=''
package_manager=''
package_manager_arguments=''
log_level=1
force=false
default_directory="$HOME"
tf2_directory="$default_directory/.local/share/Steam/steamapps/common/Team Fortress 2"
#
# parse command arguments
#
while test $# -gt 0; do
case "$1" in
-f|--force)
force=true
shift
;;
-v|--verbose)
log_level=3
shift
;;
-vv)
log_level=4
shift
;;
-d|--debug)
log_level=4
set -x
default_directory="./temp-dir"
shift
;;
*)
break
;;
esac
done
# set default_directory to the last argument as long as were not in debug mode
if [[ -n "${@: -1}" && "$default_directory" != "./temp-dir" ]]; then
default_directory="${@: -1}"
fi
# configuration variables
#
dependencies=(
stow
git
zsh
tmux
wget
bat
git-delta
)
arch_dependencies=(
neovim
sublime-text-dev
archey-git
rxvt-unicode
adobe-source-code-pro-fonts
nerd-fonts-source-code-pro
ttf-font-awesome-4
acpi
alsa-utils
xorg-xinit
xorg-xrandr
xtitle
xdo
picom
lemonbar-xft-git
rofi
feh
dunst
i3lock
scrot
imagemagick
bspwm
sxhkd
mopidy
ncmpcpp
socat
playerctl
awesome
autocutsel
jq
mpc
inotify-tools
pacman-contrib
pulseaudio
lpass
xsel
watchman # for coc.nvim rename symbol across file
alacritty
)
osx_dependencies=(
ripgrep
bitwarden-cli
bash
fzf
neovim
watchman # for coc.nvim rename symbol across file
)
debian_dependencies=()
git_dependencies=(
"--recursive https://github.com/sorin-ionescu/prezto.git $default_directory/.zprezto"
"https://github.com/n0kk/ahud.git '$tf2_directory/tf/custom/ahud-master'"
"https://github.com/tmux-plugins/tpm.git $default_directory/.tmux/plugins/tpm"
"https://github.com/chriskempson/base16-shell.git $default_directory/.config/base16-shell"
)
# dotfiles to symlink
# utility_name -> target_directory
typeset -A packages; packages=(
zsh "$default_directory"
git "$default_directory"
vim "$default_directory"
nvim "$default_directory/.config/nvim"
tmux "$default_directory"
tf2 "$tf2_directory/tf/custom"
i3 "$default_directory/.config/i3"
colors "$default_directory/.colors"
xorg "$default_directory"
dunst "$default_directory/.config/dunst"
picom "$default_directory/.config/picom"
bspwm "$default_directory/.config/bspwm"
sxhkd "$default_directory/.config/sxhkd"
mopidy "$default_directory/.config/mopidy"
ncmpcpp "$default_directory/.config/ncmpcpp"
wallpapers "$default_directory/.config/wallpapers"
bin "$default_directory/.bin"
kwm "$default_directory/.kwm"
khd "$default_directory"
awesomewm "$default_directory/.config/awesome"
systemd "$default_directory/.config/systemd"
alacritty "$default_directory/.config/alacritty"
bat "$default_directory/.config/bat"
rofi "$default_directory/.config/rofi"
)
exit_codes=(
unsupported_system
pm_install_failed
pm_depends_failed
git_depends_failed
symlink_failed
)
#
# color codes
#
reset_color="$(tput sgr0)"
bold="$(tput bold)"
typeset -A log_colors; log_colors=(
error "$(tput setaf 1)"
normal "${bold}$(tput setaf 7)"
warn "$(tput setaf 3)"
info "$(tput setaf 6)"
)
#
# function declarations
#
function prompt {
local message="$1"
# Split second argument by '/' and remove square brackets
local -a options; options=("${(@s|/|)2//[\[\]]/}")
local options_length="$(( $#options + 1 ))"
local export_variable="$3"
local default=''
local response=''
local response_index=1
# extract the default value by finding the first uppercase option
for index in {1..$#options}; do
local option="${options[$index]}"
[[ "$option" =~ '[A-Z]' ]] && default="${option:l}"
options[$index]="${option:l}"
done
unset option
vared -p "$message $2 " response
# get the first character and lowercase it
response="${response:0:1:l}"
# check if the response is one of the provided options
response_index="${options[(i)$response]}"
# set to default if response isn't found in options
[[ "$response_index" -eq "$options_length" ]] && response="$default"
# export the response if an export variable was provided
if [[ -n "$export_variable" ]]; then
typeset -g "$3"="$response"
return 0
fi
echo $response
}
function log {
local -a levels; levels=("${(@k)log_colors}")
local log_string="${log_colors[$1]}${2}${reset_color}"
# test if we should log
if [[ "$1" != 'normal' && "${levels[(i)$1]}" -gt "$log_level" ]]; then
return 0
fi
# redirect to stderr if we're logging an error
if [[ "$1" == 'error' ]]; then
echo $log_string >&2
else
echo $log_string
fi
}
function fatal_error {
local status_code="${1:-1}"
local msg="${2:-Fatal error occured}"
log error "$msg"
exit $status_code
}
function install_package_manager {
case "$1" in
osx) ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)";;
arch) install_from_aur pikaur;;
*) false;;
esac
echo $?
}
function install_from_aur {
local package_name="$1"
local package_base_url="https://aur.archlinux.org/cgit/aur.git/snapshot"
local package_url="$package_base_url/$package_name.tar.gz"
local install_dir="${2:-./temp-build}"
local return_dir="$(pwd)"
mkdir -p "$install_dir" && cd "$install_dir" && wget "$package_url"
[[ "$?" -ne 0 ]] && return 1
tar xfv "$package_name.tar.gz"
[[ "$?" -ne 0 ]] && return 1
cd "$package_name" && makepkg -s --noconfirm
[[ "$?" -ne 0 ]] && return 1
sudo pacman --noconfirm -U "$package_name"*.pkg.tar.xz
[[ "$?" -ne 0 ]] && return 1
cd "$return_dir" && rm -r "$install_dir"
[[ "$?" -ne 0 ]] && return 1
return 0
}
function construct_dependency_arguments {
case "$1" in
pikaur) echo --noconfirm -S $dependencies $arch_dependencies;;
brew) echo install $dependencies $osx_dependencies;;
apt-get) echo install $dependencies $debian_dependencies;;
*) return 1;;
esac
}
#
# detect platform and package manager
#
log info 'Detecting platform'
case "$(uname -rv | tr '[:upper:]' '[:lower:]')" in
*arch*) platform='arch';;
*darwin*) platform='osx';;
*debian*|*ubuntu*) platform='debian';;
*) platform='unknown';;
esac
log info "Platform: [$platform]"
log info 'Detecting package manager'
if hash pikaur 2> /dev/null; then
package_manager='pikaur'
elif hash brew 2> /dev/null; then
package_manager='brew'
elif hash apt-get 2> /dev/null; then
package_manager='apt-get'
else
package_manager='unknown'
fi
log info "Package manager: [$package_manager]"
if [[ "$platform" == 'unknown' && "$package_manager" == 'unknown' ]]; then
fatal_error $exit_codes[(i)unsupported_system] 'Unsupported system'
fi
#
# install missing package managers
#
if [[ "$package_manager" == 'unknown' ]]; then
log info 'Installing missing package manager'
if ! install_package_manager "$platform"; then
fatal_error $exit_codes[(i)pm_install_failed] 'Failed installing package manager'
fi
fi
#
# install dependencies with package manager
#
prompt "Install dependencies with $package_manager?" '[y/N]' install_dependencies
if [[ "$install_dependencies" == 'y' ]]; then
package_manager_arguments="$(construct_dependency_arguments "$package_manager")"
echo $package_manager_arguments | xargs "$package_manager"
# prompt user if install failed
if [[ "$?" -ne 0 ]]; then
prompt "$(log warn 'An error occured while install dependencies. Continue?')" '[y/N]' continue_install
[[ "$continue_install" == 'n' ]] && exit $exit_codes[(i)pm_depends_failed]
fi
fi
#
# install dependencies with git
#
prompt "Install dependencies with git?" '[y/N]' install_git_dependencies
if [[ "$install_git_dependencies" == 'y' ]]; then
if hash git 2> /dev/null; then
for dep in $git_dependencies; do
echo "$dep" | xargs git clone
done
fi
# prompt user if install failed
if [[ "$?" -ne 0 ]]; then
prompt "$(log warn 'An error occured while installing with git. Continue?')" '[y/N]' continue_install
[[ "$continue_install" == 'n' ]] && exit $exit_codes[(i)git_depends_failed]
fi
fi
#
# symlink packages from this repo
#
prompt 'Symlink dotfiles?' '[y/N]' symlink_dotfiles
if [[ "$symlink_dotfiles" == 'y' ]];then
mkdir -p "$default_directory"
for package in "${(@k)packages}"; do
local destination_dir="${packages[$package]:-"$default_directory"}"
mkdir -p "$destination_dir" && stow -t "$destination_dir" "$package"
done
# prompt user if install failed
if [[ "$?" -ne 0 ]]; then
log warn $'An error occured while symlinking files.\n'
exit $exit_codes[(i)symlink_failed]
fi
# if we're on OS X, use keychain for credentials instead
[[ "$platform" == "osx" ]] && git config --global credential.helper 'osxkeychain'
fi