-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathinstall
executable file
·326 lines (264 loc) · 6.27 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
325
326
#!/bin/sh
set -e
# Check if command is available
has() {
command -v "$1" >/dev/null 2>&1
}
is_under_termux() {
has termux-setup-storage
}
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0
else
return 1
fi
}
install_completions() {
test "$OS" = "Darwin" && return 0
COMPLETIONS="$1"
if has zsh;
then
info "Installing zsh completions"
TARGET="/usr/share/zsh/vendor-completions"
sudo mkdir -p "$TARGET"
sudo cp "$COMPLETIONS/mangal.zsh" "$TARGET/_mangal"
fi
if has fish;
then
info "Installing fish completions"
TARGET="/usr/share/fish/completions"
sudo mkdir -p "$TARGET"
sudo cp "$COMPLETIONS/mangal.fish" "$TARGET/mangal.fish"
fi
if has bash;
then
info "Installing bash completions"
TARGET="/usr/share/bash-completion/completions"
sudo mkdir -p $TARGET
sudo cp "$COMPLETIONS/mangal.bash" "$TARGET/mangal"
fi
}
info() {
printf "\033[1;35m%s\033[0m %s\n" "info:" "$1"
}
warn() {
printf "\033[1;33m%s\033[0m %s\n" "warning:" "$1"
}
err() {
printf "\033[1;31m%s\033[0m %s\n" "error:" "$1" >&2
}
die() {
err "$1"
exit 1
}
# Download file. first argument is out path, second is URL
download() {
if has curl; then
curl -sfLo "$1" "$2"
elif has wget; then
wget -qO "$1" "$2"
else
die "No download program (curl, wget) found, please install one."
fi
}
verify_checksums() {
info "Verifying checksums"
if has sha256sum; then
OK=$(sha256sum --ignore-missing --quiet --check checksums.txt)
else
OK=$(shasum -a 256 --ignore-missing --quiet --check checksums.txt)
fi
$OK || die "Checksums did not match! Abort"
}
install_deb() {
if ! has dpkg; then
warn "dpkg is not available. Using alternative method"
install_binary
return
fi
case "$ARCH" in
aarch64)
ARCH="arm64"
;;
armv*)
ARCH="armhf"
;;
x86_64)
ARCH="amd64"
;;
i*)
ARCH="i386"
;;
esac
DEB_NAME="${FILE_BASENAME}_${VERSION}_${ARCH}.deb"
DEB_FILE="$TMPDIR/$DEB_NAME"
export DEB_FILE DEB_NAME
(
cd "$TMPDIR"
info "Downloading $DEB_NAME"
download "$DEB_FILE" "$RELEASES_URL/download/$TAG/$DEB_NAME"
info "Downloading checksums"
download "checksums.txt" "$RELEASES_URL/download/$TAG/checksums.txt"
verify_checksums
)
info "Installing"
sudo dpkg --install "$DEB_FILE"
}
install_termux() {
info "Installing for Termux"
pkg install mangal
}
install_macos() {
if ! has brew; then
warn "Homebrew is not installed. Using alternative method"
install_binary
return
fi
info "Installing with Homebrew"
brew tap metafates/mangal
if has mangal; then
brew reinstall mangal
else
brew install mangal
fi
}
install_rpm() {
if ! has rpm; then
warn "rpm is not available. Using alternative method"
install_binary
return
fi
case "$ARCH" in
arm64)
ARCH="aarch64"
;;
armv*)
ARCH="armv6hl"
;;
amd64)
ARCH="x86_64"
;;
i*)
ARCH="i386"
;;
esac
RPM_NAME="${FILE_BASENAME}-${VERSION}.${ARCH}.rpm"
RPM_FILE="$TMPDIR/$DEB_NAME"
export RPM_FILE RPM_NAME
(
cd "$TMPDIR"
info "Downloading $RPM_NAME"
download "$DEB_FILE" "$RELEASES_URL/download/$TAG/$RPM_NAME"
info "Downloading checksums"
download "checksums.txt" "$RELEASES_URL/download/$TAG/checksums.txt"
verify_checksums
)
info "Installing"
sudo rpm --install "$RPM_FILE"
}
install_binary() {
info "Installing raw binary"
case "$ARCH" in
aarch64)
ARCH="arm64"
;;
armv*)
ARCH="armv6"
;;
amd64)
ARCH="x86_64"
;;
i*)
ARCH="i386"
;;
esac
TAR_NAME="${FILE_BASENAME}_${VERSION}_${OS}_${ARCH}.tar.gz"
TAR_FILE="$TMPDIR/$TAR_NAME"
export TAR_NAME TAR_FILE
(
cd "$TMPDIR"
info "Downloading $TAR_NAME"
download "$TAR_FILE" "$RELEASES_URL/download/$TAG/$TAR_NAME"
info "Downloading checksums"
download "checksums.txt" "$RELEASES_URL/download/$TAG/checksums.txt"
verify_checksums
)
tar -xf "$TAR_FILE" -C "$TMPDIR"
if [ -d "/usr/local/bin/" ];
then
OUT="/usr/local/bin/"
else
OUT="/usr/bin/"
fi
info "Moving to ${OUT}"
sudo sh -c "install -Dm755 '$TMPDIR/$FILE_BASENAME' '${OUT}'"
install_completions "$TMPDIR/completions"
}
pre_install() {
has mangal && warn "mangal is installed already, reinstalling"
RELEASES_URL="https://github.com/metafates/mangal/releases"
FILE_BASENAME="mangal"
info "Fetching latest version"
TAG="$(curl -sfL -o /dev/null -w "%{url_effective}" "$RELEASES_URL/latest" |
rev |
cut -f1 -d'/' |
rev)"
test -z "$TAG" && {
die "Unable to get mangal version."
}
OS=$(uname -s)
ARCH=$(uname -m)
VERSION=${TAG#?}
info "Latest version is $VERSION"
TMPDIR="$(mktemp -d)"
}
post_install() {
if has mangal; then
printf "\n🎉 \033[1;32mMangal was installed successfully\033[0m\n\n"
else
die "Mangal was not installed or it's not in the PATH."
fi
}
install() {
pre_install
case $OS in
Darwin)
info "macOS detected"
install_macos
;;
Linux)
case $(cat /etc/*release 2>/dev/null | tr "[:upper:]" "[:lower:]") in
*ubuntu*)
info "Ubuntu detected"
install_deb
;;
*debian*)
info "Debian detected"
install_deb
;;
*fedora*)
info "Fedora detected"
install_rpm
;;
*)
if is_under_termux; then
info "Termux detected"
install_termux
else
info "Linux detected"
install_binary
fi
;;
esac
;;
*)
err "Unknown OS detected: $OS"
;;
esac
post_install
}
install