2023-11-10 20:08:41 +00:00
|
|
|
#!/usr/bin/bash
|
|
|
|
|
2023-12-01 18:45:01 +00:00
|
|
|
|
|
|
|
######################################################################################
|
|
|
|
# install-discord-linux.sh #
|
|
|
|
######################################################################################
|
|
|
|
# #
|
|
|
|
# This script was created to automate the install of discord on your system. #
|
|
|
|
# It installs it separately from a package manager (snap, apt, dnf, etc), so this #
|
|
|
|
# can be used to install it on any linux system regardless of distro #
|
|
|
|
# as long as the OS & processor supports it. #
|
|
|
|
# #
|
|
|
|
# This script was originally just setup to handle installing or updating the #
|
|
|
|
# existing installation. It can be setup in crontab to handle it automatically so #
|
|
|
|
# you never have to manually do it again. All you have to do is add it to your #
|
|
|
|
# crontab to run daily or hourly (assuming you have a cron service installed) #
|
|
|
|
# #
|
|
|
|
# EX: #
|
|
|
|
# """ #
|
|
|
|
# CRONTAB="$(crontab -l)" #
|
|
|
|
# if [[ $? -eq 0 ]]; then #
|
|
|
|
# CRONTAB+="\n0 0 * * * /path/to/script.sh" #
|
|
|
|
# echo -e "$CRONTAB" | crontab - #
|
|
|
|
# else #
|
|
|
|
# echo "0 0 * * * /path/to/script.sh" | crontab - #
|
|
|
|
# fi #
|
|
|
|
# """ #
|
|
|
|
# #
|
|
|
|
# eventually I am wanting this script to also provide additional functionalities: #
|
|
|
|
# - Install BetterDiscord #
|
|
|
|
# - Handle multiple installations of discord (makes named config directories #
|
|
|
|
# and will symlink them based on what "profile" you want to use). This will also #
|
|
|
|
# allow you to have a installation that contains mods and others without. #
|
|
|
|
# - Handle manual configurations of discord (where applicable) #
|
|
|
|
# #
|
|
|
|
######################################################################################
|
|
|
|
# Dependencies # #
|
|
|
|
################ #
|
|
|
|
# #
|
|
|
|
# 1) wget #
|
|
|
|
# 2) bash (of course) #
|
|
|
|
# #
|
|
|
|
######################################################################################
|
|
|
|
|
2023-11-10 20:08:41 +00:00
|
|
|
# BEGIN: Variables
|
|
|
|
|
2023-11-19 01:32:42 +00:00
|
|
|
DEBUG=1
|
2023-11-10 20:08:41 +00:00
|
|
|
ACTION=""
|
|
|
|
DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.gz'
|
|
|
|
VERSION_REGEX='[0-9]+\.[0-9]+\.[0-9]+'
|
2023-11-14 03:36:33 +00:00
|
|
|
INSTALL_DIR=~/.opt
|
2023-12-01 18:22:49 +00:00
|
|
|
LOCAL_APPLICATION_DIR=~/.local/share/applications
|
2023-11-10 20:08:41 +00:00
|
|
|
EXISTING_INSTALL=$INSTALL_DIR/Discord
|
|
|
|
BUILD_FILE=$EXISTING_INSTALL/resources/build_info.json
|
2023-11-19 00:40:49 +00:00
|
|
|
DESKTOP_FILE=$EXISTING_INSTALL/discord.desktop
|
2023-12-01 18:22:49 +00:00
|
|
|
INSTALLED_DESKTOP_FILE=$LOCAL_APPLICATION_DIR/discord.desktop
|
2023-11-10 20:08:41 +00:00
|
|
|
PACKAGE_DOWNLOAD_URL_BASE='https://dl.discordapp.net/apps/linux/{VERSION}/discord-{VERSION}.tar.gz'
|
2023-12-01 18:15:51 +00:00
|
|
|
ICON_DIR=~/.local/share/icons
|
2023-12-01 18:22:49 +00:00
|
|
|
DESKTOP_FILE_NEEDS_UPDATE=0
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
# END: Variables
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Helper Functions
|
|
|
|
|
2023-11-19 01:32:42 +00:00
|
|
|
function log () {
|
2023-12-01 18:28:52 +00:00
|
|
|
if [[ $DEBUG -eq 1 ]]; then
|
|
|
|
local DATE=`date`
|
|
|
|
local MESSAGE="${1:?"log: Message not provided"}"
|
2023-11-19 01:32:42 +00:00
|
|
|
echo "$DATE : $MESSAGE"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:08:41 +00:00
|
|
|
function get_remote_version(){
|
2023-11-19 01:35:41 +00:00
|
|
|
local REMOTE_VERSION=`curl -s $DOWNLOAD_URL | grep -Eo $VERSION_REGEX | head -n 1`
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
if [[ "$REMOTE_VERSION" ]]; then
|
|
|
|
echo "$REMOTE_VERSION"
|
|
|
|
return 0
|
|
|
|
else
|
2023-11-19 01:32:42 +00:00
|
|
|
log "error: remote version not found"
|
2023-11-10 20:08:41 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_download(){
|
|
|
|
local VERSION="${1:?"do_download : Version was not provided"}"
|
|
|
|
|
|
|
|
## If the provided version doesn't match the format #.#.#
|
|
|
|
if [[ ! "$VERSION" =~ ^$VERSION_REGEX$ ]]; then
|
2023-11-19 01:32:42 +00:00
|
|
|
log "do_download : the version ($VERSION) provided, does not match format $VERSION_REGEX."
|
2023-11-10 20:08:41 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
## Making the URL to download the package
|
|
|
|
### replacing all {VERSION} in PACKAGE_DOWNLOAD_URL_BASE with $REMOTE_VERSION using string substitution
|
|
|
|
PACKAGE_DOWNLOAD_URL="${PACKAGE_DOWNLOAD_URL_BASE//\{VERSION\}/$REMOTE_VERSION}"
|
2023-11-19 01:32:42 +00:00
|
|
|
log "Download url created ($PACKAGE_DOWNLOAD_URL)"
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
## Getting filename from url
|
|
|
|
### Removing all characters from the last / back to leave only the filename
|
|
|
|
FILENAME="${PACKAGE_DOWNLOAD_URL/*\/}"
|
2023-11-19 01:32:42 +00:00
|
|
|
log "File was downloaded as $FILENAME"
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
## Downloading the discord package (tar.gz)
|
2023-11-14 03:36:33 +00:00
|
|
|
curl "$PACKAGE_DOWNLOAD_URL" -o "$FILENAME" >/dev/null 2>&1
|
2023-11-10 20:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_desktop_file() {
|
|
|
|
|
2023-11-19 01:32:42 +00:00
|
|
|
log "Updating desktop file"
|
|
|
|
|
2023-11-10 20:08:41 +00:00
|
|
|
## Desktop file in the current/new install
|
|
|
|
DESKTOP_FILE=$EXISTING_INSTALL/discord.desktop
|
|
|
|
|
|
|
|
## Escaped becuase it would mess up the replacement syntax
|
|
|
|
PATH_TO_REPLACE=\/usr\/bin
|
|
|
|
PATH_REPLACEMENT=$EXISTING_INSTALL
|
|
|
|
|
|
|
|
## Escaped becuase it would mess up the replacement syntax
|
|
|
|
EXEC_TO_REPLACE=\/usr\/share\/discord\/Discord
|
2023-11-11 00:15:22 +00:00
|
|
|
EXEC_REPLACEMENT=$EXISTING_INSTALL/Discord
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
## Loading the desktop file contents into a variable to use replacement without sed
|
|
|
|
DESKTOP_FILE_CONTENTS="$(<$DESKTOP_FILE)"
|
|
|
|
|
|
|
|
## Replacing Path
|
|
|
|
DESKTOP_FILE_CONTENTS="${DESKTOP_FILE_CONTENTS/$PATH_TO_REPLACE/$PATH_REPLACEMENT}"
|
|
|
|
## Replacing Exec
|
|
|
|
DESKTOP_FILE_CONTENTS="${DESKTOP_FILE_CONTENTS/$EXEC_TO_REPLACE/$EXEC_REPLACEMENT}"
|
|
|
|
|
|
|
|
## Pushing replacement values to desktop file
|
|
|
|
echo -e "$DESKTOP_FILE_CONTENTS" > $DESKTOP_FILE
|
2023-11-19 01:32:42 +00:00
|
|
|
log "Desktop file has been updated"
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_install() {
|
|
|
|
|
|
|
|
cd "$INSTALL_DIR"
|
|
|
|
|
|
|
|
REMOTE_VERSION=`get_remote_version`
|
|
|
|
do_download "$REMOTE_VERSION"
|
|
|
|
|
|
|
|
## Error handling in case curl couldn't do the download (or error in script)
|
|
|
|
if [[ -f $FILENAME ]]; then
|
|
|
|
tar xf "$FILENAME"
|
|
|
|
else
|
2023-11-19 01:32:42 +00:00
|
|
|
log "$FILENAME failed to download. Exiting now"
|
|
|
|
exit 3
|
2023-11-10 20:08:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
update_desktop_file
|
|
|
|
|
|
|
|
[[ ! -d $ICON_DIR ]] && mkdir -p $ICON_DIR
|
|
|
|
cp $EXISTING_INSTALL/discord.png $ICON_DIR/
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_upgrade(){
|
|
|
|
cd "$INSTALL_DIR"
|
|
|
|
|
|
|
|
REMOTE_VERSION=`get_remote_version`
|
|
|
|
do_download "$REMOTE_VERSION"
|
|
|
|
|
|
|
|
## FILENAME provided by do_download
|
|
|
|
FILENAME="${FILENAME:?"do_upgrade : FILENAME not set by do_download"}"
|
|
|
|
|
|
|
|
tar xf "$FILENAME"
|
|
|
|
|
|
|
|
update_desktop_file
|
|
|
|
}
|
|
|
|
|
|
|
|
# END: Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Pre-Work Check
|
|
|
|
|
|
|
|
if [[ -d $EXISTING_INSTALL ]] && [[ -f $BUILD_FILE ]]; then
|
|
|
|
LOCAL_VERSION=`grep -Eo $VERSION_REGEX $BUILD_FILE`
|
2023-11-19 01:32:42 +00:00
|
|
|
log "Local version was found to be ($LOCAL_VERSION)"
|
2023-11-10 20:08:41 +00:00
|
|
|
else
|
|
|
|
ACTION=INSTALL
|
|
|
|
fi
|
|
|
|
|
2023-11-19 01:23:23 +00:00
|
|
|
REMOTE_VERSION=`get_remote_version`
|
2023-11-19 01:32:42 +00:00
|
|
|
log "Retrieved remote version ($REMOTE_VERSION)"
|
2023-11-10 20:08:41 +00:00
|
|
|
|
|
|
|
# END: Pre-Work Check
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Work
|
|
|
|
|
|
|
|
|
|
|
|
## If ACTION hasn't already been set
|
|
|
|
if [[ ! $ACTION ]]; then
|
|
|
|
|
|
|
|
## Use read to get version numbers in arrays
|
|
|
|
OIFS=$IFS
|
|
|
|
IFS='.'
|
|
|
|
read -a local_versions <<< "$LOCAL_VERSION"
|
|
|
|
read -a remote_versions <<< "$REMOTE_VERSION"
|
|
|
|
IFS=$OIFS
|
|
|
|
|
|
|
|
## Check if the remote version is greater than the local
|
|
|
|
for (( i=0; i<3; i++)); do
|
|
|
|
## If at any point the remote version is greater than the local this will break and set NEEDS_UPGRADE flag
|
|
|
|
if [[ ${local_versions[$i]} -lt ${remote_versions[$i]} ]]; then
|
|
|
|
ACTION=UPGRADE
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
case $ACTION in
|
|
|
|
INSTALL)
|
|
|
|
do_install
|
|
|
|
;;
|
|
|
|
|
|
|
|
UPGRADE)
|
|
|
|
do_upgrade
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
# END: Work
|
2023-12-01 18:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: End Work Check
|
|
|
|
|
2023-12-01 18:24:18 +00:00
|
|
|
## If discord isn't already setup, go ahead and mark it for install/update
|
2023-12-01 18:22:49 +00:00
|
|
|
if [[ ! -f $INSTALLED_DESKTOP_FILE ]]; then
|
2023-12-01 18:24:18 +00:00
|
|
|
log "Desktop file not found in the local applications dir ($LOCAL_APPLICATION_DIR) installing now"
|
2023-12-01 18:22:49 +00:00
|
|
|
DESKTOP_FILE_NEEDS_UPDATE=1
|
|
|
|
|
|
|
|
else
|
|
|
|
# If there is a change for any reason update it
|
|
|
|
if [[ "$(<$DESKTOP_FILE)" != "$(<$INSTALLED_DESKTOP_FILE)" ]]; then
|
2023-12-01 18:26:13 +00:00
|
|
|
log "Desktop file for new version of discord has changed. Updating the existing installation"
|
2023-12-01 18:22:49 +00:00
|
|
|
DESKTOP_FILE_NEEDS_UPDATE=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $DESKTOP_FILE_NEEDS_UPDATE -eq 1 ]]; then
|
|
|
|
log "Updating/Installing desktop file"
|
2023-12-01 18:10:59 +00:00
|
|
|
desktop-file-install --dir=$LOCAL_APPLICATION_DIR $DESKTOP_FILE
|
2023-12-01 18:08:37 +00:00
|
|
|
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
log "There was an issue with installtion"
|
|
|
|
else
|
|
|
|
log "Desktop file was successfully installed"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# END: End Work Check
|