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

# Download binaries from hosted location
bin_prefix = "https://github.com/SimonDanisch/LAMEBuilder/releases/download/3.100.0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, :glibc) => ("$bin_prefix/liblame.v3.100.0.aarch64-linux-gnu.tar.gz", "1be606f6b7cb9a015cfcd22c3cf58ef8752b46da7edc25a5412b6b9fc34697f4"),
    Linux(:aarch64, :musl) => ("$bin_prefix/liblame.v3.100.0.aarch64-linux-musl.tar.gz", "651340c2e3e2b0231d04f023bea996285c9e396660d4f47a2f2c9a8268b8e437"),
    Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/liblame.v3.100.0.arm-linux-gnueabihf.tar.gz", "1729adb511dad94c2dc2e358438fdc90c213ad97655752c6ec66251ac063fa95"),
    Linux(:armv7l, :musl, :eabihf) => ("$bin_prefix/liblame.v3.100.0.arm-linux-musleabihf.tar.gz", "0c119cdb397603005936095a51534b4c52a5a3dcc717d781a52510d17c735511"),
    Linux(:i686, :glibc) => ("$bin_prefix/liblame.v3.100.0.i686-linux-gnu.tar.gz", "97e12758e2dfb72cf788a2f968ef82e7e1ed772564b0fa097087180bfeb3a370"),
    Linux(:i686, :musl) => ("$bin_prefix/liblame.v3.100.0.i686-linux-musl.tar.gz", "7a254261af6e6d3fe9ff3dcbad91a5d2aa22ab9b943e61171cc2d2291c616422"),
    Linux(:powerpc64le, :glibc) => ("$bin_prefix/liblame.v3.100.0.powerpc64le-linux-gnu.tar.gz", "b39355b30705e7332e3d4097ac1cd7a6c4d7d0297c589cdc31a804321fb5bee9"),
    Linux(:x86_64, :glibc) => ("$bin_prefix/liblame.v3.100.0.x86_64-linux-gnu.tar.gz", "879f3e79384d0a24408d4a49c46b1934be9d379eb21967bb2fb2786531681909"),
    Linux(:x86_64, :musl) => ("$bin_prefix/liblame.v3.100.0.x86_64-linux-musl.tar.gz", "7ab49f13b4606371f045efcb104fc439b6eb8d606ed8b0162a398138a780cba5"),
    FreeBSD(:x86_64) => ("$bin_prefix/liblame.v3.100.0.x86_64-unknown-freebsd11.1.tar.gz", "03cb5033e9984d7cd51f6169ad69b494248bdd9d94e40877a78d0e623aaea4ce"),
)

# 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_codec.jl"), products)
