-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
259 lines (214 loc) · 7.28 KB
/
install.sh
File metadata and controls
259 lines (214 loc) · 7.28 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env bash
set -euo pipefail
REPO_OWNER="lemuray"
REPO_NAME="rustfetch"
BINARY_NAME="rustfetch"
# this lists the most famous fetching tools to detect them in the shell config file
FETCH_TOOLS=(fastfetch screenfetch neofetch pfetch sysfetch hyfetch macchina ufetch nitch)
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
# standardize architecture and OS names to match release naming
case "${ARCH}" in
x86_64|amd64) ARCH="x86_64" ;;
aarch64|arm64) ARCH="aarch64" ;;
*) echo "Unsupported architecture: ${ARCH}" >&2; exit 1 ;;
esac
case "${OS}" in
linux) OS="unknown-linux-gnu" ;;
darwin) OS="apple-darwin" ;;
*) echo "Unsupported OS: ${OS}" >&2; exit 1 ;;
esac
detect_shell_config() {
# detect shell and get its config file
local shell_name
shell_name="$(basename "${SHELL:-}")"
case "${shell_name}" in
bash) echo "${HOME}/.bashrc" ;;
zsh) echo "${HOME}/.zshrc" ;;
fish) echo "${HOME}/.config/fish/config.fish" ;;
*) return 1 ;;
esac
}
detect_shell_name() {
basename "${SHELL:-}"
}
ensure_install_dir_on_path() {
local config_file="$1"
local shell_name="$2"
local install_dir="$3"
# only needed for user-local installs
if [[ "${install_dir}" != "${HOME}/.local/bin" ]]; then
return
fi
if [[ -z "${config_file}" ]]; then
echo "Skipping PATH update (no shell config file found)."
return
fi
mkdir -p "$(dirname "${config_file}")"
touch "${config_file}"
if [[ ! -w "${config_file}" ]]; then
echo "Skipping PATH update (config file not writable): ${config_file}"
return
fi
local path_line
case "${shell_name}" in
fish) path_line='fish_add_path -m "$HOME/.local/bin"' ;;
*) path_line='export PATH="$HOME/.local/bin:$PATH"' ;;
esac
local line
while IFS= read -r line || [[ -n "${line}" ]]; do
if [[ "${line}" == "${path_line}" ]]; then
return
fi
# broader check to avoid adding duplicates when a different but valid PATH
# expression already includes ~/.local/bin
if [[ "${shell_name}" == "fish" ]]; then
if [[ "${line}" == *".local/bin"* ]] && [[ "${line}" == *"fish_add_path"* || "${line}" == *"set"*"PATH"* ]]; then
return
fi
else
if [[ "${line}" == *".local/bin"* ]] && [[ "${line}" == *"PATH"* ]]; then
return
fi
fi
done < "${config_file}"
printf '\n%s\n' "${path_line}" >> "${config_file}"
echo "Added ~/.local/bin to PATH in ${config_file}."
}
prompt_yes_no() {
local prompt="$1"
local reply
# read from /dev/tty so prompts work even when the script is piped.
# in case it is not available default to no not to silently modify
# shell config files
if [[ ! -t 0 ]] && [[ ! -e /dev/tty ]]; then
echo "${prompt} [Y/n] (non-interactive, defaulting to no)"
return 1
fi
while true; do
read -r -p "${prompt} [Y/n] " reply < /dev/tty
reply="$(printf '%s' "${reply}" | tr '[:upper:]' '[:lower:]')"
case "${reply}" in
""|y|yes) return 0 ;;
n|no) return 1 ;;
*) echo "Invalid input" ;;
esac
done
}
is_standalone_fetch_line() {
# regex vodoo magic to make sure the fetching tool we are detecting is a
# standalone line and is not in some complicated logic, if it is, we skip
# the automated shell integration
local line="$1"
local cmd="$2"
local trimmed
trimmed="${line#"${line%%[!$' \t']*}"}"
trimmed="${trimmed%"${trimmed##*[!$' \t']}"}"
# ignore empty lines and comments
[[ -z "${trimmed}" ]] && return 1
[[ "${trimmed}" == \#* ]] && return 1
# if there's an inline comment, we shouldn't be touching it
[[ "${trimmed}" == *"#"* ]] && return 1
# check for any shell operator, in case there is one the user must have had a reason for it,
# skip this one
if [[ "${trimmed}" =~ [\;\|\&\<\>\`\$\(\)] ]]; then
return 1
fi
# match the standalone command with flags if present
if [[ "${trimmed}" =~ ^${cmd}([[:space:]]+--?[[:alnum:]_-]+(=[^[:space:]]+)*)*$ ]]; then
return 0
fi
return 1
}
update_shell_config() {
local config_file="$1"
if [[ -z "${config_file}" || ! -f "${config_file}" ]]; then
echo "Skipping shell integration (no config file found)."
return
fi
if [[ ! -w "${config_file}" ]]; then
echo "Skipping shell integration (config file not writable): ${config_file}"
return
fi
local -a lines=()
local line
local line_no=0
# read file into an array so we can do inner edits and write back once
while IFS= read -r line || [[ -n "${line}" ]]; do
line_no=$((line_no + 1))
lines+=("${line}")
done < "${config_file}"
local found=0
local changed=0
local idx cmd
# scan each line for standalone fetch commands, prompt at each match
for idx in "${!lines[@]}"; do
line="${lines[$idx]}"
for cmd in "${FETCH_TOOLS[@]}"; do
if is_standalone_fetch_line "${line}" "${cmd}"; then
found=1
if prompt_yes_no "${cmd} found in ${config_file} at line $((idx + 1)), would you like to replace it with rustfetch?"; then
local indent
# preserve indentation
indent="${line%%[!$' \t']*}"
lines[$idx]="${indent}rustfetch"
changed=1
echo "Replaced ${cmd} with rustfetch at line $((idx + 1)) in ${config_file}."
else
echo "Skipping shell integration in ${config_file}."
fi
break
fi
done
done
# if no fetching CLI tool is present, ask to append rustfetch
if [[ "${found}" -eq 0 ]]; then
if prompt_yes_no "No other fetching tool was found in ${config_file}, would you like rustfetch to run automatically when opening a terminal?"; then
lines+=("rustfetch")
changed=1
echo "Added rustfetch to ${config_file}."
else
echo "Skipping shell integration in ${config_file}."
fi
fi
# only rewrite the file if we changed anything
if [[ "${changed}" -eq 1 ]]; then
local tmp_file
tmp_file="$(mktemp)"
printf '%s\n' "${lines[@]}" > "${tmp_file}"
mv "${tmp_file}" "${config_file}"
fi
}
ASSET="${BINARY_NAME}-${ARCH}-${OS}.tar.gz"
# fetch latest release
LATEST_TAG="$(curl -fsSL "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases/latest" | \
grep -E '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')"
if [[ -z "${LATEST_TAG}" ]]; then
echo "Could not determine latest release tag." >&2
exit 1
fi
TMP_DIR="$(mktemp -d)"
curl -fsSL "https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${LATEST_TAG}/${ASSET}" \
-o "${TMP_DIR}/${ASSET}"
tar -xzf "${TMP_DIR}/${ASSET}" -C "${TMP_DIR}"
# fnstall to /usr/local/bin (fallback: ~/.local/bin)
INSTALL_DIR="/usr/local/bin"
if [[ ! -w "${INSTALL_DIR}" ]]; then
INSTALL_DIR="${HOME}/.local/bin"
mkdir -p "${INSTALL_DIR}"
fi
install -m 0755 "${TMP_DIR}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
echo "Installed ${BINARY_NAME} to ${INSTALL_DIR}"
echo "Run: ${BINARY_NAME} --help"
# optional shell integration to replace or add rustfetch on terminal start
if CONFIG_FILE="$(detect_shell_config)"; then
SHELL_NAME="$(detect_shell_name)"
ensure_install_dir_on_path "${CONFIG_FILE}" "${SHELL_NAME}" "${INSTALL_DIR}"
update_shell_config "${CONFIG_FILE}"
else
if [[ "${INSTALL_DIR}" == "${HOME}/.local/bin" ]]; then
echo "Installed to ~/.local/bin; add this to your shell config if needed:"
echo ' export PATH="$HOME/.local/bin:$PATH"'
fi
echo "Skipping shell integration (unsupported shell)."
fi