-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.sh
executable file
·515 lines (402 loc) · 13.5 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
#!/usr/bin/env bash
##########################################################################
# DO NOT MODIFY BEYOND THIS LINE
##########################################################################
# Program name and version
program_name=$(basename "$0")
program_version='0.2.1'
# Script exits immediately if any command within it exits with a non-zero status
set -o errexit
# Script will catch the exit status of a previous command in a pipe.
set -o pipefail
# Script exits immediately if tries to use an undeclared variables.
set -o nounset
# Uncomment this to enable debug
# set -o xtrace
# Initialize variables in order to be used later
# 0 - Quiet, 1 - Errors, 2 - Warnings, 3 - Normal, 4 - Verbose, 9 - Debug
verbosity_level=3
########## Params setup
## get the real path of install.sh
SOURCE="${BASH_SOURCE[0]}"
# resolve $SOURCE until the file is no longer a symlink
while [[ -L "$SOURCE" ]]; do
APP_PATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path
# where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$APP_PATH/$SOURCE"
done
APP_PATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
##########################################################################
# Functions
##########################################################################
# Do every cleanup task before exit.
function safe_exit () {
local _error_code=${1:-0}
exit "${_error_code}"
}
# Print a message, do format and treat verbose level
declare -A LOG_LEVELS
LOG_LEVELS=([error]=1 [warning]=2 [notice]=3 [info]=4 [debug]=9)
function _alert () {
# TODO: This variables are reserved for future use
local color=""; local reset=""
# Print to console depending of verbosity level
if [[ "${verbosity_level}" -ge "${LOG_LEVELS[${1}]}" ]]; then
echo -e "$(date +"%X") ${color}$(printf "[%s]" "${1}") ${_message}${reset}"
fi
}
# Print a message and exit
function die () {
local _error_code=0
[[ "${1}" = "-e" ]] && shift; _error_code=${1}; shift
error "${*} Exiting."
safe_exit "${_error_code}"
}
# Deal with severity level messages
function error() { local _message="${*}"; _alert error >&2; }
function warning() { local _message="${*}"; _alert warning >&2; }
function notice() { local _message="${*}"; _alert notice; }
function info() { local _message="${*}"; _alert info; }
function debug() { local _message="${*}"; _alert debug; }
function input() { local _message="${*}"; printf "%s " "${_message}"; }
# Usage info
function show_help () {
# Variables for formatting
local U; U=$(tput smul) # Underline
local RU; RU=$(tput rmul) # Remove underline
local B; B=$(tput bold) # Bold
local N; N=$(tput sgr0) # Normal
cat <<-EOF
${B}Usage${N}:
${B}${program_name}${N} <task>[ ${U}taskFoo${RU} ${U}taskBar${RU} ...]
${B}Tasks:${N}
${B}aws_credentials${N} Configure AWS credentials in .aws/credentials
${B}backup-tool${N} Install & configure the backup-tool
${B}bash_rc${N} Install & configure bash-it
${B}bin${N} Make ~/bin accessible
${B}editorconfig${N} Configure .editorconfig
${B}env_private${N} Configure TOKENS in environment variables (needs tokens/TOKENS in pass)
${B}defaults${N} Configure some defaults
${B}gnupg${N} Configure GNUPG
${B}git_config${N} Configure git
${B}vim_rc${N} Configure Vim
${B}yams_credentials${N} Configure YAMS credentials in .yams/credentials
${B}zsh_rc${N} Install & configure oh-my-zsh
${B}all${N} Install all dotfiles & configuration
Version: ${program_version}
EOF
}
# Check if a program is installed
function program_is_installed() {
# set to 1 initially
local return_=1
# set to 0 if not found
type "$1" >/dev/null 2>&1 && { return_=0; }
# return value
return "$return_"
}
# Check file exists or die
function must_file_exists () {
for file in "$@"; do
if [[ ! -e "$file" ]]; then
die -e 1 "You must have file *${file}*"
fi
done
}
# Check if a program exists, if not recommends you to install it
function better_program_exists_one () {
local exists=0
for program in "$@"; do
if ( program_is_installed "$program" ); then
exists=1
break
fi
done
if [[ "$exists" = "0" ]]; then
notice "Maybe you can take full use of this by installing one of ($*)~"
fi
}
# Check if a program exists or die
function must_program_exists() {
for program in "$@"; do
if ( ! program_is_installed "$program" ); then
die -e 1 "You must have *$program* installed!"
fi
done
}
# Check if platform is Linux
function is_linux () {
[[ "$(uname)" = "Linux" ]] && return 0 || return 1
}
# Link a file if exists
function lnif(){
if [ -e "$1" ]; then
info "Linking $1 to $2"
rm -rf "$2"
ln -s "$1" "$2"
fi
}
# Copy a file if exists
function cpif(){
if [ -e "$1" ]; then
info "Copying $1 to $2"
rm -rf "$2"
cp "$1" "$2"
fi
}
# Clone a github repo
function sync_repo() {
must_program_exists "git"
local repo_uri=$1
local repo_path=$2
local repo_branch=${3:-master}
local repo_name=${1:19} # length of (https://github.com/)
if [[ ! -d "$repo_path" ]]; then
info "Cloning $repo_name ..."
mkdir -p "$repo_path"
git clone --depth 1 --branch "$repo_branch" "$repo_uri" "$repo_path"
notice "Successfully cloned $repo_name."
else
info "Updating $repo_name ..."
cd "$repo_path" && git pull origin "$repo_branch"
notice "Successfully updated $repo_name."
fi
if [[ -e "$repo_path/.gitmodules" ]]; then
info "Updating $repo_name submodules ..."
cd "$repo_path"
git submodule update --init --recursive
notice "Successfully updated $repo_name submodules."
fi
}
# Configure GNUPG
function install_gnupg_config() {
notice "Installing GNUPG configuration ..."
lnif "$APP_PATH/gnupg/gpg-agent.conf" \
"$HOME/.gnupg/gpg-agent.conf"
lnif "$APP_PATH/gnupg/gpg.conf" \
"$HOME/.gnupg/gpg.conf"
lnif "$APP_PATH/gnupg/dirmngr.conf" \
"$HOME/.gnupg/dirmngr.conf"
notice "Successfully installed GNUPG configuration."
}
# Configure bin scripts
function install_bin() {
notice "Installing useful small scripts ..."
local source_path="$APP_PATH/bin"
mkdir -p "$HOME/bin"
for bin in "$source_path"/*; do
local script_name
script_name=$(basename "$bin")
lnif "$bin" "$HOME/bin/$script_name"
done
notice "Successfully installed useful scripts."
}
# Configure backup tool
function install_backup_tool() {
notice "Installing backups tool..."
mkdir -p "$HOME/bin/backup-tool"
lnif "$APP_PATH/backup-tool/make_snapshot.sh" \
"$HOME/bin/backup-tool/make_snapshot.sh"
lnif "$APP_PATH/backup-tool/do_backup.sh" \
"$HOME/bin/backup-tool/do_backup.sh"
lnif "$APP_PATH/backup-tool/daily.sh" \
"$HOME/bin/backup-tool/daily.sh"
lnif "$APP_PATH/backup-tool/weekly.sh" \
"$HOME/bin/backup-tool/weekly.sh"
lnif "$APP_PATH/backup-tool/excludes_from_backup" \
"$HOME/.excludes_from_backup"
(crontab -l 2>/dev/null || true ) | awk '!/backup-tool/' | cat - "$APP_PATH/backup-tool/crontab_schedule" | crontab -
notice "Successfully installed backup tool."
}
# Configure editorconfig
function install_editorconfig() {
notice "Installing editorconfig ..."
lnif "$APP_PATH/editorconfig/editorconfig" \
"$HOME/.editorconfig"
notice "Maybe you should install editorconfig plugin for vim or sublime"
notice "Successfully installed editorconfig."
}
# Configure git config
function install_git_config() {
must_program_exists "git"
notice "Installing git defaults and config..."
lnif "$APP_PATH/git/gitattributes" \
"$HOME/.gitattributes"
lnif "$APP_PATH/git/gitignore" \
"$HOME/.gitignore"
lnif "$APP_PATH/git/gitconfig" \
"$HOME/.gitconfig"
lnif "$APP_PATH/git/git_identify_adevinta" \
"$HOME/.git_identify_adevinta"
notice "Successfully installed gitconfig."
notice "Maybe you should configure your user.name, user.email and user.signingkey on .gitconfig"
}
# Configure vim_rc with Vundle and plugins
function install_vim_rc() {
must_program_exists "vim"
notice "Installing vimrc ..."
sync_repo "https://github.com/VundleVim/Vundle.vim.git" \
"$APP_PATH/vim/plugins/Vundle.vim"
lnif "$APP_PATH/vim" \
"$HOME/.vim"
lnif "$APP_PATH/vim/vimrc" \
"$HOME/.vimrc"
vim +PlugInstall +qall
notice "Successfully installed vimrc."
notice "You can add your own configs to ~/.vimrc.local, vim will source them automatically"
}
# Configure bash_rc with bash-it and plugins
function install_bash_rc() {
must_program_exists "bash"
notice "Installing bashrc..."
sync_repo "https://github.com/Bash-it/bash-it.git" \
"$APP_PATH/bash/bash_it"
lnif "$APP_PATH/bash/bash_it" \
"$HOME/.bash_it"
lnif "$APP_PATH/bash/bash_profile" \
"$HOME/.bash_profile"
lnif "$APP_PATH/bash/bashrc" \
"$HOME/.bashrc"
lnif "$APP_PATH/bash/inputrc" \
"$HOME/.inputrc"
notice "Successfully installed bash and bash-it."
notice "You can add your own configs to ~/.bashrc.local , bash will source them automatically"
notice "Please open a new bash terminal to make configs go into effect."
}
# Configure zsh_rc with oh-my-zsh and plugins
function install_zsh_rc() {
must_program_exists "zsh"
notice "Installing zshrc ..."
sync_repo "https://github.com/robbyrussell/oh-my-zsh.git" \
"$APP_PATH/zsh/oh-my-zsh"
# add zsh plugin zsh-autosuggestions support
sync_repo "https://github.com/zsh-users/zsh-autosuggestions" \
"$APP_PATH/zsh/oh-my-zsh/custom/plugins/zsh-autosuggestions"
# add powerline10k theme
sync_repo "https://github.com/romkatv/powerlevel10k.git" \
"$APP_PATH/zsh/oh-my-zsh/custom/themes/powerlevel10k"
lnif "$APP_PATH/zsh/oh-my-zsh" \
"$HOME/.oh-my-zsh"
lnif "$APP_PATH/zsh/zshenv" \
"$HOME/.zshenv"
lnif "$APP_PATH/zsh/zshrc" \
"$HOME/.zshrc"
lnif "$APP_PATH/zsh/p10k.zsh" \
"$HOME/.p10k.zsh"
notice "Successfully installed zsh and oh-my-zsh."
notice "You can add your own configs to ~/.zshrc.local , zsh will source them automatically"
notice "Please open a new zsh terminal to make configs go into effect."
}
# Configure private TOKENS into environment variables
function install_env_private() {
must_program_exists "pass"
must_file_exists "$HOME/.password-store/tokens/ENV_TOKENS.gpg"
notice "Installing environment private tokens ..."
info "You will be asked for decryption key!"
pass show tokens/ENV_TOKENS > "$HOME/.env_private"
notice "Successfully installed environment private tokens"
notice "Please open a new terminal to make configs go into effect."
}
# Configure AWS credentials
function install_aws_credentials() {
must_program_exists "pass"
must_file_exists "$HOME/.password-store/tokens/AWS_CREDENTIALS.gpg"
must_file_exists "$HOME/.password-store/tokens/AWS_Extend_Switch_Roles.gpg"
notice "Installing AWS credentials ..."
info "You will be asked for decryption key!"
mkdir -p "$HOME/.aws"
pass show tokens/AWS_CREDENTIALS > "$HOME/.aws/credentials"
pass show tokens/AWS_Extend_Switch_Roles > "$HOME/.aws/AWS_Extend_Switch_Roles.conf"
notice "Successfully installed AWS_CREDENTIALS"
notice "Please open a new terminal to make configs go into effect."
}
# Configure YAMS credentials
function install_yams_credentials() {
must_program_exists "pass"
must_file_exists "$HOME/.password-store/tokens/YAMS_CREDENTIALS.gpg"
notice "Installing YAMS credentials ..."
info "You will be asked for decryption key!"
mkdir -p "$HOME/.yams"
pass show tokens/YAMS_CREDENTIALS > "$HOME/.yams/credentials"
notice "Successfully installed YAMS credentials"
}
# Configure some defaults
function configure_defaults() {
notice "Configuring some defaults ..."
lnif "$APP_PATH/defaults/profile" \
"$HOME/.profile"
notice "Successfully configured defaults"
}
##########################################################################
# Main function
##########################################################################
function main () {
if [[ $# = 0 ]]; then
show_help
safe_exit 2
fi
for arg in "$@"; do
case "$arg" in
aws_credentials)
install_aws_credentials
;;
backup-tool)
install_backup_tool
;;
bin)
install_bin
;;
bash_rc)
install_bash_rc
;;
zsh_rc)
install_zsh_rc
;;
editorconfig)
install_editorconfig
;;
env_private)
install_env_private
;;
defaults)
configure_defaults
;;
gnupg)
install_gnupg_config
;;
git_config)
install_git_config
;;
vim_rc)
install_vim_rc
;;
yams_credentials)
install_yams_credentials
;;
all)
configure_defaults
install_zsh_rc
install_bin
install_backup_tool
install_gnupg_config
install_git_config
install_editorconfig
install_aws_credentials
install_yams_credentials
install_env_private
install_vim_rc
;;
*)
error "Invalid params $arg"
show_help
safe_exit 2
;;
esac
done
}
##########################################################################
# Main code
##########################################################################
main "$@"