%D \module
%D   [       file=file-job, % copied from main-001, later core-job
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Job Handling,
%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.

\writestatus{loading}{ConTeXt File Macros / Job Handling}

\unprotect

%D This module delegates most of the work to \LUA\ and therefore also let it
%D define the commands, which is more efficient.

\ifdefined\textlevel \else \newinteger\textlevel \fi % used at lua end

\registerctxluafile{data-hsh}{autosuffix}
\registerctxluafile{file-job}{autosuffix}

%D Here are some helpers for processing and path control. In the following example
%D the lookup order is \type {.}, {/foo}, \type {foo/bar} as one can always
%D explicitly prefix a wanted file.
%D
%D \starttyping
%D \pushpath[foo]
%D     \pushpath[foo/bar]
%D         \input test
%D     \poppath
%D \poppath
%D \stoptyping

% \usepath           [#path] % defined at lua end
% \usesubpath        [#path] % defined at lua end
% \pushpath          [#path] % defined at lua end
% \poppath                   % defined at lua end
% \resetpath                 % defined at lua end (not the pushed paths)
% \allinputpaths             % defined at lua end
% \doifelseinputfile #name   % defined at lua end
% \processfile       #name   % defined at lua end

\aliased\let\doifinputfileelse\doifelseinputfile

% helper (not really needed nowadays)

\mutable\lettonothing\locatedfilepath

\permanent\protected\def\locatefilepath#name%
  {\edef\locatedfilepath{\clf_locatefilepath{#name}}} % todo: set at the lua end, catcodes and such

% % maybe:
%
% % \dirlist{c:/data/temporary/foo}{images/bar.jpg}
% % \dirfile{c:/data/temporary/foo}{images/bar.jpg}
%
% \def\dirlist#1#2{dirlist://#1/**/#2}
% \def\dirfile#1#2{dirlist://#1/**/#2?option=fileonly}

% zip files are tree'd
% exa stuff might become obsolete:

% \usezipfile   [#zipname]#spacer[#subtree] % defined at lua end
% \loadexamodes [#name]                     % defined at lua end

\protected\def\syst_files_load
  {\syst_files_load_indeed\f!newfilename  % new code, to be integrated at some point, plus fixes posted on the list
   \syst_files_load_indeed\f!locfilename  % new code, somewhat experimental, not distributed (outside the dev group)
   \syst_files_load_indeed\f!expfilename  % new code, very experimental, can be engine specific, mostly for me only
   \syst_files_load_indeed\f!sysfilename  % local settings, but probably not that good an idea to use
  %\syst_files_load_indeed\f!fntfilename  % maybe some day, can load goodies and patches
  }

\def\syst_files_load_indeed#name% from now on we assume a suffix to be part of the name
  {\readsysfile{#name}{\showmessage\m!system2{#name}}\donothing}

% document structure

\ifdefined\strc_pagenumbers_flush_final_page \else \let\strc_pagenumbers_flush_final_page\relax \fi % ugly

\permanent\protected\def\dostarttext
  {\enforced\glet\dostarttext\relax
   \expand\everystarttext
   \global\everystarttext\emptytoks}

\permanent\protected\def\dostoptext
  {\enforced\glet\dostoptext\relax
   \strc_pagenumbers_flush_final_page
   \page % hm, bonus
   \expand\everystoptext
   \global\everystoptext\emptytoks
   \expand\everybye
   \global\everybye\emptytoks
   \expand\everygoodbye
   \global\everygoodbye\emptytoks
   \ifempty\everynotabene\else
     \writeline
     \expand\everynotabene
     \writeline
   \fi
   % tex's \end:
   \normalend}

% \starttext     % defined at lua end
% \stoptext      % defined at lua end
% \forcequitjob  % defined at lua end
% \autostarttext % defined at lua end
% \autostoptext  % defined at lua end

\pushoverloadmode
    \aliased\let\end\forceendjob
\popoverloadmode

\aliased\let\finishjob\stoptext % nicer in luatex call commandline

\newtoks\everystartnotext
\newtoks\everystopnotext

\permanent\protected\def\startnotext{\expand\everystartnotext}
\permanent\protected\def\stopnotext {\expand\everystopnotext\normalend}

% protect structure

% \processfilemany  {#name} % defined at lua end
% \processfileonce  {#name} % defined at lua end
% \processfilenone  {#name} % defined at lua end

% \useproject       [#name] % defined at lua end
% \useproduct       [#name] % defined at lua end
% \usecomponent     [#name] % defined at lua end
% \useenvironment   [#name] % defined at lua end

% \project          [#name] or #name<space> % defined at lua end
% \product          [#name] or #name<space> % defined at lua end
% \component        [#name] or #name<space> % defined at lua end
% \environment      [#name] or #name<space> % defined at lua end

% \startproject     [#name] or #name<space> % defined at lua end
% \startproduct     [#name] or #name<space> % defined at lua end
% \startcomponent   [#name] or #name<space> % defined at lua end
% \startenvironment [#name] or #name<space> % defined at lua end

% \currentproject            % defined at lua end
% \currentproduct            % defined at lua end
% \currentcomponent          % defined at lua end
% \currentenvironment        % defined at lua end
% \processedfile             % defined at lua end
% \processedfiles            % defined at lua end
%
% \dostarttextfile    #name  % defined at lua end
% \dostoptextfile            % defined at lua end
%
% \loadtexfile       [#name] % defined at lua end
% \loadluafile       [#name] % defined at lua end
% \loadcldfile       [#name] % defined at lua end
% \loadanyfile       [#name] % defined at lua end
%
% \loadtexfileonce   [#name] % defined at lua end
% \loadluafileonce   [#name] % defined at lua end
% \loadcldfileonce   [#name] % defined at lua end
% \loadanyfileonce   [#name] % defined at lua end

%D Handy for modules that have a test/demo appended (true added).

% \permanent\protected\def\continueifinputfile#name{\clf_doifelsecontinuewithfile{#name}\relax\endinput} % we cannot do \endinput via lua

% \continueifinputfile #name % defined at lua end

% \startproject test
%     1: \startmode[*project] project \stopmode \endgraf
%     2: \startmode[*product] product \stopmode \endgraf
% \stopproject

% A bit weird place:

\neverypar\emptytoks

%D Wrappers. The \type {\endinput} is tricky because we call macros at the \LUA\
%D end. In \LMTX\ we now do an immediate one, so the ending happens here now. It
%D is different that in \MKIV, but you can compare the code.

\newsystemmode\v!project
\newsystemmode\v!product
\newsystemmode\v!component
\newsystemmode\v!environment

\permanent\protected\defcsname\e!start:\v!project:\v!global\endcsname
  {%starttext
   \pushsystemmode\v!project
   \setsystemmode\v!project}

\permanent\protected\defcsname\e!stop:\v!project:\v!global\endcsname
  {\popsystemmode\v!project
   %stoptext
   \endinput}

\permanent\protected\defcsname\e!start:\v!product:\v!global\endcsname
  {\starttext
   \pushsystemmode\v!product
   \setsystemmode\v!product}

\permanent\protected\defcsname\e!stop:\v!product:\v!global\endcsname
  {\popsystemmode\v!product
   \stoptext
   \endinput}

\permanent\protected\defcsname\e!start:\v!component:\v!global\endcsname
  {\starttext
   \pushreferenceprefix\currentcomponent
   \pushsystemmode\v!component
   \setsystemmode\v!component}

\permanent\protected\defcsname\e!stop:\v!component:\v!global\endcsname
  {\popsystemmode\v!component
   \popreferenceprefix
   \stoptext
   \endinput}

\permanent\protected\defcsname\e!start:\v!component:\v!local\endcsname
  {\starttext
   \pushreferenceprefix\currentcomponent
   \pushsystemmode\v!component
   \setsystemmode\v!component}

\permanent\protected\defcsname\e!stop:\v!component:\v!local\endcsname
  {\popsystemmode\v!component
   \popreferenceprefix
   \stoptext}

\permanent\protected\defcsname\e!start:\v!environment:\v!global\endcsname
  {\pushsystemmode\v!environment
   \setsystemmode\v!environment}

\permanent\protected\defcsname\e!stop:\v!environment:\v!global\endcsname
  {\popsystemmode\v!environment
   \endinput}

%D Upgraded after the ctx 2020 meeting:
%D
%D \starttyping
%D \definedocument[thesis]
%D
%D \setupdocument[a=b,e=f]
%D \setupdocument[thesis][a=x,c=d]
%D
%D % \startsetups[thesis:start]
%D \startsetups[document:start]
%D     START
%D \stopsetups
%D
%D \startsetups[thesis:stop]
%D % \startsetups[document:stop]
%D     STOP
%D \stopsetups
%D
%D % \startdocument[thesis]
%D \startthesis
%D     (\currentdocument/a): \documentvariable{a}\par
%D     (\currentdocument/c): \documentvariable{c}\par
%D     (\currentdocument/e): \documentvariable{e}\par
%D \stopthesis
%D % \stopdocument
%D \stoptyping

\installcorenamespace {document}

\installcommandhandler \??document \s!document \??document

\setupdocument
  [\c!before=\directsetup{\s!document:\v!start},
   \c!after =\directsetup{\s!document:\v!stop}]

% to be discussed:
%
% \setupdocument
%   [metadata:title=\documentvariable\c!title,
%    metadata:subject=\documentvariable\c!subtitle,
%    metadata:subtitle=\documentvariable\c!subtitle,
%    metadata:author=\documentvariable\c!author,
%    metadata:authors=\documentvariable\c!author,
%    metadata:keyword=\documentvariable\c!keyword,
%    metadata:keywords=\documentvariable\c!keyword,
%    metadata:date=\documentvariable\c!date]

\def\syst_document_setup#1%
  {\directsetup{\doifelsesetups{\currentdocument:\v!start}\currentdocument\s!document:\v!start}}

\appendtoks
    \expanded {%
        \startsetups[\currentdocument:\v!start]\noexpand\directsetup{\ifempty\currentdocumentparent\s!document\else\currentdocumentparent\fi:\v!start}\stopsetups
        \startsetups[\currentdocument:\v!stop ]\noexpand\directsetup{\ifempty\currentdocumentparent\s!document\else\currentdocumentparent\fi:\v!stop }\stopsetups
        \permanent\defcsname\e!start\currentdocument\endcsname{\startdocument[\currentdocument]}%
        \permanent\defcsname\e!stop \currentdocument\endcsname{\stopdocument}
    }%
\to \everydefinedocument

% todo: dostarttagged\t!document

\permanent\tolerant\protected\def\startdocument[#S#namespace]#spacer[#S#settings]%
  {\pushmacro\currentdocument
   \ifparameters
   \or
     \ifhastok={#namespace}%
       \lettonothing\currentdocument
       \setupcurrentdocument[#namespace]%
     \else
       \cdef\currentdocument{#namespace}%
     \fi
   \or
     \cdef\currentdocument{#namespace}%
     \setupcurrentdocument[#settings]%
   \fi
   \ifempty\currentdocument\else\setmode\currentdocument\fi
   \starttext
   \expand\everystartdocument
   \documentvariable\c!before}

\permanent\protected\def\stopdocument % todo: dostoptagged\t!document
  {\documentvariable\c!after
   \expand\everystopdocument
   \stoptext
   \ifempty\currentdocument\else\resetmode\currentdocument\fi
   \popmacro\currentdocument}

\aliased            \let\documentvariable           \documentparameter
\permanent\protected\def\unexpandeddocumentvariable{\documentparameter}

\permanent\protected\def\doifelsedocumentvariable#name{\doifelsesomething{\documentvariable{#name}}}
\permanent\protected\def\doifdocumentvariable    #name{\doifsomething    {\documentvariable{#name}}}
\permanent\protected\def\doifnotdocumentvariable #name{\doifnot          {\documentvariable{#name}}}

\aliased\let\doifdocumentvariableelse\doifelsedocumentvariable

\permanent\tolerant\protected\def\presetdocument[#S#namespace]#spacer[#S#settings]%
  {\pushmacro\currentdocument
   \ifparameters
   \or
     \ifhastok={#namespace}%
       \lettonothing\currentdocument
       \setupcurrentdocument[#namespace]%
     \fi
   \or
     \cdef\currentdocument{#namespace}%
     \setupcurrentdocument[#settings]%
   \fi
   \popmacro\currentdocument}

% Bonus:

% \installcorenamespace{samplefile}
%
% \protected\def\samplefile#1%
%   {\ifcsname\??samplefile#1\endcsname \else
%      \xdefcsname\??samplefile#1\endcsname{\cldloadfile{#1}}%
%    \fi
%    \lastnamedcs}

% \permanent\protected\def\samplefile#1{\clf_samplefile{#1}} % defined at the lua end

% \adddefaultsuffix[foo,{},crap]
% \adddefaultsuffix[{}]
%
% \typefile{./crapcrap}
% \typefile{crapcrap}


\protect \endinput