forked from anowell/mia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·347 lines (277 loc) · 8.82 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
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
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/sh
set -e
main() {
for cmd in curl tar gzip; do
need_cmd $cmd
done
set_globals
handle_command_line_args "$@"
}
set_globals() {
algo_version="1.0.1"
default_prefix="${ALGO_PREFIX-/usr/local}"
base_url="https://github.com/algorithmiaio/algorithmia-cli/releases/download"
completions_url="https://raw.githubusercontent.com/algorithmiaio/algorithmia-cli/master/completions"
}
set_architecture() {
echo_verbose "detecting architecture"
_ostype="$(uname -s)"
_cputype="$(uname -m)"
echo_verbose "uname -s reports: $_ostype"
echo_verbose "uname -m reports: $_cputype"
if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then
# Darwin `uname -s` lies
if sysctl hw.optional.x86_64 | grep -q ': 1'; then
local _cputype=x86_64
fi
fi
case "$_ostype" in
Linux)
local _ostype=unknown-linux-gnu
;;
Darwin)
local _ostype=apple-darwin
;;
MINGW* | MSYS* | CYGWIN*)
local _ostype=pc-windows-gnu
;;
*)
err "unrecognized OS type: $_ostype"
;;
esac
case "$_cputype" in
i386 | i486 | i686 | i786 | x86)
local _cputype=i686
;;
x86_64 | x86-64 | x64 | amd64)
local _cputype=x86_64
;;
*)
err "unknown CPU type: $_cputype"
esac
# Detect 64-bit linux with 32-bit userland
if [ $_ostype = unknown-linux-gnu -a $_cputype = x86_64 ]; then
# $SHELL does not exist in standard 'sh', so probably only exists
# if configure is running in an interactive bash shell. /usr/bin/env
# exists *everywhere*.
local _bin_to_probe="${SHELL-bogus_shell}"
if [ ! -e "$_bin_to_probe" -a -e "/usr/bin/env" ]; then
_bin_to_probe="/usr/bin/env"
fi
# $SHELL may be not a binary
if [ -e "$_bin_to_probe" ]; then
file -L "$_bin_to_probe" | grep -q "text" || _bin_to_probe="/usr/bin/env"
fi
if [ -e "$_bin_to_probe" ]; then
file -L "$_bin_to_probe" | grep -q "x86[_-]64" || local _cputype=i686
fi
fi
_algo_arch="$_cputype-$_ostype"
echo_verbose "architecture is $_algo_arch"
}
set_shell() {
if command -v getent > /dev/null 2>&1 ; then
_shell=$(getent passwd $LOGNAME | cut -d: -f7)
echo_verbose "detected user shell as '$_shell'"
elif command -v dscl > /dev/null 2>&1 ; then
_shell=$(dscl . -read /Users/$(id -un) UserShell | cut -f2 -d' ')
echo_verbose "detected user shell as '$_shell'"
else
_shell=/bin/bash
echo_verbose "guessing user shell as '$_shell'"
fi
}
print_welcome_message() {
local _prefix="$1"
local _uninstall="$2"
local _disable_sudo="$3"
cat <<"EOF"
/\
/ \ Welcome to Algorithmia
/ \ Command Line Tools
/\ /\
/ \ / \
EOF
# if [ "$_disable_sudo" = false ]; then
# if [ "$(id -u)" = 0 ]; then
# cat <<EOF
# WARNING: This script appears to be running as root. While it will work
# correctly, it is not necessary to run this install script as root.
# EOF
# fi
# fi
if [ "$_uninstall" = false ]; then
cat <<EOF
This script will download algo and install it to $_prefix.
EOF
# You may install elsewhere by running this script with the --prefix=<path> option.
else
cat <<EOF
This script will uninstall the existing algo installation at $_prefix.
EOF
fi
# if [ "$_disable_sudo" = false ]; then
# cat <<EOF
# The installer will run under 'sudo' and may ask you for your password. If you do
# not want the script to run 'sudo' then pass it the --disable-sudo flag.
# EOF
# fi
}
# Migrate from pre-1.0 config location of ~/.algorithmia
# to the 1.0 location: ~/.algorithmia/config
migrate_config() {
if [ -f ~/.algorithmia ]; then
echo_verbose "migrating configuration..."
mv ~/.algorithmia ~/.algorithmia.bak
mkdir ~/.algorithmia
mv ~/.algorithmia.bak ~/.algorithmia/config
fi
}
handle_command_line_args() {
local _prefix="$default_prefix"
local _uninstall=false
local _help=false
local _disable_sudo=false
case "$(uname -s)" in
MINGW* | MSYS* | CYGWIN*)
echo "Sorry, but this script doesn't currently support Windows. You can find the Windows install instructions here: https://algorithmia.com/developers/clients/cli#installing-the-algorithmia-cli"
return
;;
*)
esac
local _arg
for _arg in "$@"; do
case "${_arg%%=*}" in
--uninstall )
_uninstall=true
;;
-h | --help )
_help=true
;;
--verbose)
# verbose is a global flag
flag_verbose=true
;;
--disable-sudo)
_disable_sudo=true
;;
-y | --yes)
# yes is a global flag
flag_yes=true
;;
--prefix)
if is_value_arg "$_arg" "prefix"; then
_prefix="$(get_value_arg "$_arg")"
fi
;;
*)
echo "Unknown argument '$_arg', displaying usage:"
echo ${_arg%%=*}
_help=true
;;
esac
done
if [ "$_help" = true ]; then
print_help
exit 0
fi
print_welcome_message $_prefix $_uninstall $_disable_sudo
set_architecture
set_shell
if [ "$_uninstall" = true ]; then
uninstall_cli
else
install_cli
fi
}
install_cli() {
# download algo for platform
local tmpdir=$(mktemp -d)
cd $tmpdir
echo_verbose "working directory: '$tmpdir'"
local release_url="${base_url}/v${algo_version}/algorithmia-v${algo_version}-${_algo_arch}.tar.gz"
echo_verbose "downloading release tarball..."
curl -sSfL "$release_url" -o "algo.tar.gz"
# Remove old versions - this should be removed from future releases
if which algo > /dev/null 2>&1; then
echo_verbose "removing old version: $(which algo)"
maybe_sudo rm -f $(which algo)
fi
echo_verbose "extracting release tarball..."
tar -xzf algo.tar.gz
# copy to $_prefix/bin
echo_verbose "installing 'algo'..."
maybe_sudo cp $tmpdir/algo $_prefix/bin/
migrate_config
# install completions
echo_verbose "installing shell completions..."
maybe_sudo mkdir -p /usr/local/share/zsh/site-functions/ || true
maybe_sudo cp $tmpdir/completions/zsh/_algo /usr/local/share/zsh/site-functions/
maybe_sudo mkdir -p /etc/bash_completion.d/
maybe_sudo cp $tmpdir/completions/bash/algo /etc/bash_completion.d/
if [ "$_shell" = "/bin/zsh" ]; then
echo "Zsh completions should load in subsequent shells if your \$fpath contains '/usr/local/share/zsh/site-functions'. Reload completions in your current shell by running:"
echo
echo " compinit"
echo
elif [ "$_shell" = "/bin/bash" ]; then
echo "Bash completions should be automatically sourced in subsequent shells if 'bash-completion' is installed. You may manually source them by running:"
echo
echo " source /etc/bash_completion.d/algo"
echo
fi
echo "Installation complete! Run 'algo --help' to get started."
}
uninstall_cli() {
maybe_sudo rm -f $_prefix/bin/algo || true
maybe_sudo rm -f /usr/local/share/zsh/site-functions/_algo || true
maybe_sudo rm -f /etc/bash_completion.d/algo || true
# Remove old versions - this should be removed from future releases
if which algo > /dev/null 2>&1; then
maybe_sudo rm -f $(which algo)
fi
echo "Algorithmia Command Line Tools uninstalled!"
}
print_help() {
echo '
Usage: install-algo.sh [--verbose]
Options:
--uninstall Uninstall instead of install
--help, -h Display usage information
'
}
# --prefix=<path> Install to a specific location (default /usr/local)
# --disable-sudo Do not run installer under sudo
# --yes, -y Disable the interactive mode
echo_verbose() {
if [ "$flag_verbose" = true ]; then
echo "$1"
fi
}
err() {
echo "$1" >&2
exit 1
}
maybe_sudo() {
local _is_windows=false
case "$_ostype" in
*windows*)
_is_windows=true
;;
esac
local _is_root=false
if [ $(id -u) = 0 ]; then
_is_root=true
fi
if [ "$_disable_sudo" = true -o "$_is_windows" = true -o "$_is_root" = true ]; then
"$@"
else
sudo "$@"
fi
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1; then
err "need '$1' (command not found)"
fi
}
main "$@"