using BinaryProvider

# 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 = Product[
    LibraryProduct(prefix, String["libbid"], :libbid),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/quinnj/DecFPBuilder/releases/download/v0.4"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    BinaryProvider.Linux(:i686, :glibc) => ("$bin_prefix/DecFP.i686-linux-gnu.tar.gz", "7a6f2460ffdf4f07a5b03246400a65f57f7d378490252af6b67f25da2df5b53d"),
    BinaryProvider.Windows(:i686) => ("$bin_prefix/DecFP.i686-w64-mingw32.tar.gz", "f7dbf57fce1450d1eaf891580491a1a982b8c3491b9e2aba7992d62da89d202d"),
    BinaryProvider.MacOS() => ("$bin_prefix/DecFP.x86_64-apple-darwin14.tar.gz", "b4fe9a370eb4b9bbe486c9a93a0aae0c1f1cca51e74d9b39bd7cdb0d9553e221"),
    BinaryProvider.Linux(:x86_64, :glibc) => ("$bin_prefix/DecFP.x86_64-linux-gnu.tar.gz", "ac336480e4c0200ff0d320e68f30feb40b2894105b5eaf49ae7b4ddf73078a67"),
    BinaryProvider.Windows(:x86_64) => ("$bin_prefix/DecFP.x86_64-w64-mingw32.tar.gz", "25eceff1f2b4d39042704f9a44a0295032d481d4240ec22e8000fc38ccf19be7"),
)

# First, check to see if we're all satisfied
if any(!satisfied(p; verbose=verbose) for p in products)
    if haskey(download_info, platform_key())
        # Download and install binaries
        url, tarball_hash = download_info[platform_key()]
        install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose)
    else
        # 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 more even more ambitious here.
        error("Your platform $(Sys.MACHINE) is not supported by this package!")
    end
end

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