# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder
using SHA

name = "libpg_query"
version = v"0.0.6"

# Collection of sources required to build libpg_query
sources = [
    GitSource(
        "https://github.com/relationalai-oss/libpg_query.git",
        "c18afd7395a19a35043e1af94ac01360b8730106",
    ),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir
cd libpg_query/
make build
mkdir -p $prefix/lib
cp libpg_query.* $prefix/lib/

"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = [
    Linux(:i686, libc=:glibc),
    Linux(:x86_64, libc=:glibc),
    Linux(:i686, libc=:musl),
    Linux(:x86_64, libc=:musl),
    MacOS(:x86_64)
]

# The products that we will ensure are always built
products = [
    LibraryProduct("libpg_query", :libpg_query)
]

# Dependencies that must be installed before this package can be built
dependencies = [

]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

version_string = string(version)

function compute_file_sha(file_name)
    return open(joinpath(".", "products", file_name)) do file
        bytes2hex(sha2_256(file))
    end
end

build_jl_file = """
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, ["libpg_query"], :libpg_query),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/RelationalAI-oss/libpg_query_binary_builder/releases/download/$(version_string)"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:i686, libc=:glibc) => ("\$bin_prefix/libpg_query.v$(version_string).i686-linux-gnu.tar.gz", "$(compute_file_sha("libpg_query.v$(version_string).i686-linux-gnu.tar.gz"))"),
    Linux(:i686, libc=:musl) => ("\$bin_prefix/libpg_query.v$(version_string).i686-linux-musl.tar.gz", "$(compute_file_sha("libpg_query.v$(version_string).i686-linux-musl.tar.gz"))"),
    MacOS(:x86_64) => ("\$bin_prefix/libpg_query.v$(version_string).x86_64-apple-darwin14.tar.gz", "$(compute_file_sha("libpg_query.v$(version_string).x86_64-apple-darwin14.tar.gz"))"),
    Linux(:x86_64, libc=:glibc) => ("\$bin_prefix/libpg_query.v$(version_string).x86_64-linux-gnu.tar.gz", "$(compute_file_sha("libpg_query.v$(version_string).x86_64-linux-gnu.tar.gz"))"),
    Linux(:x86_64, libc=:musl) => ("\$bin_prefix/libpg_query.v$(version_string).x86_64-linux-musl.tar.gz", "$(compute_file_sha("libpg_query.v$(version_string).x86_64-linux-musl.tar.gz"))"),
)

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

"""

open(joinpath(".", "products", "build_libpg_query.v$(version_string).jl"), "w") do io
    write(io, build_jl_file)
end;
