-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·150 lines (129 loc) · 3.24 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
#!/bin/bash
set -u
# Check if script is run non-interactively (e.g. CI)
# If it is run non-interactively we should not prompt for passwords.
if [[ ! -t 0 || -n "${CI-}" ]]; then
NONINTERACTIVE=1
fi
# string formatters
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
ohai() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
warn() {
printf "${tty_red}Warning${tty_reset}: %s\n" "$(chomp "$1")"
}
abort() {
printf "${tty_red}$1 ${tty_reset}\n"
exit 1
}
execute() {
if ! "$@"; then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
have_sudo_access() {
local -a args
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A")
elif [[ -n "${NONINTERACTIVE-}" ]]; then
args=("-n")
fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
if [[ -n "${args[*]-}" ]]; then
SUDO="/usr/bin/sudo ${args[*]}"
else
SUDO="/usr/bin/sudo"
fi
if [[ -n "${NONINTERACTIVE-}" ]]; then
${SUDO} -l mkdir &>/dev/null
else
${SUDO} -v && ${SUDO} -l mkdir &>/dev/null
fi
HAVE_SUDO_ACCESS="$?"
fi
if [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
abort "Need sudo access on macOS (e.g. the user $USER to be an Administrator)!"
fi
return "$HAVE_SUDO_ACCESS"
}
execute_sudo() {
local -a args=("$@")
if have_sudo_access; then
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A" "${args[@]}")
fi
ohai "/usr/bin/sudo" "${args[@]}"
execute "/usr/bin/sudo" "${args[@]}"
else
ohai "${args[@]}"
execute "${args[@]}"
fi
}
getc() {
local save_state
save_state=$(/bin/stty -g)
/bin/stty raw -echo
IFS= read -r -n 1 -d '' "$@"
/bin/stty "$save_state"
}
wait_for_user() {
local c
echo
echo "Press RETURN to continue or any other key to abort"
getc c
# we test for \r and \n because some stuff does \r instead
if ! [[ "$c" == $'\r' || "$c" == $'\n' ]]; then
exit 1
fi
}
# First check if the OS is MacOSX
if [[ "$(uname)" = "Darwin" ]]; then
OS_MACOSX=1
else
abort "$(cat <<EOABORT
NativeLib installer only works on MacOSX.
For other OSes you can manually download it and place somewhere your shell find it. See:
${tty_underline}https://github.com/DrMoriarty/nativelib-cli${tty_reset}
EOABORT
)"
fi
if ! command -v curl >/dev/null; then
abort "$(cat <<EOABORT
You must install cURL before installing NativeLib. See:
${tty_underline}https://github.com/DrMoriarty/nativelib-cli${tty_reset}
EOABORT
)"
fi
ohai "$(cat <<EOF
This script will install nativelib into /usr/local/bin directory.
You may be prompted for your password in order to perform install operations.
EOF
)"
wait_for_user
curl -fsSL https://raw.githubusercontent.com/DrMoriarty/nativelib-cli/HEAD/nativelib -o /tmp/nativelib
execute_sudo "mv" "/tmp/nativelib" "/usr/local/bin/"
execute_sudo "chmod" "755" "/usr/local/bin/nativelib"
echo "Run ${tty_bold}nativelib -h${tty_reset} for help"