forked from tomeichlersmith/denv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·324 lines (300 loc) · 8.83 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
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
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the denv project: https://github.com/tomeichlersmith/denv
#
# Copyright (C) 2021 denv contributors
#
# denv is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# denv is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with denv; if not, see <http://www.gnu.org/licenses/>.
# POSIX
set -o errexit
set -o nounset
if [ -n "${DENV_DEBUG+x}" ]; then
set -o xtrace
fi
_denv_info() {
printf "\033[32;1m INFO: \033[0m\033[32m%s\n" "$1"
shift
while [ "$#" -gt "0" ]; do
printf ' %s\n' "$1"
shift
done
printf "\033[0m"
}
# print each argument on its own line with the first line
# prefixed with "ERROR: ".
_denv_error() {
printf >&2 "\033[1;31mERROR: \033[0m\033[31m%s\n" "$1"
shift
while [ "$#" -gt "0" ]; do
printf >&2 " %s\n" "$1"
shift
done
printf >&2 "\033[0m"
}
# print the question passed to us and process user input
# continues in infinite loop if user is not explicitly answering yes or no
# Arguments
# 1 - question to ask the user (yes or no)
# Output
# returns 0 if user answers yes and 1 if user answers no
_denv_user_confirm() {
question="$*"
while true; do
printf "\033[1m%s\033[0m [Y/n] " "${question}"
read -r ans </dev/tty
case "${ans}" in
Y|y|yes)
return 0
;;
N|n|no)
return 1
;;
*)
_denv_error "${ans} is not one of Y, y, yes, N, n, or no."
;;
esac
done
}
# 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)
--devel/-d: symlink to install location rather than copy, helpful for testing during development
ignored if not in the repository
--next/-n: install HEAD of main branch rather than last release
--help/-h: show this message
-v: show more verbosity
EOF
}
version=v1.1.1
next=0
verbose=0
devel=0
prefix=""
# Parse arguments
while [ "$#" -gt 0 ]; 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
;;
-d | --devel)
shift
devel=1
;;
-n | --next)
shift
next=1
;;
-p | --prefix)
if [ -n "$2" ]; then
[ -d "$2" ] || mkdir -p "$2"
prefix="$(cd "$2" && pwd -P)"
shift
shift
fi
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
if [ -z "${prefix}" ]; then
prefix="/usr/local"
# in case we're not root, just default to the home directory
if [ "$(id -u || true)" -ne 0 ]; then
prefix="${HOME}/.local"
fi
fi
if [ ! -d "${prefix}" ] && ! mkdir "${prefix}"; then
_denv_error "Cannot create ${prefix}, permission denied."
exit 1
fi
if [ ! -w "${prefix}" ]; then
_denv_error "Cannot write into ${prefix}, permission denied"
exit 1
fi
completion_dest_path="${prefix}/share/bash-completion/completions/"
dest_path="${prefix}/bin"
man_dest_path="${prefix}/share/man/man1"
# get current dir
curr_dir=$(dirname "$0")
cd "${curr_dir}" || exit 1
# make sure destination is writable
if [ ! -d "${dest_path}" ]; then
mkdir -p "${dest_path}"
fi
if [ ! -d "${man_dest_path}" ]; then
mkdir -p "${man_dest_path}"
fi
if [ ! -d "${completion_dest_path}" ]; then
mkdir -p "${completion_dest_path}"
fi
# if files are available here, install files in dest directory
if [ -e "${curr_dir}/denv" ]; then
if [ "${devel}" -ne 0 ]; then
copy="ln -sf"
if ! ${copy} "${PWD}/denv" "${dest_path}"; then
_denv_error "Copying to ${dest_path} failed." \
"Do you have permission to write there?"
exit 1
fi
${copy} "${PWD}/_denv_entrypoint" "${dest_path}"
else
copy="install -m 0644"
if ! install -m 0775 denv "${dest_path}"; then
_denv_error "Copying to ${dest_path} failed." \
"Do you have permission to write there?"
exit 1
fi
install -m 0775 _denv_entrypoint "${dest_path}"
fi
if [ -e "man" ]; then
for file in "${PWD}"/man/man1/*; do
${copy} "${file}" "${man_dest_path}"
done
fi
if [ -e "completions" ]; then
for file in "${PWD}"/completions/*; do
${copy} "${file}" "${completion_dest_path}"
done
fi
else
# files are not here so we are going to download from github
_denv_info "Checking dependencies..."
# check that we have base dependencies
if ! { command -v curl > /dev/null || command -v wget > /dev/null; } || ! command -v tar > /dev/null; then
_denv_error "Online install depends on 'tar' and either 'curl' or 'wget'" \
"Ensure these dependencies are installed and then rerun the online install."
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
_denv_info "Downloading..."
if [ "${next}" -eq 0 ]; then
release_ver="tomeichlersmith/denv/archive/refs/tags/${version}.tar.gz"
release_name=$(echo "${release_ver}" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.tar\.gz$')
else
release_ver="tomeichlersmith/denv/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
_denv_info "Unpacking..."
if [ "${verbose}" -ne 0 ]; then
tar xvf "${release_name}"
else
tar xf "${release_name}"
fi
# enter source
cd "denv-$(echo "${release_name}" | sed 's|.tar.gz||' || true)"
# deploy
copy="install -m 0644"
if ! install -m 0775 denv "${dest_path}"; then
_denv_error "Copying to ${dest_path} failed." \
"Do you have permission to write there?"
exit 1
fi
install -m 0775 _denv_entrypoint "${dest_path}"
if [ -e "man" ]; then
for file in "${PWD}"/man/man1/*; do
${copy} "${file}" "${man_dest_path}"
done
fi
if [ -e "completions" ]; then
for file in "${PWD}"/completions/*; do
${copy} "${file}" "${completion_dest_path}"
done
fi
# securely delete unneeded files
cd
if [ -n "${tmp_dir}" ] && [ -e "${tmp_dir}" ]; then
rm -rf "${tmp_dir}"
fi
fi
# double check that the installation is functional
unset DENV_INSTALL_FAILURE
for executable in "denv" "_denv_entrypoint"; do
if [ -x "${dest_path}/${executable}" ] && [ -f "${dest_path}/${executable}" ]; then
continue
fi
_denv_error "'${executable}' does not exist as an executable file at '${dest_path}/${executable}'"
DENV_INSTALL_FAILURE=1
done
if [ -z "${DENV_INSTALL_FAILURE+x}" ]; then
_denv_info "Successfully installed denv to ${dest_path}"
else
_denv_error "Installation corrupted!"
fi
case "${PATH}" in
*"${dest_path}"*)
_denv_info "Found ${dest_path} in PATH, you're all set!"
;;
*)
_denv_error "Unable to find ${dest_path} in PATH."
if _denv_user_confirm "Should we update your current shell's profile accordingly?"; then
case "${SHELL}" in
*bash)
# update ~/.bashrc since that's the only one I know is always sourced
{
echo ""
echo "# add the path to the installation of denv to PATH"
echo "export PATH=\"\${PATH}:${dest_path}\""
} >> ~/.bashrc
_denv_info "Open a new terminal to get the updated PATH (or '. ~/.bashrc' directly)."
;;
*zsh)
{
echo ""
echo "# add the path to the installation of denv to list of executable paths"
echo "path+=(\"${dest_path}\")"
echo "export PATH"
} >> ~/.zshrc
_denv_info "Open a new terminal to get the updated PATH (or '. ~/.zshrc' directly)."
;;
*)
_denv_error "Current shell ${SHELL} is not known." \
"You are responsible for adding the destination to your PATH." \
"Feel free to open an issue with the solution: https://github.com/tomeichlersmith/denv/issues/new/choose"
;;
esac
else
_denv_info "Be sure that directory in is your \$PATH environment variable for denv to work." \
"A common way to do this is to update the definition of PATH in your shell profile." \
"For example, you could put the following line at the bottom of your ~/.bashrc file." \
" export PATH=\"\${PATH}:${dest_path}\""
fi
;;
esac