Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active September 23, 2024 23:50
Show Gist options
  • Save dayne/40c45988e7ecfd9b74b954e711470b9c to your computer and use it in GitHub Desktop.
Save dayne/40c45988e7ecfd9b74b954e711470b9c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to download and then install latest .deb Discord client.
# Created 2024-01-29 by Dayne Broderson
#
# Script Overview
# 1. Sends a request to the Discord URL to get the headers and extract the
# location header, which contains the final download URL.
# 2. It extracts the desired filename from the final download URL and cleans
# up any trailing whitespace or carriage return characters.
# 3. It checks if the file with the extracted filename already exists in the
# current directory. If it exists, it skips the download.
# 4. If the file doesn't exist, it proceeds to download the file using curl
# and checks the exit code to determine if the download was successful
# or not.
# 5. Finally it prompts user if they want to do the sudo dpkg -i install of the download.
#
# Recommended install dance:
# cd .local/bin # or $HOME/bin # or put in /usr/local/bin
# curl -O https://gist.githubusercontent.com/dayne/40c45988e7ecfd9b74b954e711470b9c/raw/discord-deb-update.sh
# chmod +x discord-deb-update.sh
function dx-discord-deb-update() {
echo "starting dx-discord-deb-update()"
DOWNDIR=/tmp
cd $DOWNDIR
url='https://discord.com/api/download/stable?platform=linux&format=deb'
location=$(curl -sI -L "$url"| grep location)
if [ $? -eq 0 ]; then
echo $location
else
echo "ERROR: unable to get location of latest discord .deb file"
exit 1
fi
filename=$(echo "$location" | awk -F'/' '{print $NF}' | tr -d '\r' )
if [ -f $filename ]; then
echo "File exists: $filename"
echo "Skipping download"
else
echo "Downloading $filename"
curl -JL -o $filename $url
if [ $? -eq 0 ]; then
echo "SUCCESS: Download of $filename complete"
echo "File downloadedto: /tmp/$filename"
else
echo "ERROR: Download of $filename failed."
exist 1
fi
fi
# ensure interactive shell is happening
if [ ! -t 0 ]; then
echo "file downloaded - non interactive shell detected - exiting"
exit
fi
while true; do
read -p "Do you want to run install using dpkg? (Y/N): " answer
case $answer in
[Yy]|[Yy][Ee][Ss])
# Run your command here
echo "Installing $filename"
sudo dpkg -i $DOWNDIR/$filename
if [ $? -eq 0 ]; then
echo "install successful"
exit 0
else
echo "install failed: try yourself with:"
echo "sudo dpkg -i $DOWNDIR/$filename"
exit 1
fi
break
;;
[Nn]|[Nn][Oo])
echo "Exiting..."
exit 0
;;
*)
echo "Invalid input. Please enter Y, N, yes, or no."
;;
esac
done
}
function _dx-discord_check_for_update() {
local version=$(dpkg-query -W -f='${Version}' "discord" 2>/dev/null)
local installed_version="discord-$version.deb"
local url='https://discord.com/api/download/stable?platform=linux&format=deb'
local location=$(curl -sI -L "$url"| grep location)
latest_url=$(curl -sI -L 'https://discord.com/api/download/stable?platform=linux&format=deb' | grep location | awk '{print $2}')
if [ $? -eq 0 ]; then
latest_version="$(basename "$latest_url" | sed 's/[[:space:]]*$//')"
if [ "${latest_version}" == "" ]; then
echo "error: latest_version empty."
exit 1
fi
else
echo "error: curl for latest_version failed to $latest_url"
exit 1
fi
echo "installed_version=[${installed_version}]"
echo "latest_version=[${latest_version}]"
if [ "${installed_version}" == "${latest_version}" ]; then
echo "we are updated -- no discord update needed"
echo "$installed_version == $latest_version"
else
echo "update for discord available"
echo "'${installed_version}' != '${filename}' ...."
dx-discord-deb-update
fi
}
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
# The script is being sourced. Check to see if update is needed.
echo "source logic"
else
echo "execute logic"
fi
_dx-discord_check_for_update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment