-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh
165 lines (135 loc) · 3.27 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/sh
# shellcheck shell=dash
set -e
set -o noglob
GUARDLLAMA_DOWNLOAD_URL="${GUARDLLAMA_DOWNLOAD_URL:-https://release.guardllama.net/latest}"
GUARDLLAMA_DESTIATION_DIR="${GUARDLLAMA_DESTIATION_DIR:-/usr/local/bin}"
main() {
downloader --check
need_cmd uname
need_cmd tar
need_cmd mktemp
need_cmd chown
need_cmd cp
get_architecture || return 1
local _arch="$RETVAL"
local _file="guardllama_${_arch}.tar.gz"
local _url="${GUARDLLAMA_DOWNLOAD_URL}/${_file}"
local _destdir="${GUARDLLAMA_DESTIATION_DIR}"
local _guardllama_dest="${_destdir}/glm-installer"
local _tmpdir
_tmpdir="$(mktemp -d -t guardllama-install.XXXXXXXXXX)"
if [ "${SUDO}" = "false" ]; then
SUDO=
else
SUDO=sudo
fi
if [ "$(id -u)" -eq 0 ]; then
SUDO=
fi
ensure downloader "${_url}" "${_tmpdir}/${_file}"
ensure tar -xf "${_tmpdir}/${_file}" --directory "${_tmpdir}"
[ -n "${SUDO}" ] && ensure ${SUDO} chown -R root:root "${_tmpdir}/bin"
ensure ${SUDO} cp -r "${_tmpdir}/bin/." "${_destdir}/"
ensure printf "Installing with %s\n" "$(${_guardllama_dest} version)"
ignore "${_guardllama_dest}" install "$@"
local _retval=$?
return "$_retval"
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
"$@"
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
say() {
printf 'install: %s\n' "$1"
}
err() {
say "$1" >&2
exit 1
}
# This wraps curl or wget. Try curl first, if not installed,
# use wget instead.
downloader() {
local _dld
if check_cmd curl; then
_dld=curl
elif check_cmd wget; then
_dld=wget
else
_dld='curl or wget' # to be used in error message of need_cmd
fi
if [ "$1" = --check ]; then
need_cmd "$_dld"
elif [ "$_dld" = curl ]; then
if [ -z "$2" ]; then
curl --silent --show-error --fail --location "$1"
else
curl --silent --show-error --fail --location "$1" --output "$2"
fi
elif [ "$_dld" = wget ]; then
if [ -z "$2" ]; then
wget "$1"
else
wget "$1" -O "$2"
fi
else
err "Unknown downloader" # should not reach here
fi
}
get_architecture() {
local _ostype _cputype _arch
_ostype="$(uname -s)"
_cputype="$(uname -m)"
if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then
# Darwin `uname -m` lies
if sysctl hw.optional.x86_64 | grep -q ': 1'; then
_cputype=x86_64
fi
fi
case "$_ostype" in
Linux)
_ostype=linux
;;
Darwin)
_ostype=darwin
;;
*)
err "unrecognized OS type: $_ostype"
;;
esac
case "$_cputype" in
i386 | i486 | i686 | i786 | x86)
_cputype=386
;;
xscale | arm | armv6l | armv7l |armv8l)
_cputype=arm
;;
aarch64 | arm64)
_cputype=arm64
;;
x86_64 | x86-64 | x64 | amd64)
_cputype=amd64
;;
*)
err "unknown CPU type: $_cputype"
esac
_arch="${_ostype}_${_cputype}"
RETVAL="$_arch"
}
main "$@" || exit 1