-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·154 lines (124 loc) · 3.37 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
#!/usr/bin/env bash
# Based on https://github.com/holman/dotfiles/blob/master/script/bootstrap
export DOTFILES_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OVERWRITE_ALL=false
SKIP_ALL=false
success() {
printf "\r\033[2K [ \033[00;32mOK\033[0m ] %s\n" "$1"
}
warning() {
printf "\r\033[2K [ \033[00;33m!!\033[0m ] \033[00;33m%s\033[0m\n" "$1"
}
info() {
printf "\r\033[2K [ \033[00;34m..\033[0m ] \033[00;34m%s\033[0m\n" "$1"
}
ask() {
printf "\r [ \033[0;34m??\033[0m ] %s " "$1"
}
fail() {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] %s\n" "$1"
echo ''
exit
}
header() {
echo ''
printf "\033[00;4m%s\033[0m\n" "$1"
}
subheader() {
echo ''
printf "\r \033[00;1m%s\033[0m\n" "$1"
}
# (try to) create a symbolic link between $1 and $2, exiting script if fails
_slink() {
ln -sf "$1" "$2" || { fail "Something went wrong"; exit; }
}
# Creates a symbolic link between $1 and $2, asking for what to do if:
# - destiny already exists (overwrite or skip)
# - destiny's dirname does not exist (create dir or skip)
link() {
src=$(readlink -f "$1")
dest=$2
skip=false
overwrite=false
if [ ! -f "$src" ]; then
warning "(skipped) Not a file: ${src}"
return
fi
dirname=$(dirname "$dest")
if [ ! -d "$dirname" ]; then
warning "Directory $dirname does not exist"
ask "What do you want to do? [s]kip, [c]reate dir"
read -n 1 action
if [ "$action" == "c" ]; then
mkdir -p "$dirname"
success "Directory created: $dirname"
else
success "Skipped $src"
return
fi
fi
if [ -a "$dest" ] || [ -d "$dest" ]; then
if [ "$OVERWRITE_ALL" == "false" ] && [ "$SKIP_ALL" == "false" ]; then
warning "File already exists: ${dest}"
ask "What do you want to do? [s]kip, [S]kip all, [o]verwrite, [O]verwrite all:"
read -n 1 action
case "$action" in
o )
overwrite=true;;
O )
OVERWRITE_ALL=true;;
s )
skip=true;;
S )
SKIP_ALL=true;;
* )
skip=true;;
esac
fi
if [ "$overwrite" == "true" ] || [ "$OVERWRITE_ALL" == "true" ]; then
rm -rf "$dest"
fi
if [ "$skip" == "false" ] && [ "$SKIP_ALL" == "false" ]; then
_slink "$src" "$dest"
success "(overwrite) Created symbolic link ${dest} -> ${src}"
else
success "Skipped $src"
fi
else
_slink "$src" "$dest"
success "Created symbolic link ${dest} -> ${src}"
fi
}
# Creates a symbolic link of every `*.ln` file in `~/`
create_symlinks() {
header 'Linking *.ln files to ~/'
for source in $(find "$DOTFILES_HOME" -name *.ln); do
basename=$(basename "$source" .ln)
dest="$HOME/$basename"
link "$source" "$dest"
done
}
# Creates a symbolic link of every `*.bin` file in `~/bin/`
link_bin_files() {
header 'Linking *.bin files to ~/bin/'
for src in $(find "$DOTFILES_HOME" -name *.bin); do
basename=$(basename "$src" .bin)
dest="$HOME/bin/$basename"
link "$src" "$dest"
done
}
# Executes every `*.setup` file
# The current path is changed to the .setup path that is being executed
run_setups() {
header 'Running *.setup files'
for setup_file in $(find "$DOTFILES_HOME" -name *.setup); do
cd "$(dirname "$setup_file")"
subheader "Running $setup_file"
source "$setup_file"
done
cd "$DOTFILES_HOME"
}
create_symlinks
link_bin_files
run_setups
success 'All done!'