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

# Download binaries from hosted location
bin_prefix = "https://github.com/ax1ine/GSW/releases/download/v0.1.0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.aarch64-linux-gnu.tar.gz", "0f9a8b5efad03240fc3e9564ec778386a70785fc1b8817f7261a2fb0252287ca"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.aarch64-linux-musl.tar.gz", "8cc63777b3c805cabc8f0f356a0ae39a643125b744747862cc626e9825fe0ceb"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.arm-linux-musleabihf.tar.gz", "4186cc2742f4a23886a386ccc2c6da64a3723b43fa71ebc917b6cca2e927ffb9"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.i686-linux-gnu.tar.gz", "2c1ed97d14ce8d8a20be9e76a302d712d64c89adb77320400bcb383d564d3e1b"),
    Linux(:i686, libc=:musl) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.i686-linux-musl.tar.gz", "aa2a980f11b93199687f23731995a66de2931f0a4cfc6b41fb71685ba46206eb"),
    Windows(:i686) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.i686-w64-mingw32.tar.gz", "d51430ced9abb951f13f391ba57fb838a1de04b6459191dc729460f0093af15d"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.powerpc64le-linux-gnu.tar.gz", "7c8bb59ef5d150c1a9b105e51dd1789296384bc050287b954eeebb66404c15bf"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.x86_64-linux-gnu.tar.gz", "3dfbc3d88176e9a2db62ca70498236226fe5303b6c218928ae549c64d9525b27"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.x86_64-linux-musl.tar.gz", "b75d1d866dac0b859fdb9a92a157cf3dfe14f5a64963087c01cfaeecc69cede0"),
    FreeBSD(:x86_64) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.x86_64-unknown-freebsd11.1.tar.gz", "b07ed41554de85b1a426abb9bc1ffc4af694e5bcc39bf64a8f1161f07bd82568"),
    Windows(:x86_64) => ("$bin_prefix/GSW_BinaryBuilder.v0.1.0.x86_64-w64-mingw32.tar.gz", "109047f1c97867c6c5f763c2d9f4e2795b72e44b4d7bce561d03d98f8bcf3ab7"),
)

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