#################################################
#
#  (C) 2013 Alexander Golubev (Fat-Zer)
#  fatzer2 (AT) gmail.com
#
#  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 #####################

project( k3b-i18n )


##### 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 #######################

include( TDESetupPaths )
tde_setup_paths( )

option( BUILD_DOC "Build documentation" ON )
option( BUILD_MESSAGES "Build message and GUI translation" ON )

string( REGEX REPLACE "[ \r\n\t]+" ";" _linguas "$ENV{LINGUAS}" )


###### build documentations for all languages ###

if( BUILD_DOC )
  file( GLOB_RECURSE _doc_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} */doc/* )
  foreach( _doc_file IN LISTS _doc_files )
    get_filename_component( _dir ${_doc_file} PATH )
    list( APPEND _dirs ${_dir} )
  endforeach()
  if( _dirs )
    list( SORT _dirs )
    list( REMOVE_DUPLICATES _dirs )
  endif()

  foreach( _doc_dir IN LISTS _dirs )
    string( REGEX REPLACE "/.*" "" _lang ${_doc_dir} )
    if( NOT ${_lang} MATCHES "^(html|man|misc|other)$"
        AND ( ${_lang} STREQUAL "en" OR
              "${_linguas}" MATCHES "^;*$" OR
              ";${_linguas};" MATCHES ";${_lang};" ))

      file( GLOB _doc_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/${_doc_dir} ${_doc_dir}/*.docbook )
      if( ${_doc_dir} MATCHES "^${_lang}/doc/[^/]*/" )
        string( REGEX REPLACE "^${_lang}/doc/(.*)" "\\1" _doc_dest "${_doc_dir}" )
      else()
        string( REGEX REPLACE "^${_lang}/doc/(.*)" "\\1" _doc_dest "${_doc_dir}/k3b" )
      endif()
      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}/*.jpg ${_doc_dir}/*.png )
        install(
          FILES ${_doc_files}
          DESTINATION ${HTML_INSTALL_DIR}/${_lang}/${_doc_dest}
        )
      endif()
    endif()
  endforeach()

endif()


###### build translations for all languages #####

if( BUILD_MESSAGES )
  file( GLOB_RECURSE po_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} */messages/*.po )

  foreach( _po ${po_files} )
    string( REGEX REPLACE "/.*" "" _lang ${_po} )
    if( "${_linguas}" MATCHES "^;*$" OR ";${_linguas};" MATCHES ";${_lang};" )
      tde_create_translation( FILES ${_po} LANG ${_lang} )
    endif()
  endforeach( )
endif( )
