-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·245 lines (218 loc) · 6.56 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
#!/usr/bin/env bash
set -e
# This script was downloaded from https://github.com/qzind/qz.sh and is part of the QZ Tray product
# Contribute to this script by visting the above website and opening an issue or pull request
# Console colors
RED="\\x1B[1;31m";GREEN="\\x1B[1;32m";BLUE="\\x1B[1;34m";PURPLE="\\x1B[1;35m";YELLOW="\\x1B[1;33m";PLAIN="\\x1B[0m"
OWNER="qzind"
REPO="tray"
URL="https://api.github.com/repos/${OWNER}/${REPO}/releases?per_page=100"
FETCH=""
# Determine if curl or wget are available
if which curl >/dev/null 2>&1 ; then
FETCH="curl -Lks"
elif which wget >/dev/null 2>&1 ; then
FETCH="wget -q -O -"
else
echo -e "${RED}Either \"curl\" or \"wget\" are required to use this script${PLAIN}"
exit 2
fi
RELEASE="auto" # e.g. "stable", "unstable"
TAG="auto" # e.g. "2.2.1", "v2.1.6"
if [ ! -z "$1" ]; then
echo -e "Picked up argument: ${BLUE}$1${PLAIN}"
case $1 in
*"help")
SCRIPT="$FETCH qz.sh |bash -s --"
if [ -t 0 ]; then
SCRIPT="install.sh"
fi
echo -e "\nUsage:\n $SCRIPT [\"${GREEN}stable${PLAIN}\"|\"${YELLOW}beta${PLAIN}\"|<${BLUE}version${PLAIN}>|\"${PURPLE}help${PLAIN}\"]"
echo -e " ${GREEN}stable${PLAIN} Downloads and installs the latest stable release"
echo -e " ${YELLOW}beta${PLAIN} Downloads and installs the latest beta release"
echo -e " ${BLUE}version${PLAIN} Downloads and installs the exact version specified, e.g. \"2.2.1\""
echo -e " ${PURPLE}help${PLAIN} Displays this help and exits"
echo -e "\n The default behavior is to download and install the ${GREEN}stable${PLAIN} version\n"
exit 0
;;
"stable")
RELEASE="stable"
;;
"beta")
RELEASE="beta"
;;
"unstable")
RELEASE="beta"
;;
esac
# If a parameter was provided but we don't recognize it, treat it as a tag
if [ "$RELEASE" == "auto" ]; then
TAG="$1"
# Append "v" to version if missing (e.g. 2.2.1 vs v2.2.1)
first_char="$(echo "$TAG"| cut -c1)"
if [ "$first_char" != "v" ]; then
TAG="v${TAG}"
fi
fi
fi
# Determine architecture
# valid values: "amd64", "arm64", "riscv"
ARCH="$(uname -m)"
case $ARCH in
*"arm64"*)
ARCH="arm64"
;;
*"aarch64"*)
ARCH="arm64"
;;
*"riscv"*)
ARCH="riscv"
;;
*)
ARCH="amd64"
;;
esac
# Determine file extension
# valid values: ".run", ".pkg", ".exe"
EXTENSION=".run"
case $OSTYPE in
"darwin"*)
EXTENSION=".pkg"
;;
esac
if [ "$RELEASE" == "auto" ]; then
RELEASE="stable"
fi
echo -e "Parsing ${BLUE}${URL}${PLAIN}..."
JSON="$($FETCH "$URL")"
# Gather stable and beta tagged releases by loop over JSON returned from GitHub API
if [ "$TAG" == "auto" ]; then
STABLE_TAGS=""
BETA_TAGS=""
tag_name=""
while IFS= read -r line; do
case $line in
*"\"tag_name\":"*)
# assume "tag_name": comes before "prerelease":
tag_name="$(echo "$line" |cut -d '"' -f4 |tr -d '"'|tr -d ',' |tr -d ' ')"
;;
*"\"prerelease\": false,"*)
STABLE_TAGS+="$tag_name"$'\n'
BETA_TAGS+="$tag_name"$'\n'
;;
*"\"prerelease\": true,"*)
BETA_TAGS+="$tag_name"$'\n'
;;
*"\"assets\":"*)
# we've gone too far
tag_name=""
;;
esac
done <<< "$JSON"
# Sort the results
LATEST_STABLE="$(echo "${STABLE_TAGS}" |sort -Vr|head -1)"
LATEST_BETA="$(echo "${BETA_TAGS}" |sort -Vr|head -1)"
case $RELEASE in
"stable")
TAG="$LATEST_STABLE"
;;
"beta")
TAG="$LATEST_BETA"
;;
esac
if [ -z "$TAG" ]; then
echo -e "${RED}Unable to locate a tag for this release${PLAIN}"
exit 2
fi
echo -e "Latest ${GREEN}${RELEASE}${PLAIN} version found: ${BLUE}$TAG${PLAIN}"
fi
# Get URL for latest release
echo -e "Searching ${BLUE}${EXTENSION}${PLAIN} downloads for ${BLUE}${TAG}${PLAIN} matching ${BLUE}${ARCH}${PLAIN}..."
OS_URLS=""
while IFS= read -r line; do
url=""
case $line in
*"download/$TAG/"*)
url=$(echo "$line" |cut -d '"' -f4 |tr -d '"'|tr -d ',' |tr -d ' ')
;;
esac
case $url in
*"$EXTENSION")
OS_URLS+="$url"$'\n'
;;
esac
done <<< "$JSON"
# Gather all URLs that match current architecture
AMD64_URLS=""
ARM64_URLS=""
RISCV_URLS=""
while IFS= read -r line; do
url=""
case $line in
*"arm64"*)
ARM64_URLS+="$line"$'\n'
;;
*"riscv"*)
RISCV_URLS+="$line"$'\n'
;;
*)
AMD64_URLS+="$line"$'\n'
;;
esac
done <<< "$OS_URLS"
# Echo the proper download URL
DOWNLOAD_URL=""
case $ARCH in
*"arm64"*)
DOWNLOAD_URL=$(echo "$ARM64_URLS" |head -1)
;;
*"riscv"*)
DOWNLOAD_URL=$(echo "$RISCV_URLS" |head -1)
;;
*)
DOWNLOAD_URL=$(echo "$AMD64_URLS" |head -1)
;;
esac
if [ -z "$DOWNLOAD_URL" ]; then
echo -e "${RED}Unable to locate a download for this platform${PLAIN}"
exit 2
fi
echo -e "Downloading ${BLUE}${DOWNLOAD_URL}${PLAIN}..."
# Determine if curl or wget are available
TEMP_FILE="/tmp/${REPO}-${TAG}${EXTENSION}"
# Remove old copy if needed
if [ -f "$TEMP_FILE" ]; then
rm -f "$TEMP_FILE" >/dev/null
fi
# Download installer using curl or wget
if which curl >/dev/null 2>&1 ; then
# Note: GitHub uses redirects, make sure "curl -L" is specifed
curl -Lks "$DOWNLOAD_URL" --output "/tmp/${REPO}-${TAG}${EXTENSION}"
elif which wget >/dev/null 2>&1 ; then
# Note: GitHub uses redirects, but wget should follow redirects automatically
wget -q -O $TEMP_FILE "$DOWNLOAD_URL"
else
echo -e "${RED}Either \"curl\" or \"wget\" are required to use this script${PLAIN}"
exit 2
fi
# Install using unattended techniques: https://github.com/qzind/tray/wiki/deployment
echo -e "Download successful, beginning the install..."
case $OSTYPE in
"darwin"*)
# Assume .pkg (installer) for MacOS
sudo installer -pkg "$TEMP_FILE" -target /
;;
*)
# Assume .run (makeself) for others
if which sudo >/dev/null 2>&1 ; then
# use "sudo" if available
sudo bash "$TEMP_FILE" --nox11 -- -y
else
# fallback to "su -c"
su root -c "bash '$TEMP_FILE' --nox11 -- -y"
fi
;;
esac
# Clean up
rm -f "$TEMP_FILE" >/dev/null
exit 0