#-------------------------------------------------------------------
# This file is part of the CMake build system for OGRE
#     (Object-oriented Graphics Rendering Engine)
# For the latest info, see http://www.ogre3d.org/
#
# The contents of this file are placed in the public domain. Feel
# free to make use of it in any way you like.
#-------------------------------------------------------------------

# - Try to find pocketsphinx
# Once done, this will define
#
#  pocketsphinx_FOUND - system has pocketsphinx
#  pocketsphinx_INCLUDE_DIRS - the pocketsphinx include directories 
#  pocketsphinx_LIBRARIES - link these to use pocketsphinx

include(FindPkgMacros)
findpkg_begin(pocketsphinx)

# Get path, convert backslashes as ${ENV_${var}}
getenv_path(pocketsphinx_HOME)

# construct search paths
set(pocketsphinx_PREFIX_PATH ${pocketsphinx_HOME} ${ENV_pocketsphinx_HOME} )
create_search_paths(pocketsphinx)
# redo search if prefix path changed
clear_if_changed(pocketsphinx_PREFIX_PATH
  pocketsphinx_LIBRARY_FWK
  pocketsphinx_LIBRARY_REL
  pocketsphinx_LIBRARY_DBG
  pocketsphinx_LIBRARY_RELEASE
  pocketsphinx_LIBRARY_DEBUG
  pocketsphinx_INCLUDE_DIR
)

set(pocketsphinx_LIBRARY_NAMES pocketsphinx)
get_debug_names(pocketsphinx_LIBRARY_NAMES)

findpkg_framework(pocketsphinx)

find_path(pocketsphinx_INCLUDE_DIR NAMES pocketsphinx.h HINTS ${pocketsphinx_INC_SEARCH_PATH} ${pocketsphinx_PKGC_INCLUDE_DIRS} NO_CMAKE_FIND_ROOT_PATH)
find_library(pocketsphinx_LIBRARY_REL NAMES ${pocketsphinx_LIBRARY_NAMES} HINTS ${pocketsphinx_LIB_SEARCH_PATH} ${pocketsphinx_PKGC_LIBRARY_DIRS} PATH_SUFFIXES "" Release RelWithDebInfo MinSizeRel ${ANDROID_ABI} NO_CMAKE_FIND_ROOT_PATH)
find_library(pocketsphinx_LIBRARY_DBG NAMES ${pocketsphinx_LIBRARY_NAMES_DBG} HINTS ${pocketsphinx_LIB_SEARCH_PATH} ${pocketsphinx_PKGC_LIBRARY_DIRS} PATH_SUFFIXES "" Debug ${ANDROID_ABI} NO_CMAKE_FIND_ROOT_PATH)
set(pocketsphinx_LIBRARY_RELEASE ${pocketsphinx_LIBRARY_REL})
set(pocketsphinx_LIBRARY_DEBUG ${pocketsphinx_LIBRARY_DBG})
make_library_set(pocketsphinx_LIBRARY)

findpkg_finish(pocketsphinx)

if(pocketsphinx_FOUND)
  if(NOT TARGET pocketsphinx::pocketsphinx)
    add_library(pocketsphinx::pocketsphinx UNKNOWN IMPORTED)
    if(pocketsphinx_INCLUDE_DIRS)
      set_target_properties(pocketsphinx::pocketsphinx PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${pocketsphinx_INCLUDE_DIRS}")
    endif()
    if(EXISTS "${pocketsphinx_LIBRARY}")
      set_target_properties(pocketsphinx::pocketsphinx PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES "C"
        IMPORTED_LOCATION "${pocketsphinx_LIBRARY}")
    endif()
    if(EXISTS "${pocketsphinx_LIBRARY_RELEASE}")
      set_property(TARGET pocketsphinx::pocketsphinx APPEND PROPERTY
        IMPORTED_CONFIGURATIONS RELEASE)
      set_target_properties(pocketsphinx::pocketsphinx PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
        IMPORTED_LOCATION_RELEASE "${pocketsphinx_LIBRARY_RELEASE}")
    endif()
    if(EXISTS "${pocketsphinx_LIBRARY_DEBUG}")
      set_property(TARGET pocketsphinx::pocketsphinx APPEND PROPERTY
        IMPORTED_CONFIGURATIONS DEBUG)
      set_target_properties(pocketsphinx::pocketsphinx PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
        IMPORTED_LOCATION_DEBUG "${pocketsphinx_LIBRARY_DEBUG}")
    endif()
  endif()
endif()