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

local format = string.format
local concat = table.concat

if not resolvers.initialized() then

    resolvers.reset()

    -- we now load the file database as we might need files other than
    -- tex and lua file on the given path

    resolvers.load()

    if callback then -- why not plural

        local findbinfile = resolvers.findbinfile
        local findtexfile = resolvers.findtexfile
        local opentexfile = resolvers.opentexfile
        local register    = callbacks.register

        local ioflush     = io.flush
        local ioread      = io.read
        local writenl     = texio.writenl

        local function terminal()
            writenl("\ntex console > ")
            ioflush()
            local line = ioread()
            writenl("")
            ioflush()
            return line
        end

     -- local function find_data_file(name)
     --     if not name or name == "" then
     --         return "context terminal"
     --     else
     --         return findbinfile(name,"tex")
     --     end
     -- end
     --
     -- local function open_data_file(name)
     --     if not name or name == "" or name == "context terminal" then
     --         return {
     --             reader   = terminal,
     --             noflines = 1,
     --             filename = name,
     --         }
     --     else
     --         name = opentexfile(name)
     --         return name ~= "" and name or false
     --     end
     -- end

        local function open_data_file(name)
            if not name or name == "" then
                return {
                    reader   = terminal,
                    noflines = 1,
                    filename = "context terminal"
                }
            else
                local fullname = findbinfile(name,"tex")
                if fullname then
                    return opentexfile(fullname)
                else
                    return false
                end
            end
        end

        local function find_any_file(name)
            return name
        end

        register { -- why do we have this callback if we just return the name
            name    = "find_log_file",
            action  = find_any_file,
            comment = "provide the log file name",
            frozen  = true
        }

        register { -- why do we have this callback if we just return the name
            name    = "find_format_file",
            action  = find_any_file,
            comment = "locate the format file",
            frozen  = true
        }

        register {
            name   = "open_data_file",
            action = open_data_file,
            comment = "open the given file for reading",
            frozen = true,
        }

        register {
            name    = "process_jobname",
            action  = false,
            comment = "manipulate jobname",
            frozen  = true,
        }

    end

end

statistics.register("resource resolver", function()
    local scandata = resolvers.scandata()
    return format("loadtime %s seconds, %s scans with scantime %s seconds, %s shared scans, %s found files, scanned paths: %s",
        resolvers.loadtime(),
        scandata.n,
        scandata.time,
        scandata.shared,
        #resolvers.foundintrees(),
        #scandata.paths > 0 and concat(scandata.paths," ") or "<none>"
    )
end)
