-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·292 lines (233 loc) · 6.03 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
#! /usr/bin/env bash
#
# Configuration Installer
#
VERSION="0.3.0"
REMOTE_INSTALLER="https://raw.githubusercontent.com/eviweb/config-installer/main/install.sh"
main_dir()
{
dirname "$(readlink -f "${BASH_SOURCE}")"
}
config_dir()
{
echo "$(main_dir)/config"
}
config_copy_dir()
{
echo "$(main_dir)/config/copy"
}
config_link_dir()
{
echo "$(main_dir)/config/link"
}
config_run_dir()
{
echo "$(main_dir)/config/run"
}
log()
{
echo -e "$@"
}
version()
{
cat << VERSION
Configuration Installer
Version: ${VERSION}
VERSION
}
usage()
{
version
cat << USAGE
Usage:
Run ./install.sh [OPTIONS]
Options:
-f force overwriting files
-h display this help message
-i initialize the current project directory
-V display the version number
-v set verbosity level, can be used multiple times
USAGE
}
sort_files()
{
local files=( $@ )
printf '%s\n' "${files[@]}" | sed -r 's/common/000000/g' | sort | sed -r 's/000000/common/g'
}
find_files()
{
local directory="$1"
sort_files "$(find "${directory}" \( -type f -or -type l \) -and \( -path "*/common/*" -or -path "*/${HOSTNAME}/*" \))"
}
get_target_file()
{
local sourcefile="$1"
local configdir="$(config_dir)"
echo "${sourcefile}" | sed -r "s/${configdir//\//\\\/}\/[^\/]+\/(common|${HOSTNAME})//"
}
ensure_parent_dir()
{
local parentdir="$(dirname "$1")"
[ -f "${parentdir}" ] || mkdir -p "${parentdir}"
}
assert_same_file()
{
local file1="$1"
local file2="$2"
cmp --silent "${file1}" "${file2}"
}
assert_same_source()
{
local source="$1"
local link="$2"
[ "${source}" == "$(readlink "${link}")" ]
}
is_sourced()
{
[ "${BASH_SOURCE[0]}" != "${0}" ]
}
is_verbosed()
{
local level="$1"
[ -n "${level}" ] || level=1
[ "${VERBOSITY}" -ge "${level}" ]
}
copy_files()
{
local files=( $(find_files "$(config_copy_dir)") )
local targetfile
local msg=""
for file in "${files[@]}"; do
targetfile="$(get_target_file "${file}")"
msg=" Copy ${file/$(main_dir)/.} to ${targetfile}"
ensure_parent_dir "${targetfile}"
cp -a "${OVERWRITING}" "${file}" "${targetfile}"
if ([[ -h "${targetfile}" ]] && assert_same_source "${file}" "${targetfile}") ||
([[ -f "${targetfile}" ]] && assert_same_file "${file}" "${targetfile}"); then
msg="${msg}: \e[32mSucceed\e[0m"
else
msg="${msg}: \e[31mFailed\e[0m"
fi
is_verbosed && log "${msg}"
done
}
link_files()
{
local files=( $(find_files "$(config_link_dir)") )
local targetfile
local msg=""
for file in "${files[@]}"; do
targetfile="$(get_target_file "${file}")"
msg=" Link ${file/$(main_dir)/.} to ${targetfile}"
ensure_parent_dir "${targetfile}"
ln -s "${OVERWRITING}" "${file}" "${targetfile}"
if assert_same_source "${file}" "${targetfile}"; then
msg="${msg}: \e[32mSucceed\e[0m"
else
msg="${msg}: \e[31mFailed\e[0m"
fi
is_verbosed && log "${msg}"
done
}
find_scripts()
{
local priority="$1"
sort_files "$(find "$(config_run_dir)" \( -path "*/common/${priority}/*" -or -path "*/${HOSTNAME}/${priority}/*" \) -and \( -name "*.sh" -or -name "*.bash" \) -executable)"
}
run_scripts()
{
local scripts=( $@ )
local msg=""
local result
for script in "${scripts[@]}"; do
msg=" Run ${script/$(main_dir)/.}"
result="$(bash -c "${script}")"
if [ "$?" -eq 0 ]; then
msg="${msg}: \e[32mSucceed\e[0m"
else
msg="${msg}: \e[31mFailed\e[0m"
fi
is_verbosed && log "${msg}"
is_verbosed 2 && log "${result}"
done
}
initialize()
{
mkdir -p "$(main_dir)"/config/{copy,link,run}
}
get_latest_version()
{
curl -Ls "${REMOTE_INSTALLER}" | grep -Poe '\d+\.\d+\.\d+'
}
should_update()
{
local compareversions=( "${VERSION}" $@ )
test "$(echo "${compareversions[@]}" | tr " " "\n" | sort -rV | head -n 1)" != "${VERSION}";
}
check_for_update()
{
local latestversion="$(get_latest_version)"
local answer=""
if should_update "${latestversion}"; then
while ! [[ "${answer}" =~ ^[YyNn] ]]; do
read -p "A new version is available, do you want to update? (y/n)" answer
done
[[ "${answer}" =~ ^[Nn] ]] || {
log "Start updating..."
curl -Ls "${REMOTE_INSTALLER}" -o "$(main_dir)"/install.sh
log "Done, please restart the installer."
exit 0
}
fi
}
if ! is_sourced; then
VERBOSITY=0
OVERWRITING="-i"
while getopts "fhiVv" option; do
case "${option}" in
f) OVERWRITING="-f"
;;
h) usage && exit 0
;;
i) initialize && exit $?
;;
V) version && exit 0
;;
v) ((VERBOSITY=VERBOSITY+1))
;;
\?) log "\e[31mInvalid Option: ${OPTARG}\e[0m" && exit 1
;;
esac
done
check_for_update
before_scripts=(
$(find_scripts "before")
)
if [ 0 -lt "${#before_scripts[@]}" ]; then
log "\e[34mRun configuration scripts...\e[0m"
run_scripts "${before_scripts[@]}"
else
log "\e[34mNothing to execute\e[0m"
fi
if [ -d "$(config_copy_dir)" ]; then
log "\e[34mCopy configuration files...\e[0m"
copy_files
else
log "\e[34mNothing to copy\e[0m"
fi
if [ -d "$(config_link_dir)" ]; then
log "\e[34mLink configuration files...\e[0m"
link_files
else
log "\e[34mNothing to link\e[0m"
fi
after_scripts=(
$(find_scripts "after")
)
if [ 0 -lt "${#after_scripts[@]}" ]; then
log "\e[34mRun configuration scripts...\e[0m"
run_scripts "${after_scripts[@]}"
else
log "\e[34mNothing to execute\e[0m"
fi
fi