#!/bin/bash

set -e

root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.."
platform=`uname -a | awk '{print tolower($1)}'`
arch=`uname -m`
version="0.13.2"
url="https://downloads.feathercoin.com/"

if [ "${platform}" == "linux" ]; then
    if [ "${arch}" == "x86_64" ]; then
        tarball_name="feathercore-${version}-linux64.tar.gz"
    fi
else
    echo "Feathercoin binary distribution not available for platform and architecture"
    exit -1
fi

binary_url="${url}/${tarball_name}"

download_bitcoind() {

    cd "${root_dir}/bin"

    echo "Downloading feathercoin: ${binary_url}"

    is_curl=true
    if hash curl 2>/dev/null; then
        curl --fail -I $binary_url >/dev/null 2>&1
    else
        is_curl=false
        wget --server-response --spider $binary_url >/dev/null 2>&1
    fi

    if test $? -eq 0; then
        if [ "${is_curl}" = true ]; then
            curl -L $binary_url > $tarball_name
        else
            wget $binary_url
        fi
        if test -e "${tarball_name}"; then
            echo "Unpacking feathercoin distribution"
            tar -xvzf $tarball_name
	    return;
        fi
    fi
    echo "Feathercoin binary distribution could not be downloaded"
    exit -1
}

download_bitcoind

exit 0
