%D \module
%D   [       file=mtx-context-compare,
%D        version=2015.07.14,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Compare 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.

% begin help
%
% usage: context --extra=compare [options] file-1 file-2
%
% example: context --extra=compare file1.pdf file-2.pdf
%          context --extra=compare file1.pdf file-2.pdf --colors=red,blue
%
% end help

\input mtx-context-common.tex

\starttext

% \setupinteraction
%   [state=start]

% \showmakeup

\definecolor[colorred]    [r=1,a=1,t=.5]
\definecolor[colorgreen]  [g=1,a=1,t=.5]
\definecolor[colorblue]   [b=1,a=1,t=.5]
\definecolor[colorgray]   [s=0,a=1,t=.5]
\definecolor[colorcyan]   [g=1,b=1,a=1,t=.5]
\definecolor[colormagenta][r=1,b=1,a=1,t=.5]
\definecolor[coloryellow] [r=1,g=1,a=1,t=.5]

% \definecolor[colorone][black] % no, keep undefined !
% \definecolor[colortwo][black] % no, keep undefined !

% \defineviewerlayer[one] % we use filename as title
% \defineviewerlayer[two] % we use filename as title

\starttexdefinition unexpanded ShowBoth #1#2#3
    \startTEXpage[width=21cm]
        \startoverlay
            {
                \startviewerlayer[one]
                    \signalcharacter\externalfigure[#1][page=#3,width=21cm,foregroundcolor=colorone]
                \stopviewerlayer
            }
            {
                \startviewerlayer[two]
                    \signalcharacter\externalfigure[#2][page=#3,width=21cm,foregroundcolor=colortwo]
                \stopviewerlayer
            }
        \stopoverlay
    \stopTEXpage
\stoptexdefinition

\starttexdefinition unexpanded ShowOne #1#2
    \startTEXpage[width=21cm]
        \startviewerlayer[one]
            \signalcharacter
            \externalfigure[#1][page=#2,width=21cm,foregroundcolor=colorone]
        \stopviewerlayer
    \stopTEXpage
\stoptexdefinition

\starttexdefinition unexpanded ShowTwo #1#2
    \startTEXpage[width=21cm]
        \startviewerlayer[two]
            \signalcharacter
            \externalfigure[#1][page=#2,width=21cm,foregroundcolor=colortwo]
        \stopviewerlayer
    \stopTEXpage
\stoptexdefinition

\startluacode

local report = logs.reporter("compare")

local fileone = document.files[1] or ""
local filetwo = document.files[2] or ""

local colorone = false
local colortwo = false

local colors = document.arguments.colors

if colors then
    colorone, colortwo = string.splitup(colors,",")
end

local valid = {
    red     = "colorred",
    green   = "colorgreen",
    blue    = "colorblue",
    cyan    = "colorcyan",
    magenta = "colormagenta",
    yellow  = "coloryellow",
    gray    = "colorgray",
    grey    = "colorgray",
}

if fileone == "" or filetwo == "" then
    report("provide two filenames")
    os.exit()
end

if not lfs.isfile(fileone) then
    report("unknown file %a",fileone)
    os.exit()
end

if not lfs.isfile(filetwo) then
    report("unknown file %a",filetwo)
    os.exit()
end

if valid[colorone] and valid[colortwo] then
    context.definecolor({ "colorone" }, { valid[colorone] })
    context.definecolor({ "colortwo" }, { valid[colortwo] })
end

context.defineviewerlayer({ "one" }, { state = "start", title = file.nameonly(fileone) })
context.defineviewerlayer({ "two" }, { state = "start", title = file.nameonly(filetwo) })

local function check(name)
    local fig = figures.push { name = name }
    figures.identify()
    figures.check()
    local used = fig.used
    figures.pop()
    return used
end

local one = check(fileone) -- can crash
local two = check(filetwo) -- can crash

if not one then
    report("invalid file %a",fileone)
    os.exit()
end

if not two then
    report("invalid file %a",filetwo)
    os.exit()
end

local n_one = tonumber(one.pages) or 0
local n_two = tonumber(two.pages) or 0

if n_one == 0 or n_two == 0 or n_one ~= n_two then
    report("files have different nofpages (%s vs %s)",n_one or "?",n_two or "?")
end

if n_one > n_two then
    for i=1,n_two do
        context.ShowBoth(fileone,filetwo,i)
    end
    for i=n_two+1,n_one do
        context.ShowOne(fileone,i)
    end
else
    for i=1,n_one do
        context.ShowBoth(fileone,filetwo,i)
    end
    for i=n_one+1,n_two do
        context.ShowTwo(filetwo,i)
    end
end

\stopluacode

\stoptext

\endinput
