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")))

# This is the library we care about
products = Product[
    LibraryProduct(prefix, "libpq", :LIBPQ_HANDLE),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/invenia/LibPQBuilder/releases/download/v10.3-1-4"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Windows(:i686)          => ("$bin_prefix/libpq.i686-w64-mingw32.tar.gz", "6acdd37081a3d80313263cac41e161ba6636e0d8a2f61cdae7447717f8f2aadb"),
    Windows(:x86_64)        => ("$bin_prefix/libpq.x86_64-w64-mingw32.tar.gz", "c3af66cf3f3b149dc1fcec4c4e4a6bb42d6514019e90be9b1b01e5e367725354"),
    Linux(:i686, :glibc)    => ("$bin_prefix/libpq.i686-linux-gnu.tar.gz", "89543222abea46936b9fe0426e4dab4d2fea8aff49823999100b1487dee42f6e"),
    Linux(:x86_64, :glibc)  => ("$bin_prefix/libpq.x86_64-linux-gnu.tar.gz", "f31d0f5f43acc458223b0f66711993106e98a75c7666d5839ae561981f22bb5c"),
    MacOS()                 => ("$bin_prefix/libpq.x86_64-apple-darwin14.tar.gz", "d2fa7faa26caa02790eac3743c864c3153998015a3ffdc68a229415cf2840045"),
)
# First, check to see if we're all satisfied
if any(!satisfied(p; verbose=verbose) for p in products)
    if platform_key() in keys(download_info)
        # Download and install binaries
        url, tarball_hash = download_info[platform_key()]
        install(url, tarball_hash; prefix=prefix, force=true, verbose=true)
    else
        error("Your platform $(Sys.MACHINE) is not supported by this package!")
    end

    # Finally, write out a deps.jl file
    write_deps_file(joinpath(@__DIR__, "deps.jl"), products)
end
