From 14d8f7cb445a886efedf280095045fb6185b1fbe Mon Sep 17 00:00:00 2001 From: Emanuele Bernardi Date: Fri, 29 Nov 2024 08:39:58 +0100 Subject: [PATCH] use sudo if ran as user, use case instead of if --- install.sh | 57 +++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/install.sh b/install.sh index 68d70f4..a849481 100755 --- a/install.sh +++ b/install.sh @@ -1,36 +1,41 @@ #!/bin/bash +# Check if the script is running as root +[ "$EUID" -eq 0 ] || exec sudo bash "$0" "$@" set -e -o pipefail shopt -s nocaseglob OUT_FILE=/usr/local/bin/autorestic +TMP_FILE=/tmp/autorestic # Type NATIVE_OS=$(uname | tr '[:upper:]' '[:lower:]') -if [[ $NATIVE_OS == *"linux"* ]]; then - OS=linux -elif [[ $NATIVE_OS == *"darwin"* ]]; then - OS=darwin -elif [[ $NATIVE_OS == *"freebsd"* ]]; then - OS=freebsd -else - echo "Could not determine OS automatically, please check the release page manually: https://github.com/cupcakearmy/autorestic/releases" +case $NATIVE_OS in + *linux*) + OS=linux;; + *darwin*) + OS=darwin;; + *freebsd*) + OS=freebsd;; + *) + echo "Could not determine OS automatically, please check the release page"\ + "manually: https://github.com/cupcakearmy/autorestic/releases" exit 1 -fi + ;; +esac echo $OS NATIVE_ARCH=$(uname -m | tr '[:upper:]' '[:lower:]') -if [[ $NATIVE_ARCH == *"x86_64"* || $NATIVE_ARCH == *"amd64"* ]]; then - ARCH=amd64 -elif [[ $NATIVE_ARCH == *"arm64"* || $NATIVE_ARCH == *"aarch64"* ]]; then - ARCH=arm64 -elif [[ $NATIVE_ARCH == *"x86"* ]]; then - ARCH=386 -elif [[ $NATIVE_ARCH == *"armv7"* ]]; then - ARCH=arm -else - echo "Could not determine Architecure automatically, please check the release page manually: https://github.com/cupcakearmy/autorestic/releases" - exit 1 -fi +case $NATIVE_ARCH in + *x86_64*|*amd64*) ARCH=amd64 ;; + *arm64*|*aarch64*) ARCH=arm64 ;; + *x86*) ARCH=386 ;; + *armv7*) ARCH=arm ;; + *) + echo "Could not determine Architecure automatically, please check the"\ + "release page manually: https://github.com/cupcakearmy/autorestic/releases" + exit 1 + ;; +esac echo $ARCH if ! command -v bzip2 &>/dev/null; then @@ -40,11 +45,11 @@ fi wget -qO - https://api.github.com/repos/cupcakearmy/autorestic/releases/latest \ | grep "browser_download_url.*_${OS}_${ARCH}" \ -| cut -d : -f 2,3 \ -| tr -d \" \ -| wget -O "${OUT_FILE}.bz2" -i - -bzip2 -fd "${OUT_FILE}.bz2" +| xargs | cut -d ' ' -f 2 \ +| wget -O "${TMP_FILE}.bz2" -i - +bzip2 -cd "${TMP_FILE}.bz2" > "${OUT_FILE}" chmod +x ${OUT_FILE} +rm "${TMP_FILE}.bz2" autorestic install -echo "Successfully installed autorestic" +echo "Successfully installed autorestic under ${OUT_FILE}"