%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

\nopdfcompression

\input mtx-context-common.tex

\starttext

% \setupinteraction
%   [state=start]

% \showmakeup

\lettonothing\colorone
\lettonothing\colortwo

\startluacode
    local identify = {
        content   = true,
        resources = true,
        page      = true,
    }
    graphics.registerpdfcompactor ( "color:red", {
        identify = identify,
        strip    = { color = { 1, 0, 0 } },
    } )
    graphics.registerpdfcompactor ( "color:green", {
        identify = identify,
        strip    = { color = { 0, 1, 0 } },
    } )
    graphics.registerpdfcompactor ( "color:blue", {
        identify = identify,
        strip    = { color = { 0, 0, 1 } },
    } )
    graphics.registerpdfcompactor ( "color:cyan", {
        identify = identify,
        strip    = { color = { 0, 1, 1 } },
    } )
    graphics.registerpdfcompactor ( "color:magenta", {
        identify = identify,
        strip    = { color = { 1, 0, 1 } },
    } )
    graphics.registerpdfcompactor ( "color:yellow", {
        identify = identify,
        strip    = { color = { 1, 1, 0 } },
    } )
    graphics.registerpdfcompactor ( "color:gray", {
        identify = identify,
        strip    = { color = 0 },
    } )
\stopluacode

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

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

\starttexdefinition unexpanded ShowTwo #1#2
    \startTEXpage[width=21cm]
        \startviewerlayer[two]
            \signalcharacter
            \externalfigure[#1][page=#2,width=21cm,compact=\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     = "color:red",
    green   = "color:green",
    blue    = "color:blue",
    cyan    = "color:cyan",
    magenta = "color:magenta",
    yellow  = "color:yellow",
    gray    = "color:gray",
    grey    = "color:gray",
}

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
    token.setmacro("colorone", valid[colorone])
    token.setmacro("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
