-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisericorde.sh
executable file
·170 lines (144 loc) · 5.6 KB
/
misericorde.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
# Enable debug mode if --debug argument is provided
DEBUG=false
if [[ "$1" == "--debug" ]]; then
DEBUG=true
fi
# Function to check if the OS is Debian-based
check_debian_os() {
if ! grep -iq "debian" /etc/os-release; then
zenity --error --title="Incompatible OS" --text="This script is designed to run on Debian-based systems only." --width=300
exit 1
fi
}
# Check if the system is Debian-based
check_debian_os
# Base URL for Discord API and download
DISCORD_API_BASE_URL="https://discord.com/api"
DISCORD_DOWNLOAD_URL="${DISCORD_API_BASE_URL}/download?platform=linux&format=deb"
DISCORD_UPDATE_URL="${DISCORD_API_BASE_URL}/updates?platform=linux&format=deb"
# Function to log messages in debug mode
log_debug() {
if [ "$DEBUG" = true ]; then
echo "$1"
fi
}
# Function to update the Zenity progress bar
update_progress() {
echo "$1" # Progress percentage (0-100)
echo "# $2" # Message to display in progress bar
sleep 1
}
# Function to get the current installed Discord version
get_installed_version() {
log_debug "Checking installed Discord version..."
dpkg-query -W -f='${Version}\n' discord 2>/dev/null | grep -oP '[0-9]+\.[0-9]+\.[0-9]+'
}
# Function to get the latest version of Discord from the update API
get_latest_version() {
log_debug "Fetching latest Discord version from API..."
curl -s "$DISCORD_UPDATE_URL" | grep -oP '"name":\s*"\K[0-9]+\.[0-9]+\.[0-9]+'
}
# Function to send desktop notifications
notify_user() {
notify-send "Discord Update" "$1"
log_debug "Notification sent to user: $1"
}
# Function to check if Discord is running
check_discord_running() {
pgrep Discord > /dev/null
}
# Function to stop Discord gracefully
stop_discord() {
log_debug "Attempting to stop Discord gracefully..."
pkill -SIGTERM Discord # Send SIGTERM to allow Discord to exit gracefully
# Wait for up to 5 seconds for Discord to close on its own
for i in {1..5}; do
if ! pgrep Discord > /dev/null; then
log_debug "Discord closed gracefully."
return 0
fi
sleep 1
done
# If Discord is still running after 5 seconds, force kill it
log_debug "Discord did not close gracefully. Sending SIGKILL..."
pkill -SIGKILL Discord
# Check again if the force kill worked
if pgrep Discord > /dev/null; then
log_debug "Failed to stop Discord even after SIGKILL. Exiting with error."
zenity --error --title="Discord Stop Error" --text="Failed to stop Discord process." --width=300
exit 1
fi
log_debug "Discord forcefully terminated."
}
# Function to download and install Discord with progress
update_discord() {
# Step 1: Downloading Discord
log_debug "Downloading latest Discord version..."
update_progress 50 "Downloading Discord..."
if [ "$DEBUG" = true ]; then
wget -O /tmp/discord.deb "$DISCORD_DOWNLOAD_URL"
else
wget -q -O /tmp/discord.deb "$DISCORD_DOWNLOAD_URL" > /dev/null 2>&1
fi
if [ $? -ne 0 ]; then
zenity --error --title="Discord Update" --text="Failed to download Discord." --width=300
exit 1
fi
# Step 2: Stopping Discord if it's running
if check_discord_running; then
zenity --question --title="Discord Running" --text="Discord is currently running. Do you want to stop it to proceed with the update?" --width=300
if [ $? -eq 0 ]; then
stop_discord
notify_user "Discord has been stopped."
log_debug "Discord has been stopped."
else
zenity --info --title="Update Canceled" --text="Update canceled because Discord is still running." --width=300
log_debug "User canceled the update because Discord is running."
exit 0
fi
fi
# Step 3: Installing Discord
log_debug "Installing downloaded Discord package..."
update_progress 75 "Installing Discord..."
if [ "$DEBUG" = true ]; then
pkexec dpkg -i /tmp/discord.deb
else
pkexec dpkg -i /tmp/discord.deb > /dev/null 2>&1
fi
if [ $? -eq 0 ]; then
log_debug "Discord successfully updated."
update_progress 100 "Discord successfully updated!"
notify_user "Discord has been successfully updated to version $latest_version."
else
zenity --error --title="Discord Update" --text="Failed to install Discord." --width=300
notify_user "Discord update failed."
exit 1
fi
}
# Get the installed and latest versions
installed_version=$(get_installed_version)
latest_version=$(get_latest_version)
# Log the versions for debugging
log_debug "Installed version: $installed_version"
log_debug "Latest version: $latest_version"
# Check if we need to update Discord
if [ "$installed_version" == "$latest_version" ]; then
exit 0
else
# Notify the user that an update is available
notify_user "New Discord version available: $latest_version. You are running version $installed_version."
# Prompt the user with a Zenity confirmation dialog
zenity --question --title="Discord Update Available" --text="Current version: $installed_version\nLatest version: $latest_version\nDo you want to update now?" --width=300
if [ $? -eq 0 ]; then
(
update_progress 25 "Preparing to update Discord..."
update_discord
) | zenity --progress --title="Updating Discord" --width=400 --percentage=0 --auto-close --auto-kill
else
zenity --info --title="Discord Update" --text="Update canceled by the user." --width=300
log_debug "User canceled the update."
notify_user "Discord update was canceled."
exit 0
fi
fi