added rclone install script

trantor
David Kebler 2021-01-18 15:33:14 -08:00
parent 470ba6f55c
commit e1822f681e
1 changed files with 171 additions and 0 deletions

View File

@ -0,0 +1,171 @@
#!/bin/bash
rclone_install () {
local INSTALL_DIR="/opt"
local BIN_DIR="/opt/bin"
local FORCE=false
local VERSION=current
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'i:b:v:f' OPTION; do
case "$OPTION" in
i)
INSTALL_DIR=$OPTARG
;;
b)
BIN_DIR=$OPTARG
;;
v)
echo "Installing Beta"
VERSION=$OPTARG
;;
f)
echo "FORCING download/overwrite"
FORCE=true
;;
esac
done
shift $(( OPTIND - 1 ))
# error codes
# 0 - exited without problems
# 1 - parameters not supported were used or some unexpected error occurred
# 2 - OS or Architecture not supported by this script
# 3 - installed version of rclone is up to date
# 4 - supported unzip tools are not available
# 5 - unable to install without sudo permission
[[ ! -w "$INSTALL_DIR" ]] && echo $INSTALL is not writable by user $USER, can\'t install there && exit 5
[[ ! -w "$BIN_DIR" ]] && echo $BIN_DIR is not writable by user $USER, won\'t be able to link binary there && exit 5
INSTALL_DIR=$INSTALL_DIR/rclone
# if [[ $EUID != 0 ]]; then
# sudo "$0" "$@"
# exit $?
# fi
#detect the platform
set +e
OS="`uname`"
case $OS in
Linux)
OS='linux'
;;
*)
echo 'Only Linux OS not supported by this script'
exit 2
;;
esac
OS_type="`uname -m`"
case $OS_type in
x86_64|amd64)
OS_type='amd64'
;;
i?86|x86)
OS_type='386'
;;
arm*)
OS_type='arm'
;;
aarch64)
OS_type='arm64'
;;
*)
echo 'OS architecture not supported'
exit 2
;;
esac
set -e
#when adding a tool to the list make sure to also add its corresponding command further in the script
# exit if no unzip tools available
[[ -z "unzip" ]] && printf "Please install unzip and try again.\n\n" && exit 4
# Make sure we don't create a root owned .config/rclone directory #2127
# export XDG_CONFIG_HOME=config
#check installed version of rclone to determine if update is necessary
# installed_version=`rclone --version 2>>errors | head -n 1`
# if [ -z "${install_beta}" ]; then
# current_version=`curl https://downloads.rclone.org/version.txt`
# else
# current_version=`curl https://beta.rclone.org/version.txt`
# fi
#
# if [ "$version" = "$current_version" ]; then
# printf "\nThe latest ${install_beta}version of rclone ${version} is already installed.\n\n"
# exit 3
# fi
echo Installing to $INSTALL_DIR
mkdir -p $INSTALL_DIR
cd $INSTALL_DIR
# mv rclone.bin rclone.bin.prev
#create tmp directory and move to it with macOS compatibility fallback
# mkdir $INSTALL_DIR/zip
# $(mktemp -d -f rclone-zip)
# cd $zip_dir
# echo made temp
#download and unzip
if [[ $VERSION = "current" ]]; then
download_link="https://downloads.rclone.org/rclone-current-$OS-$OS_type.zip"
rclone_zip="rclone-current-$OS-$OS_type.zip"
else
download_link="https://beta.rclone.org/rclone-beta-latest-$OS-$OS_type.zip"
rclone_zip="rclone-beta-latest-$OS-$OS_type.zip"
fi
echo downloading rclone binary archive $download_link
# curl -O $download_link
echo expanding archive
unzip -j -o -a $rclone_zip
mkdir -p $INSTALL_DIR/docs 2> /dev/null || true
rm $INSTALL_DIR/docs/*.* 2> /dev/null || true
echo moving documentation files to docs/
mv *.1 *.txt README.* $INSTALL_DIR/docs/ 2> /dev/null || true
# rm *.zip
echo preparing customized rclone command
rm -rf rclone.bin
mv rclone rclone.bin
chown :sudo rclone.bin
chmod 775 rclone.bin
touch $INSTALL_DIR/rclone
touch $INSTALL_DIR/rclone.conf
mkdir $INSTALL_DIR/cache 2> /dev/null || true
echo '#!/bin/bash' > $INSTALL_DIR/rclone
CMD=$(echo $INSTALL_DIR/rclone.bin \
--config=$INSTALL_DIR/rclone.conf \
--cache-dir=$INSTALL_DIR/cache \
'$@' \
)
echo "$CMD" >> $INSTALL_DIR/rclone
chown :sudo rclone
chmod 775 rclone
rm $BIN_DIR/rclone
ln -s $INSTALL_DIR/rclone $BIN_DIR/rclone
echo -e "rclone customized command can be launched from echo where: $(command -v rclone) \n $CMD"
cd ..
#update version variable post install
version=`rclone --version 2>>errors | head -n 1`
printf "\n${version} has successfully installed."
printf '\nNow run "rclone config" for setup. Check https://rclone.org/docs/ for more details.\n\n'
exit 0
}