|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# error codes |
| 4 | +# 0 - exited without problems |
| 5 | +# 1 - parameters not supported were used or some unexpected error occurred |
| 6 | +# 2 - OS not supported by this script |
| 7 | +# 3 - installed version of rclone is up to date |
| 8 | +# 4 - supported unzip tools are not available |
| 9 | + |
| 10 | +set -e |
| 11 | + |
| 12 | +#when adding a tool to the list make sure to also add its corresponding command further in the script |
| 13 | +unzip_tools_list=('unzip' '7z' 'busybox') |
| 14 | + |
| 15 | +usage() { echo "Usage: sudo -v ; sudo bash install-dm.sh" 1>&2; exit 1; } |
| 16 | + |
| 17 | +#check for beta flag |
| 18 | +if [ -n "$1" ] && [ "$1" != "beta" ]; then |
| 19 | + usage |
| 20 | +fi |
| 21 | + |
| 22 | +#create tmp directory and move to it with macOS compatibility fallback |
| 23 | +tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'rclone-install.XXXXXXXXXX') |
| 24 | +cd "$tmp_dir" |
| 25 | + |
| 26 | +#make sure unzip tool is available and choose one to work with |
| 27 | +set +e |
| 28 | +for tool in ${unzip_tools_list[*]}; do |
| 29 | + trash=$(hash "$tool" 2>>errors) |
| 30 | + if [ "$?" -eq 0 ]; then |
| 31 | + unzip_tool="$tool" |
| 32 | + break |
| 33 | + fi |
| 34 | +done |
| 35 | +set -e |
| 36 | + |
| 37 | +# exit if no unzip tools available |
| 38 | +if [ -z "$unzip_tool" ]; then |
| 39 | + printf "\nNone of the supported tools for extracting zip archives (${unzip_tools_list[*]}) were found. " |
| 40 | + printf "Please install one of them and try again.\n\n" |
| 41 | + exit 4 |
| 42 | +fi |
| 43 | + |
| 44 | +# Make sure we don't create a root owned .config/rclone directory #2127 |
| 45 | +export XDG_CONFIG_HOME=config |
| 46 | + |
| 47 | +#detect the platform |
| 48 | +OS="$(uname)" |
| 49 | +case $OS in |
| 50 | + Linux) |
| 51 | + OS='linux' |
| 52 | + ;; |
| 53 | + FreeBSD) |
| 54 | + OS='freebsd' |
| 55 | + ;; |
| 56 | + NetBSD) |
| 57 | + OS='netbsd' |
| 58 | + ;; |
| 59 | + OpenBSD) |
| 60 | + OS='openbsd' |
| 61 | + ;; |
| 62 | + Darwin) |
| 63 | + OS='osx' |
| 64 | + binTgtDir=/usr/local/bin |
| 65 | + man1TgtDir=/usr/local/share/man/man1 |
| 66 | + ;; |
| 67 | + SunOS) |
| 68 | + OS='solaris' |
| 69 | + echo 'OS not supported' |
| 70 | + exit 2 |
| 71 | + ;; |
| 72 | + *) |
| 73 | + echo 'OS not supported' |
| 74 | + exit 2 |
| 75 | + ;; |
| 76 | +esac |
| 77 | + |
| 78 | +OS_type="$(uname -m)" |
| 79 | +case "$OS_type" in |
| 80 | + x86_64|amd64) |
| 81 | + OS_type='amd64' |
| 82 | + ;; |
| 83 | + i?86|x86) |
| 84 | + OS_type='386' |
| 85 | + ;; |
| 86 | + aarch64|arm64) |
| 87 | + OS_type='arm64' |
| 88 | + ;; |
| 89 | + arm*) |
| 90 | + OS_type='arm' |
| 91 | + ;; |
| 92 | + *) |
| 93 | + echo 'OS type not supported' |
| 94 | + exit 2 |
| 95 | + ;; |
| 96 | +esac |
| 97 | + |
| 98 | + |
| 99 | +#download and unzip |
| 100 | +rclone_zip="/tmp/rclone-build/rclone-current-${OS}-${OS_type}.zip" |
| 101 | + |
| 102 | +unzip_dir="tmp_unzip_dir_for_rclone" |
| 103 | +# there should be an entry in this switch for each element of unzip_tools_list |
| 104 | +case "$unzip_tool" in |
| 105 | + 'unzip') |
| 106 | + unzip -a "$rclone_zip" -d "$unzip_dir" |
| 107 | + ;; |
| 108 | + '7z') |
| 109 | + 7z x "$rclone_zip" "-o$unzip_dir" |
| 110 | + ;; |
| 111 | + 'busybox') |
| 112 | + mkdir -p "$unzip_dir" |
| 113 | + busybox unzip "$rclone_zip" -d "$unzip_dir" |
| 114 | + ;; |
| 115 | +esac |
| 116 | + |
| 117 | +cd $unzip_dir/* |
| 118 | + |
| 119 | +#mounting rclone to environment |
| 120 | + |
| 121 | +case "$OS" in |
| 122 | + 'linux') |
| 123 | + #binary |
| 124 | + cp rclone /usr/bin/rclone.new |
| 125 | + chmod 755 /usr/bin/rclone.new |
| 126 | + chown root:root /usr/bin/rclone.new |
| 127 | + mv /usr/bin/rclone.new /usr/bin/rclone |
| 128 | + #manual |
| 129 | + if ! [ -x "$(command -v mandb)" ]; then |
| 130 | + echo 'mandb not found. The rclone man docs will not be installed.' |
| 131 | + else |
| 132 | + mkdir -p /usr/local/share/man/man1 |
| 133 | + cp rclone.1 /usr/local/share/man/man1/ |
| 134 | + mandb |
| 135 | + fi |
| 136 | + ;; |
| 137 | + 'freebsd'|'openbsd'|'netbsd') |
| 138 | + #binary |
| 139 | + cp rclone /usr/bin/rclone.new |
| 140 | + chown root:wheel /usr/bin/rclone.new |
| 141 | + mv /usr/bin/rclone.new /usr/bin/rclone |
| 142 | + #manual |
| 143 | + mkdir -p /usr/local/man/man1 |
| 144 | + cp rclone.1 /usr/local/man/man1/ |
| 145 | + makewhatis |
| 146 | + ;; |
| 147 | + 'osx') |
| 148 | + #binary |
| 149 | + mkdir -m 0555 -p ${binTgtDir} |
| 150 | + cp rclone ${binTgtDir}/rclone.new |
| 151 | + mv ${binTgtDir}/rclone.new ${binTgtDir}/rclone |
| 152 | + chmod a=x ${binTgtDir}/rclone |
| 153 | + #manual |
| 154 | + mkdir -m 0555 -p ${man1TgtDir} |
| 155 | + cp rclone.1 ${man1TgtDir} |
| 156 | + chmod a=r ${man1TgtDir}/rclone.1 |
| 157 | + ;; |
| 158 | + *) |
| 159 | + echo 'OS not supported' |
| 160 | + exit 2 |
| 161 | +esac |
| 162 | + |
| 163 | + |
| 164 | +#update version variable post install |
| 165 | +version=$(rclone --version 2>>errors | head -n 1) |
| 166 | + |
| 167 | +printf "\n${version} has successfully installed." |
| 168 | +printf '\nNow run "rclone config" for setup. Check https://rclone.org/docs/ for more details.\n\n' |
| 169 | +exit 0 |
| 170 | + |
0 commit comments