-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.sh
201 lines (178 loc) · 4.7 KB
/
init.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
#!/bin/sh
# Copyright 2017 Dominik Picheta and Nim developers.
#
# Licensed under the BSD-3-Clause license.
#
# This script performs some platform detection, downloads the latest version
# of choosenim and initiates its installation.
set -u
set -e
url_prefix="https://github.com/dom96/choosenim/releases/download/"
temp_prefix="${TMPDIR:-/tmp}"
CHOOSE_VERSION="${CHOOSENIM_CHOOSE_VERSION:-stable}"
need_tty=yes
debug=""
has_curl() {
command -v curl >/dev/null 2>&1
}
has_wget() {
command -v wget >/dev/null 2>&1
}
install() {
get_platform || return 1
local platform=$RET_VAL
local stable_version=
if has_curl; then
stable_version=`curl -sSfL https://nim-lang.org/choosenim/stable`
elif has_wget; then
stable_version=`wget -qO - https://nim-lang.org/choosenim/stable`
fi
local filename="choosenim-$stable_version"_"$platform"
local url="$url_prefix"v"$stable_version/$filename"
local ext=""
case $platform in
*macosx_amd64* | *linux_amd64* )
;;
*windows_amd64* )
# Download ZIP for Windows
local ext=".exe"
local filename="$filename$ext"
local url="$url$ext"
;;
* )
say_err "Sorry, your platform ($platform) is not supported by choosenim."
say_err "You will need to install Nim using an alternative method."
say_err "See the following link for more info: https://nim-lang.org/install.html"
exit 1
;;
esac
say "Downloading $filename"
if has_curl; then
curl -sSfL "$url" -o "$temp_prefix/$filename"
elif has_wget; then
wget -qO "$temp_prefix/$filename" "$url"
fi
chmod +x "$temp_prefix/$filename"
if [ "$need_tty" = "yes" ]; then
# The installer is going to want to ask for confirmation by
# reading stdin. This script was piped into `sh` though and
# doesn't have stdin to pass to its children. Instead we're going
# to explicitly connect /dev/tty to the installer's stdin.
if [ ! -t 1 ]; then
err "Unable to run interactively. Run with -y to accept defaults."
fi
# Install Nim from desired channel.
"$temp_prefix/$filename" $CHOOSE_VERSION --firstInstall ${debug} < /dev/tty
else
"$temp_prefix/$filename" $CHOOSE_VERSION --firstInstall -y ${debug}
fi
# Copy choosenim binary to Nimble bin.
local nimbleBinDir=`"$temp_prefix/$filename" --getNimbleBin`
cp "$temp_prefix/$filename" "$nimbleBinDir/choosenim$ext"
say "ChooseNim installed in $nimbleBinDir"
say "You must now ensure that the Nimble bin dir is in your PATH."
if [ "$platform" != "windows_amd64" ]; then
say "Place the following line in the ~/.profile or ~/.bashrc file."
say " export PATH=$nimbleBinDir:\$PATH"
case "${SHELL:=sh}" in
*fish*)
say "Running fish shell?"
say "set -ga fish_user_paths $nimbleBinDir"
;;
esac
fi
}
get_platform() {
# Get OS/CPU info and store in a `myos` and `mycpu` variable.
local ucpu=`uname -m`
local uos=`uname`
local ucpu=`echo $ucpu | tr "[:upper:]" "[:lower:]"`
local uos=`echo $uos | tr "[:upper:]" "[:lower:]"`
case $uos in
*linux* )
local myos="linux"
;;
*dragonfly* )
local myos="freebsd"
;;
*freebsd* )
local myos="freebsd"
;;
*openbsd* )
local myos="openbsd"
;;
*netbsd* )
local myos="netbsd"
;;
*darwin* )
local myos="macosx"
if [ "$HOSTTYPE" = "x86_64" ] ; then
local ucpu="amd64"
fi
if [ "$HOSTTYPE" = "arm64" ] ; then
# TODO: We don't have arm binaries for choosenim so far, rely on Rosetta.
local ucpu="amd64"
fi
;;
*aix* )
local myos="aix"
;;
*solaris* | *sun* )
local myos="solaris"
;;
*haiku* )
local myos="haiku"
;;
*mingw* | *msys* )
local myos="windows"
;;
*)
err "unknown operating system: $uos"
;;
esac
case $ucpu in
*i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
local mycpu="i386" ;;
*amd*64* | *x86-64* | *x86_64* )
local mycpu="amd64" ;;
*sparc*|*sun* )
local mycpu="sparc"
if [ "$(isainfo -b)" = "64" ]; then
local mycpu="sparc64"
fi
;;
*ppc64* )
local mycpu="powerpc64" ;;
*power*|*ppc* )
local mycpu="powerpc" ;;
*mips* )
local mycpu="mips" ;;
*arm*|*armv6l* )
local mycpu="arm" ;;
*aarch64* )
local mycpu="arm64" ;;
*)
err "unknown processor: $ucpu"
;;
esac
RET_VAL="$myos"_"$mycpu"
}
say() {
echo "choosenim-init: $1"
}
say_err() {
say "Error: $1" >&2
}
err() {
say_err "$1"
exit 1
}
# check if we have to use /dev/tty to prompt the user
while getopts "dy" opt; do
case "$opt" in
y) need_tty=no
;;
d) debug="--debug"
esac
done
install