Repository navigation
Expand file tree
/
Copy pathugu.zsh
More file actions
executable file
·289 lines (225 loc) · 7.45 KB
/
Copy pathugu.zsh
File metadata and controls
executable file
·289 lines (225 loc) · 7.45 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env zsh
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright (c) 2017-2026 Maulik Mistry
#
# ugu - Script to update Ubuntu system and reduce wait.
#
# Author: Maulik Mistry
# Please share support: https://www.paypal.com/paypalme/m1st0
# https://venmo.com/code?user_id=3319592654995456106&created=1753283702
SCRIPT_DIR="${0:A:h}"
source "$SCRIPT_DIR/vendor/tput_shell_colorize/tput_shell_colorize.sh"
# Avoid sudo use directly for more separating logic.
if (( EUID == 0 )); then
messenger_std "Error: Run this script as a normal user, NOT as root/sudo."
exit 1
fi
APP_ROOT="$HOME/my_applications"
TMPDIR="/tmp"
SUDO_HEARTBEAT_PID=""
retry_curl() {
local url="$1"
local attempts=3
local count=0
while [[ $count -lt $attempts ]]; do
curl -LO "$url"
if [[ $? -eq 0 ]]; then
return 0 # Success
fi
count=$((count + 1))
messenger_std "Retrying... ($count/$attempts)"
sleep 2 # Wait before retrying
done
messenger_std "Error: Failed to download $url after $attempts attempts."
return 1 # Failure after retries
}
get_github_latest_version() {
local repo="$1"
local latest_version
latest_version="$(curl -fsSL \
"https://api.github.com/repos/$repo/releases/latest" |
jq -r '.tag_name')"
if [[ -z "$latest_version" || "$latest_version" == "null" ]]; then
return 1
fi
# GitHub projects such as Neovim use tags like v0.11.3.
latest_version="${latest_version#v}"
print -r -- "$latest_version"
}
update_app() (
local app_name="$1"
local app_root="$2"
local app_dir="$app_root"
local app_bin
if [[ -d "$app_dir/$app_name" ]]; then
app_bin="$app_dir/$app_name/$app_name"
messenger_std "Found $app_name at $app_bin."
elif [[ -d "$app_dir/bin/$app_name" ]]; then
app_bin="$app_dir/bin/$app_name"
messenger_std "Found $app_name at $app_dir/bin/$app_name."
else
messenger_std "Cannot find $app_name."
return 1
fi
local download_url="$3"
local github_repo="$4"
local current_version="$($app_bin --version | awk '{print $NF}')"
local latest_version
local final_url
if [[ -n "$github_repo" ]]; then
messenger_std "Checking GitHub version for $app_name..."
if ! latest_version="$(get_github_latest_version "$github_repo")"; then
messenger_std "Error: Failed to retrieve the latest GitHub release for $app_name."
return 1
fi
final_url="$download_url"
else
messenger_std "Checking online version for $app_name..."
if ! final_url="$(curl -fsSL \
--connect-timeout 10 \
--max-time 30 \
--retry 2 \
-o /dev/null \
-w '%{url_effective}' \
"$download_url")"; then
messenger_std "Error: Failed to retrieve the final URL from $download_url."
return 1
fi
local latest_file="${final_url##*/}"
latest_version="$(print -r -- "$latest_file" | grep -oP '[0-9]+(\.[0-9]+)+')"
messenger_std "$app_name: URL check complete (version $latest_version)."
fi
autoload -Uz is-at-least
if is-at-least "$latest_version" "$current_version"; then
messenger_std "$app_name is up to date (version $current_version)."
return 0
fi
messenger_std "Updating $app_name: $current_version → $latest_version"
cd "$TMPDIR" || return 1
retry_curl "$final_url"
if [[ $? -ne 0 ]]; then
return 1
fi
local tarball="${final_url##*/}"
mkdir -p "${tarball%.tar.*}" &&
cd "${tarball%.tar.*}" || return 1
tar -xf "$TMPDIR/$tarball" || return 1
local timestamp="$(date +%s)"
if ! mv "$app_dir/$app_name" "$TMPDIR/${app_name}-backup-$timestamp"; then
messenger_std "Error: Failed to back up $app_name."
return 1
fi
if ! mv "$TMPDIR/${tarball%.tar.*}/$app_name" "$app_dir/$app_name"; then
messenger_std "Error: Failed to install updated $app_name."
return 1
fi
messenger_std "$app_name updated to version $latest_version"
)
check_sudo_run() {
# prompt for sudo once; fail if user cancels
if ! sudo -v; then
messenger_end "Requires sudo privileges."
exit 1
fi
# start background keep-alive to refresh the sudo timestamp
while true; do
sudo -v
sleep 60
done 2>/dev/null &
SUDO_HEARTBEAT_PID=$!
messenger_std "Privileges verified. Sudo heartbeat active (PID: ${SUDO_HEARTBEAT_PID})."
}
end_sudo_run() {
# Check if the heartbeat PID exists and is actively running
if [[ -n "${SUDO_HEARTBEAT_PID}" ]] && kill -0 "${SUDO_HEARTBEAT_PID}" 2>/dev/null; then
messenger_std "Stopping sudo heartbeat loop (PID: ${SUDO_HEARTBEAT_PID})..."
# Terminate the background loop
kill "${SUDO_HEARTBEAT_PID}" 2>/dev/null
# NOTE FOR ZSH: Zsh will complain if you try to 'wait' on a process
# that it knows was forcefully terminated, so redirect stderr here
# to keep the terminal perfectly pristine
wait "${SUDO_HEARTBEAT_PID}" 2>/dev/null
linefeed
messenger_end "Heartbeat stopped cleanly."
linefeed
fi
}
# -------------------------------------------------------
# Optional updates (uncomment as needed)
messenger_std "Starting optional updates..."
linefeed
update_app "firefox" "$APP_ROOT" \
"https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US" &
update_app "thunderbird" "$APP_ROOT" \
"https://download.mozilla.org/?product=thunderbird-latest&os=linux64&lang=en-US" &
update_app "zen" "$APP_ROOT" \
"https://github.com/zen-browser/desktop/releases/latest/download/zen.linux-x86_64.tar.xz" \
"zen-browser/desktop" &
#update_app "nvim" "$APP_ROOT" \
# "https://github.com/neovim/neovim/releases/latest/download/nvim-linux-x86_64.tar.gz" \
# "neovim/neovim" &
wait # Let asynchronous subsheel app updates finalize
#messenger_std "Finding firmware updates..."
# No "sudo" needed
#fwupdmgr get-updates
# Manual for now
#fwupdmgr update
#linefeed
#messenger_std "Updating flatpak. . ."
#flatpak update
messenger_std "Updating rust toolchain. . ."
rustup update
linefeed
messenger_std "Updating uv. . ."
uv self update
linefeed
messenger_std "Updating AstroNvim template configuration..."
git -C $HOME/.config/nvim pull
linefeed
messenger_end "Done with optional updates."
linefeed
# -------------------------------------------------------
# Start a sudo heartbeat for processes that need it
check_sudo_run
messenger_std "Updating snaps. . ."
refresh_output=$(sudo snap refresh 2>&1)
refresh_status=$?
messenger_end "$refresh_output"
if (( refresh_status == 0 )) && [[ "$refresh_output" != "All snaps up to date." ]]; then
python3 "$SCRIPT_DIR/vendor/snap_cleanup/snap_cleanup.py"
fi
linefeed
messenger_end "Done."
linefeed
messenger_std "Updating packages. . ."
sudo apt-fast update
linefeed
messenger_end "Done."
linefeed
update_status=$?
if (( $update_status != 0 )); then
messenger_std "Failed to update package lists."
exit $update_status
fi
# Grep counts non-matching lines, so 0 if no upgrades.
upgrade_count=$(apt list --upgradable 2>/dev/null | grep -vc '^Listing')
if (( $upgrade_count > 0 )); then
messenger_std "Upgrading. . ."
sudo apt-fast full-upgrade
upgrade_exit_code=$?
if (( $upgrade_exit_code != 0 )); then
messenger_std "Failure to apt full-upgrade command."
exit $upgrade_exit_code
fi
linefeed
messenger_std "Cleaning out installed debs. . ."
# Prevent apt lock.
sleep 1
sudo apt clean
messenger_end "Done."
linefeed
else
messenger_std "Nothing to upgrade."
fi
end_sudo_run
messenger_end "Script done."