%D \module
%D   [       file=mtx-context-arrange,
%D        version=2009.03.21,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Arrange Files,
%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.

%D This is a \TEXEXEC\ features that has been moved to \MKIV.

% begin help
%
% usage: context --extra=arrange [options] list-of-files
%
% --sort                  : sort filenames first
% --paperoffset=dimension : left-top-offset
% --noduplex              : singlesided (doublesided is default)
% --backspace=dimension   : extra left offset
% --topspace=dimension    : extra top offset
% --marking               : add cutmarks
% --addempty=list         : add empty pages at/after (comma separated list)
% --printformat           : 2UP, etc
% --paperformat=spec      : paper*print or paperxprint
%
% example: context --extra=arrange --printformat=2UP --paperformat=A4*A3,landscape               myfile
%          context --extra=arrange --printformat=xy  --paperformat=A4*A2           --nx=2 --ny=2 myfile
% end help

\input mtx-context-common.tex

\doifdocumentargument {paperoffset} {
    \definepapersize
      [offset=\getdocumentargument{paperoffset}]
}

\doifdocumentargumentelse {noduplex} {
    \setuppagenumbering
        [alternative=doublesided]
    \setdocumentargument{sided}{doublesided}
} {
    \setdocumentargument{sided}{singlesided}
}


\setuppapersize
  [\getdocumentargument{paperformat_paper}]
  [\getdocumentargument{paperformat_print}]

% todo: autopapersize
%
% \setuppapersize
%   [A4]
%   [A3,landscape]

\setdocumentargumentdefault {textwidth} {0cm}
\setdocumentargumentdefault {backspace} {0cm}
\setdocumentargumentdefault {topspace}  {0cm}

\setuplayout
    [backspace=\getdocumentargument{backspace},
     topspace=\getdocumentargument{topspace},
     width=middle,
     height=middle,
     location=middle,
     header=0pt,
     footer=0pt]

\doifdocumentargument {marking} {yes} {
    \setuplayout
        [marking=on]
}

\startluacode
    local printformat = document.arguments.printformat or ""
    if printformat == "" then
        printformat = "normal"
    elseif string.find(printformat,"xy") then
        if false then
            context.setuplayout {
                nx = document.arguments.nx or 1,
                ny = document.arguments.ny or 1,
            }
            printformat = "XY,\\v!rotated"
        else
            context.setuppaper {
                nx = document.arguments.nx or 1,
                ny = document.arguments.ny or 1,
            }
            printformat = "XY"
        end
    elseif string.find(printformat,".*up") then
        printformat = "2UP,\\v!rotated"
    elseif string.find(printformat,".*down") then
        printformat = "2DOWN,\\v!rotated"
    elseif string.find(printformat,".*side") then
        printformat = "2SIDE,\\v!rotated"
    end
    document.setargument("printformat",printformat)
\stopluacode

\setuparranging
  [\getdocumentargument{sided},
   \getdocumentargument{printformat}]

\starttext

\startluacode
    local arguments = document.arguments
    local files     = document.files
    local noffiles  = #files
    if noffiles > 0 then
        if arguments.sort then
            table.sort(files)
        end
        local emptypages = arguments.addempty  or ""
        local textwidth  = arguments.textwidth or "0cm"
        for i=1,noffiles do
            local filename = files[i]
            if not string.find(file.basename(filename),"^mtx%-context%-") then
                context.insertpages (
                    { filename },
                    { emptypages },
                    { width = textwidth }
                )
            end
        end
    else
        context("no files given")
    end
\stopluacode

\stoptext
