# - Locate Pwlib include paths and libraries.
#  Pwlib can be found at www.voxgratia.org .
#  Written by Frederic Heem, frederic.heem _at_ telsey.it
#
# This module defines:
#  PWLIB_FOUND, true if Pwlib has been found
#  PWLIB_VERSION, the Pwlib version 
#  PWLIB_INCLUDE_DIRS, where to find ptlib.h, etc.
#  PWLIB_LIBRARIES, library to link againt Pwlib
#  PWLIB_DEFINITIONS, the definitions used to compile Pwlib

INCLUDE(UsePkgConfig)

#Find the Pwlib package
PKGCONFIG_FOUND(pwlib PWLIB_FOUND)

IF(PWLIB_FOUND)
  MESSAGE(STATUS "Pwlib found")
ELSE(PWLIB_FOUND)
  MESSAGE(FATAL "Pwlib cannot be found")
ENDIF(PWLIB_FOUND)

#use pkgconfig to fill information
PKGCONFIG(pwlib PWLIB_INCLUDE_DIR_SYSTEM PWLIB_LIBRARY_DIR PWLIB_LINK_FLAGS PWLIB_C_FLAGS)

#Include path
SET(PWLIB_INCLUDE_PATH_DESCRIPTION "directory containing the pwlib include files. E.g /home/fred/pwlib/include or c:\\pwlib\\include")


#NO_DEFAULT_PATH is used because $ENV{PWLIBDIR} and ${PWLIB_INCLUDE_DIR_SYSTEM} have the prioriry
FIND_PATH(PWLIB_INCLUDE_DIR ptlib.h
  $ENV{PWLIBDIR}/include
  ${PWLIB_INCLUDE_DIR_SYSTEM}
  DOC " ${PWLIB_INCLUDE_PATH_DESCRIPTION}"
  NO_DEFAULT_PATH  
)

#In case of the search in $ENV{PWLIBDIR}/include and ${PWLIB_INCLUDE_DIR_SYSTEM} fails, search in the system path 
FIND_PATH(PWLIB_INCLUDE_DIR ptlib.h
  DOC " ${PWLIB_INCLUDE_PATH_DESCRIPTION}"
)

SET(PWLIB_INCLUDE_DIRS ${PWLIB_INCLUDE_DIR})

IF(PWLIB_INCLUDE_DIR)
  MESSAGE(STATUS "Pwlib include dir set to ${PWLIB_INCLUDE_DIR}")
ELSE(PWLIB_INCLUDE_DIR)
  MESSAGE(FATAL "Pwlib include dir cannot be found")
ENDIF(PWLIB_INCLUDE_DIR)

#Library
SET(PWLIB_LIB_PATH_DESCRIPTION "The directory containing the pwlib library. E.g /home/fred/pwlib/lib or c:\\pwlib\\lib")

#NO_DEFAULT_PATH is used because $ENV{PWLIBDIR}/lib and ${PWLIB_LIBRARY_DIR} have the prioriry
FIND_LIBRARY(PWLIB_LIBRARY
  NAMES 
    pt
  PATHS
    $ENV{PWLIBDIR}/lib
    ${PWLIB_LIBRARY_DIR}
  DOC " ${PWLIB_LIB_PATH_DESCRIPTION}"
  NO_DEFAULT_PATH  
)

#In case of the search in $ENV{PWLIBDIR}/lib and ${PWLIB_LIBRARY_DIR} fails, search in the system path
FIND_LIBRARY(PWLIB_LIBRARY
  NAMES 
    pt
  DOC " ${PWLIB_LIB_PATH_DESCRIPTION}"
)

SET(PWLIB_LIBRARIES ${PWLIB_LIBRARY}) 

IF(PWLIB_LIBRARIES)
  MESSAGE(STATUS "Pwlib library set to  ${PWLIB_LIBRARIES}")
ELSE(PWLIB_LIBRARIES)
  MESSAGE(FATAL "Pwlib library cannot be found")
ENDIF(PWLIB_LIBRARIES)

#Version
PKGCONFIG_VERSION(pwlib PWLIB_VERSION)
MESSAGE(STATUS "Pwlib version is ${PWLIB_VERSION}")

#Definition
PKGCONFIG_DEFINITION(pwlib PWLIB_DEFINITIONS)
MESSAGE(STATUS "Pwlib definitions are ${PWLIB_DEFINITIONS}")


MARK_AS_ADVANCED(
  PWLIB_INCLUDE_DIR
  PWLIB_LIBRARY
) 
