Improve install.sh (#521)

- Refactor a big portion of the code
- Use the least amount of dependencies
- Many tweaks and improvements
This commit is contained in:
Sir RaptaG 2023-07-06 22:47:17 +03:00 committed by GitHub
parent ef4f6c429f
commit 704fcca298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 139 deletions

View File

@ -161,15 +161,15 @@ You can compile Spotube's source code by [following these instructions](CONTRIBU
- [Kingkor Roy Tirtho](https://github.com/KRTirtho) - The Founder, Maintainer and Lead Developer - [Kingkor Roy Tirtho](https://github.com/KRTirtho) - The Founder, Maintainer and Lead Developer
- [Owen Connor](https://github.com/owencz1998) - The Cool Discord Moderator - [Owen Connor](https://github.com/owencz1998) - The Cool Discord Moderator
- [Piotr Rogowski](https://github.com/karniv00l) - The MacOS Developer
- [RaptaG](https://github.com/RaptaG) - The GitHub Moderator and Community Manager - [RaptaG](https://github.com/RaptaG) - The GitHub Moderator and Community Manager
- [Piotr Rogowski](https://github.com/karniv00l) - The MacOS Developer
- [Rusty Apple](https://github.com/RustyApple) - The Mysterious Unknown Guy - [Rusty Apple](https://github.com/RustyApple) - The Mysterious Unknown Guy
## 💼 License ## 💼 License
Spotube is open source and licensed under the [BSD-4-Clause](/LICENSE) License. Spotube is open source and licensed under the [BSD-4-Clause](/LICENSE) License.
If you are concerned, feel free to [read the reason of choosing this license](https://dev.to/krtirtho/choosing-open-source-license-wisely-1m3p). If you are concerned, you can [read the reason of choosing this license](https://dev.to/krtirtho/choosing-open-source-license-wisely-1m3p).
<details> <details>
<summary> <summary>

View File

@ -1,182 +1,166 @@
#!/bin/bash #!/usr/bin/env bash
# Varibles
fname="$(basename $0)"
installDir='/usr/share/spotube'
desktopFile='/usr/share/applications/spotube.desktop'
appdata='/usr/share/appdata/spotube.appdata.xml'
icon='/usr/share/icons/spotube/spotube-logo.png'
symlink='/usr/bin/spotube'
temp='/tmp/spotube-installer'
latestVer="$(wget -qO- "https://api.github.com/repos/KRTirtho/spotube/releases/latest" \ | grep -Po '"tag_name": "\K.*?(?=")')"
INSTLLATION_DIR=/usr/share/spotube # Root check - From CAAIS (https://codeberg.org/RaptaG/CAAIS), under GPL-3.0
DESKTOP_FILE_PATH=/usr/share/applications/spotube.desktop function rootCheck() {
APP_DATA_PATH=/usr/share/appdata/spotube.appdata.xml if [ "${EUID}" -ne 0 ]; then
ICON_PATH=/usr/share/icons/spotube/spotube-logo.png echo "Error: Root permissions are required for ${fname} to work."
BIN_SYMLINK_PATH=/usr/bin/spotube echo "Please run './${fname}' for more information."
exit 1
fi
}
TEMP_DIR=/tmp/spotube-installer # Flags
function help(){
# get latest version from github api echo "Usage: sudo ./${fname} [flags]"
VERSION=$(curl --silent "https://api.github.com/repos/KRTirtho/spotube/releases/latest" \ echo 'Flags:'
| grep -Po '"tag_name": "\K.*?(?=")') echo ' -i, --install <version> Install any Spotube version (if not specified, the latest is installed).'
echo ' -h, --help This help menu'
function spotube_help(){ echo ' -r, --remove Removes Spotube from your system'
# available flags are -v or --version to specify what version to download
echo "Usage: ./install.sh [flags]"
echo "Flags:"
echo " -v, --version <version> Specify what version to download. Default: $VERSION"
echo " -h, --help Show this help message"
echo " -r, --remove Remove spotube from your system"
exit 0 exit 0
} }
# a function to check if a given command exists or not and returns bool # Checks whether a given command exists or not and returns bool
function command_exists() { function command_exists() {
command -v "$@" >/dev/null 2>&1 command -v "$@" >/dev/null 2>&1
} }
function install_deps(){ function install_deps(){
local DEBIAN_DEPS="curl tar mpv libappindicator3-1 gir1.2-appindicator3-0.1 libsecret-1-0 libnotify-bin libjsoncpp25" local debianDeps='mpv libappindicator3-1 gir1.2-appindicator3-0.1 libsecret-1-0 libnotify-bin libjsoncpp25'
local RPM_DEPS="curl tar mpv libappindicator jsoncpp libsecret libnotify" local rpmDeps='mpv libappindicator jsoncpp libsecret libnotify'
local ARCH_DEPS="curl tar mpv libappindicator-gtk3 libsecret jsoncpp libnotify" local archDeps='mpv libappindicator-gtk3 libsecret jsoncpp libnotify'
if command_exists apt; then if command_exists apt; then
sudo apt install -y $DEBIAN_DEPS apt install -y ${debianDeps}
elif command_exists dnf; then elif command_exists dnf; then
sudo dnf install -y $RPM_DEPS dnf install -y ${debianDeps}
elif command_exists yum; then elif command_exists yum; then
sudo yum install -y $RPM_DEPS yum install -y ${rpmDeps}
elif command_exists zypper; then elif command_exists zypper; then
sudo zypper install -y $RPM_DEPS zypper install -y ${rpmDeps}
elif command_exists pacman; then elif command_exists pacman; then
sudo pacman -Sy $ARCH_DEPS pacman -Sy ${archDeps}
else else
echo "Your package manager is not supported by this script. Please install the dependencies manually." # Maybe one day
echo "The dependencies are: curl, tar, mpv, appindicator, libsecret, jsoncpp, libnotify" # # Deps
fi # # JsonCpp
# wget https://github.com/open-source-parsers/jsoncpp/tarball/master -O jsoncpp.tar.gz
# tar -xf jsoncpp.tar.gz && cd open-source-parsers-jsoncpp-*
echo 'You have to install some dependancies manually in order for Spotube to work.'
echo "The deps are the following: ${rpmDeps}"
fi
} }
function download_extract_spotube(){ function download_extract_spotube(){
local TAR_PATH=/tmp/spotube-$VERSION.tar.xz local tarPath="/tmp/spotube-${ver}.tar.xz"
local DOWNLOAD_URL=https://github.com/KRTirtho/spotube/releases/download/v$VERSION/spotube-linux-$VERSION-x86_64.tar.xz local donwloadURL="https://github.com/KRTirtho/spotube/releases/download/v${ver}/spotube-linux-${ver}-x86_64.tar.xz"
# check if version is nightly if [ "${ver}" = "nightly" ]; then
downloadURL"=https://github.com/KRTirtho/spotube/releases/download/nightly/spotube-linux-nightly-x86_64.tar.xz"
if [ "$VERSION" = "nightly" ]; then
DOWNLOAD_URL=https://github.com/KRTirtho/spotube/releases/download/nightly/spotube-linux-nightly-x86_64.tar.xz
fi fi
rm -rf ${temp}
rm -rf $TEMP_DIR mkdir -p ${temp}
mkdir -p $TEMP_DIR
# check if already exists downloaded file # Check if already exists downloaded file
if [ -f $TAR_PATH ]; then if [ -f ${tarPath} ]; then
echo "Found spotube-$VERSION.tar.xz in /tmp. Skipping download..." echo "Installation file detected. Skipping download..."
else else
echo "Downloading spotube-$VERSION.tar.xz..." echo "Downloading spotube-${ver}.tar.xz..."
curl -L $DOWNLOAD_URL -o $TAR_PATH wget -q ${downloadURL} -P ${tarPath}
fi fi
# Extract the tarball tar -xf ${tarPath} -C ${temp}
tar -xf $TAR_PATH -C $TEMP_DIR
# check if $TEMP_DIR empty or not # Is $temp empty or not
if [ ! "$(ls -A ${temp})" ]; then
if [ ! "$(ls -A $TEMP_DIR)" ]; then echo 'Failed to extract the tarball. Redownloading...'
echo "Failed to extract the tarball. Redownloading..." rm -f ${tarPath}
rm -f $TAR_PATH wget -q ${downloadURL} -P ${tarPath}
curl -L $DOWNLOAD_URL -o $TAR_PATH tar -xf ${tarPath} -C ${temp}
tar -xf $TAR_PATH -C $TEMP_DIR
fi fi
# checking one last time # Once again
if [ ! "$(ls -A $TEMP_DIR)" ]; then if [ ! "$(ls -A ${temp})" ]; then
echo "Failed to extract the tarball. Aborting installation..." echo 'Failed to extract the tarball. Installation aborted.'
exit 1 exit 1
fi fi
} }
function install_spotube(){ function install_spotube(){
# check if exists and uninstall if user allows if [ -d ${installDir} ]; then
echo -n "Spotube is already installed. Do you want to reinstall it? [y/N] "
read reinstall
if [ -d $INSTLLATION_DIR ]; then case "${reinstall}" in
echo "Spotube is already installed. Do you want to uninstall it and then install? [y/N]" [yY]*)
read -r uninstall uninstall_spotube ;;
if [ "$uninstall" = "y" ] || [ "$uninstall" = "Y" ]; then *)
uninstall_spotube echo 'Aborting installation...'
else exit 1 ;;
echo "Aborting installation..." esac
exit 1
fi
fi fi
# Move the files to /usr/share/spotube
sudo mkdir -p $INSTLLATION_DIR # Install Spotube from temp dir
mkdir -p ${installDir}
mv ${temp}/data ${installDir}
mv ${temp}/lib ${installDir}
mv ${temp}/spotube ${installDir}
mv ${temp}/spotube.desktop ${desktopDir}
mv ${temp}/com.github.KRTirtho.Spotube.appdata.xml ${appdata}
mkdir -p /usr/share/icons/spotube
mv ${temp}/spotube-logo.png ${icon}
ln -s /usr/share/spotube/spotube ${symlink}
sudo mv $TEMP_DIR/data $INSTLLATION_DIR rm -rf ${temp} # Remove temp dir
sudo mv $TEMP_DIR/lib $INSTLLATION_DIR echo "Spotube ${ver} has been installed successfully!"
sudo mv $TEMP_DIR/spotube $INSTLLATION_DIR
# Move the desktop file to /usr/share/applications
sudo mv $TEMP_DIR/spotube.desktop $DESKTOP_FILE_PATH
# Move the appdata file to /usr/share/appdata
sudo mv $TEMP_DIR/com.github.KRTirtho.Spotube.appdata.xml $APP_DATA_PATH
# Move the logo to /usr/share/icons/spotube
sudo mkdir -p /usr/share/icons/spotube
sudo mv $TEMP_DIR/spotube-logo.png $ICON_PATH
# Create a symlink to /usr/bin
sudo ln -s /usr/share/spotube/spotube $BIN_SYMLINK_PATH
# Clean up
rm -rf $TEMP_DIR
echo "Spotube $VERSION has been installed successfully!"
} }
function uninstall_spotube(){ function uninstall_spotube(){
# confirm echo -n "Are you sure you want to uninstall Spotube? [y/N] "
read confirm
echo "Are you sure you want to uninstall Spotube?" case "${confirm}" in
echo [yY]*)
echo "This will remove following files and directories:" echo 'Unstalling Spotube..'
echo " $INSTLLATION_DIR" rm -rf ${installDir} ${desktopDir} ${appdata} ${icon} ${symlink} ;;
echo " $DESKTOP_FILE_PATH" *)
echo " $APP_DATA_PATH" echo 'Aborting...'
echo " $ICON_PATH" exit 0 ;;
echo " $BIN_SYMLINK_PATH" esac
echo
echo "[y/N]"
read -r CONFIRMATION
if [[ "$CONFIRMATION" != "y" ]]; then
echo "Aborting uninstallation..."
exit 0
fi
# remove the files
sudo rm -rf $INSTLLATION_DIR $DESKTOP_FILE_PATH $APP_DATA_PATH $ICON_PATH $BIN_SYMLINK_PATH
} }
# parse arguments -v, --version, -r, --remove, -h, --help case "$1" in
-i | --install)
while [[ "$#" -gt 0 ]]; do if [ "$2" != "" ]; then
case $1 in ver="$2"
-v|--version) VERSION="$2"; shift ;; else
-r|--remove) uninstall_spotube; exit 0 ;; ver="${latestVer}"
-h|--help) spotube_help; exit 0 ;; fi
*) echo "Unknown parameter passed: $1"; spotube_help; exit 1 ;;
esac rootCheck
shift install_deps
done download_extract_spotube
install_spotube
install_deps exit 0 ;;
download_extract_spotube -r | --remove)
install_spotube rootCheck
uninstall_spotube
exit 0 ;;
-h | --help | "")
help
exit 0 ;;
*)
echo "Invalid flag '$1'"
echo "Please run ./${fname} for more information."
exit 1 ;;
esac