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

local lower, format, gsub = string.lower, string.format, string.gsub
local suffixonly, basename, removesuffix = file.suffix, file.basename, file.removesuffix

local environment  = environment
local context      = context
local implement    = interfaces.implement

local report_files = logs.reporter("system","files")

function environment.initializefilenames()

    local arguments      = environment.arguments

    local jobname        = arguments.jobname
    local fulljobname    = arguments.fulljobname
    local inputfilename  = arguments.input

    if not fulljobname then
        fulljobname = inputfilename or tex.jobname
    end
    if not inputfilename then
        inputfilename = fulljobname
    end
    if not jobname then
        jobname = removesuffix(fulljobname)
    end

    local inputfilebase  = basename(inputfilename)
    local outputfilename = type(arguments.result) =="string" and arguments.result or removesuffix(fulljobname)

--     local inputfilename  = suffixonly(inputfilename) == "tex" and removesuffix(inputfilename) or inputfilename or ""

    local filename       = fulljobname
    local suffix         = suffixonly(filename)

    local filename       = ctxrunner.resolve(filename) -- in case we're prepped

    local jobfilename    = jobname -- alias
    local jobfilebase    = basename(jobfilename)

 -- jobfilename          = gsub(jobfilename,  "^./","")
 -- inputfilename        = gsub(inputfilename,"^./","")

    environment.jobfilefullname   = fulljobname
    environment.jobfilename       = jobfilebase
    environment.jobfilesuffix     = lower(suffixonly(jobfilebase))

    environment.inputfilename     = inputfilename -- so here we keep e.g. ./ or explicit paths
    environment.inputfilebarename = removesuffix(inputfilebase)
    environment.inputfilesuffix   = lower(suffixonly(inputfilebase))

    environment.outputfilename    = outputfilename or environment.inputfilebarename or ""

    environment.filename          = filename
    environment.suffix            = suffix

 -- if tex then
 --     tex.jobname = jobfilename
 -- end

    report_files("jobname %a, input %a, result %a",jobfilename,inputfilename,outputfilename)

    function environment.initializefilenames() end
end

-- we could set a macro (but will that work when we're expanding? needs testing!)

implement { name = "operatingsystem",     public = true, actions = function() context(os.platform)                     end }
implement { name = "jobfilefullname",     public = true, actions = function() context(environment.jobfilefullname)     end }
implement { name = "jobfilename",         public = true, actions = function() context(environment.jobfilename)         end }
implement { name = "jobfilesuffix",       public = true, actions = function() context(environment.jobfilesuffix)       end }
implement { name = "inputfilebarename",   public = true, actions = function() context(environment.inputfilebarename)   end }
implement { name = "inputfilerealsuffix", public = true, actions = function() context(environment.inputfilerealsuffix) end }
implement { name = "inputfilesuffix",     public = true, actions = function() context(environment.inputfilesuffix)     end }
implement { name = "inputfilename",       public = true, actions = function() context(environment.inputfilename)       end }
implement { name = "outputfilename",      public = true, actions = function() context(environment.outputfilename)      end }

implement {
    name      = "systemlog",
    arguments = "3 strings",
    public    = true,
    protected = true,
    actions   = function(whereto,category,text)
        logs.system(whereto,"context",tex.jobname,category,text)
    end,
}
