%D \module
%D   [       file=mtx-context-domotica,
%D        version=2016.10.20,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Domotica Goodies,
%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=domotica [options] list-of-files
%
% --topspace=dimension  : distance above first line
% --backspace=dimension : distance before left margin
% --bodyfont=list       : additional bodyfont settings
% --paperformat=spec    : paper*print or paperxprint
% --compact             : small margins, 8pt font
% --verycompact         : small margins, 7pt font
%
% --openzwave           : process openzwave xml files
% --hue                 : process hue task file
%
% --pattern=spec        : files to process
%
% example: context --extra=domotica --openzwave ./config/fibaro/fgms.xml ./open-zwave-master/config/aeotec/zw100.xml
% example: context --extra=domotica --openzwave --pattern="./open-zwave-master/config/**.xml"
% example: context --extra=domotica --hue       hue-pragma-tasks.lua
%
% end help

% --pattern="e:/domotica/open-zwave/open-zwave-master/config/**.xml"

\input mtx-context-common.tex

\usemodule[domotica-settings]

\doifdocumentargument {compact} {
    \setdocumentargument{topspace} {5mm}
    \setdocumentargument{backspace}{5mm}
    \setdocumentargument{bodyfont} {8pt}
}

\doifdocumentargument {verycompact} {
    \setdocumentargument{topspace} {5mm}
    \setdocumentargument{backspace}{5mm}
    \setdocumentargument{bodyfont} {7pt}
}

\setupbodyfont
  [dejavu,11pt,\getdocumentargument{bodyfont}] % dejavu is more complete

\setuplayout
  [header=0cm,
   footer=1.5cm,
   topspace=\getdocumentargumentdefault{topspace}{1.5cm},
   backspace=\getdocumentargumentdefault{backspace}{1.5cm},
   width=middle,
   height=middle]

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

\setuphead
  [section]
  [style=bold]

\doifdocumentargument {openzwave} {\enablemode[openzwave]}
\doifdocumentargument {hue}       {\enablemode[hue]}

\startmode[openzwave]

    \starttext

    \setuplist
      [chapter]
      [style=bold,
       width=4em]

    \setuplist
      [section]
      [width=4em]

    \setupheadertexts

    \setupheadertexts
      [chapter][pagenumber]

    \starttitle[title=Zwave devices]

        \placelist[chapter,section]

    \stoptitle

    \startluacode
        local arguments = document.arguments
        local files     = document.files
        local pattern   = arguments.pattern
        local noffiles  = #files

        if type(pattern) == "string" then

            local pattern = file.addsuffix(pattern,"xml")

            moduledata.zwave.show_settings(pattern)

        elseif noffiles > 0 then

         -- if arguments.sort then
         --     table.sort(files)
         -- end

            for i=1,#files do
                local filename = file.addsuffix(files[i],"xml")
                moduledata.zwave.show_settings(filename)
            end

        else

            context("no files given")

        end
    \stopluacode

    \stoptext

\stopmode

\startmode[hue]

    \starttext

    \setupheadertexts

    \startluacode

    local arguments = document.arguments
    local files     = document.files
    local pattern   = arguments.pattern
    local filename  = files[1]

    if filename then
        context.starttitle { title = "Hue: " .. file.nameonly(filename) }
        filename = file.addsuffix(filename,"lua")
        if lfs.isfile(filename) then
            moduledata.hue.show_state(filename)
        else
            context("unknown file %a",filename)
        end
        context.stoptitle()
    else
        context("no files given")
        context.stoptitle()
    end

    \stopluacode

    \stoptext

\stopmode


