Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·222 lines (195 loc) · 6.07 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·222 lines (195 loc) · 6.07 KB
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
#!/bin/sh
set -eu
repository="XIAZY/wcctl"
version="latest"
default_install_directory=""
if [ -n "${HOME:-}" ]; then
default_install_directory="${HOME}/.local/bin"
fi
install_directory="${WCCTL_INSTALL_DIR:-${default_install_directory}}"
usage() {
cat <<'EOF'
Install the latest wcctl binary release for macOS.
Usage: install.sh [--dir DIRECTORY] [--version VERSION]
Options:
--dir DIRECTORY Install directory (default: ~/.local/bin)
--version VERSION Install a specific release, such as v0.0.1
-h, --help Show this help
Environment:
WCCTL_INSTALL_DIR Alternative install directory
WCCTL_ARCH Override architecture: amd64 or arm64
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
--dir)
[ "$#" -ge 2 ] || { echo "--dir requires a directory" >&2; exit 2; }
install_directory="$2"
shift 2
;;
--version)
[ "$#" -ge 2 ] || { echo "--version requires a release tag" >&2; exit 2; }
version="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
if [ -z "${install_directory}" ]; then
echo "Install directory cannot be empty. Set HOME or use --dir." >&2
exit 2
fi
if [ "$(uname -s)" != "Darwin" ]; then
echo "wcctl binary releases currently support macOS only." >&2
exit 1
fi
for command_name in awk curl grep install mkdir mktemp shasum tar uname; do
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "Required command not found: ${command_name}" >&2
exit 1
fi
done
architecture="${WCCTL_ARCH:-}"
if [ -z "${architecture}" ]; then
architecture="$(uname -m)"
if [ "${architecture}" = "x86_64" ] \
&& [ "$(sysctl -in hw.optional.arm64 2>/dev/null || true)" = "1" ]; then
architecture="arm64"
fi
case "${architecture}" in
arm64|aarch64) architecture="arm64" ;;
x86_64|amd64) architecture="amd64" ;;
*)
echo "Unsupported Mac architecture: ${architecture}" >&2
exit 1
;;
esac
fi
case "${architecture}" in
amd64|arm64) ;;
*)
echo "Unsupported WCCTL_ARCH: ${architecture}" >&2
exit 1
;;
esac
case "${version}" in
latest)
release_base="https://github.com/${repository}/releases/latest/download"
;;
v*)
release_base="https://github.com/${repository}/releases/download/${version}"
;;
*)
version="v${version}"
release_base="https://github.com/${repository}/releases/download/${version}"
;;
esac
archive_name="wcctl-darwin-${architecture}.tar.gz"
temporary_directory="$(mktemp -d "${TMPDIR:-/tmp}/wcctl-install.XXXXXX")"
cleanup() {
rm -rf "${temporary_directory}"
}
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
download() {
curl --fail --location --proto '=https' --tlsv1.2 --retry 3 \
--silent --show-error --output "$2" "$1"
}
echo "Downloading wcctl ${version} for macOS/${architecture} ..."
download "${release_base}/${archive_name}" "${temporary_directory}/${archive_name}"
download "${release_base}/SHA256SUMS" "${temporary_directory}/SHA256SUMS"
expected_checksum="$(
awk -v archive="${archive_name}" '$2 == archive { print $1 }' \
"${temporary_directory}/SHA256SUMS"
)"
if ! printf '%s\n' "${expected_checksum}" | grep -Eq '^[0-9a-fA-F]{64}$'; then
echo "No valid checksum found for ${archive_name}." >&2
exit 1
fi
actual_checksum="$(shasum -a 256 "${temporary_directory}/${archive_name}" | awk '{ print $1 }')"
if [ "${actual_checksum}" != "${expected_checksum}" ]; then
echo "Checksum verification failed for ${archive_name}." >&2
exit 1
fi
mkdir "${temporary_directory}/extracted"
tar -xzf "${temporary_directory}/${archive_name}" \
-C "${temporary_directory}/extracted"
binary="${temporary_directory}/extracted/wcctl"
if [ ! -f "${binary}" ]; then
echo "Release archive does not contain wcctl." >&2
exit 1
fi
if [ ! -d "${install_directory}" ]; then
if ! mkdir -p "${install_directory}" 2>/dev/null; then
if command -v sudo >/dev/null 2>&1; then
echo "Creating ${install_directory} with administrator access ..."
sudo mkdir -p "${install_directory}"
else
echo "Cannot create ${install_directory}. Choose a writable directory with --dir." >&2
exit 1
fi
fi
fi
destination="${install_directory}/wcctl"
if [ -w "${install_directory}" ]; then
install -m 0755 "${binary}" "${destination}"
elif command -v sudo >/dev/null 2>&1; then
echo "Installing to ${install_directory} with administrator access ..."
sudo install -m 0755 "${binary}" "${destination}"
else
echo "Cannot write to ${install_directory}. Choose a writable directory with --dir." >&2
exit 1
fi
echo "Installed wcctl to ${destination}"
path_contains_directory() {
case ":${PATH:-}:" in
*:"$1":*) return 0 ;;
*) return 1 ;;
esac
}
if ! path_contains_directory "${install_directory}"; then
if [ "${install_directory}" = "${default_install_directory}" ] \
&& [ -n "${HOME:-}" ]; then
shell_path="${SHELL:-}"
shell_name="${shell_path##*/}"
case "${shell_name}" in
bash) shell_config="${HOME}/.bashrc" ;;
zsh) shell_config="${HOME}/.zshrc" ;;
*)
if [ -f "${HOME}/.zshrc" ] || [ ! -f "${HOME}/.bashrc" ]; then
shell_config="${HOME}/.zshrc"
else
shell_config="${HOME}/.bashrc"
fi
;;
esac
path_entry='export PATH="$HOME/.local/bin:$PATH"'
path_configured=true
if [ ! -f "${shell_config}" ] \
|| ! grep -Fqx "${path_entry}" "${shell_config}"; then
if printf '\n%s\n' "${path_entry}" >> "${shell_config}"; then
echo "Added ${install_directory} to PATH in ${shell_config}"
else
path_configured=false
echo "Warning: could not update ${shell_config}." >&2
echo "Add ${install_directory} to PATH manually." >&2
fi
else
echo "${install_directory} is already configured in ${shell_config}"
fi
if [ "${path_configured}" = true ]; then
echo "Open a new terminal for the PATH change to take effect."
fi
else
echo "Warning: ${install_directory} is not in PATH." >&2
echo "Add it to your shell configuration before running wcctl by name." >&2
fi
fi