add_subdirectory(glossary)

# Set of available languages:
set (ALL_LINGUAS ar as az bg brx ca cs da de doi el en_AU en_GB en_NZ es es_NI et eu fa fi fr gu he hi hr hu id it ja kn ko kok kok@latin ks lt lv mai mk mni mni@bengali mr nb ne nl pl pt pt_BR ro rw sk sr sv ru ta te tr uk ur vi zh_CN zh_TW)
# LINGUAS is used to create localized destkop and appdata files
string(REPLACE ";" " " LINGUAS "${ALL_LINGUAS}")
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/LINGUAS "${LINGUAS}")

set (CATALOGS "")
set (po_SOURCES "")

foreach (lingua ${ALL_LINGUAS})
  list (APPEND po_SOURCES ${lingua}.po)

  set (_LOCALE_DIR ${DATADIR_BUILD}/locale/${lingua}/LC_MESSAGES)
  file (MAKE_DIRECTORY "${_LOCALE_DIR}") # Unix Makefiles won't do this automatically, ninja does
  set (_MO_FILE ${_LOCALE_DIR}/gnucash.mo)
  list (APPEND CATALOGS ${_MO_FILE})

  add_custom_command(
      OUTPUT ${_MO_FILE}
      COMMAND ${GETTEXT_MSGFMT_EXECUTABLE}
            -o ${_MO_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/${lingua}.po
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${lingua}.po
  )

  install (FILES ${_MO_FILE} DESTINATION ${CMAKE_INSTALL_DATADIR}/locale/${lingua}/LC_MESSAGES)
endforeach (lingua)

add_custom_target(po-gmo ALL DEPENDS ${CATALOGS})

set_local_dist(po_DIST_local ${po_SOURCES} CMakeLists.txt
  POTFILES.in POTFILES.ignore POTFILES.skip README gnucash-pot.cmake)
set(po_DIST ${po_DIST_local} ${po_glossary_DIST} PARENT_SCOPE)

function(read_file_removing_comments OUTPUT PATH)
  set(RESULT "")
  file(STRINGS "${PATH}" LINES_IN)
  foreach(line ${LINES_IN})
    string(REGEX REPLACE "#.*" "" line2 ${line})
    list(APPEND RESULT ${line2})
  endforeach()
  set(${OUTPUT} ${RESULT} PARENT_SCOPE)
endfunction()


function(make_gnucash_potfiles)
  # Create a list of candidate translation files
  file (GLOB_RECURSE FILES_IN RELATIVE ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/*.c ${CMAKE_SOURCE_DIR}/*.cpp
    ${CMAKE_SOURCE_DIR}/*.glade ${CMAKE_SOURCE_DIR}/*.desktop.in.in
    ${CMAKE_SOURCE_DIR}/*.gschema.xml.in ${CMAKE_SOURCE_DIR}/*.appdata.xml.in.in
    ${CMAKE_SOURCE_DIR}/*.keys.in ${CMAKE_SOURCE_DIR}/*.scm
    ${CMAKE_SOURCE_DIR}/*.py ${CMAKE_SOURCE_DIR}/*/qofbookslots.h
    ${CMAKE_SOURCE_DIR}/*/gnc-commodity.h
    ${CMAKE_SOURCE_DIR}/*.ui
  )

  # Only consider files in a selection of the source directories and
  # additionally check against list of ignore patterns
  set (GOOD_FILES "")
  foreach (path ${FILES_IN})
    if (${path} MATCHES "^(bindings/|borrowed/|common/|doc/|libgnucash/|gnucash/)"
        AND
        NOT ${path} MATCHES "gw-|mocks|test|experimental|python-bindings|swig-.*\\.c"
        # Skip POSIX style hidden files even if they have the proper extension
        # These are typically temporary files from editors like emacs
        AND
        NOT ${path} MATCHES "/[.][^/]*$")
        list (APPEND GOOD_FILES ${path})
    endif()
  endforeach (path)

  # Remove the paths that we have marked as explicitly skipped
  read_file_removing_comments(SKIP_LINES POTFILES.skip)
  foreach (path ${SKIP_LINES})
      list(REMOVE_ITEM GOOD_FILES ${path})
  endforeach ()

  # Remove the paths that we have marked as explicitly ignore
  read_file_removing_comments(SKIP_LINES POTFILES.ignore)
  foreach (path ${SKIP_LINES})
      list(REMOVE_ITEM GOOD_FILES ${path})
  endforeach ()


  # CMake sorting is different from UNIX sorting. Use perl to
  # sort POTFILES.in universally. This may no longer be needed
  # now we have dropped autotools support.
  string(REPLACE ";" "\n" SORT_IN "${GOOD_FILES}")
  set(SORT_IN "${SORT_IN}\n")
  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.in "${SORT_IN}")

  execute_process(COMMAND "${PERL_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/util/elegant-sort.pl"
    INPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.in
    OUTPUT_VARIABLE POTFILES_IN
  )
  string(REPLACE "\n" ";" POTFILES "${POTFILES_IN}")

  # Write out the final list.
  # intltool-update insists that this file be in the source directory. :-(
  set(POTFILES_IN_PATH ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in)
  file(WRITE ${POTFILES_IN_PATH} "# This is a list of files which contain translatable strings.
# This file was autogenerated by cmake.
")

  set(POTFILE_DEPS "")
  foreach(path ${POTFILES})
    list(APPEND POTFILE_DEPS ${CMAKE_SOURCE_DIR}/${path})
    file(APPEND ${POTFILES_IN_PATH} "${path}\n")
  endforeach()

  configure_file(${POTFILES_IN_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/POTFILES.in NEWLINE_STYLE LF)

  set(gnucash_pot_depends ${POTFILE_DEPS}  CACHE INTERNAL "List of files with translatable strings. If any of these change, gnucash.pot should be regenerated")
endfunction()

if(BUILD_GNUCASH_POT)

  make_gnucash_potfiles()

  find_program(XGETTEXT xgettext)

  if (${XGETTEXT} STREQUAL "XGETTEXT-NOTFOUND")
    message(FATAL_ERROR "Can't find the 'xgettext' program.")
  endif()

  add_custom_command(OUTPUT gnucash.pot
        COMMAND ${CMAKE_COMMAND}
           -D TOP_SRC_DIR=${CMAKE_SOURCE_DIR}
           -D PO_SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
           -D PO_BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
           -D PROJECT_NAME=${PROJECT_NAME}
           -D PACKAGE_NAME=${PACKAGE_NAME}
           -D PROJECT_VERSION=${PROJECT_VERSION}
           -D XGETTEXT=${XGETTEXT}
           -P ${CMAKE_CURRENT_SOURCE_DIR}/gnucash-pot.cmake
        DEPENDS ${gnucash_pot_depends}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  )
  add_custom_target (pot DEPENDS gnucash.pot)
endif()
dist_add_generated (${BUILDING_FROM_VCS} gnucash.pot)

if(BUILD_GNUCASH_POT)
  add_dependencies (dist-po-gnucash-pot pot)
endif()
