Skip to content

Instantly share code, notes, and snippets.

@adryd325
Last active December 10, 2023 03:43
Show Gist options
  • Save adryd325/b086a02123effa58195a9c7e9e472680 to your computer and use it in GitHub Desktop.
Save adryd325/b086a02123effa58195a9c7e9e472680 to your computer and use it in GitHub Desktop.
discord download/updater script
#!/usr/bin/env bash
##
## A standalone and less opinionated version of the local user Discord installer from gitlab.com/adryd325/dotfiles
## Tested on Arch Linux, will most likely work on anything else that can run Discord.
##
tempDir=$(mktemp -d)
DOWNLOAD_ENDPOINT='https://discord.com/api/download'
DOWNLOAD_OPTIONS='?platform=linux&format=tar.gz'
function getDiscordDownloadURL {
if [[ "$1" = "stable" ]] || [[ -z "$1" ]]; then
printf %s%s "${DOWNLOAD_ENDPOINT}" "${DOWNLOAD_OPTIONS}"
else
printf %s/%s%s "${DOWNLOAD_ENDPOINT}" "$1" "${DOWNLOAD_OPTIONS}"
fi
}
function isValidBranch {
invalid=1
for i in "${DISCORD_BRANCHES[@]}"; do
if [[ "${i}" = "$1" ]]; then
invalid=0
fi
done
return "${invalid}"
}
function getDiscordName {
branch=$1
if [[ "${branch}" = "stable" ]]; then
printf "Discord"
elif [[ "${branch}" = "ptb" ]]; then
printf "DiscordPTB"
else
printf "Discord%s" "${branch^}"
fi
}
function getDiscordPrettyName {
branch=$1
if [[ "${branch}" = "stable" ]]; then
printf "Discord"
elif [[ "${branch}" = "ptb" ]]; then
printf "Discord PTB"
else
printf "Discord %s" "${branch^}"
fi
}
function getDiscordPkgName {
branch=$1
if [[ "${branch}" = "stable" ]]; then
printf "discord"
else
printf "discord-%s" "${branch}"
fi
}
function download {
if [[ -x "$(command -v curl)" ]]; then
curl -fsSL "$1" -o "$2"
return $?
elif [[ -x "$(command -v wget)" ]]; then
wget -qo "$2" "$1"
return $?
fi
echo "Failed to download $1. curl or wget not installed"
return 1
}
function installBranch {
branch=$1
AR_LOG_PREFIX="${branch}"
name=$(getDiscordName "${branch}")
pkgName=$(getDiscordPkgName "${branch}")
installationDir="${HOME}/.local/share/${name}"
desktopFile="${HOME}/.local/share/${name}/${pkgName}.desktop"
echo "Downloading ${branch}"
if ! download "$(getDiscordDownloadURL "${branch}")" "${tempDir}/${name}.tar.gz"; then
echo "Failed to download ${branch}"
return 1
fi
if [[ -d "${installationDir}" ]]; then
echo "Deleting existing ${branch} installation"
rm -rf "${installationDir}"
fi
echo "Extracting ${branch}"
if ! tar -xf "${tempDir}/${name}.tar.gz" -C "${HOME}/.local/share"; then
echo "Failed to extract ${branch}"
return 1
fi
echo "Installing ${branch}"
# The new (May 2021) icon looks too big on the GNOME dock
if [[ -x "$(command -v convert)" ]]; then
echo "Add margin to the ${branch} icon"
convert \
"${installationDir}/discord.png" \
-bordercolor Transparent \
-compose copy \
-border 16 \
-resize 256x256 \
"${installationDir}/discord.png"
fi
# Install icon
mkdir -p "${HOME}/.local/share/icons/hicolor/256x256"
cp -f "${installationDir}/discord.png" "${HOME}/.local/share/icons/hicolor/256x256/${pkgName}.png" &> /dev/null
# .desktop file fixes
sed -i "s:Exec=/usr/share/${pkgName}/${name}:Exec=\"${installationDir}/${name}\":" \
"${desktopFile}"
sed -i "s/StartupWMClass=discord/StartupWMClass=${name}/" \
"${desktopFile}"
sed -i "s:Icon=${pkgName}:Icon=${HOME}/.local/share/icons/hicolor/256x256/${pkgName}.png:" \
"${desktopFile}"
sed -i "s:Path=/usr/bin:path=${installationDir}:" \
"${desktopFile}"
# Copy over new .desktop files
mkdir -p "${HOME}/.local/share/applications"
cp -f "${installationDir}/${pkgName}.desktop" "${HOME}/.local/share/applications/${pkgName}.desktop"
}
if [[ $# -gt 0 ]]; then
for branch in "$@"; do
if ! isValidBranch "${branch}"; then
echo "The branch \"${branch}\" does not exist"
continue
fi
installBranch "${branch}" &
done
wait
else
installBranch stable
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment