if( WIN32 )
    set( CPPUTEST_PATHS "C:/Program Files/CppUTest" "C:/CppUTest" )

    if( "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" )
        set( CPPUTEST_LIBNAME "CppUTest64" )
        set( CPPUTESTEXT_LIBNAME "CppUTestExt64" )
    elseif( "${CMAKE_SIZEOF_VOID_P}" STREQUAL "4" )
        set( CPPUTEST_LIBNAME "CppUTest" )
        set( CPPUTESTEXT_LIBNAME "CppUTestExt" )
    else()
        message( FATAL_ERROR "Could not determine architecture bitsize (32 or 64)" )
    endif()
else()
    set( CPPUTEST_PATHS "/usr/local" "/usr" )
    set( CPPUTEST_LIBNAME "CppUTest" )
    set( CPPUTESTEXT_LIBNAME "CppUTestExt" )
endif()

set( CPPUTEST_HINTS "" )
if( DEFINED ENV{CPPUTEST_HOME} )
    set( CPPUTEST_HINTS "$ENV{CPPUTEST_HOME}/lib" ${CPPUTEST_HINTS} )
endif()
if( CPPUTEST_HOME )
    set( CPPUTEST_HINTS "${CPPUTEST_HOME}/lib" ${CPPUTEST_HINTS} )
endif()
if( CppUTest_HOME )
    set( CPPUTEST_HINTS "${CppUTest_HOME}/lib" ${CPPUTEST_HINTS} )
endif()

find_library( CppUTest_LIB_PATH NAMES ${CPPUTEST_LIBNAME} PATHS ${CPPUTEST_PATHS} HINTS ${CPPUTEST_HINTS} )
mark_as_advanced( CppUTest_LIB_PATH  )

get_filename_component( CppUTest_LIBRARY_DIR ${CppUTest_LIB_PATH} DIRECTORY )

if( NOT MSVC )
    find_library( CppUTestExt_LIB_PATH NAMES ${CPPUTESTEXT_LIBNAME} HINTS ${CppUTest_LIBRARY_DIR} NO_DEFAULT_PATH )
    mark_as_advanced( CppUTestExt_LIB_PATH  )
endif()

if( WIN32 )
    find_library( CppUTest_DEBUG_LIB_PATH NAMES ${CPPUTEST_LIBNAME}d HINTS ${CppUTest_LIBRARY_DIR} NO_DEFAULT_PATH )
    mark_as_advanced( CppUTest_DEBUG_LIB_PATH  )

    if( NOT MSVC )
        find_library( CppUTestExt_DEBUG_LIB_PATH NAMES ${CPPUTESTEXT_LIBNAME}d HINTS ${CppUTest_LIBRARY_DIR} NO_DEFAULT_PATH )
        mark_as_advanced( CppUTestExt_DEBUG_LIB_PATH  )
    endif()
endif()

include( FindPackageHandleStandardArgs )
if( MSVC )
    find_package_handle_standard_args( CppUTest DEFAULT_MSG CppUTest_LIB_PATH )
else()
    find_package_handle_standard_args( CppUTest DEFAULT_MSG CppUTest_LIB_PATH CppUTestExt_LIB_PATH )
endif()

unset( CPPUTEST_PATHS )
unset( CPPUTEST_LIBNAME )

get_filename_component( CppUTest_ROOT ${CppUTest_LIBRARY_DIR} DIRECTORY )
set( CppUTest_INCLUDE_DIR ${CppUTest_ROOT}/include )
set( CppUTest_LIBRARY ${CppUTest_LIB_PATH} )
set( CppUTestExt_LIBRARY ${CppUTestExt_LIB_PATH} )
if( DEFINED CppUTest_DEBUG_LIB_PATH AND (NOT CppUTest_DEBUG_LIB_PATH MATCHES "^.*-NOTFOUND$" ) )
    set( CppUTest_DEBUG_LIBRARY ${CppUTest_DEBUG_LIB_PATH} )
endif()
if( DEFINED CppUTestExt_DEBUG_LIB_PATH AND (NOT CppUTestExt_DEBUG_LIB_PATH MATCHES "^.*-NOTFOUND$" ) )
    set( CppUTestExt_DEBUG_LIBRARY ${CppUTestExt_DEBUG_LIB_PATH} )
endif()
