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(joinpath(@__DIR__, "usr"), ["liblsl"], :liblsl),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/samuelpowell/LSLBuilder/releases/download/v1.13.0-b13"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc) => ("$bin_prefix/liblsl.v1.13.0-b13.aarch64-linux-gnu.tar.gz", "48c55cb92e300cfa26c08fe507fc2cc20159f04513a542678eb794040c49a452"),
    Linux(:aarch64, libc=:musl) => ("$bin_prefix/liblsl.v1.13.0-b13.aarch64-linux-musl.tar.gz", "172955b82e88ad0fb9862fa30382d7f3811896fad31f1850f99f210445373ddf"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf) => ("$bin_prefix/liblsl.v1.13.0-b13.arm-linux-gnueabihf.tar.gz", "3f63e740da7ac0977daa8469ee621c69853da5657992781d78c10e1fa63daf54"),
    Linux(:armv7l, libc=:musl, call_abi=:eabihf) => ("$bin_prefix/liblsl.v1.13.0-b13.arm-linux-musleabihf.tar.gz", "33358a73b71c34f0c8fe21b5c701169de1bbf4305357efb97270c0eea4f12d10"),
    Linux(:i686, libc=:glibc) => ("$bin_prefix/liblsl.v1.13.0-b13.i686-linux-gnu.tar.gz", "5019a298d6f3fd1f36eb9a200a721ad29cc83b9697df707061e820dcba224460"),
    Linux(:i686, libc=:musl) => ("$bin_prefix/liblsl.v1.13.0-b13.i686-linux-musl.tar.gz", "4960c3a3ec498fc3e1dc855687f78dcb7330284b0db3c2329b9b921e1360c369"),
    Windows(:i686) => ("$bin_prefix/liblsl.v1.13.0-b13.i686-w64-mingw32.tar.gz", "b2323a2fe16b2f061a4330573e41394f7cf1d09d54c0ce4f689f93f32085f41e"),
    Linux(:powerpc64le, libc=:glibc) => ("$bin_prefix/liblsl.v1.13.0-b13.powerpc64le-linux-gnu.tar.gz", "279f3460c90b01d0db68058af7c69e59c623b57136da14229dfcc237be443b92"),
    MacOS(:x86_64) => ("$bin_prefix/liblsl.v1.13.0-b13.x86_64-apple-darwin14.tar.gz", "ef3f353ee80856d47178b5a9aff579cae66bcb81381300cda3d59b79c1e4e3d8"),
    Linux(:x86_64, libc=:glibc) => ("$bin_prefix/liblsl.v1.13.0-b13.x86_64-linux-gnu.tar.gz", "65e5d90fdc40024f404f553fcb49a285cceb27297c6b93482719a016460b6375"),
    Linux(:x86_64, libc=:musl) => ("$bin_prefix/liblsl.v1.13.0-b13.x86_64-linux-musl.tar.gz", "40ccd453480aa09c5dfca5df73efda9f4a23468ddd4702597004c427eb6e491b"),
    FreeBSD(:x86_64) => ("$bin_prefix/liblsl.v1.13.0-b13.x86_64-unknown-freebsd11.1.tar.gz", "2a133452203fc808c2c62413851a116815a98eb7d40fdfc181806a837ed20054"),
    Windows(:x86_64) => ("$bin_prefix/liblsl.v1.13.0-b13.x86_64-w64-mingw32.tar.gz", "eac664995173453adfb2145f80a1923a28be5a7f8123c675629be297d73ad234"),
)

# 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

# Cludge official binary library to avoid problems with BinaryBuilder
@static if Sys.iswindows()

    dllprefix = joinpath(@__DIR__, "usr") 
    exe7z = joinpath(Sys.BINDIR, "7z.exe")
    dlpath = joinpath(@__DIR__, "usr", "downloads", "liblsl-1.13.0-official.7z")
    
    if Sys.WORD_SIZE == 64
        dlurl = "https://github.com/sccn/liblsl/releases/download/1.13.0-b13/liblsl-1.13.0-Win64.7z"
        dllfn = "liblsl64.dll"
    else
        dlurl = "https://github.com/sccn/liblsl/releases/download/1.13.0-b13/liblsl-1.13.0-Win32.7z"
        dllfn = "liblsl32.dll" 
    end

    download(dlurl, dlpath)

    run(`$exe7z e $dlpath -o$(dllprefix) $dllfn -r -aoa`)
    mv(joinpath(dllprefix, dllfn), joinpath(dllprefix, "liblsl.dll"), force=true)
end

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