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, ["libX11"], :libX11),
    LibraryProduct(prefix, ["libX11-xcb"], :libX11_xcb),
    LibraryProduct(prefix, ["libXau"], :libXau),
    LibraryProduct(prefix, ["libxcb-composite"], :libxcb_composite),
    LibraryProduct(prefix, ["libxcb-damage"], :libxcb_damage),
    LibraryProduct(prefix, ["libxcb-dpms"], :libxcb_dpms),
    LibraryProduct(prefix, ["libxcb-dri2"], :libxcb_dri2),
    LibraryProduct(prefix, ["libxcb-dri3"], :libxcb_dri3),
    LibraryProduct(prefix, ["libxcb-glx"], :libxcb_glx),
    LibraryProduct(prefix, ["libxcb-present"], :libxcb_present),
    LibraryProduct(prefix, ["libxcb-randr"], :libxcb_randr),
    LibraryProduct(prefix, ["libxcb-record"], :libxcb_record),
    LibraryProduct(prefix, ["libxcb-render"], :libxcb_render),
    LibraryProduct(prefix, ["libxcb-res"], :libxcb_res),
    LibraryProduct(prefix, ["libxcb-screensaver"], :libxcb_screensaver),
    LibraryProduct(prefix, ["libxcb-shape"], :libxcb_shape),
    LibraryProduct(prefix, ["libxcb-shm"], :libxcb_shm),
    LibraryProduct(prefix, ["libxcb"], :libxcb),
    LibraryProduct(prefix, ["libxcb-sync"], :libxcb_sync),
    LibraryProduct(prefix, ["libxcb-xf86dri"], :libxcb_xf86dri),
    LibraryProduct(prefix, ["libxcb-xfixes"], :libxcb_xfixes),
    LibraryProduct(prefix, ["libxcb-xinerama"], :libxcb_xinerama),
    LibraryProduct(prefix, ["libxcb-xinput"], :libxcb_xinput),
    LibraryProduct(prefix, ["libxcb-xkb"], :libxcb_xkb),
    LibraryProduct(prefix, ["libxcb-xtest"], :libxcb_xtest),
    LibraryProduct(prefix, ["libxcb-xvmc"], :libxcb_xvmc),
    LibraryProduct(prefix, ["libxcb-xv"], :libxcb_xv),
    LibraryProduct(prefix, ["libXcomposite"], :libXcomposite),
    LibraryProduct(prefix, ["libXcursor"], :libXcursor),
    LibraryProduct(prefix, ["libXdamage"], :libXdamage),
    LibraryProduct(prefix, ["libXext"], :libXext),
    LibraryProduct(prefix, ["libXfixes"], :libXfixes),
    LibraryProduct(prefix, ["libXinerama"], :libXinerama),
    LibraryProduct(prefix, ["libXi"], :libXi),
    LibraryProduct(prefix, ["libXrandr"], :libXrandr),
    LibraryProduct(prefix, ["libXrender"], :libXrender),
    LibraryProduct(prefix, ["libxshmfence"], :libxshmfence),
    LibraryProduct(prefix, ["libXtst"], :libXtst),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaBinaryWrappers/X11_jll.jl/releases/download/X11-v1.6.8+4"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/X11.v1.6.8.aarch64-linux-gnu.tar.gz", "3e0d549012b0f547e2be29434a38a9fc539bf3907563c35bb1b5b671f180eb2f"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/X11.v1.6.8.aarch64-linux-musl.tar.gz", "5e23e284f230561e9511b39a787389f1572481adefeeff67aaf8b0324b20473a"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf) => ("$bin_prefix/X11.v1.6.8.arm-linux-gnueabihf.tar.gz", "005f53458fedc6a62539fc3e8251be1db6ea9959ff664dde67a6e65894b78ef5"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/X11.v1.6.8.arm-linux-musleabihf.tar.gz", "8b23a814b230e2daf224fe50e9a04b36d0cdc330a0b46d93f6aae35d5f3bc319"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/X11.v1.6.8.i686-linux-gnu.tar.gz", "9e453780811b27923660c43aa96c7f46a7c946ac34b626fca4f0de920857b7ec"),
    Linux(:i686, libc=:musl) => ("$bin_prefix/X11.v1.6.8.i686-linux-musl.tar.gz", "70ac7542eed7d4bad979eaa84ecdb50ed735a435b3cf024b677255efc421f51e"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/X11.v1.6.8.powerpc64le-linux-gnu.tar.gz", "b3974379065f2dd3c9ef16ffc88ae57c7b6a2f9367ec2d055f527d92ceaac5ec"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/X11.v1.6.8.x86_64-linux-gnu.tar.gz", "31ccdbc12db080ea40d6c04b4197a26135dba7af63181b36ed1590e21f8b3ce1"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/X11.v1.6.8.x86_64-linux-musl.tar.gz", "f8f576c34046f84b10df0c4116ba602ac43c55409386d6c34a17c97923d4a788"),
    FreeBSD(:x86_64) => ("$bin_prefix/X11.v1.6.8.x86_64-unknown-freebsd11.1.tar.gz", "164ad28c9573225f59dd987eaf4580c8b2855a560cbe1ba443907ed08fa2f63c"),
)

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