%D \module
%D   [      file=x-dir-05,
%D        version=2003.05.10, % around that time -)
%D          title=\CONTEXT\ Directory Handling,
%D       subtitle=Access,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\setvariables
  [filestate]
  [name=,
   base=,
   type=,
   size=,
   permissions=,
   date=]

% \savefilestate[zip-latest][context/latest/cont-#2.zip]%

% TODO: move to module namespace

\startluacode
    local filestates = { }
    function commands.savefilestate(tag,name)
        if not filestates[tag] then
            local attr = lfs.attributes(name)
            if attr then attr.name = name end
            filestates[tag] = attr
        end
    end
    function commands.getfilestatevariable(tag,name)
        local fs = filestates[tag]
        if fs then
            local value
            if name == "base" then
                value = file.basename(fs.name)
            elseif name == "type" then
                value = file.extname(fs.name)
            elseif name == "date" then
                value = os.date("%Y-%m-%d %H:%M",fs.modification)
            else
                value = fs[name] or ""
            end
            context.sprint(tex.vrbcatcodes,value)
        end
    end
\stopluacode

\def\getfilestatevariable#1#2%
  {\ctxcommand{getfilestatevariable("#1","#2")}}

\def\savefilestate
  {\dodoubleargument\dosavefilestate}

\def\dosavefilestate[#1][#2]%
  {\ctxcommand{savefilestate("#1","#2")}%
   \setxvariables
     [#1]
     [name={#2},
      base=\getfilestatevariable{#1}{base},
      type=\getfilestatevariable{#1}{type},
      size=\getfilestatevariable{#1}{size},
      date=\getfilestatevariable{#1}{date},
      permissions=\getfilestatevariable{#1}{permissions}]}

\def\getfilestate#1% old one
  {\savefilestate[filestate][#1]}

\endinput
