-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·165 lines (143 loc) · 3.3 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
#!/bin/sh
usage() {
cat <<-USAGE
Usage: ${0##*/} [<option>...] [<path>]
Install exercism client to <path>. Default:
* determined interactively if possible
* /usr/local/bin if run as root
* /usr/local/bin if it is writable
* $HOME/bin otherwise
Options:
-v <version> Install client version <version>. Default: $DEFAULT_VERSION
-o <operating system> Install client for <operating system>. Default: $DEFAULT_OS
-a <architecture> Install client for <architecture>. Default: $DEFAULT_ARCH
USAGE
}
warn() {
echo "${0##*/}: $*" >&2
}
fail() {
warn "$*"
exit 1
}
setup_downloader() {
if command -v curl >/dev/null; then
DOWNLOADER=curl
elif command -v wget >/dev/null; then
DOWNLOADER=wget
else
fail "missing dependencies, please install one of: curl, wget"
fi
}
setup_defaults() {
DEFAULT_DIR="$(default_dir)"
DEFAULT_OS="$(default_os)"
DEFAULT_ARCH="$(default_arch)"
DEFAULT_VERSION="$(latest_version)"
}
default_dir() {
local DIR=/usr/local/bin
if [ "$(id -u)" -eq 0 ]; then
echo "$DIR"
elif [ -d "$DIR" -a -w "$DIR" ]; then
echo "$DIR"
else
echo "$HOME/bin"
fi
}
default_os() {
case "$(uname -s)" in
Darwin) echo "mac";;
*) echo "linux";;
esac
}
default_arch() {
echo "$(getconf LONG_BIT)bit"
}
latest_version() {
follow https://github.com/exercism/cli/releases/latest | filter_version_tag
}
follow() {
case "$DOWNLOADER" in
curl) curl --head --silent "$@";;
wget) wget --server-response --quiet --output-document=/dev/null "$@" 2>&1;;
esac | tr -d '\r'
}
filter_version_tag() {
awk -v FS=/ '/Location:/{print $NF}'
}
parse_options() {
while getopts o:a:v:h OPTNAME; do
case $OPTNAME in
o) OS=$OPTARG;;
a) ARCH=$OPTARG;;
v) VERSION=$OPTARG;;
h) usage; exit;;
*) usage >&2; exit 64;;
esac
done
shift $(expr $OPTIND - 1)
case $# in
0) ;;
1) DIR=$1;;
*) usage >&2; exit 64;;
esac
}
use_defaults() {
: ${DIR:="$(read_directory)"}
: ${DIR:=$DEFAULT_DIR}
: ${OS:=$DEFAULT_OS}
: ${ARCH:=$DEFAULT_ARCH}
: ${VERSION:=$DEFAULT_VERSION}
}
read_directory() {
if [ -t 0 ]; then
read -p "install client into: [$DEFAULT_DIR] " REPLY
if [ -n "$REPLY" ]; then
echo "$REPLY"
else
echo "$DEFAULT_DIR"
fi
fi
}
check_directory() {
check_directory_exists
check_directory_writable
check_directory_in_path
}
check_directory_exists() {
test -d "$DIR" ||
mkdir -p "$DIR" ||
fail "unable to create installation directory $DIR"
}
check_directory_writable() {
test -w "$DIR" ||
fail "unable to write installation directory $DIR"
}
check_directory_in_path() {
echo "$PATH" | tr : "\n" | grep -q "^$DIR$" ||
warn "installation directory $DIR not in PATH"
}
install() {
EXERCISM_FILES="https://github.com/exercism/cli/releases/download/$VERSION/exercism-$OS-$ARCH.tgz"
echo "\nDownloading $EXERCISM_FILES...\n"
download $EXERCISM_FILES | extract "$DIR"
[ $? -eq 0 ] && echo "Installed exercism to $DIR"
}
download() {
case "$DOWNLOADER" in
curl) curl -s --location "$@";;
wget) wget --output-document=- "$@";;
esac ||
fail "failed to download $*"
}
extract() {
tar xz -C "$1" exercism ||
fail "failed to extract exercism"
}
setup_downloader
setup_defaults
parse_options "$@"
use_defaults
check_directory
install