Skip to content

Instantly share code, notes, and snippets.

@iitalics
Last active February 10, 2024 21:20
Show Gist options
  • Save iitalics/9e9fa435fdae77ff5e621e1b264da4f6 to your computer and use it in GitHub Desktop.
Save iitalics/9e9fa435fdae77ff5e621e1b264da4f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# ~/.local/libexec/discord-update.bash
which tar >/dev/null 2>/dev/null || (echo "missing command: 'tar'"; exit 1)
which wget >/dev/null 2>/dev/null || (echo "missing command: 'wget'"; exit 1)
which curl >/dev/null 2>/dev/null || (echo "missing command: 'curl'"; exit 1)
which jq >/dev/null 2>/dev/null || (echo "missing command: 'jq'"; exit 1)
if [[ "$EUID" == 0 ]]; then
DEFAULT_PREFIX=/usr/local
else
DEFAULT_PREFIX=$HOME/.local
fi
PREFIX=${PREFIX:-$DEFAULT_PREFIX}
SH=$PREFIX/share
D=$SH/discord
DEFAULT_DL_URL="https://discord.com/api/download?platform=linux&format=tar.gz"
DL_URL=${DL_URL:-$DEFAULT_DL_URL}
function notify() {
if [[ -n "$NOTIFY_SEND" ]]; then
NS_ID=$($NOTIFY_SEND -p $NS_REPLACE -a "Discord Update" "$1")
NS_REPLACE="-r $NS_ID"
fi
}
CURRENT=""
if [[ -f $D/resources/build_info.json ]]; then
CURRENT=$(jq -r .version $D/resources/build_info.json)
echo "* installed version: '$CURRENT'" 1>&2
fi
VER="$(curl -sI -H 'Cache-Control: no-cache' "$DL_URL" \
| sed -ne 's/[Ll]ocation:.*discord-\(.*\)\.tar\.gz\r*/\1/p' \
)"
echo "* latest version: '$VER'" 1>&2
if [[ "$CURRENT" == "$VER" && -z "$FORCE" ]]; then
echo "* already up to date"
exit
fi
notify "Downloading Discord $VER..."
TMP=$(mktemp -d "/tmp/dl-discord-$VER.XXXXXXXXXXX")
trap 'rm -rf $TMP' EXIT
SRC=$TMP/Discord
if [[ -z "$ARCHIVE" ]]; then
ARCHIVE=$TMP/discord.tar.gz
echo "* downloading .tar.gz" 1>&2
wget "$DL_URL" -O $ARCHIVE --no-verbose || exit 1
else
ARCHIVE=$(realpath "$ARCHIVE")
fi
echo "* extracting .tar.gz" 1>&2
tar -C $TMP -xf "$ARCHIVE" Discord || exit 1
notify "Updating Discord $VER"
while pidof Discord >/dev/null; do
echo "* stopping active discord process" 1>&2
kill $(pidof Discord)
sleep 1
done
if [[ -d $D ]]; then
echo "* removing previous installation" 1>&2
rm -f $SH/applications/discord.desktop $SH/pixmaps/discord.png $PREFIX/bin/discord
rm -rf $D
fi
set -e
echo "* moving files into $PREFIX" 1>&2
mkdir -p $SH/applications $SH/pixmaps $PREFIX/bin
sed -se "s#/usr#$PREFIX#" -i $SRC/discord.desktop
mv $SRC/discord.desktop $SH/applications
mv $SRC $D
ln -s ../discord/discord.png $SH/pixmaps/discord.png
ln -s ../share/discord/Discord $PREFIX/bin/discord
set +e
which update-desktop-database >/dev/null 2>/dev/null && update-desktop-database $SH/applications
notify "Discord updated to $VER!"
# ~/.config/systemd/user/discord-update.service
[Unit]
Description=Update Discord because it can't figure out how to do this on its own
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'exec $HOME/.local/libexec/discord-update.bash'
# this line enables notification boxes
Environment=NOTIFY_SEND=notify-send
# ~/.config/systemd/user/discord-update.timer
[Unit]
Description=Try to update Discord daily
[Timer]
OnCalendar=daily
Persistent=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment