cmake_minimum_required(VERSION 3.16)

project(kindle2notes VERSION 1.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(KINDLE2NOTES_VERSION "" CACHE STRING "Override the version reported by kindle2notes")
if(KINDLE2NOTES_VERSION STREQUAL "")
  set(KINDLE2NOTES_VERSION "${PROJECT_VERSION}")
  find_package(Git QUIET)
  if(Git_FOUND)
    execute_process(
      COMMAND ${GIT_EXECUTABLE} describe --tags --long --dirty --always
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      RESULT_VARIABLE KINDLE2NOTES_GIT_RESULT
      OUTPUT_VARIABLE KINDLE2NOTES_GIT_DESCRIBE
      OUTPUT_STRIP_TRAILING_WHITESPACE
      ERROR_QUIET
    )

    if(KINDLE2NOTES_GIT_RESULT EQUAL 0 AND NOT KINDLE2NOTES_GIT_DESCRIBE STREQUAL "")
      if(KINDLE2NOTES_GIT_DESCRIBE MATCHES "^(.+)-0-g[0-9a-f]+$")
        set(KINDLE2NOTES_VERSION "${CMAKE_MATCH_1}")
      else()
        set(KINDLE2NOTES_VERSION "${KINDLE2NOTES_GIT_DESCRIBE}")
      endif()
    endif()
  endif()
endif()

message(STATUS "kindle2notes version: ${KINDLE2NOTES_VERSION}")

function(kindle2notes_enable_warnings target)
  if(MSVC)
    target_compile_options(${target} PRIVATE /W4)
  else()
    target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic)
  endif()
endfunction()

add_executable(kindle2notes
  typefactory/convertasciidoc.cpp
  typefactory/convertasciidoc.h
  typefactory/convertlatex.cpp
  typefactory/convertlatex.h
  typefactory/convertmarkdown.cpp
  typefactory/convertmarkdown.h
  typefactory/convertorg.cpp
  typefactory/convertorg.h
  typefactory/converter.cpp
  typefactory/converter.h
  typefactory/main.cpp
  typefactory/typefactory.h
)
kindle2notes_enable_warnings(kindle2notes)
target_compile_definitions(kindle2notes PRIVATE
  KINDLE2NOTES_VERSION="${KINDLE2NOTES_VERSION}"
)

include(GNUInstallDirs)
install(TARGETS kindle2notes
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
