-
Notifications
You must be signed in to change notification settings - Fork 4
/
install
executable file
·276 lines (242 loc) · 8.71 KB
/
install
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
#!/bin/sh
# POSIX
next=0
verbose=0
version=0.4.0
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
install --prefix /usr/local
Options:
--prefix/-P: base bath where all files will be deployed (default /usr/local if root, ~/.local if not)
--next/-N: install latest development version from git, instead of the latest stable release.
--help/-h: show this message
-v: show more verbosity
EOF
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit
;;
-v | --verbose)
shift
verbose=1
;;
-N | --next)
shift
next=1
;;
-P | --prefix)
if [ -n "$2" ]; then
prefix="$2"
shift
shift
fi
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
if [ -z "${prefix}" ]; then
prefix="/usr/local"
# in case we're not root, just default to the home directory
if [ "$(id -u)" -ne 0 ]; then
prefix="${HOME}/.local"
fi
fi
dest_path="${prefix}/bin"
template_dest_path="${prefix}/share/blincus/templates"
template_config_dest_path="${HOME}/.config/blincus/templates"
init_dest_path="${prefix}/share/blincus/cloud-init"
init_config_dest_path="${HOME}/.config/blincus/cloud-init"
profile_dest_path="${prefix}/share/blincus/profiles"
profile_config_dest_path="${HOME}/.config/blincus/profiles"
script_dest_path="${prefix}/share/blincus"
script_config_dest_path="${HOME}/.config/blincus"
man_dest_path="${prefix}/share/man/man1"
completion_dest_path="${prefix}/share/bash-completion/completions/"
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# get current dir
curr_dir=$(dirname "$0")
cd "${curr_dir}" || exit 1
# if files are available, install files in dest directory
# else download targz and uncompress it
if [ -e "${curr_dir}/blincus" ]; then
printf >&2 "Installing to %s\n" "${dest_path}"
for file in blincus*; do
if ! install -D -m 0755 -t "${dest_path}" "${file}"; then
printf >&2 "Do you have permission to write to %s?\n" "${dest_path}"
exit 1
fi
done
if [ -e "man" ]; then
mkdir -p "${man_dest_path}"
for file in man/man1/*; do
install -D -m 0644 -t "${man_dest_path}" "${file}"
done
fi
if [ -e "completions" ]; then
mkdir -p "${completion_dest_path}"
for file in completions/*; do
install -D -m 0644 -t "${completion_dest_path}" "${file}"
done
fi
if [ -e "scripts" ]; then
mkdir -p "${script_dest_path}"
cp -R scripts "${script_dest_path}"
if [ -d $script_config_dest_path/scripts ]; then
printf >&2 "\033[0m Refusing to overwrite user scripts at \033[1;31m%s\n\033[0m" "${script_config_dest_path}/scripts"
printf >&2 "\033[0m\033[1;31m Updated scripts available at %s\n\033[0m" "${script_dest_path}/scripts"
else
mkdir -p "${script_config_dest_path}"
cp -R scripts "${script_config_dest_path}"
fi
fi
if [ -e "cloud-init" ]; then
mkdir -p "${init_dest_path}"
for file in cloud-init/*; do
install -D -m 0644 -t "${init_dest_path}" "${file}"
done
if [ -d $init_config_dest_path ]; then
printf >&2 "\033[0m Refusing to overwrite user cloud-init files at \033[1;31m%s\n\033[0m" "${init_config_dest_path}"
printf >&2 "\033[0m\033[1;31m Updated cloud-init files available at %s\n\033[0m" "${init_dest_path}"
else
mkdir -p "${init_config_dest_path}"
for file in cloud-init/*; do
install -D -m 0644 -t "${init_config_dest_path}" "${file}"
done
fi
fi
if [ -e "profiles" ]; then
mkdir -p "${profile_dest_path}"
for file in profiles/*; do
install -D -m 0644 -t "${profile_dest_path}" "${file}"
done
if [ -d $profile_config_dest_path ]; then
printf >&2 "\033[0m Refusing to overwrite user profiles at \033[1;31m%s\n\033[0m" "${profile_config_dest_path}"
printf >&2 "\033[0m\033[1;31m Updated profiles available at %s\n\033[0m" "${profile_dest_path}"
else
mkdir -p "${profile_config_dest_path}"
for file in profiles/*; do
install -D -m 0644 -t "${profile_config_dest_path}" "${file}"
done
fi
fi
else
printf >&2 "\033[1;31m Checking dependencies...\n\033[0m"
# check that we have base dependencies
if ! { command -v curl >/dev/null || command -v wget >/dev/null; } || ! command -v tar >/dev/null; then
printf >&2 "Online install depends on tar and either curl or wget\n"
printf >&2 "Ensure you have all dependencies installed.\n"
exit 1
fi
if command -v curl >/dev/null 2>&1; then
download="curl -sLo"
elif command -v wget >/dev/null 2>&1; then
download="wget -qO"
fi
printf >&2 "\033[1;31m Downloading...\n\033[0m"
if [ "${next}" -eq 0 ]; then
release_ver="ublue-os/blincus/archive/refs/tags/${version}.tar.gz"
release_name=$(basename "${release_ver}")
else
release_ver="ublue-os/blincus/archive/refs/heads/main.tar.gz"
release_name="main"
fi
# go in tmp
tmp_dir="$(mktemp -d)"
cd "${tmp_dir}"
# download our target
${download} "${release_name}" "https://github.com/${release_ver}"
# uncompress
printf >&2 "\033[1;31m Unpacking...\n\033[0m"
if [ "${verbose}" -ne 0 ]; then
tar xvf "${release_name}"
else
tar xf "${release_name}"
fi
# deploy our files
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/blincus*; do
if ! install -D -m 0755 -t "${dest_path}" "${file}"; then
printf >&2 "Do you have permission to write to %s?\n" "${dest_path}"
exit 1
fi
done
if [ -e "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')/man/" ]; then
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/man/man1/*; do
install -D -m 0644 -t "${man_dest_path}" "${file}"
done
fi
if [ -e "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')/completions/" ]; then
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/completions/*; do
install -D -m 0644 -t "${completion_dest_path}" "${file}"
done
fi
if [ -e "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')/scripts/" ]; then
mkdir -p "${script_dest_path}"
cp -R "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/scripts "${script_dest_path}"
if [ -d $script_config_dest_path/scripts ]; then
printf >&2 "\033[0m Refusing to overwrite user scripts at \033[1;31m%s\n\033[0m" "${script_config_dest_path}/scripts"
printf >&2 "\033[0m\033[1;31m Updated scripts available at %s\n\033[0m" "${script_dest_path}/scripts"
else
mkdir -p "${script_config_dest_path}"
cp -R "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/scripts "${script_config_dest_path}"
fi
fi
if [ -e "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')/profiles/" ]; then
mkdir -p "${profile_dest_path}"
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/profiles/*; do
install -D -m 0644 -t "${profile_dest_path}" "${file}"
done
if [ -d $profile_config_dest_path ]; then
printf >&2 "\033[0m Refusing to overwrite profiles at \033[1;31m%s\n\033[0m" "${profile_config_dest_path}"
printf >&2 "\033[0m\033[1;31m Updated profiles available at %s\n\033[0m" "${template_dest_path}"
else
mkdir -p "${profile_config_dest_path}"
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/profiles/*; do
install -D -m 0644 -t "${profile_config_dest_path}" "${file}"
done
fi
fi
if [ -e "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')/cloud-init/" ]; then
mkdir -p "${init_dest_path}"
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/cloud-init/*; do
install -D -m 0644 -t "${init_dest_path}" "${file}"
done
if [ -d $init_config_dest_path ]; then
printf >&2 "\033[0m Refusing to overwrite cloud-init files at \033[1;31m%s\n\033[0m" "${init_config_dest_path}"
printf >&2 "\033[0m\033[1;31m Updated cloud-init files available at %s\n\033[0m" "${init_dest_path}"
else
mkdir -p "${init_config_dest_path}"
for file in "blincus-$(echo "${release_name}" | sed 's/.tar.gz//g')"/cloud-init/*; do
install -D -m 0644 -t "${init_config_dest_path}" "${file}"
done
fi
fi
# securely delete unneeded files
cd
if [ -n "${tmp_dir}" ] && [ -e "${tmp_dir}" ]; then
rm -rf "${tmp_dir}"
fi
fi
[ ! -w "${dest_path}" ] && printf >&2 "Cannot write into %s, permission denied.\n" "${dest_path}" && exit 1
#[ ! -w "${man_dest_path}" ] && printf >&2 "Cannot write into %s, permission denied.\n" "${man_dest_path}" && exit 1
printf >&2 "\033[1;32m Installation successful!\n\033[0m"
printf >&2 "\033[0m Blincus command installed at \033[1;31m%s\n\033[0m" "${dest_path}"
printf >&2 "\033[0m Configuration files are located in \033[1;31m%s\n\033[0m" "${script_config_dest_path}"
if ! echo "${PATH}" | grep -q "${dest_path}"; then
printf >&2 "\033[0m Be sure that \033[1;31m%s\033[0m is in your \033[1;31m\$PATH\033[0m environment variable to be able to use blincus without specifying the full path.\n\033[0m" "${dest_path}"
fi