#################################################
#
#  (C) 2013 Alexander Golubev (Fat-Zer)
#  fatzer2 (AT) gmail.com
#
#  (C) 2020-2022 Slávek Banko
#  slavek.banko (AT) axis.cz
#
#  Improvements and feedback are welcome
#
#  This file is released under GPL >= 2
#
#################################################


##### set project version ########################

include( TDEVersion )
cmake_minimum_required( VERSION ${TDE_CMAKE_MINIMUM_VERSION} )
tde_set_project_version( )


##### general package setup #####################

string( REGEX REPLACE ".*-([^-]*)$" "\\1" _lang ${CMAKE_CURRENT_SOURCE_DIR} )
message( STATUS "Configure for language ${_lang}" )

project( tde-i18n-${_lang} )


##### include essential cmake modules ###########

include( FindPkgConfig ) # required for find_package( TDE )


##### include our cmake modules #################

include( TDEMacros )


##### find required stuff #######################

find_package( TDE )


##### setup install paths #######################

tde_save_and_set( CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "${_lang}" )
include( TDESetupPaths )
tde_setup_paths( )


##### user requested modules ####################

option( BUILD_ALL "Build all" OFF )
option( BUILD_DOC "Build documentation" ${BUILD_ALL} )
option( BUILD_DATA "Build data" ${BUILD_ALL} )
option( BUILD_MESSAGES "Build message and GUI translation" ${BUILD_ALL} )


###### subdirectories ###########################

# build data files
if( BUILD_DATA AND
    EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/data/CMakeLists.txt )
  add_subdirectory( data )
endif()


# build documentation
if( BUILD_DOC )
  # search directories with documentation
  file( GLOB_RECURSE _doc_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} docs/* )
  foreach( _doc_file IN LISTS _doc_files )
    get_filename_component( _doc_dir ${_doc_file} PATH )
    list( APPEND _doc_dirs ${_doc_dir} )
  endforeach()
  if( _doc_dirs )

    # sort and clear the list of directories
    list( SORT _doc_dirs )
    list( REMOVE_DUPLICATES _doc_dirs )
    list( REMOVE_ITEM _doc_dirs "docs/common" )

    # install common files
    file( GLOB common_docs_INSTALL
      RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/docs/common
      docs/common/*
    )
    file( GLOB common_docs_LINK
      RELATIVE ${TDE_HTML_DIR}/en/common/
      ${TDE_HTML_DIR}/en/common/*
    )
    if( common_docs_INSTALL )
      list( REMOVE_ITEM common_docs_INSTALL CMakeLists.txt Makefile.am )
      foreach( _file IN LISTS common_docs_INSTALL )
        list( REMOVE_ITEM common_docs_LINK ${_file} )
        install(
          FILES docs/common/${_file}
          DESTINATION ${HTML_INSTALL_DIR}/${_lang}/common
        )
      endforeach( )
    endif()
    foreach( _file IN LISTS common_docs_LINK )
      file( RELATIVE_PATH _common_link_target
        "${HTML_INSTALL_DIR}/${_lang}/common"
        "${TDE_HTML_DIR}/en/common/${_file}"
      )
      tde_install_symlink(
        ${_common_link_target}
        ${HTML_INSTALL_DIR}/${_lang}/common/${_file}
      )
    endforeach( )

    # create handbooks or install other files
    foreach( _doc_dir IN LISTS _doc_dirs )
      file( GLOB _doc_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/${_doc_dir} ${_doc_dir}/*.docbook )
      string( REGEX REPLACE "^[^/]*/[^/]*/" "" _doc_dest ${_doc_dir} )
      if( _doc_files )
        list( FIND _doc_files "index.docbook" _find_index )
        if( -1 EQUAL _find_index )
          set( _noindex "NOINDEX" )
        else()
          unset( _noindex )
        endif()
        tde_create_handbook(
          SOURCE_BASEDIR ${_doc_dir}
          ${_noindex}
          LANG ${_lang}
          DESTINATION ${_doc_dest}
        )
      else()
        file( GLOB _doc_files
              RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
              ${_doc_dir}/*.css ${_doc_dir}/*.png )
        install(
          FILES ${_doc_files}
          DESTINATION ${HTML_INSTALL_DIR}/${_lang}/${_doc_dest}
        )
      endif()
    endforeach()
  endif()
endif()


# build messages
if( BUILD_MESSAGES )
  # install base files for locale
  file( GLOB locale_base_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
        messages/flag.png messages/charset )
  if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/messages/entry.desktop )
    tde_create_translated_desktop(
      SOURCE messages/entry.desktop
      DESTINATION ${LOCALE_INSTALL_DIR}/${_lang}
      PO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../translations/desktop_files/entry.desktop
    )
  endif()
  if( locale_base_files )
    install(
      FILES ${locale_base_files}
      DESTINATION ${LOCALE_INSTALL_DIR}/${_lang}
    )
  endif()

  # create translations
  file( GLOB_RECURSE _messages RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} messages/*.po )
  if( _messages )
    tde_create_translation( FILES ${_messages} LANG ${_lang} )
  endif()
endif()


# restore default install component name

tde_restore( CMAKE_INSTALL_DEFAULT_COMPONENT_NAME )