using BinaryProvider # requires BinaryProvider 0.3.0 or later

# Parse some basic command-line arguments
const verbose = "--verbose" in ARGS
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))
products = [
    LibraryProduct(prefix, String["libbkmaxflow"], :libbkmaxflow),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/Gnimuc/BKMaxflowBuilder/releases/download/v3.0.1.1"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/BKMaxflow.v3.0.1.aarch64-linux-gnu.tar.gz", "4e34f1164d2ab6c7bfb691e6b2fb5e0cdf5f92fc61062cf3c6ac60e6e43fb8a9"),
    Linux(:aarch64, :musl) => ("$bin_prefix/BKMaxflow.v3.0.1.aarch64-linux-musl.tar.gz", "699c182be216b665310404b9b76c0f41572b613eb8110c7c4ad8848cdda62e7d"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/BKMaxflow.v3.0.1.arm-linux-gnueabihf.tar.gz", "2405eacd8ae8869e2b274fdc25e9812d997c03ae7dd720b92a8f89d290e50c75"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/BKMaxflow.v3.0.1.arm-linux-musleabihf.tar.gz", "df5103480a48474e6a12ba71ca37b577f19952c5175ba8564c9562f9d15dd39b"),
    Linux(:i686, :glibc) => ("$bin_prefix/BKMaxflow.v3.0.1.i686-linux-gnu.tar.gz", "a44e6fcc583fe1997bba6c8229472449548844575a3e90bdd6c6e8b9ddb33637"),
    Linux(:i686, :musl) => ("$bin_prefix/BKMaxflow.v3.0.1.i686-linux-musl.tar.gz", "4c80c9a4044d3a496f8a333696be21c5ec89a87f6c4df1f84d36c9e2fd86bce8"),
    Windows(:i686) => ("$bin_prefix/BKMaxflow.v3.0.1.i686-w64-mingw32.tar.gz", "dc2ac1539661c7bbbce62ac9e3bbfb5fc1622c77bb439f7acd29aa4510a99ac0"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/BKMaxflow.v3.0.1.powerpc64le-linux-gnu.tar.gz", "80011beb757ce1b2c37b111418b01c7f74f84d809da0acdb8909bd43b2855241"),
    MacOS(:x86_64) => ("$bin_prefix/BKMaxflow.v3.0.1.x86_64-apple-darwin14.tar.gz", "5a150601288704665cdac03cec1e59d8f38bc50616099743afdb405ae3e93fb3"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/BKMaxflow.v3.0.1.x86_64-linux-gnu.tar.gz", "3f4ef5f7e99be9d6b736d8e0f2d9f7cdefcf3837e78d6e91efcc430087da6300"),
    Linux(:x86_64, :musl) => ("$bin_prefix/BKMaxflow.v3.0.1.x86_64-linux-musl.tar.gz", "c6371cc21a2926e62157eb51b683ce534035c0b58fefec7c185b290a6db1e439"),
    FreeBSD(:x86_64) => ("$bin_prefix/BKMaxflow.v3.0.1.x86_64-unknown-freebsd11.1.tar.gz", "f67d5f82a5aebc4802fea33738e15b1b60d21da4f60dceb9f35ab3ff74c975b1"),
    Windows(:x86_64) => ("$bin_prefix/BKMaxflow.v3.0.1.x86_64-w64-mingw32.tar.gz", "f36f9e187cafe396b8180fafa24c262ad4647dc4b6dffc212678c819d60ecdce"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
if haskey(download_info, platform_key())
    url, tarball_hash = download_info[platform_key()]
    if unsatisfied || !isinstalled(url, tarball_hash; prefix=prefix)
        # Download and install binaries
        install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose)
    end
elseif unsatisfied
    # If we don't have a BinaryProvider-compatible .tar.gz to download, complain.
    # Alternatively, you could attempt to install from a separate provider,
    # build from source or something even more ambitious here.
    error("Your platform $(triplet(platform_key())) is not supported by this package!")
end

# Write out a deps.jl file that will contain mappings for our products
write_deps_file(joinpath(@__DIR__, "deps.jl"), products)
