-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSnapUpgradeFirefoxChromium.sh
executable file
·151 lines (111 loc) · 4.25 KB
/
SnapUpgradeFirefoxChromium.sh
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
#!/bin/bash
# Script version 1.00.
#
# This script is a workaround for this annoying prompt under Ubuntu 22.04:
# Pending update of "firefox" snap
# Close the app to avoid disruptions (4 days left)
# Plenty of people on the Internet hat written about this usability issue.
#
# This script upgrades the Snaps for both Firefox and Chromium.
# You may have to adjust it for your needs.
#
# Example desktop launchers:
# - This example uses 'tee' to record the output of the process inside the new console:
# "/home/rdiez/rdiez/Tools/RunInNewConsole/run-in-new-console.sh" --console-title="Snap Upgrade Firefox and Chromium" -- "\"$HOME/rdiez/Tools/SnapUpgradeFirefoxChromium/SnapUpgradeFirefoxChromium.sh\" 2>&1 | tee --output-error=exit -- \"$HOME/SnapUpgradeFirefoxChromium.log\""
# - Simple version:
# "/home/rdiez/rdiez/Tools/RunInNewConsole/run-in-new-console.sh" --console-title="Snap Upgrade Firefox and Chromium" -- "\"$HOME/rdiez/Tools/SnapUpgradeFirefoxChromium/SnapUpgradeFirefoxChromium.sh\""
#
# Suggested icon for the desktop launcher:
# https://github.com/ubuntu/yaru/blob/master/icons/src/fullcolor/default/apps/snap-store.svg
# You will have to remove the small icon versions with Inkscape, and then under "Document Properties",
# choose "Resize page to content...".
#
# You probably want to add a line like this to a file under /etc/sudoers.d :
# %sudo ALL=(root) NOPASSWD: /usr/bin/snap refresh firefox chromium
# Copyright (c) 2022 R. Diez
# Licensed under the GNU Affero General Public License version 3.
set -o errexit
set -o nounset
set -o pipefail
declare -r -i EXIT_CODE_ERROR=1
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit "$EXIT_CODE_ERROR"
}
verify_tool_is_installed ()
{
local TOOL_NAME="$1"
local DEBIAN_PACKAGE_NAME="$2"
command -v "$TOOL_NAME" >/dev/null 2>&1 || abort "Tool '$TOOL_NAME' is not installed. You may have to install it with your Operating System's package manager. For example, under Ubuntu/Debian the corresponding package is called \"$DEBIAN_PACKAGE_NAME\"."
}
prompt_for_confirmation ()
{
local MSG="There are running instances of Firefox or Chromium. Kill them?"
local OK_BUTTON_CAPTION="Kill running instances"
# Unfortunately, there is no way to set the cancel button to be the default.
local CMD
printf -v CMD \
"%q --no-markup --image dialog-question --title %q --text %q --button=%q:0 --button=gtk-cancel:1" \
"$TOOL_YAD" \
"Snap Upgrade Confirmation" \
"$MSG" \
"$OK_BUTTON_CAPTION!gtk-ok"
if false; then
echo "$CMD"
fi
set +o errexit
eval "$CMD"
local CMD_EXIT_CODE="$?"
set -o errexit
case "$CMD_EXIT_CODE" in
0) DID_USER_CANCEL=false ;;
1|252) # If the user presses the ESC key, or closes the window, YAD yields an exit code of 252.
DID_USER_CANCEL=true ;;
*) abort "Unexpected exit code $CMD_EXIT_CODE from \"$TOOL_YAD\"." ;;
esac
}
# ----------- Entry point -----------
if (( $# != 0 )); then
abort "This script takes no command-line arguments."
fi
declare -r TOOL_YAD="yad"
verify_tool_is_installed "$TOOL_YAD" "yad"
echo "Checking whether there are running instances of Firefox or Chromium..."
declare -r PROCESS_REGEX="^(firefox|chrome)\$"
set +o errexit
pgrep --list-name -- "$PROCESS_REGEX" >/dev/null
PGREP_EXIT_CODE="$?"
set -o errexit
DID_USER_CANCEL=false
KILL_PROCESSES=false
case "$PGREP_EXIT_CODE" in
0) echo "There are running instances. Prompting the user for confirmation..."
prompt_for_confirmation
KILL_PROCESSES=true
;;
1) echo "There are no running instances."
;;
*) abort "Unexpected exit code of $PGREP_EXIT_CODE from 'pgrep'."
esac
if $DID_USER_CANCEL; then
echo "The user cancelled the operation."
else
if $KILL_PROCESSES; then
echo "Killing running instances of Firefox and Chromium..."
set +o errexit
pkill --signal=SIGTERM -- "$PROCESS_REGEX" >/dev/null
PKILL_EXIT_CODE="$?"
set -o errexit
case "$PKILL_EXIT_CODE" in
0|1) # Nothing to do here.
;;
*) abort "Unexpected exit code of $PKILL_EXIT_CODE from 'pkill'."
esac
fi
echo "Upgrading Snap packages..."
CMD="sudo snap refresh firefox chromium"
echo "$CMD"
eval "$CMD"
echo "Finished upgrading the Snap browser packages."
fi