#  Copyright (C) 2026 ChiPass Team <contact@chipass.org>
#
#  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 or (at your option)
#  version 3 of the License.
#
#  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/>.

function (asciidoc_command src_file out_file)
  cmake_parse_arguments(PARSE_ARGV 2 arg "" "INSTALL_TO" "DEPENDS")

  add_custom_command(
    OUTPUT ${out_file}
    DEPENDS ${src_file} ${arg_DEPENDS}
    COMMAND ${ASCIIDOCTOR}
      ${src_file}
      -D ${CMAKE_CURRENT_BINARY_DIR}
      -o ${out_file}
      -a revnumber=${CHIPASS_VERSION}
    VERBATIM
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  )
  install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/${out_file}
    DESTINATION ${arg_INSTALL_TO}
  )

  list(APPEND asciidoc_outputs ${out_file})
  return(PROPAGATE asciidoc_outputs)
endfunction()

file(GLOB manual_depends *.adoc topics/* styles/* images/*)
asciidoc_command(GettingStarted.adoc GettingStarted.html
  INSTALL_TO ${CHIPASS_INSTALL_DATADIR}/docs/
  DEPENDS ${manual_depends}
)
asciidoc_command(UserGuide.adoc UserGuide.html
  INSTALL_TO ${CHIPASS_INSTALL_DATADIR}/docs/
  DEPENDS ${manual_depends}
)
asciidoc_command(topics/KeyboardShortcuts.adoc KeyboardShortcuts.html
  INSTALL_TO ${CHIPASS_INSTALL_DATADIR}/docs/
  DEPENDS ${manual_depends}
)

if (UNIX)
  asciidoc_command(man/ChiPass.1.adoc ChiPass.1
    INSTALL_TO ${CHIPASS_INSTALL_DATADIR}/man/man1/
  )
  asciidoc_command(man/ChiPass-cli.1.adoc ChiPass-cli.1
    INSTALL_TO ${CHIPASS_INSTALL_DATADIR}/man/man1/
  )
endif()

add_custom_target(manual ALL DEPENDS ${asciidoc_outputs})
