myscripts/install-discord-linux.sh

186 lines
4.5 KiB
Bash
Raw Normal View History

#!/usr/bin/bash
# BEGIN: Variables
ACTION=""
DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.gz'
VERSION_REGEX='[0-9]+\.[0-9]+\.[0-9]+'
INSTALL_DIR=~/.opt
EXISTING_INSTALL=$INSTALL_DIR/Discord
BUILD_FILE=$EXISTING_INSTALL/resources/build_info.json
DESKTOP_FILE=$EXISTING_INSTALL/discord.desktop
PACKAGE_DOWNLOAD_URL_BASE='https://dl.discordapp.net/apps/linux/{VERSION}/discord-{VERSION}.tar.gz'
ICON_DIR=~/.icons
DESKTOP_FILE_INSTALLED=0
[[ -f /usr/share/applications/discord.desktop ]] && DESKTOP_FILE_INSTALLED=1
# END: Variables
# BEGIN: Helper Functions
function get_remote_version(){
local REMOTE_VERSION=`curl $DOWNLOAD_URL | grep -Eo $VERSION_REGEX | head -n 1`
if [[ "$REMOTE_VERSION" ]]; then
echo "$REMOTE_VERSION"
return 0
else
echo "error: remote version not found"
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
echo "do_download : the version ($VERSION) provided, does not match format $VERSION_REGEX."
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}"
## Getting filename from url
### Removing all characters from the last / back to leave only the filename
FILENAME="${PACKAGE_DOWNLOAD_URL/*\/}"
## Downloading the discord package (tar.gz)
curl "$PACKAGE_DOWNLOAD_URL" -o "$FILENAME" >/dev/null 2>&1
}
function update_desktop_file() {
## 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
EXEC_REPLACEMENT=$EXISTING_INSTALL/Discord
## 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
}
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
echo "$FILENAME failed to download. Exiting now"
exit 1
fi
update_desktop_file
## If your icon dir (configured above) exists in the env variable
if [[ "$XDG_DATA_DIRS" != *$ICON_DIR* ]]; then
export XDG_DATA_DIRS=$XDG_DATA_DIRS:$ICON_DIR
if [[ $SHELL == *bash ]]; then
echo 'export XDG_DATA_DIRS=$XDG_DATA_DIRS':$ICON_DIR > ~/.bashrc
else
echo 'export XDG_DATA_DIRS=$XDG_DATA_DIRS':$ICON_DIR > ~/.profile
fi
fi
[[ ! -d $ICON_DIR ]] && mkdir -p $ICON_DIR
cp $EXISTING_INSTALL/discord.png $ICON_DIR/
sudo desktop-file-isntall $DESKTOP_FILE
}
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
if [[ $DESKTOP_FILE_INSTALLED -eq 0 ]]; then
sudo desktop-file-install $DESKTOP_FILE
fi
}
# END: Helper Functions
# BEGIN: Pre-Work Check
if [[ -d $EXISTING_INSTALL ]] && [[ -f $BUILD_FILE ]]; then
LOCAL_VERSION=`grep -Eo $VERSION_REGEX $BUILD_FILE`
else
ACTION=INSTALL
fi
REMOTE_VERSION=`get_remote_version`
# 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