scripts/app/check.discord.update

81 lines
3.3 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# Purpose {{{
## Create a temp file (to monitor) if an upgrade is available for Discord client
## from official website https://discord.com/download
## It's based on .deb package installation to check the current version.
## It can also compare the current available version in APT repositories
## if "repo" is given as first argument.
## How-to use {{{
### 1. Create a cron job, eg:
#00 20 * * * root /opt/repos/ipr.scripts/app/check.discord.update
### 2-1 Create a cron job to compare the version available in an APT repository:
#00 20 * * * root /opt/repos/ipr.scripts/app/check.discord.update repo
### 2. Monitor the temp file: /tmp/.discord.upgrade
# Or enable MAILTO in cronjob and edit the script to print a message.
# Or send a mail.
# …
## }}}
# }}}
# Expect maximum 1 argument {{{
if [ $# -gt 1 ]
then
cat << HELP
check.discord.update --
Compare current version of an installed Discord client and the last available.
EXAMPLE:
- Compare the current version installed from .deb file
check.discord.update
- Compare the current version from apt's repository
check.discord.update repo
HELP
exit 1
fi
# }}}
# Vars {{{
DEBUG=1
if [ $# -eq 1 ] && [ "${1}" = "repo" ] ## If repository's version should be compared
then
discord_current_version=$(apt-cache policy -- discord | awk '/Candidate:/ {print $2}' | sed 's/.:\(.*\)-.*/\1/')
else
discord_current_version=$(dpkg --list -- discord | awk '/^ii *discord/ {print $3}' | sed 's/.:\(.*\)-.*/\1/')
fi
discord_new_version=$(curl -s 'https://discord.com/api/download?platform=linux&format=deb' --header 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36' | grep " href=\"https" | sed -e 's;.*.deb">.*/apps/linux/\(.*\)/discord.*.deb</a>.*;\1;')
discord_new_version_file="/tmp/.discord.upgrade"
discord_new_pkg_url=$(curl -s 'https://discord.com/api/download?platform=linux&format=deb' --header 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36' | grep " href=\"https" | sed -e 's;.*href="\(.*.deb\)">.*;\1;')
discord_new_pkg_path="/tmp/discord-${discord_new_version}.deb"
# }}}
# Check if the current version is the last one {{{
if [ "${discord_current_version}" != "${discord_new_version}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — Current version (${discord_current_version}) and new one (${discord_new_version}) seems to be different."
## Create a temp file to monitor
touch -- "${discord_new_version_file}"
printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for Discord client (current: ${discord_current_version}): ${discord_new_version}." >> "${discord_new_version_file}"
## If it doesn't already exists, download this new package
if [ ! -f "${discord_new_pkg_path}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Download .deb file from discord.com to ${discord_new_pkg_path} ."
wget --quiet "${discord_new_pkg_url}" --output-document="${discord_new_pkg_path}"
fi
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — The current version is up-to-date."
## Ensure to remove any temp file and useless .deb file
rm --force -- "${discord_new_version_file}" "${discord_new_pkg_path}"
fi
# }}}
exit 0