forked from alrra/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·335 lines (229 loc) · 9.34 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
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
#!/usr/bin/env bash
# ----------------------------------------------------------------------
# | Declare read-only variables/constants |
# ----------------------------------------------------------------------
declare -r GITHUB_REPOSITORY="wingy3181/dotfiles"
declare -r DOTFILES_ORIGIN="git@github.com:$GITHUB_REPOSITORY.git"
declare -r DOTFILES_TARBALL_URL="https://github.com/$GITHUB_REPOSITORY/tarball/main"
declare -r DOTFILES_UTILS_URL="https://raw.githubusercontent.com/$GITHUB_REPOSITORY/main/src/os/utils.sh"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ----------------------------------------------------------------------
# | Declare variables |
# ----------------------------------------------------------------------
declare dotfilesDirectory="$HOME/.dotfiles"
declare skipQuestions=false
# ----------------------------------------------------------------------
# | Helper Functions |
# ----------------------------------------------------------------------
download() {
# $1 : url to download
# $2 : file location to download to
local url="$1"
local output="$2"
# Try using curl first if it exists
if command -v "curl" &> /dev/null; then
curl -LsSo "$output" "$url" &> /dev/null
# │││└─ write output to file
# ││└─ show error messages
# │└─ don't show the progress meter
# └─ follow redirects
return $? # exit status of curl
# Otherwise try use wget if it exists
elif command -v "wget" &> /dev/null; then
wget -qO "$output" "$url" &> /dev/null
# │└─ write output to file
# └─ don't show output
return $? # exit status of wget
fi
return 1
}
download_dotfiles() {
local tmpFile=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print_in_purple "\n * Download and extract archive\n\n"
tmpFile="$(mktemp /tmp/XXXXX)"
download "$DOTFILES_TARBALL_URL" "$tmpFile"
print_result $? "Download archive" "true"
printf "\n"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
ask_for_confirmation "Do you want to store the dotfiles in '$dotfilesDirectory'?"
if ! answer_is_yes; then
dotfilesDirectory=""
while [ -z "$dotfilesDirectory" ]; do # -z : True if the length of string is zero.
ask "Please specify another location for the dotfiles (path): "
dotfilesDirectory="$(get_answer)"
done
fi
# Ensure the `dotfiles` directory is available.
while [ -e "$dotfilesDirectory" ]; do # -e : True if file exists (regardless of type).
ask_for_confirmation "'$dotfilesDirectory' already exists, do you want to overwrite it?"
if answer_is_yes; then
rm -rf "$dotfilesDirectory"
break
else
dotfilesDirectory=""
while [ -z "$dotfilesDirectory" ]; do # -z : True if the length of string is zero.
ask "Please specify another location for the dotfiles (path): "
dotfilesDirectory="$(get_answer)"
done
fi
done
printf "\n"
else
rm -rf "$dotfilesDirectory" &> /dev/null
fi
mkdir -p "$dotfilesDirectory"
print_result $? "Create '$dotfilesDirectory'" "true"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Extract archive in the `dotfiles` directory.
extract "$tmpFile" "$dotfilesDirectory"
print_result $? "Extract archive" "true"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rm -rf "$tmpFile"
print_result $? "Remove archive"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cd "$dotfilesDirectory/src/os" \
|| return 1
}
download_utils() {
local tmpFile=""
tmpFile="$(mktemp /tmp/XXXXX)"
download "$DOTFILES_UTILS_URL" "$tmpFile" \
&& . "$tmpFile" \
&& rm -rf "$tmpFile" \
&& return 0
return 1
}
extract() {
local archive="$1"
local outputDir="$2"
if command -v "tar" &> /dev/null; then
tar -zxf "$archive" --strip-components 1 -C "$outputDir"
# │││ │ │ └─ change to directory (i.e. $outputDir)
# │││ │ └─ number of leading components from file names to strip
# │││ └─ strip NUMBER leading components from file names on extraction.
# │││ That is, the root folder
# ││└─ use archive file (i.e. $archive)
# │└─ extract files from an archive
# └─ filter the archive through gzip
# See http://linux.die.net/man/1/tar
return $?
fi
return 1
}
verify_os() {
declare -r MINIMUM_MACOS_VERSION="10.10"
local os_name="$(get_os)"
local os_version="$(get_os_version)"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if the OS is `macOS` and
# it's above the required version.
if [ "$os_name" == "macos" ]; then
if is_supported_version "$os_version" "$MINIMUM_MACOS_VERSION"; then
return 0
else
printf "Sorry, this script is intended only for macOS %s+" "$MINIMUM_MACOS_VERSION"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
else
printf "Sorry, this script is intended only for macOS!"
fi
return 1
}
# ----------------------------------------------------------------------
# | Main |
# ----------------------------------------------------------------------
main() {
# Ensure that the following actions
# are made relative to this file's path.
cd "$(dirname "${BASH_SOURCE[0]}")" \
|| exit 1
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Load utils
if [ -x "utils.sh" ]; then # -x = True if file exists and is executable
. "utils.sh" || exit 1
else
download_utils || exit 1
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Ensure the OS is supported and
# it's above the required version.
verify_os \
|| exit 1
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
skip_questions "$@" \
&& skipQuestions=true
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ask_for_sudo
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if this script was run directly (./<path>/setup.sh),
# and if not, it most likely means that the dotfiles were not
# yet set up, and they will need to be downloaded.
printf "%s" "${BASH_SOURCE[0]}" | grep "setup.sh" &> /dev/null \
|| download_dotfiles
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
printf "\n\n\n"
ask_for_confirmation "Do you want the additional directories to be created?"
printf "\n"
fi
if $skipQuestions || answer_is_yes; then
./create_directories.sh
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
./create_symbolic_links.sh
else
./create_symbolic_links.sh -y
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
printf "\n\n\n"
ask_for_confirmation "Do you want local config files for bash, git and vim to be created?"
printf "\n"
fi
if $skipQuestions || answer_is_yes; then
./create_local_config_files.sh
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
printf "\n\n\n"
ask_for_confirmation "Do you want to install the applications/command line tools?"
printf "\n"
fi
if $skipQuestions; then
./installs/setup.sh -y
elif answer_is_yes; then
./installs/setup.sh
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
printf "\n\n\n"
ask_for_confirmation "Do you want to set the custom preferences?"
printf "\n"
fi
if $skipQuestions; then
./preferences/setup.sh -y
elif answer_is_yes; then
./preferences/setup.sh
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if cmd_exists "git"; then
if [ "$(git config --get remote.origin.url)" != "$DOTFILES_ORIGIN" ]; then
./initialize_git_repository.sh "$DOTFILES_ORIGIN"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
./update_content.sh
fi
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
./print_optional_manual_items.sh
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ! $skipQuestions; then
./restart.sh
fi
}
# Pass '-y' to script to skip questions
main "$@"