-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·219 lines (188 loc) · 5.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·219 lines (188 loc) · 5.2 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
#!/bin/sh
set -eu
repo="${CODETRAIL_REPO:-mars167/CodeTrail}"
version="${CODETRAIL_VERSION:-latest}"
install_dir="${CODETRAIL_INSTALL_DIR:-$HOME/.local/bin}"
dry_run="${CODETRAIL_DRY_RUN:-0}"
usage() {
cat <<'USAGE'
Install codetrail from GitHub release assets.
Usage:
install.sh [--version <tag>] [--repo <owner/repo>] [--install-dir <dir>] [--dry-run]
Environment:
CODETRAIL_VERSION Release tag to install, defaults to latest.
CODETRAIL_REPO GitHub repository, defaults to mars167/CodeTrail.
CODETRAIL_INSTALL_DIR Destination directory, defaults to $HOME/.local/bin.
CODETRAIL_OS Override detected OS for tests.
CODETRAIL_ARCH Override detected architecture for tests.
CODETRAIL_DRY_RUN Set to 1 to print selected asset without downloading.
USAGE
}
while [ "$#" -gt 0 ]; do
case "$1" in
--version)
[ "$#" -ge 2 ] || { echo "Missing value for --version" >&2; exit 1; }
version="$2"
shift 2
;;
--repo)
[ "$#" -ge 2 ] || { echo "Missing value for --repo" >&2; exit 1; }
repo="$2"
shift 2
;;
--install-dir)
[ "$#" -ge 2 ] || { echo "Missing value for --install-dir" >&2; exit 1; }
install_dir="$2"
shift 2
;;
--dry-run)
dry_run=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing required command: $1" >&2
exit 1
fi
}
download() {
url="$1"
output="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$output"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$output" "$url"
else
echo "Missing required command: curl or wget" >&2
exit 1
fi
}
checksum() {
file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | awk '{print $1}'
else
echo "Missing required command: sha256sum or shasum" >&2
exit 1
fi
}
os_name="${CODETRAIL_OS:-$(uname -s)}"
arch_name="${CODETRAIL_ARCH:-$(uname -m)}"
case "$os_name" in
Darwin) os="darwin" ;;
Linux) os="linux" ;;
*)
echo "Unsupported OS: $os_name" >&2
exit 1
;;
esac
case "$arch_name" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*)
echo "Unsupported architecture: $arch_name" >&2
exit 1
;;
esac
case "${os}-${arch}" in
darwin-amd64|darwin-arm64|linux-amd64|linux-arm64)
asset="codetrail-${os}-${arch}.tar.gz"
;;
*)
echo "Unsupported platform: ${os}-${arch}" >&2
exit 1
;;
esac
if [ "$version" = "latest" ]; then
base_url="https://github.com/${repo}/releases/latest/download"
else
base_url="https://github.com/${repo}/releases/download/${version}"
fi
if [ "$dry_run" = "1" ]; then
printf 'repo=%s\nversion=%s\nasset=%s\ninstall_dir=%s\nurl=%s/%s\n' \
"$repo" "$version" "$asset" "$install_dir" "$base_url" "$asset"
exit 0
fi
need_cmd awk
need_cmd tar
need_cmd mktemp
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT INT TERM
asset_path="$tmp_dir/$asset"
checksums_path="$tmp_dir/SHA256SUMS"
echo "Downloading ${asset}..."
download "${base_url}/${asset}" "$asset_path"
download "${base_url}/SHA256SUMS" "$checksums_path"
expected="$(awk -v file="$asset" '$2 == file { print $1 }' "$checksums_path")"
if [ -z "$expected" ]; then
echo "Checksum for ${asset} was not found in SHA256SUMS." >&2
exit 1
fi
actual="$(checksum "$asset_path")"
if [ "$expected" != "$actual" ]; then
echo "Checksum mismatch for ${asset}." >&2
echo "expected: $expected" >&2
echo "actual: $actual" >&2
exit 1
fi
extract_dir="$tmp_dir/extract"
mkdir -p "$extract_dir"
tar -xzf "$asset_path" -C "$extract_dir"
if [ ! -f "$extract_dir/codetrail" ]; then
echo "Release archive did not contain codetrail." >&2
exit 1
fi
mkdir -p "$install_dir"
cp "$extract_dir/codetrail" "$install_dir/codetrail"
chmod +x "$install_dir/codetrail"
echo "Installed codetrail to ${install_dir}/codetrail"
# Refresh shell command hash so a newly installed binary is visible immediately.
hash -r 2>/dev/null || true
if command -v codetrail >/dev/null 2>&1; then
echo "codetrail is ready to use."
exit 0
fi
install_dir_in_path=false
case ":$PATH:" in
*":$install_dir:"*) install_dir_in_path=true ;;
esac
if $install_dir_in_path; then
echo "Run 'hash -r' or open a new terminal for codetrail to be available."
exit 0
fi
shell_name="$(basename "${SHELL:-/bin/sh}")"
rc_file=""
case "$shell_name" in
zsh) rc_file="${ZDOTDIR:-$HOME}/.zshrc" ;;
bash)
if [ -f "$HOME/.bash_profile" ]; then
rc_file="$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
rc_file="$HOME/.bashrc"
else
rc_file="$HOME/.bashrc"
fi
;;
*) rc_file="$HOME/.profile" ;;
esac
if grep -qF "export PATH=\"$install_dir" "$rc_file" 2>/dev/null; then
echo "PATH entry already exists in $rc_file."
else
printf '\n# Added by codetrail installer\n' >> "$rc_file"
printf 'export PATH="%s:$PATH"\n' "$install_dir" >> "$rc_file"
echo "Added $install_dir to PATH in $rc_file"
fi
echo "Run 'source $rc_file' or open a new terminal to use codetrail."