-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·171 lines (151 loc) · 4.66 KB
/
install.sh
File metadata and controls
executable file
·171 lines (151 loc) · 4.66 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
#!/usr/bin/env bash
set -euo pipefail
TOOL_NAME="${TOOL_NAME:-memorph}"
TOOL_ALIAS="${TOOL_ALIAS:-memo}"
REPOSITORY="${REPOSITORY:-ip2a/memorph}"
GITHUB_BASE_URL="${GITHUB_BASE_URL:-https://github.com}"
VERSION_INPUT="${VERSION:-}"
if [ -n "${INSTALL_DIR:-}" ]; then
TARGET_DIR="${INSTALL_DIR}"
elif [ "${EUID:-$(id -u)}" -eq 0 ]; then
TARGET_DIR="/usr/local/bin"
else
TARGET_DIR="${HOME}/.local/bin"
fi
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "[error] Required command not found: $1" >&2
exit 1
fi
}
compute_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
return
fi
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
return
fi
if command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$1" | awk '{print $NF}'
return
fi
echo "[error] No SHA256 tool found (need sha256sum, shasum, or openssl)" >&2
exit 1
}
normalize_version() {
local raw="$1"
raw="${raw#v}"
printf '%s' "$raw"
}
resolve_latest_version() {
local latest_url
latest_url="$(
curl -fsSLI -o /dev/null -w '%{url_effective}' \
"${GITHUB_BASE_URL}/${REPOSITORY}/releases/latest"
)"
local tag="${latest_url##*/}"
if [ -z "${tag}" ] || [ "${tag}" = "latest" ]; then
echo "[error] Unable to resolve latest GitHub release tag" >&2
exit 1
fi
normalize_version "${tag}"
}
detect_platform_id() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "${os}" in
Darwin)
case "${arch}" in
arm64|aarch64) printf 'darwin-arm64' ;;
x86_64) printf 'darwin-x64' ;;
*)
echo "[error] Unsupported macOS architecture: ${arch}" >&2
exit 1
;;
esac
;;
Linux)
case "${arch}" in
x86_64|amd64) printf 'linux-x64-gnu' ;;
arm64|aarch64) printf 'linux-arm64-gnu' ;;
*)
echo "[error] Unsupported Linux architecture: ${arch}" >&2
exit 1
;;
esac
;;
*)
echo "[error] Unsupported operating system: ${os}" >&2
echo "[hint] Download the matching binary manually from GitHub Releases." >&2
exit 1
;;
esac
}
main() {
require_command curl
require_command tar
require_command mktemp
require_command grep
require_command awk
require_command install
local version platform_id tag archive_name download_base_url
if [ -n "${VERSION_INPUT}" ]; then
version="$(normalize_version "${VERSION_INPUT}")"
else
version="$(resolve_latest_version)"
fi
tag="v${version}"
platform_id="$(detect_platform_id)"
archive_name="${TOOL_NAME}-${tag}-${platform_id}.tar.gz"
download_base_url="${GITHUB_BASE_URL}/${REPOSITORY}/releases/download/${tag}"
local archive_path checksum_path expected actual extract_dir installed_bin alias_path
local tmpdir
tmpdir="$(mktemp -d)"
trap 'rm -rf "'"${tmpdir}"'"' EXIT
archive_path="${tmpdir}/${archive_name}"
checksum_path="${tmpdir}/SHA256SUMS.txt"
extract_dir="${tmpdir}/extract"
installed_bin="${TARGET_DIR}/${TOOL_NAME}"
alias_path="${TARGET_DIR}/${TOOL_ALIAS}"
echo "[info] Installing ${TOOL_NAME} ${version} for ${platform_id}"
mkdir -p "${TARGET_DIR}" "${extract_dir}"
curl -fsSL "${download_base_url}/SHA256SUMS.txt" -o "${checksum_path}"
curl -fsSL "${download_base_url}/${archive_name}" -o "${archive_path}"
expected="$(
grep -F " ${archive_name}" "${checksum_path}" | awk '{print $1}'
)"
if [ -z "${expected}" ]; then
echo "[error] Could not find checksum for ${archive_name}" >&2
exit 1
fi
actual="$(compute_sha256 "${archive_path}")"
if [ "${expected}" != "${actual}" ]; then
echo "[error] SHA256 mismatch for ${archive_name}" >&2
exit 1
fi
echo "[ok] Verified SHA256 for ${archive_name}"
tar -xzf "${archive_path}" -C "${extract_dir}" "${TOOL_NAME}"
install -m 0755 "${extract_dir}/${TOOL_NAME}" "${installed_bin}"
if [ -n "${TOOL_ALIAS}" ] && [ "${TOOL_ALIAS}" != "${TOOL_NAME}" ]; then
ln -sfn "${TOOL_NAME}" "${alias_path}"
fi
local installed_version
installed_version="$("${installed_bin}" --version)"
if [[ "${installed_version}" != *"${version}"* ]]; then
echo "[error] Installed binary version mismatch: ${installed_version}" >&2
exit 1
fi
echo "[ok] Installed ${installed_version} to ${installed_bin}"
if [ -n "${TOOL_ALIAS}" ] && [ "${TOOL_ALIAS}" != "${TOOL_NAME}" ]; then
echo "[ok] Installed alias ${TOOL_ALIAS} -> ${TOOL_NAME}"
fi
if [[ ":${PATH}:" != *":${TARGET_DIR}:"* ]]; then
echo "[hint] ${TARGET_DIR} is not in PATH"
echo "[hint] Add this line to your shell profile:"
echo " export PATH=\"${TARGET_DIR}:\$PATH\""
fi
}
main "$@"