Skip to content

Commit

Permalink
try other mirror if download fails
Browse files Browse the repository at this point in the history
  • Loading branch information
eminga committed Jul 15, 2018
1 parent e2a28d5 commit 807aea9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A lightweight script to install or update a TeamSpeak 3 server on Linux or FreeB
* tar
* bzip2

All other dependencies (awk, basename, cd, command, cut, dirname, echo, grep, mktemp, printf, read, sed, test, touch, uname, wc, and an sh-compatible shell) are installed by default on most systems.
All other dependencies (basename, cd, command, cut, dirname, echo, grep, mktemp, printf, read, sed, test, touch, uname, wc, and an sh-compatible shell) are installed by default on most systems.

## How to use
### Install a new TeamSpeak 3 server
Expand Down
28 changes: 20 additions & 8 deletions ts3updater.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
# Script Name: ts3updater.sh
# Author: eminga
# Version: 1.3
# Version: 1.4
# Description: Installs and updates TeamSpeak 3 servers
# License: MIT License

Expand Down Expand Up @@ -55,16 +55,28 @@ version=$(printf '%s' "$server" | jq -r '.version')
if [ "$old_version" != "$version" ]; then
echo "New version available: $version"
checksum=$(printf '%s' "$server" | jq -r '.checksum')
link=$(printf '%s' "$server" | jq -r '.mirrors | values[]')
links=$(printf '%s' "$server" | jq -r '.mirrors | values[]')

# select random mirror
i=$(printf '%s\n' "$link" | wc -l)
i=$(awk "BEGIN{srand(); printf \"%d\",(rand()*$i) + 1}")
link=$(printf '%s' "$link" | sed -n "$i"p)
# order mirrors randomly
if command -v shuf > /dev/null 2>&1; then
links=$(printf '%s' "$links" | shuf)
fi

tmpfile=$(mktemp)
echo "Downloading the file $link"
curl -Lo "$tmpfile" "$link"
i=1
n=$(printf '%s\n' "$links" | wc -l)

# try to download from mirrors until download is successful or all mirrors tried
while [ "$i" -le "$n" ]; do
link=$(printf '%s' "$links" | sed -n "$i"p)
echo "Downloading the file $link"
curl -Lo "$tmpfile" "$link"
if [ $? = 0 ]; then
i=$(( n + 1 ))
else
i=$(( i + 1 ))
fi
done

if command -v sha256sum > /dev/null 2>&1; then
sha256=$(sha256sum "$tmpfile" | cut -b 1-64)
Expand Down

0 comments on commit 807aea9

Please sign in to comment.