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, ["libcairo"], :libcairo),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaBinaryWrappers/Cairo_jll.jl/releases/download/Cairo-v1.16.0+1"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/Cairo.v1.16.0.aarch64-linux-gnu.tar.gz", "222d35c12fc2d40fb7ccc501ad50d27ccc08803c5b636cc5d83a8c8fe56c6ceb"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/Cairo.v1.16.0.aarch64-linux-musl.tar.gz", "fb49455b7d677e01e565babd7f1f7eb062acb5991aaf9c0034a5e8faea63bf47"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf) => ("$bin_prefix/Cairo.v1.16.0.arm-linux-gnueabihf.tar.gz", "69340376016f0525745fc483e3dc12962d0a1576c44f1243b64f7a56aa78bdb6"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/Cairo.v1.16.0.arm-linux-musleabihf.tar.gz", "8a9ab11c22dc7eefc58728c26857baed97004b5c0c5f4e8c4fada49fa5294cf4"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/Cairo.v1.16.0.i686-linux-gnu.tar.gz", "73bf7f7f1c2cbac14f74e16f201b963a8158aa69e797977a3cd3e5633a8281ee"),
    Linux(:i686, libc=:musl) => ("$bin_prefix/Cairo.v1.16.0.i686-linux-musl.tar.gz", "347db2e09c6e5add3202f44510036ab8f15d153dcbfb6f492b2083b2fb8d72b1"),
    Windows(:i686) => ("$bin_prefix/Cairo.v1.16.0.i686-w64-mingw32.tar.gz", "5993e7d41106670203614f887b684d22d456b36af7345a782a34c2146613bb64"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/Cairo.v1.16.0.powerpc64le-linux-gnu.tar.gz", "ca629c1b3f48f3659cca5ce953d2c9a8223da1722c122778cd61c013e7b8337e"),
    MacOS(:x86_64) => ("$bin_prefix/Cairo.v1.16.0.x86_64-apple-darwin14.tar.gz", "fbb3c311d23ccb800a1b18355fbf52328e90769e45550df98bd78342ef650f6c"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/Cairo.v1.16.0.x86_64-linux-gnu.tar.gz", "06bc332f8efd661d7806d9a02fd40813be1dd34d568df52a4e7e38e852ecfb6e"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/Cairo.v1.16.0.x86_64-linux-musl.tar.gz", "ee07ba9ce973b47429ca32384550cee97b5ff285099f06fe56939365342f836b"),
    FreeBSD(:x86_64) => ("$bin_prefix/Cairo.v1.16.0.x86_64-unknown-freebsd11.1.tar.gz", "5eab2fca884e26edc741b7be2fee542f657ac6342c59f6a630e57fa05b42e60b"),
    Windows(:x86_64) => ("$bin_prefix/Cairo.v1.16.0.x86_64-w64-mingw32.tar.gz", "d85155e3031b0d81da5320a9944821dc47e1b7f299e3c528883a44d776bb533c"),
)

# Install unsatisfied or updated dependencies:
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
dl_info = choose_download(download_info, platform_key_abi())
if dl_info === nothing && unsatisfied
    # If we don't have a 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 (\"$(Sys.MACHINE)\", parsed as \"$(triplet(platform_key_abi()))\") is not supported by this package!")
end

# If we have a download, and we are unsatisfied (or the version we're
# trying to install is not itself installed) then load it up!
if unsatisfied || !isinstalled(dl_info...; prefix=prefix)
    # Download and install binaries
    install(dl_info...; prefix=prefix, force=true, verbose=verbose)
end

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