From dae246b1b0695217587a3070d6ee2b7c60b95f06 Mon Sep 17 00:00:00 2001 From: Ali Mehraji Date: Wed, 20 Nov 2024 14:40:20 +0330 Subject: [PATCH] Update install.sh Script , also add uninstall.sh script (#202) * Update: scripts/install.sh * Add: uninstallation script (scripts/uninstall.sh) * Update: README.md and add uninstallation instructions --- README.md | 50 +++++----- scripts/install.sh | 229 ++++++++++++++++++++++++++++--------------- scripts/uninstall.sh | 74 ++++++++++++++ 3 files changed, 251 insertions(+), 102 deletions(-) create mode 100755 scripts/uninstall.sh diff --git a/README.md b/README.md index b1ad798..c3fc5d0 100644 --- a/README.md +++ b/README.md @@ -34,40 +34,42 @@ Please visit [Project Website](https://abdownloadmanager.com) for more info ## Installation +[Download Instructions](https://abdownloadmanager.com) + +[GitHub Releases](https://github.com/amir1376/ab-download-manager/releases/latest) + in order to download and install the app -- You can visit [Project Website](https://abdownloadmanager.com) for Download Instructions -- using an installation script - -
- bash script (Linux) +### installation script (Linux) - you can run this to install or update existing installed version - ```bash - curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/install.sh | bash - ``` +```bash +curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/install.sh | bash +``` -
-- using a package manager - -
- winget (for Windows) +### winget or scoop (for Windows) - ```bash - winget install amir1376.ABDownloadManager - ``` +**winget**: -
- -
- scoop (for Windows) +```bash +winget install amir1376.ABDownloadManager +``` - ```bash - scoop install extras/abdownloadmanager - ``` +**scoop**: -
-- Or you can find download links for your OS - from [GitHub Releases](https://github.com/amir1376/ab-download-manager/releases/latest) +```bash +scoop install extras/abdownloadmanager +``` + +## Uninstall + +perform below command to uninstall + +```bash +curl -fsSL https://raw.githubusercontent.com/amir1376/ab-download-manager/master/scripts/uninstall.sh | bash +``` + ## Screenshots
diff --git a/scripts/install.sh b/scripts/install.sh index 3f92793..19cf880 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -5,51 +5,167 @@ set -euo pipefail -# Get the latest version of the app from the GitHub API -get_latest_version() { - curl -s https://api.github.com/repos/amir1376/ab-download-manager/releases/latest | jq -r '.tag_name' +DEPENDENCIES=(curl tar) +LOG_FILE="/tmp/ab-dm-installer.log" + +# --- Custom Logger +logger() { + timestamp=$(date +"%Y/%m/%d %H:%M:%S") + + if [[ "$1" == "error" ]]; then + # Red color for errors + echo -e "${timestamp} -- "$0" [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE} + else + # Default color for non-error messages + echo -e "${timestamp} -- "$0" [Info]: $@" | tee -a ${LOG_FILE} + fi +} + +# --- Detect OS and The Package Manager to use +detect_package_manager() { + if [ -f /etc/os-release ]; then + source /etc/os-release + local OS=${NAME} + elif type lsb_release >/dev/null 2>&1; then + local OS=$(lsb_release -si) + elif [ -f /etc/lsb-release ]; then + source /etc/lsb-release + local OS="${DISTRIB_ID}" + elif [ -f /etc/debian_version ]; then + local OS=Debian + else + logger error "Your Linux Distro is not Supperted." + logger error "Please install ${DEPENDENCIES[@]} Manually." + exit 1 + fi + + if `grep -E 'Debian|Ubuntu' <<< $OS > /dev/null` ; then + systemPackage="apt" + elif `grep -E 'Fedora|CentOS|Red Hat|AlmaLinux' <<< $OS > /dev/null`; then + systemPackage="dnf" + fi +} + +detect_package_manager + +# --- Install dependencies +install_dependencies() { + + local answer + read -p "Do you want to install $1? [Y/n]: " -r answer + answer=${answer:-Y} # Set default to 'Y' if no input is given + + case $answer in + [Yy]* ) + sudo ${systemPackage} update -y + logger "installing $1 package ..." + sudo ${systemPackage} install -y $1 + ;; + [Nn]* ) + logger "Skipping the installation of $1." + ;; + * ) + logger error "Please answer yes or no." + install_dependencies "$1" # re-prompt for the same package + ;; + esac +} + +# Check dependencies and install if missing +check_dependencies() { + for pkg in "${DEPENDENCIES[@]}"; do + if ! command -v "$pkg" >/dev/null 2>&1; then + logger "$pkg is not installed. Installing..." + install_dependencies "$pkg" + else + logger "$pkg is already installed." + fi + done } APP_NAME="ABDownloadManager" PLATFORM="linux" EXT="tar.gz" -LATEST_VERSION=$(get_latest_version) -ASSET_NAME=${APP_NAME}_${LATEST_VERSION:1}_${PLATFORM}.${EXT} -BINARY_PATH=$HOME/.local/$APP_NAME/bin/$APP_NAME -# Check if curl, jq, and tar are installed -check_dependencies() { - for cmd in curl jq tar; do - if ! command -v "$cmd" &>/dev/null; then - print_message "Error: $cmd is not installed. Please install it and try again." - exit 1 - fi - done +RELEASE_URL="https://api.github.com/repos/amir1376/ab-download-manager/releases/latest" +GITHUB_RELEASE_DOWNLOAD="https://github.com/amir1376/ab-download-manager/releases/download" + +LATEST_VERSION=$(curl -fSs "${RELEASE_URL}" | grep '"tag_name":' | sed -E 's/.*"tag_name": ?"([^"]+)".*/\1/') + +ASSET_NAME="${APP_NAME}_${LATEST_VERSION:1}_${PLATFORM}.${EXT}" +DOWNLOAD_URL="$GITHUB_RELEASE_DOWNLOAD/${LATEST_VERSION}/$ASSET_NAME" + +BINARY_PATH="$HOME/.local/$APP_NAME/bin/$APP_NAME" + + +# --- Delete the old version Application if exists +delete_old_version() { + # --- Killing Any Application Process + pkill -f "$APP_NAME" + rm -rf "$HOME/.local/$APP_NAME" + rm -rf "$HOME/.local/bin/$APP_NAME" + logger "removed old version AB Download Manager" } -# Get the download URL for the latest version of the app -get_download_url() { - echo "https://github.com/amir1376/ab-download-manager/releases/download/${LATEST_VERSION}/$ASSET_NAME" +# --- Generate a .desktop file for the app +generate_desktop_file() { + cat < "$HOME/.local/share/applications/abdownloadmanager.desktop" +[Desktop Entry] +Name=AB Download Manager +Comment=Manage and organize your download files better than before +GenericName=Downloader +Categories=Utility;Network; +Exec=$BINARY_PATH +Icon=$HOME/.local/$APP_NAME/lib/$APP_NAME.png +Terminal=false +Type=Application +StartupWMClass=com-abdownloadmanager-desktop-AppKt +EOF } -close_if_running() { - pid=$(pidof -s "$APP_NAME") +# --- Download the latest version of the app +download_zip() { + # Remove the app tarball if it exists in /tmp + rm -f "/tmp/$ASSET_NAME" - if [ -n "$pid" ]; then - kill -9 "$pid" - echo "Closed running instance of AB Download Manager before updating." + logger "downloading AB Download Manager ..." + # Perform the download with curl + if curl --progress-bar -fSL -o "/tmp/$ASSET_NAME" "${DOWNLOAD_URL}"; then + logger "download finished successfully" + else + logger error "Download failed! Something Went Wrong" + logger error "Hint: Check Your Internet Connectivity" + # Optionally remove the partially downloaded file + rm -f "/tmp/$ASSET_NAME" fi } -# Delete the old version of the app if it exists -delete_old_version() { - close_if_running - rm -rf "$HOME/.local/$APP_NAME" - rm -rf "$HOME/.local/bin/$APP_NAME" - echo "Old version of AB Download Manager deleted" + +# --- Install the app +install_app() { + + logger "Installing AB Download Manager ..." + # --- Setup ~/.local directories + mkdir -p "$HOME/.local/bin" "$HOME/.local/share/applications" + tar -xzf "/tmp/$ASSET_NAME" -C "$HOME/.local" + + # --- remove tarball after installation + rm "/tmp/$ASSET_NAME" + + # Link the binary to ~/.local/bin + ln -s "$BINARY_PATH" "$HOME/.local/bin/$APP_NAME" + + # Create a .desktop file in ~/.local/share/applications + generate_desktop_file + + logger "AB Download Manager installed successfully" + logger "it can be found in Applications menu or run '$APP_NAME' in terminal" + logger "Make sure $HOME/.local/bin exists in PATH" + logger "installation logs saved in: ${LOG_FILE}" + } -# Check if the app is installed +# --- Check if the app is installed check_if_installed() { local installed_version installed_version=$($APP_NAME --version 2>/dev/null) @@ -60,70 +176,27 @@ check_if_installed() { fi } -# Generate a .desktop file for the app -generate_desktop_file() { - desktop_file_content="[Desktop Entry] -Name=AB Download Manager -Comment=Manage and organize your download files better than before -GenericName=Downloader -Categories=Utility;Network; -Exec=$BINARY_PATH -Icon=$HOME/.local/$APP_NAME/lib/$APP_NAME.png -Terminal=false -Type=Application -StartupWMClass=com-abdownloadmanager-desktop-AppKt" - - echo "$desktop_file_content" >"$HOME/.local/share/applications/abdownloadmanager.desktop" -} - -# Download the latest version of the app -download_zip() { - echo "Downloading AB Download Manager ..." - url=$(get_download_url) - curl -L -o "/tmp/$ASSET_NAME" "$url" - echo "Download Finished" -} - -# Install the app -install_app() { - echo "Installing AB Download Manager ..." - mkdir -p "$HOME/.local" - tar -xzf "/tmp/$ASSET_NAME" -C "$HOME/.local" - - # Setup ~/.local directories - mkdir -p "$HOME/.local/bin" "$HOME/.local/share/applications" - - # Link the binary to ~/.local/bin - ln -s "$BINARY_PATH" "$HOME/.local/bin/$APP_NAME" - - # Create a .desktop file in ~/.local/share/applications - generate_desktop_file - - echo "AB Download Manager installed successfully" - echo "Open it from your Applications menu or run '$APP_NAME' from the terminal" - echo "Make sure $HOME/.local/bin is in your PATH" -} - -# Update the app +# --- Update the app update_app() { - echo "Checking update..." + logger "checking update" if [ "$1" != "${LATEST_VERSION:1}" ]; then - echo "An update is available: v${LATEST_VERSION:1}. Updating..." + logger "new version is available: v${LATEST_VERSION:1}. Updating..." download_zip delete_old_version install_app else - echo "You have the latest version installed." + logger "You have the latest version installed." exit 0 fi } main() { + echo "" > "$LOG_FILE" local installed_version check_dependencies installed_version=$(check_if_installed) if [ -n "$installed_version" ]; then - echo "AB Download Manager v$installed_version is currently installed" + logger "AB Download Manager v$installed_version is currently installed." update_app "$installed_version" else download_zip diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh new file mode 100755 index 0000000..7e6dcc2 --- /dev/null +++ b/scripts/uninstall.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +set -euo pipefail +# set -x + + +APP_NAME="ABDownloadManager" + +LOG_FILE="/tmp/ab-dm-uninstaller.log" + +# --- Custom Logger +logger() { + timestamp=$(date +"%Y/%m/%d %H:%M:%S") + + if [[ "$1" == "error" ]]; then + # Red color for errors + echo -e "${timestamp} -- "$0" [Error]: \033[0;31m$@\033[0m" | tee -a ${LOG_FILE} + else + # Default color for non-error messages + echo -e "${timestamp} -- "$0" [Info]: $@" | tee -a ${LOG_FILE} + fi +} + +delete_app_config_dir() { + + local answer + read -p "Do you want to continue? [Y/n]: " -r answer + answer=${answer:-Y} # Set default to 'Y' if no input is given + + case $answer in + [Yy]* ) + rm -rf "$HOME/.abdm" + logger "$APP_NAME settings and download lists directory: $HOME/.abdm removed." + ;; + [Nn]* ) + logger "Remove The $HOME/.abdm directory manually." + ;; + * ) + logger error "Please answer yes or no." + delete_app_config_dir + ;; + esac +} + +delete_app() { + + logger "Killing Any $APP_NAME Processes ..." + pkill -f "$APP_NAME" + + logger "removing $APP_NAME desktop file ..." + # --- Remove the .desktop file in ~/.local/share/applications + rm "$HOME/.local/share/applications/abdownloadmanager.desktop" + + logger "unlinking $APP_NAME link ..." + unlink "$HOME/.local/bin/$APP_NAME" + + logger "removing $APP_NAME binary ..." + rm -rf "$HOME/.local/$APP_NAME" + rm -rf "$HOME/.local/bin/$APP_NAME" + + logger "removing $APP_NAME autostart at boot file ..." + rm -f "$HOME/.config/autostart/AB Download Manager.desktop" + + logger "removing $APP_NAME settings and download lists" + delete_app_config_dir + + logger "AB Download Manager completely removed" +} + +main() { + delete_app +} + +main "$@" \ No newline at end of file