if not modules then modules = { } end modules ['libs-imp-zstd'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkxl",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- Instead of linking in libs like this we now do them optional. After all, once
-- we start adding more and more stuff statically we en dup with a mess. In fact,
-- this lib was the reason for no longer embedding lzo and lz4.

-- c:/data/develop/tex-context/tex/texmf-win64/bin/lib/luametatex/lua/copies/curl/libzstd.dll

-- require("libs-imp-zstd.lmt") -- only loads
--
-- local zstd = require("zstd") -- activates
--
-- local data = io.loaddata("t:/sources/char-def.lua")
-- local comp = zstd.compress  (data)
-- local back = zstd.decompress(comp)
--
-- print(#data,#comp,#back,#back==#data)

local libname = "zstd"
local libfile = "libzstd"

local zstdlib = resolvers.libraries.validoptional(libname)

if not zstdlib then return end

local zstd_compress   = zstdlib.compress
local zstd_decompress = zstdlib.decompress

local function okay()
    if resolvers.libraries.optionalloaded(libname,libfile) then
        okay = function() return true end
    else
        okay = function() return false end
    end
    return okay()
end

local zstd = {
    compress   = function (s) return okay() and zstd_compress  (s) end,
    decompress = function (s) return okay() and zstd_decompress(s) end,
}

package.loaded[libname] = zstd

return zstd

-- local foreign   = optional.loaded.foreign
-- local newbuffer = foreign.newbuffer
-- local getbuffer = foreign.getbuffer
--
-- local zstd = foreign.load("libzstd")
--
-- local ZSTD_compressBound = zstd:register {
--     name      = "ZSTD_compressBound",
--     result    = "int",
--     arguments = { "int" },
-- }
--
-- local ZSTD_getFrameContentSize = zstd:register {
--     name      = "ZSTD_getFrameContentSize",
--     result    = "int",
--     arguments = { "pointer", "int" },
-- }
--
-- local ZSTD_compress = zstd:register {
--     name      = "ZSTD_compress",
--     result    = "int",
--     arguments = { "pointer", "int", "string", "int", "int" },
--     arguments = { "pointer", "int", "pointer", "int", "int" },
-- }
--
-- local ZSTD_decompress = zstd:register {
--     name      = "ZSTD_decompress",
--     result    = "int",
--     arguments = { "pointer", "int", "string", "int" },
-- }
--
-- local function zstd_compress(source,level)
--     local sourcesize = #source
--     local targetsize = ZSTD_compressBound(sourcesize)
--     local target     = newbuffer(targetsize)
--     local result     = ZSTD_compress(target,targetsize,source,sourcesize,tonumber(level) or 3)
--     return getbuffer(target,result)
-- end
--
-- local function zstd_decompress(source)
--     local sourcesize = #source
--     local targetsize = ZSTD_getFrameContentSize(source,sourcesize)
--     local target     = newbuffer(targetsize)
--     local result     = ZSTD_decompress(target,targetsize,source,sourcesize)
--     return getbuffer(target,result)
-- end
