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

# Download binaries from hosted location
bin_prefix = "https://github.com/SimonDanisch/EarCutBuilder/releases/download/v1.0.0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/EarCut.v1.0.0.aarch64-linux-gnu.tar.gz", "5c9b3bbbefca40653a0f8c76b4fb81c1b3ffe38a595d4fea8ceb4042e61d076c"),
    Linux(:aarch64, :musl) => ("$bin_prefix/EarCut.v1.0.0.aarch64-linux-musl.tar.gz", "33804140cbc0548e5b7b6019dde699f00887874bb20d5455f72773c6c941e096"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/EarCut.v1.0.0.arm-linux-gnueabihf.tar.gz", "5530d6c7e745721df2eebb16b4434e6bc376f4f6e954cf684209bc10a7d80590"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/EarCut.v1.0.0.arm-linux-musleabihf.tar.gz", "6ad7fc447cc12a3c78520ee6f74263a7ddbc5a22efebad177e044478e9f1c009"),
    Linux(:i686, :glibc) => ("$bin_prefix/EarCut.v1.0.0.i686-linux-gnu.tar.gz", "af984da94d01608ed4e56500d98043622f0a6d8e9c89be7f871c9952dabda39e"),
    Linux(:i686, :musl) => ("$bin_prefix/EarCut.v1.0.0.i686-linux-musl.tar.gz", "fedf73674acc407fb619b838dd92624654c4e4af2589c7aa9d162544ca9dc0b4"),
    Windows(:i686) => ("$bin_prefix/EarCut.v1.0.0.i686-w64-mingw32.tar.gz", "d7e090ca7bff14ba1fcb0063445ebe29aaf67efbcefe05438169fab65c27b11e"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/EarCut.v1.0.0.powerpc64le-linux-gnu.tar.gz", "2fc25b6c4aedba11ebb6da708046d1d798e9dd3ba6a2cf7e522f08689f616de3"),
    MacOS(:x86_64) => ("$bin_prefix/EarCut.v1.0.0.x86_64-apple-darwin14.tar.gz", "fe39007cbc391b9021b37ea16cedb478f10ecae270f3950ea87fa394acee8bd0"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/EarCut.v1.0.0.x86_64-linux-gnu.tar.gz", "12624a769191b22ae1040f8171ae8d94b5ee8cf06288c58f216337d846d1258d"),
    Linux(:x86_64, :musl) => ("$bin_prefix/EarCut.v1.0.0.x86_64-linux-musl.tar.gz", "025fa1d07a067cb6b0eed1465f549ce88aac9732a39cded2f8a547eeb80a89ec"),
    FreeBSD(:x86_64) => ("$bin_prefix/EarCut.v1.0.0.x86_64-unknown-freebsd11.1.tar.gz", "6dea8e7ddea50a4e4426fd7dafe25807a6b27b3ebbbb3c821fc03e9e2f6e23e1"),
    Windows(:x86_64) => ("$bin_prefix/EarCut.v1.0.0.x86_64-w64-mingw32.tar.gz", "e0bc2089108bc2aabfb1e11d0a7ddf40adeec501fa8d1414871c0056b927762a"),
)

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