#
# Try to find the CS2 librairies
#
# CS2_FOUND - system has CS2 lib
# CS2_INCLUDE_DIR - the CS2 include directory
# CS2_LIBRARY_DIR - the CS2 library directory
# CS2_LIBRARIES - libraries needed to use CS2
# CS2_STATIC_LIBRARIES - static libraries needed to use CS2
# 

# modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR})

# cs2: base
set(CS2_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

# deps: gmp
find_package(GMP REQUIRED)

# deps: qhull
set(QHULL_BASE_DIR ${CS2_DIR}/deps/qhull)

# cs2: include
set(CS2_INCLUDE_DIR ${CS2_DIR}/inc)
set(CS2_INCLUDE_DIR ${CS2_INCLUDE_DIR} ${GMP_INCLUDE_DIR})
set(CS2_INCLUDE_DIR ${CS2_INCLUDE_DIR} ${QHULL_BASE_DIR}/include)

# cs2: shared
set(CS2_LIBRARY_DIR ${CS2_DIR}/lib)
set(CS2_LIBRARIES cs2)
set(CS2_LIBRARIES ${CS2_LIBRARIES} ${GMP_LIBRARIES})
set(CS2_LIBRARIES ${CS2_LIBRARIES} ${QHULL_BASE_DIR}/lib/libqhullstatic_r.a)
set(CS2_LIBRARIES ${CS2_LIBRARIES} c m)

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set(CS2_LIBRARIES ${CS2_LIBRARIES} dl)
endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR
   ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR
   ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD" OR
   ${CMAKE_SYSTEM_NAME} MATCHES "DragonFly")
    # link: backtrace, backtrace_symbols
    set(CS2_LIBRARIES ${CS2_LIBRARIES} execinfo)
endif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR
      ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR
      ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD" OR
      ${CMAKE_SYSTEM_NAME} MATCHES "DragonFly")

# cs2: static
set(CS2_STATIC_LIBRARIES ${CS2_DIR}/lib/libcs2_s.a)
set(CS2_STATIC_LIBRARIES ${CS2_STATIC_LIBRARIES} ${GMP_LIBRARIES})
set(CS2_STATIC_LIBRARIES ${CS2_STATIC_LIBRARIES} ${QHULL_BASE_DIR}/lib/libqhullstatic_r.a)
set(CS2_STATIC_LIBRARIES ${CS2_STATIC_LIBRARIES} c m)

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set(CS2_STATIC_LIBRARIES ${CS2_STATIC_LIBRARIES} dl)
endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR
   ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR
   ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD" OR
   ${CMAKE_SYSTEM_NAME} MATCHES "DragonFly")
    # link: backtrace, backtrace_symbols
    set(CS2_STATIC_LIBRARIES ${CS2_STATIC_LIBRARIES} execinfo)
endif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR
      ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR
      ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD" OR
      ${CMAKE_SYSTEM_NAME} MATCHES "DragonFly")

# info
message(STATUS "CS2_DIR: ${CS2_DIR}")
message(STATUS "CS2_INCLUDE_DIR: ${CS2_INCLUDE_DIR}")
message(STATUS "CS2_LIBRARY_DIR: ${CS2_LIBRARY_DIR}")
message(STATUS "CS2_LIBRARIES: ${CS2_LIBRARIES}")
message(STATUS "CS2_STATIC_LIBRARIES: ${CS2_STATIC_LIBRARIES}")

# setup
if(CS2_LIBRARY_DIR AND CS2_INCLUDE_DIR AND CS2_LIBRARIES AND CS2_STATIC_LIBRARIES)
    SET(CS2_FOUND "YES")
else(CS2_LIBRARY_DIR AND CS2_INCLUDE_DIR AND CS2_LIBRARIES AND CS2_STATIC_LIBRARIES)
    SET(CS2_FOUND "NO")
endif(CS2_LIBRARY_DIR AND CS2_INCLUDE_DIR AND CS2_LIBRARIES AND CS2_STATIC_LIBRARIES)

# summary
if(CS2_FOUND)
    message(STATUS "CS2 found")
else(CS2_FOUND)
    if(CS2_FIND_REQUIRED)
        message(FATAL_ERROR "CS2 not found")
    endif(CS2_FIND_REQUIRED)
endif(CS2_FOUND)

