#
# CMakeLists.txt - CMake project for AVRDUDE documentation
# Copyright (C) 2022 Marius Greuel
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

cmake_minimum_required(VERSION 3.12)

# TODO: Fix path to binaries
set(AVRDUDE_EXE "${PROJECT_BINARY_DIR}/avrdude")
set(AVRDUDE_CONF "${PROJECT_BINARY_DIR}/avrdude.conf")

set(TEXINFOS "${CMAKE_CURRENT_SOURCE_DIR}/avrdude.texi")
set(GENERATED_TEXINFOS
    version.texi
    programmers.texi
    programmer_types.texi
    parts.texi)

string(TIMESTAMP TODAY "%d %B %Y")

set(DOCS_VERSION "${PROJECT_VERSION}")
set(DOCS_UPDATED ${TODAY})

# =====================================
# Find executables
# =====================================

find_program(AWK_EXECUTABLE NAMES gawk awk REQUIRED)
find_program(SED_EXECUTABLE NAMES sed REQUIRED)
find_program(MAKEINFO_EXECUTABLE NAMES makeinfo REQUIRED)
find_program(TEXI2DVI_EXECUTABLE NAMES texi2dvi REQUIRED)
find_program(TEXI2HTML_EXECUTABLE NAMES texi2html REQUIRED)
find_program(DVIPS_EXECUTABLE NAMES dvips REQUIRED)

# =====================================
# Custom rules
# =====================================

# TODO: Use CMake REGEX for awk/sed magic

add_custom_command(
    OUTPUT programmers.texi
    DEPENDS avrdude
    COMMAND ${AVRDUDE_EXE} -C ${AVRDUDE_CONF} -c ? 2>&1
        | ${AWK_EXECUTABLE} "$2 ~ /^=$$/ {printf(\"@item @code{%s} @tab %s\\n\",$1,gensub(\"[^=]+=[ \t]*\",\"\",1))}"
        | ${SED_EXECUTABLE} "s# *,\\? *<\\?\\(http[s]\\?://[^ \\t>]*\\)>\\?#,@*\\n@url{\\1}#g"
        > programmers.texi
    VERBATIM
    )

add_custom_command(
    OUTPUT programmer_types.texi
    DEPENDS avrdude
    COMMAND ${AVRDUDE_EXE} -C ${AVRDUDE_CONF} -c ?type 2>&1
        | ${AWK_EXECUTABLE} "$2 ~ /^=$$/ {printf(\"@item @code{%s} @tab %s\\n\",$1,gensub(\"[^=]+=[ \t]*\",\"\",1))}"
        | ${SED_EXECUTABLE} "s#<\\?\\(http[s]\\?://[^ \\t,>]*\\)>\\?#@url{\\1}#g"
        > programmer_types.texi
    VERBATIM
    )

add_custom_command(
    OUTPUT parts.texi
    DEPENDS avrdude
    COMMAND ${AVRDUDE_EXE} -C ${AVRDUDE_CONF} -p ? 2>&1
        | ${AWK_EXECUTABLE} "$2 ~ /^=$$/ {printf(\"@item @code{%s} @tab %s\\n\",$1,gensub(\"[^=]+=[ \t]*\",\"\",1))}"
    #   | ${SED_EXECUTABLE} -e "\`sed \"s:\\([^ \\t]*\\)[ \\t]*\\(.*\\):s/\\1$/\\1 \\2/g:g\" <parts_comments.txt\`"
        > parts.texi
    VERBATIM
    )

add_custom_command(
    OUTPUT version.texi
    COMMAND ${CMAKE_COMMAND} -E echo "@set EDITION ${DOCS_VERSION}" > version.texi
    COMMAND ${CMAKE_COMMAND} -E echo "@set VERSION ${DOCS_VERSION}" >> version.texi
    COMMAND ${CMAKE_COMMAND} -E echo "@set UPDATED ${DOCS_UPDATED}" >> version.texi
    VERBATIM
    )

add_custom_command(
    OUTPUT avrdude.info
    COMMAND ${MAKEINFO_EXECUTABLE} -o avrdude.info ${TEXINFOS}
    DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS}
    VERBATIM
    )

add_custom_command(
    OUTPUT avrdude.dvi
    COMMAND ${TEXI2DVI_EXECUTABLE} --tidy -q -o avrdude.dvi ${TEXINFOS}
    DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS}
    VERBATIM
    )

add_custom_command(
    OUTPUT avrdude.ps
    COMMAND ${DVIPS_EXECUTABLE} -q -o avrdude.ps avrdude.dvi
    DEPENDS avrdude.dvi
    VERBATIM
    )

add_custom_command(
    OUTPUT avrdude.pdf
    COMMAND ${TEXI2DVI_EXECUTABLE} --pdf --batch --tidy -q -o avrdude.pdf ${TEXINFOS}
    DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS}
    VERBATIM
    )

add_custom_command(
    OUTPUT avrdude-html/avrdude.html
    COMMAND ${TEXI2HTML_EXECUTABLE}
        --split=node
        --css-include=avrdude.css
        --output=avrdude-html
        -I ${CMAKE_CURRENT_BINARY_DIR}
        ${TEXINFOS}
    DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS}
    VERBATIM
    )

# =====================================
# Targets
# =====================================

add_custom_target(info ALL DEPENDS avrdude.info)
add_custom_target(dvi ALL DEPENDS avrdude.dvi)
add_custom_target(ps ALL DEPENDS avrdude.ps)
add_custom_target(pdf ALL DEPENDS avrdude.pdf)
add_custom_target(html ALL DEPENDS avrdude-html/avrdude.html)