#!/bin/bash

VER='v1.69.0'

if [[ "$1" != "" ]]; then
	VER="$1"
fi

script_dir="$(realpath $(dirname $0))"
WORK_DIR="${script_dir}"

DL_AFTER_VER_SH_F_NAME='dl.after.ver.sh'
DL_AFTER_VER_SH="${WORK_DIR}/${DL_AFTER_VER_SH_F_NAME}"

RCLONE_DL_MIRROR='https://github.com/rclone/rclone/releases/download'

DL_CMD=( 'sudo' '-u' 'www-data' 'aria2c' '-x' '16' '--continue=true' )

# MARK: Safety for not existing mounting ${VER} directory
if [ ! -d "${WORK_DIR}/${VER}" ]; then
	sudo -u www-data mkdir -p "${WORK_DIR}/${VER}" && \
	echo "Directory '${WORK_DIR}/${VER}' created."
fi

# echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-windows-amd64.zip"` #DEBUG
# exit #DEBUG

cd "${WORK_DIR}/${VER}" && \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-windows-amd64.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-osx-amd64.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-amd64.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-amd64.deb"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-amd64.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-freebsd-amd64.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-netbsd-amd64.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-openbsd-amd64.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-plan9-amd64.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-solaris-amd64.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-windows-386.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-386.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-386.deb"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-386.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-freebsd-386.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-netbsd-386.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-openbsd-386.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-plan9-386.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm.deb"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-freebsd-arm.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-netbsd-arm.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm-v6.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm-v6.deb"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm-v6.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-freebsd-arm-v6.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-netbsd-arm-v6.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm-v7.zip"` 	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm-v7.deb"` 	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm-v7.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-freebsd-arm-v7.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-netbsd-arm-v7.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-windows-arm64.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-osx-arm64.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm64.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm64.deb"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-arm64.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-mips.zip"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-mips.deb"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-mips.rpm"`		&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-mipsle.zip"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-mipsle.deb"`	&& \
echo `${DL_CMD[@]} "${RCLONE_DL_MIRROR}/${VER}/rclone-${VER}-linux-mipsle.rpm"`	&& \
chown -R www-data:www-data . && \
echo ''  && \
echo ''  && \
echo '========== Finished ========='  && \
echo ''

if [[ -f "${DL_AFTER_VER_SH}" ]];then
	echo "${DL_AFTER_VER_SH_F_NAME} exists. Running unzip and making very different type of tar balls..." && \
	"${WORKDIR}/${DL_AFTER_VER_SH}"
fi
