forked from benweissmann/getmic.ro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.sh
executable file
·333 lines (267 loc) · 9.67 KB
/
index.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
#!/bin/sh
# This script installs micro.
#
# Quick install: `curl https://getmic.ro | bash`
#
# This script will install micro to the directory you're in. To install
# somewhere else (e.g. /usr/local/bin), cd there and make sure you can write to
# that directory, e.g. `cd /usr/local/bin; curl https://getmic.ro | sudo bash`
#
# Found a bug? Report it here: https://github.com/benweissmann/getmic.ro
#
# Acknowledgments:
# - Micro, of course: https://micro-editor.github.io/
# - Loosely based on the Chef curl|bash: https://docs.chef.io/install_omnibus.html
# - ASCII art courtesy of figlet: http://www.figlet.org/
set -e -u
githubLatestTag() {
latestJSON="$( eval "$http 'https://api.github.com/repos/$1/releases/latest'" 2>/dev/null )" || true
versionNumber=''
if ! echo "$latestJSON" | grep 'API rate limit exceeded' >/dev/null 2>&1 ; then
if ! versionNumber="$( echo "$latestJSON" | grep -oEm1 '[0-9]+[.][0-9]+[.][0-9]+' - 2>/dev/null )" ; then
versionNumber=''
fi
fi
if [ "${versionNumber:-x}" = "x" ] ; then
# Try to fallback to previous latest version detection method if curl is available
if command -v curl >/dev/null 2>&1 ; then
if finalUrl="$( curl "https://github.com/$1/releases/latest" -s -L -I -o /dev/null -w '%{url_effective}' 2>/dev/null )" ; then
trimmedVers="${finalUrl##*v}"
if [ "${trimmedVers:-x}" != "x" ] ; then
echo "$trimmedVers"
exit 0
fi
fi
fi
cat 1>&2 << 'EOA'
/=====================================\\
| FAILED TO HTTP DOWNLOAD FILE |
\\=====================================/
Uh oh! We couldn't download needed internet resources for you. Perhaps you are
offline, your DNS servers are not set up properly, your internet plan doesn't
include GitHub, or the GitHub servers are down?
EOA
exit 1
else
echo "$versionNumber"
fi
}
if [ "${GETMICRO_HTTP:-x}" != "x" ]; then
http="$GETMICRO_HTTP"
elif command -v curl >/dev/null 2>&1 ; then
http="curl -L"
elif command -v wget >/dev/null 2>&1 ; then
http="wget -O-"
else
cat 1>&2 << 'EOA'
/=====================================\\
| COULD NOT FIND HTTP PROGRAM |
\\=====================================/
Uh oh! We couldn't find either curl or wget installed on your system.
To continue with installation, you have two options:
A. Install either wget or curl on your system. You may need to run `hash -r`.
B. Define GETMICRO_HTTP to be a command (with arguments deliminated by spaces)
that both follows HTTP redirects AND prints the fetched content to stdout.
For examples of option B, getmicro uses the below values for wget and curl:
$ curl https://getmic.ro | GETMICRO_HTTP="curl -L" sh
$ wget -O- https://getmic.ro | GETMICRO_HTTP="wget -O-" sh
EOA
exit 1
fi
platform=''
machine=$(uname -m)
if [ "${GETMICRO_PLATFORM:-x}" != "x" ]; then
platform="$GETMICRO_PLATFORM"
else
case "$(uname -s | tr '[:upper:]' '[:lower:]')" in
"linux")
case "$machine" in
"arm64"* | "aarch64"* ) platform='linux-arm64' ;;
"arm"* | "aarch"*) platform='linux-arm' ;;
*"86") platform='linux32' ;;
*"64") platform='linux64' ;;
esac
;;
"darwin") platform='osx' ;;
*"freebsd"*)
case "$machine" in
*"86") platform='freebsd32' ;;
*"64") platform='freebsd64' ;;
esac
;;
"openbsd")
case "$machine" in
*"86") platform='openbsd32' ;;
*"64") platform='openbsd64' ;;
esac
;;
"netbsd")
case "$machine" in
*"86") platform='netbsd32' ;;
*"64") platform='netbsd64' ;;
esac
;;
"msys"*|"cygwin"*|"mingw"*|*"_nt"*|"win"*)
case "$machine" in
*"86") platform='win32' ;;
*"64") platform='win64' ;;
esac
;;
esac
fi
if [ "${platform:-x}" = "x" ]; then
cat 1>&2 << 'EOM'
/=====================================\\
| COULD NOT DETECT PLATFORM |
\\=====================================/
Uh oh! We couldn't automatically detect your operating system. You can file a
bug here: https://github.com/benweissmann/getmic.ro
To continue with installation, please choose from one of the following values:
- freebsd32
- freebsd64
- linux-arm
- linux-arm64
- linux32
- linux64
- netbsd32
- netbsd64
- openbsd32
- openbsd64
- osx
- win32
- win64
Export your selection as the GETMICRO_PLATFORM environment variable, and then
re-run this script.
For example:
$ curl https://getmic.ro | GETMICRO_PLATFORM=linux64 sh
EOM
exit 1
else
echo "Detected platform: $platform"
fi
TAG=$(githubLatestTag zyedidia/micro)
if command -v grep >/dev/null 2>&1 ; then
if ! echo "v$TAG" | grep -E '^v[0-9]+[.][0-9]+[.][0-9]+$' >/dev/null 2>&1 ; then
cat 1>&2 << 'EOM'
/=====================================\\
| INVALID TAG RECIEVED |
\\=====================================/
Uh oh! We recieved an invalid tag and cannot be sure that the tag will not break
this script.
Please open an issue on GitHub at https://github.com/benweissmann/getmic.ro with
the invalid tag included:
EOM
echo "> $TAG" 1>&2
exit 1
fi
fi
if [ "${platform:-x}" = "win64" ] || [ "${platform:-x}" = "win32" ]; then
extension='zip'
else
extension='tar.gz'
fi
if [ "${platform:-x}" = "linux64" ]; then
# Detect musl libc (source: https://stackoverflow.com/a/60471114)
libc=$(ldd /bin/ls | grep 'musl' | head -1 | cut -d ' ' -f1)
if [ -n "$libc" ]; then
# Musl libc; use the staticly-compiled versioon
platform='linux64-static'
fi
fi
echo "Latest Version: $TAG"
echo "Downloading https://github.com/zyedidia/micro/releases/download/v$TAG/micro-$TAG-$platform.$extension"
eval "$http 'https://github.com/zyedidia/micro/releases/download/v$TAG/micro-$TAG-$platform.$extension'" > "micro.$extension"
case "$extension" in
"zip") unzip -j "micro.$extension" -d "micro-$TAG" ;;
"tar.gz") tar -xvzf "micro.$extension" "micro-$TAG/micro" ;;
esac
mv "micro-$TAG/micro" ./micro
rm "micro.$extension"
rm -rf "micro-$TAG"
altcmd=""
if command -v alternatives >/dev/null 2>&1 ; then
# RHEL family(?)
altcmd="alternatives"
elif command -v update-alternatives >/dev/null 2>&1 ; then
# Debian family(?)
altcmd="update-alternatives"
fi
doRegister="n"
if [ "${altcmd:-x}" != "x" ] ; then
wrkdir="$(pwd)"
if echo "x${GETMICRO_REGISTER:-x}" | grep -Eqie '^xn(o)?$' 1>/dev/null 2>&1 ; then
doRegister="n"
elif echo "x${GETMICRO_REGISTER:-x}" | grep -Eqie '^xy(es)?$' 1>/dev/null 2>&1 ; then
doRegister="y"
elif [ -t 0 ] || [ -t 2 ] ; then # Check if there is a user viewing this message
cat 1>&2 << 'EOM'
/=====================================\\
| update-alternatives is supported |
\\=====================================/
getmicro can use update-alternatives to register micro as a system text editor.
For example, this will allow `crontab -e` open the cron file with micro.
To enable this feature, define the GETMICRO_REGISTER variable or use the URL
`https://getmic.ro/r`.
Note that you must install micro to a directory accessible to all users when doing
this, typically /usr/bin. cd to that directory before running this script.
E.g.:
$ cd /usr/bin
$ curl https://getmic.ro/r | sudo sh
or
$ su - root -c "cd /usr/bin; wget -O- https://getmic.ro | GETMICRO_REGISTER=y sh"
EOM
doRegister="n"
else
# default to not installing
doRegister="n"
fi
# case-insensitively matches y or yes
if [ "${doRegister:-n}" = "y" ] ; then
# Next, check if we have write permission to /etc/alternatives or other sufficient priviledges
if [ -w /etc/alternatives ] || [ -w /usr/bin/editor ] || (id | grep -Eqe '^uid=0[(]|[(]wheel[)]|[(]root[)]' 1>/dev/null 2>&1) ; then
# Show a status message that indicates what is going on
echo '/====================================='\\
echo '| Registering with update-alternatives |'
echo '\\=====================================/'
echo
# hope we are effectively running as root
echo "Installing '$wrkdir/micro' as /usr/bin/editor..."
$altcmd --install /usr/bin/editor editor "$wrkdir/micro" 80
if command -v git >/dev/null 2>&1 ; then
# set the absolute lowest priority default value of core.editor to be /usr/bin/editor
# note that this git config will error out if the config key is already set
if git config --system --path core.editor /usr/bin/editor >/dev/null 2>&1 ; then
echo "Configuring git to use /usr/bin/editor as the default core editor..."
fi
fi
if [ -w /etc/environment ] && ! grep -qi '^EDITOR=' /etc/environment ; then
# set the absolute lowest priority default value of EDITOR to be /usr/bin/editor
echo "Configuring /etc/environment to use /usr/bin/editor as the default text EDITOR..."
echo 'EDITOR=/usr/bin/editor' >> /etc/environment
fi
echo # pretty print new line to separate sections
else
cat 1>&2 << 'EOM'
/=====================================\\
| INSUFFICIENT PRIVILEGES |
\\=====================================/
Uh oh! We couldn't run update-alternatives due to insufficient privileges.
To continue, try running getmicro as root or another privileged user. Examples:
$ curl https://getmic.ro/r | sudo sh
Or:
$ su - root -c "wget -O- https://getmic.ro | GETMICRO_REGISTER=y sh"
EOM
exit 1
fi
fi
fi
cat <<-'EOM'
__ __ _ ___ _ _ _ _ _
| \/ (_) ___ _ __ ___ |_ _|_ __ ___| |_ __ | | | ___ __| | |
| |\/| | |/ __| '__/ _ \ | || '_ \/ __| __/ _\ | | |/ _ \/ _ | |
| | | | | (__| | | (_) | | || | | \__ \ || (_| | | | __/ (_| |_|
|_| |_|_|\___|_| \___/ |___|_| |_|___/\__\__,_|_|_|\___|\__,_(_)
Micro has been downloaded to the current directory.
You can run it with:
./micro
EOM