# Copyright (c) 2021 Apex.AI Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.10)

set(IOX_VERSION_STRING "1.0.1")

#find_package(iceoryx_utils REQUIRED)

#include(IceoryxVersion)
#parse_version(${IOX_VERSION_STRING})
project(iceoryx_doc VERSION ${IOX_VERSION_STRING})
#adjust_version()

find_package(Doxygen REQUIRED)

set(DOXYGEN_GENERATE_HTML YES)
set(DOXYGEN_GENERATE_LATEX YES)
set(DOXYGEN_GENERATE_XML YES)
set(DOXYGEN_XML_OUTPUT xml)

if(EXISTS "/usr/share/plantuml/plantuml.jar")
  set(DOXYGEN_PLANTUML_JAR_PATH /usr/share/plantuml/plantuml.jar)
else()
  if(DEFINED $ENV{PLANTUML_JAR_PATH})
    set(DOXYGEN_PLANTUML_JAR_PATH
        "$ENV{PLANTUML_JAR_PATH}"
        CACHE STRING "PLANTUML location.")
  else()
    message(
      WARNING
        "plantuml.jar not found at '/usr/share/plantuml/', please set environment variable PLANTUML_JAR_PATH to the correct location."
    )
  endif()
endif()

set(DOXYGEN_ALIASES
    [["concurrent=\xrefitem concurrent \"Concurrent\" \"Concurrency\" " \
"req=\xrefitem req \"Requirement\" \"Requirements\" " \
"link=\xrefitem link \"Link\" \"Links\" " \
swcomponent="@par Software Component:" \
error="@par Error Handling:" \
generatedcode="@par Generated by:"]])

set(COMPONENTS iceoryx_utils iceoryx_posh iceoryx_binding_c iceoryx_dds
               iceoryx_introspection iceoryx_component)
set(COMPONENT_DIRS
    ${CMAKE_CURRENT_SOURCE_DIR}/../../iceoryx_utils
    ${CMAKE_CURRENT_SOURCE_DIR}/../../iceoryx_posh
    ${CMAKE_CURRENT_SOURCE_DIR}/../../iceoryx_binding_c
    ${CMAKE_CURRENT_SOURCE_DIR}/../../iceoryx_dds
    ${CMAKE_CURRENT_SOURCE_DIR}/../../tools/introspection
    ${CMAKE_CURRENT_SOURCE_DIR}/example/iceoryx_component)


list(LENGTH COMPONENTS tmp_len)
math(EXPR cmp_len "${tmp_len} - 1")

foreach(val RANGE ${cmp_len})
  list(GET COMPONENTS ${val} cmp)
  list(GET COMPONENT_DIRS ${val} cmp_dir)
  message(STATUS "${cmp}  ${cmp_dir}")

  set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/doc/${cmp})

  doxygen_add_docs(doxygen_${cmp}
                  ${cmp_dir}/include
                  WORKING_DIRECTORY ${cmp_dir}/include
                  ALL)
endforeach()

# create dummy install directive to generate docu also with "make install"
install(
  FILES
    ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.doxygen_iceoryx_utils
  DESTINATION
    ${CMAKE_CURRENT_BINARY_DIR}
)
