set -e

# This script will write a temporary file. Delete it no matter how the script
# terminates.
filename=/tmp/salvus_downloader.$$
trap 'rm -f "/tmp/salvus_downloader.$$"' 0
trap 'exit $?' 1 2 3 15

# Detect architecture
arch=$(uname -m)

if [[ "$OSTYPE" == "linux-gnu" || "$OSTYPE" == "linux" ]]; then
    if [[ "$arch" == "aarch64" || "$arch" == "arm64" ]]; then
        url="https://get.mondaic.com/salvus_downloader_linux_aarch64"
    else
        url="https://get.mondaic.com/salvus_downloader_linux"
    fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
    if [[ "$arch" == "aarch64" || "$arch" == "arm64" ]]; then
        url="https://get.mondaic.com/salvus_downloader_osx_aarch64"
    else
        url="https://get.mondaic.com/salvus_downloader_osx"
    fi
else
    echo "Unsupported operating system."
    exit 1
fi

curl $url -o $filename -#
chmod +x $filename
$filename "$@"
