# -*- mode: cmake; -*-
# - Find the cppunit library.
# Find the native cppunit includes and library
# This module defines
#  CPPUNIT_INCLUDE_DIR, where to find jpeglib.h, etc.
#  CPPUNIT_LIBRARIES, the libraries needed to use CPPUNIT.
#  CPPUNIT_FOUND, If false, do not try to use CPPUNIT.
# also defined, but not for general use are
#  CPPUNIT_LIBRARY, where to find the CPPUNIT library.

message(STATUS "FindCppunit check")
IF (NOT WIN32)
  include(FindPkgConfig)
  if ( PKG_CONFIG_FOUND )

     pkg_check_modules (PC_CPPUNIT cppunit>=1.12)

     set(CPPUNIT_DEFINITIONS ${PC_CPPUNIT_CFLAGS_OTHER})
  endif(PKG_CONFIG_FOUND)
endif (NOT WIN32)

#
# set defaults
SET(_cppunit_HOME "/usr/local")
SET(_cppunit_INCLUDE_SEARCH_DIRS
  ${CMAKE_INCLUDE_PATH}
  /usr/local/include
  /usr/include
  )

SET(_cppunit_LIBRARIES_SEARCH_DIRS
  ${CMAKE_LIBRARY_PATH}
  /usr/local/lib
  /usr/lib
  )

##
  if( "${CPPUNIT_HOME}" STREQUAL "")
    if("" MATCHES "$ENV{CPPUNIT_HOME}")
      message(STATUS "CPPUNIT_HOME env is not set, setting it to /usr/local")
      set (CPPUNIT_HOME ${_cppunit_HOME})
    else("" MATCHES "$ENV{CPPUNIT_HOME}")
      set (CPPUNIT_HOME "$ENV{CPPUNIT_HOME}")
    endif("" MATCHES "$ENV{CPPUNIT_HOME}")
  else( "${CPPUNIT_HOME}" STREQUAL "")
    message(STATUS "CPPUNIT_HOME is not empty: \"${CPPUNIT_HOME}\"")
  endif( "${CPPUNIT_HOME}" STREQUAL "")

message(STATUS "Looking for cppunit in ${CPPUNIT_HOME}")

IF( NOT ${CPPUNIT_HOME} STREQUAL "" )
    SET(_cppunit_INCLUDE_SEARCH_DIRS ${CPPUNIT_HOME}/include ${_cppunit_INCLUDE_SEARCH_DIRS})
    SET(_cppunit_LIBRARIES_SEARCH_DIRS ${CPPUNIT_HOME}/lib ${_cppunit_LIBRARIES_SEARCH_DIRS})
    SET(_cppunit_HOME ${CPPUNIT_HOME})
ENDIF( NOT ${CPPUNIT_HOME} STREQUAL "" )

IF( NOT $ENV{CPPUNIT_INCLUDEDIR} STREQUAL "" )
  SET(_cppunit_INCLUDE_SEARCH_DIRS $ENV{CPPUNIT_INCLUDEDIR} ${_cppunit_INCLUDE_SEARCH_DIRS})
ENDIF( NOT $ENV{CPPUNIT_INCLUDEDIR} STREQUAL "" )

IF( NOT $ENV{CPPUNIT_LIBRARYDIR} STREQUAL "" )
  SET(_cppunit_LIBRARIES_SEARCH_DIRS $ENV{CPPUNIT_LIBRARYDIR} ${_cppunit_LIBRARIES_SEARCH_DIRS})
ENDIF( NOT $ENV{CPPUNIT_LIBRARYDIR} STREQUAL "" )

IF( CPPUNIT_HOME )
  SET(_cppunit_INCLUDE_SEARCH_DIRS ${CPPUNIT_HOME}/include ${_cppunit_INCLUDE_SEARCH_DIRS})
  SET(_cppunit_LIBRARIES_SEARCH_DIRS ${CPPUNIT_HOME}/lib ${_cppunit_LIBRARIES_SEARCH_DIRS})
  SET(_cppunit_HOME ${CPPUNIT_HOME})
ENDIF( CPPUNIT_HOME )

# find the include files
FIND_PATH(CPPUNIT_INCLUDE_DIR cppunit/Portability.h
   HINTS
     ${_cppunit_INCLUDE_SEARCH_DIRS}
     ${PC_CPPUNIT_INCLUDEDIR}
     ${PC_CPPUNIT_INCLUDE_DIRS}
    ${CMAKE_INCLUDE_PATH}
)

# locate the library
IF(WIN32)
  SET(CPPUNIT_LIBRARY_NAMES ${CPPUNIT_NAMES} cppunit cppunitd cppunit_md cppunit_mt)
  #SET(CPPUNIT_LIBRARY_NAMES ${CPPUNIT_LIBRARY_NAMES} libcppunit.lib)
ELSE(WIN32)
  SET(CPPUNIT_LIBRARY_NAMES ${CPPUNIT_LIBRARY_NAMES} libcppunit.a)
ENDIF(WIN32)
FIND_LIBRARY(CPPUNIT_LIBRARY NAMES ${CPPUNIT_LIBRARY_NAMES}
  HINTS
    ${_cppunit_LIBRARIES_SEARCH_DIRS}
    ${PC_CPPUNIT_LIBDIR}
    ${PC_CPPUNIT_LIBRARY_DIRS}
)

# if the include and the program are found then we have it
IF(CPPUNIT_INCLUDE_DIR AND CPPUNIT_LIBRARY) 
  SET(CPPUNIT_FOUND "YES")
ENDIF(CPPUNIT_INCLUDE_DIR AND CPPUNIT_LIBRARY)

if( NOT WIN32)
  list(APPEND CPPUNIT_LIBRARY "-lrt")
endif( NOT WIN32)


#message(STATUS "LIB: ${CPPUNIT_LIBRARY}, INC: ${CPPUNIT_INCLUDE_DIR}")
#
#SET(CPPUNIT_INCLUDE_DIR "${CPPUNIT_HOME}/include")
#SET(CPPUNIT_STATIC_LIBRARY_DIRS "${CPPUNIT_HOME}/include")
#SET(CPPUNIT_STATIC_LIBRARIES "${CPPUNIT_HOME}/lib/cppunit.lib")
#
#IF (CPPUNIT_LIBRARY AND CPPUNIT_INCLUDE_DIR)
#    SET(CPPUNIT_LIBRARIES ${CPPUNIT_LIBRARY})
#    SET(CPPUNIT_FOUND "YES")
#ELSE (CPPUNIT_LIBRARY AND CPPUNIT_INCLUDE_DIR)
#  SET(CPPUNIT_FOUND "NO")
#ENDIF (CPPUNIT_LIBRARY AND CPPUNIT_INCLUDE_DIR)
#    SET(CPPUNIT_FOUND "YES")
#
#
#IF (CPPUNIT_FOUND)
#   IF (NOT CPPUNIT_FIND_QUIETLY)
#      MESSAGE(STATUS "Found CPPUNIT: ${CPPUNIT_LIBRARIES}")
#   ENDIF (NOT CPPUNIT_FIND_QUIETLY)
#ELSE (CPPUNIT_FOUND)
#   IF (CPPUNIT_FIND_REQUIRED)
#      MESSAGE(FATAL_ERROR "Could not find CPPUNIT library")
#   ENDIF (CPPUNIT_FIND_REQUIRED)
#ENDIF (CPPUNIT_FOUND)

# Deprecated declarations.
#SET (NATIVE_CPPUNIT_INCLUDE_PATH ${CPPUNIT_INCLUDE_DIR} )
#GET_FILENAME_COMPONENT (NATIVE_CPPUNIT_LIB_PATH ${CPPUNIT_LIBRARY} PATH)


message(STATUS "CPPUNIT Libpath: ${CPPUNIT_STATIC_LIBRARIES}")
message(STATUS "CPPUNIT INCpath: ${CPPUNIT_INCLUDE_DIR}")
message(STATUS "CPPUNIT: -I${CPPUNIT_STATIC_INCLUDE_DIRS} -L${CPPUNIT_STATIC_LIBRARY_DIRS} -l${CPPUNIT_STATIC_LIBRARIES}")

MARK_AS_ADVANCED(
  CPPUNIT_STATIC_LIBRARIES
  CPPUNIT_LIBRARY
  CPPUNIT_INCLUDE_DIR
)
