forked from wgzhao/Addax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
218 lines (193 loc) · 5.63 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
#!/bin/bash
set -u
abort() {
printf "%s\n" "$@"
exit 1
}
if [ -z "${BASH_VERSION:-}" ]; then
abort "Bash is required to interpret this script."
fi
# First check OS.
OS="$(uname)"
if [[ "${OS}" == "Linux" ]]; then
ADDAX_ON_LINUX=1
elif [[ "${OS}" != "Darwin" ]]; then
abort "Addax is only supported on Windows and Linux."
fi
# Required installation paths. To install elsewhere (which is unsupported)
if [[ -z "${ADDAX_ON_LINUX-}" ]]; then
UNAME_MACHINE="$(/usr/bin/uname -m)"
if [[ "${UNAME_MACHINE}" == "arm64" ]]; then
# On ARM macOS, this script installs to /opt/addax only
ADDAX_PREFIX="/opt/addax"
ADDAX_REPOSITORY="${ADDAX_PREFIX}"
else
# On Intel macOS, this script installs to /usr/local only
ADDAX_PREFIX="/usr/local"
ADDAX_REPOSITORY="${ADDAX_PREFIX}/addax"
fi
STAT_FLAG="-f"
PERMISSION_FORMAT="%A"
CHOWN="/usr/sbin/chown"
CHGRP="/usr/bin/chgrp"
GROUP="admin"
TOUCH="/usr/bin/touch"
else
UNAME_MACHINE="$(uname -m)"
# On Linux, it installs to /opt/addax, you SHOULD have sudo access or you're root
ADDAX_PREFIX="/opt/addax"
ADDAX_REPOSITORY="${ADDAX_PREFIX}"
STAT_FLAG="--printf"
PERMISSION_FORMAT="%a"
CHOWN="/bin/chown"
CHGRP="/bin/chgrp"
GROUP="$(id -gn)"
TOUCH="/bin/touch"
fi
have_sudo_access() {
if [[ ! -x "/usr/bin/sudo" ]]; then
return 1
fi
local -a args
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A")
elif [[ -n "${NONINTERACTIVE-}" ]]; then
args=("-n")
fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
if [[ -n "${args[*]-}" ]]; then
SUDO="/usr/bin/sudo ${args[*]}"
else
SUDO="/usr/bin/sudo"
fi
if [[ -n "${NONINTERACTIVE-}" ]]; then
# Don't add quotes around ${SUDO} here
${SUDO} -l mkdir &>/dev/null
else
${SUDO} -v && ${SUDO} -l mkdir &>/dev/null
fi
HAVE_SUDO_ACCESS="$?"
fi
if [[ -z "${ADDAX_ON_LINUX-}" ]] && [[ "${HAVE_SUDO_ACCESS}" -ne 0 ]]; then
abort "Need sudo access on macOS (e.g. the user ${USER} needs to be an Administrator)!"
fi
return "${HAVE_SUDO_ACCESS}"
}
# get the special repo latest version
# param: $1 - repo name, e.g wgzhao/addax
# returns: the version number: e.g 4.0.6
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"
do
printf " "
printf "%s" "${arg// /\ }"
done
}
execute() {
if ! "$@"; then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
execute_sudo() {
local -a args=("$@")
if have_sudo_access; then
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A" "${args[@]}")
fi
echo "/usr/bin/sudo" "${args[@]}"
execute "/usr/bin/sudo" "${args[@]}"
else
ohai "${args[@]}"
execute "${args[@]}"
fi
}
# has installed
if [ -d "${ADDAX_REPOSITORY}" ]; then
echo "Addax is already installed to ${ADDAX_REPOSITORY}"
echo "Do you want to re-installed, if you want, the install script will cleanup the ${ADDAX_REPOSITORY}"
read -p "Do you want to continue? [y/N] " response
if response="$(echo "${response}" | tr '[:upper:]' '[:lower:]')"; then
if [[ "${response}" != "y" ]]; then
exit 1
else
execute_sudo "rm" "-rf" "${ADDAX_REPOSITORY}"
fi
else
exit 0
fi
fi
unset HAVE_SUDO_ACCESS
chmods=()
chowns=()
chgrps=()
group_chmods=()
user_chmods=()
mkdirs=()
# create install directory
if [[ -d "${ADDAX_PREFIX}" ]]; then
if [[ "${#chmods[@]}" -gt 0 ]]; then
execute_sudo "/bin/chmod" "u+rwx" "${chmods[@]}"
fi
if [[ "${#group_chmods[@]}" -gt 0 ]]; then
execute_sudo "/bin/chmod" "g+rwx" "${group_chmods[@]}"
fi
if [[ "${#user_chmods[@]}" -gt 0 ]]; then
execute_sudo "/bin/chmod" "g-w,o-w" "${user_chmods[@]}"
fi
if [[ "${#chowns[@]}" -gt 0 ]]; then
execute_sudo "${CHOWN}" "${USER}" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]; then
execute_sudo "${CHGRP}" "${GROUP}" "${chgrps[@]}"
fi
else
execute_sudo "/bin/mkdir" "-p" "${ADDAX_PREFIX}"
if [[ -z "${ADDAX_ON_LINUX-}" ]]; then
execute_sudo "${CHOWN}" "root:wheel" "${ADDAX_PREFIX}"
else
execute_sudo "${CHOWN}" "${USER}:${GROUP}" "${ADDAX_PREFIX}"
fi
fi
if [[ "${#mkdirs[@]}" -gt 0 ]]; then
execute_sudo "/bin/mkdir" "-p" "${mkdirs[@]}"
execute_sudo "/bin/chmod" "u=rwx,g=rwx" "${mkdirs[@]}"
if [[ "${#mkdirs_user_only[@]}" -gt 0 ]]; then
execute_sudo "/bin/chmod" "g-w,o-w" "${mkdirs_user_only[@]}"
fi
execute_sudo "${CHOWN}" "${USER}" "${mkdirs[@]}"
execute_sudo "${CHGRP}" "${GROUP}" "${mkdirs[@]}"
fi
if ! [[ -d "${ADDAX_REPOSITORY}" ]]; then
execute_sudo "/bin/mkdir" "-p" "${ADDAX_REPOSITORY}"
fi
execute_sudo "${CHOWN}" "-R" "${USER}:${GROUP}" "${ADDAX_REPOSITORY}"
# try to download the latest version
# tmpdir=$(mktemp -d -t addax-install.XXXXXX)
version=$(get_latest_release "wgzhao/addax")
pkg_url="https://github.com/wgzhao/Addax/releases/download/${version}/addax-${version}.tar.gz"
echo "Downloading and installing Addax..."
(
cd "${ADDAX_REPOSITORY}" >/dev/null || return
curl -L "${pkg_url}" -o addax.tar.gz
if [ $? -ne 0 ]; then
abort "Failed to download package from ${pkg_url}"
fi
tar -xzf addax.tar.gz
mv addax-${version}/* .
rm -r addax-${version}
rm -f addax.tar.gz
execute_sudo chown -R "${USER}:${GROUP}" .
)
cat <<EOS
Addax has installed on ${ADDAX_REPOSITORY}
We recommend that you execute the following command to add addax execute path to PATH environment variable:
export PATH=${ADDAX_REPOSITORY}/bin:\$PATH
EOS