using BinaryProvider

# 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["DllMain"], :dllmain),
    LibraryProduct(prefix, String["EnvConst"], :envconst),
    LibraryProduct(prefix, String["AstroFunc"], :astrofunc),
    LibraryProduct(prefix, String["TimeFunc"], :timefunc),
    LibraryProduct(prefix, String["Tle"], :tle),
    LibraryProduct(prefix, String["Sgp4Prop"], :sgp4prop),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/benelsen/SAASGP4Builder.jl/releases/download/v7.8-0"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    BinaryProvider.Linux(:x86_64, :glibc) => ("$bin_prefix/SAASGP4.x86_64-linux-gnu.tar.gz", "15369188833880f827e1d536ee30d6aabb8bd6a359699f544aa941ac83fdd1bb"),
)

# First, check to see if we're all satisfied
if any(!satisfied(p; verbose=verbose) for p in products)
    if haskey(download_info, platform_key())
        # Download and install binaries
        url, tarball_hash = download_info[platform_key()]
        install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose)
    else
        # 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 more even more ambitious here.
        error("Your platform $(Sys.MACHINE) is not supported by this package!")
    end
end

# Write out a deps.jl file that will contain mappings for our products
write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose)
