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

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaAstro/ERFABuilder/releases/download/v1.4.0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/liberfa.aarch64-linux-gnu.tar.gz", "ed2e309708bb40dda20439f9b6a7924c01e1b2b9875d2213b4d9b6669517f8b3"),
    Linux(:aarch64, :musl) => ("$bin_prefix/liberfa.aarch64-linux-musl.tar.gz", "72a4196e9a259b513fcdf62c98c8c74264b689263b5847200c796b2c7483fd65"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/liberfa.arm-linux-gnueabihf.tar.gz", "d736a6a44ce531a61dbe844ea0d6279a429bc269ee786cd32d339c5f3d8bd3e9"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/liberfa.arm-linux-musleabihf.tar.gz", "be0422495e97a64b56d35ba1c3e44317b54725cf7824f39700dc4af50904a2fd"),
    Linux(:i686, :glibc) => ("$bin_prefix/liberfa.i686-linux-gnu.tar.gz", "c75d0a48142336924e30442085f1617e2d5c48048c9a5b6045334a535893749d"),
    Linux(:i686, :musl) => ("$bin_prefix/liberfa.i686-linux-musl.tar.gz", "86db97332f22a2f057dbfcc2ce6620df1dee7297c7aaafbb285061584d09a0d5"),
    Windows(:i686) => ("$bin_prefix/liberfa.i686-w64-mingw32.tar.gz", "acc29f41824dc0db9c81c96ba11da81259f97fd87ae37d72a3d61538fc7d4e55"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/liberfa.powerpc64le-linux-gnu.tar.gz", "b56adcbcabc579e5733b91e11f158bf0e14ad4ca5fa7b1338545406b3f60d153"),
    MacOS(:x86_64) => ("$bin_prefix/liberfa.x86_64-apple-darwin14.tar.gz", "6d20ae186a6a934ee9a12bcde91d932aa2ea331387cfa6e79fd63b1e667143e0"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/liberfa.x86_64-linux-gnu.tar.gz", "3cee94d8773ed7a6737de2606758b22f3bd17fead6d8c258b4c029f104b2227b"),
    Linux(:x86_64, :musl) => ("$bin_prefix/liberfa.x86_64-linux-musl.tar.gz", "b664f8c99e1fb8caf8c0de1197ce50135b0013bd006ef3c60e9c886c542c7e88"),
    FreeBSD(:x86_64) => ("$bin_prefix/liberfa.x86_64-unknown-freebsd11.1.tar.gz", "3ced46efdf0626e5f954e3689afbb63fc1a71eed2b0e243e202ed054847122f0"),
    Windows(:x86_64) => ("$bin_prefix/liberfa.x86_64-w64-mingw32.tar.gz", "de23159790b9df6aaad1faead33aaaafc118ca1910b4baf320120c8538b0d30d"),
)

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