# On success, the macro sets the following variables:
# MOBY_FOUND       = if the library found
# MOBY_LIBRARIES   = full path to the library
# MOBY_INCLUDE_DIR = where to find the library headers

#add_definitions (-DBUILD_DOUBLE)
#add_definitions (-DSAFESTATIC=static)
#add_definitions( -DADDRESS_ATLAS_BUG )

set( CMAKE_VERBOSE_MAKEFILE ON )

find_package( Boost REQUIRED )
find_package( LibXml2 REQUIRED )

find_package( osg REQUIRED )
find_package( osgViewer REQUIRED )
find_package( osgDB REQUIRED )
find_package( osgGA REQUIRED )
find_package( OpenThreads REQUIRED )

#find_package( CBLAS REQUIRED )
#find_package( LAPACKLite REQUIRED )
#set( BLAS_LIBRARIES ${CBLAS_LIBRARIES} )

#if (LIBXML2_FOUND)
#  set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${LIBXML2_DEFINITIONS})
#  set (CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG} ${LIBXML2_DEFINITIONS})
#  set (CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${LIBXML2_DEFINITIONS})
#  set (CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} ${LIBXML2_DEFINITIONS})
#  set (CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_MINSIZEREL} ${LIBXML2_DEFINITIONS})
#  include_directories (${LIBXML2_INCLUDE_DIR})
#endif (LIBXML2_FOUND)

#include_directories( ${BOOST_INCLUDE_DIRS} )
#include_directories( ${LIBXML2_INCLUDE_DIR} )
#include_directories( ${OSG_INCLUDE_DIR} )
#include_directories( ${OSGDB_INCLUDE_DIR} )
#include_directories( ${OSGGA_INCLUDE_DIR} )
#include_directories( ${OSGVIEWER_INCLUDE_DIR} )
#include_directories( ${OPENTHREADS_INCLUDE_DIR} )

# build options
option( BUILD_SHARED_LIBS "Build Moby as a shared library?" ON )
option( BUILD_TOOLS "Build the programs in the tools subdirectory?" ON )
option( USE_OSG "Build against OpenSceneGraph library?" ON )
option( USE_PATH "Build against the PATH library?" OFF )
option( PROFILE "Build for profiling?" OFF )
option( OMP "Build with OpenMP support?" OFF )
option( ARBITRARY_PRECISION "Build with arbitrary precision?" OFF )
option( THREADSAFE "Build Moby to be threadsafe? (slower)" OFF )
option( BUILD_DOUBLE "Build with real type as double?" ON )

# check options are valid
if( THREADSAFE )
  if( OMP )
    unset( OMP )
  endif( OMP )
  if( ARBITRARY_PRECISION )
    unset( ARBITRARY_PRECISION )
  endif( ARBITRARY_PRECISION )
endif( THREADSAFE )

# modify C++ flags
if( ARBITRARY_PRECISION )
  find_package( MPFR )
  if( not MPFR_FOUND )
    unset( ARBITRARY_PRECISION )
  else( not MPFR_FOUND )
    include_directories( ${MPFR_INCLUDES} )
    add_definitions( -DBUILD_ARBITRARY_PRECISION )
    set( SOURCES ${SOURCES} ${APSOURCES} )
  endif( not MPFR_FOUND )
else( ARBITRARY_PRECISION )
  if( BUILD_DOUBLE )
    add_definitions( -DBUILD_DOUBLE )
  else( BUILD_DOUBLE )
    add_definitions( -DBUILD_SINGLE )
  endif( BUILD_DOUBLE )
endif( ARBITRARY_PRECISION )
if( THREADSAFE )
  add_definitions( -DSAFESTATIC= )
  add_definitions( -DTHREADSAFE )
else( THREADSAFE )
  add_definitions( -DSAFESTATIC=static )
endif( THREADSAFE )
if( OMP )
  find_package( OpenMP REQUIRED )
  include_directories( ${OPENMP_INCLUDE_DIRS} )
  set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS} )
#  set (CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG} ${OpenMP_CXX_FLAGS})
#  set (CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${OpenMP_CXX_FLAGS})
#  set (CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} ${OpenMP_CXX_FLAGS})
#  set (CMAKE_CXX_FLAGS_RELEASE ${OpenMP_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE})
#  set (CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_MINSIZEREL} ${OpenMP_CXX_FLAGS})
endif( OMP )
if( PROFILE )
  set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-pg -g" )
  set( CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG} "-pg -g" )
  set( CMAKE_CXX_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE} "-pg -g" )
  set( CMAKE_CXX_FLAGS_RELDEB ${CMAKE_C_FLAGS_RELDEB} "pg -g" )
  set( CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_C_FLAGS_MINSIZEREL} "pg -g" )
endif( PROFILE )

# find libraries
find_package( LibXml2 REQUIRED )
find_package( GLPK )
find_package( Boost REQUIRED )
get_property( _LANGUAGES_ GLOBAL PROPERTY ENABLED_LANGUAGES )
if( APPLE )
  find_package( BLAS REQUIRED )
  find_package( LAPACK REQUIRED )
else( APPLE )
  add_definitions( -DADDRESS_ATLAS_BUG )
  find_package( CBLAS REQUIRED )
  find_package( LAPACKLite REQUIRED )
  set( BLAS_LIBRARIES ${CBLAS_LIBRARIES} )
endif( APPLE )
find_package( QHULL REQUIRED )
find_package( osg )
find_package( osgViewer )
find_package( osgDB )
find_package( osgGA )
find_package( OpenThreads )

# setup OSG includes and additional sources
if( USE_OSG AND OSG_FOUND )
  if( OSGDB_FOUND )
    if( OSGVIEWER_FOUND )
      if( OPENTHREADS_FOUND )
        include_directories( ${OSG_INCLUDE_DIR} )
        include_directories( ${OSGDB_INCLUDE_DIR} )
        include_directories( ${OSGGA_INCLUDE_DIR} )
        include_directories( ${OSGVIEWER_INCLUDE_DIR} )
        include_directories( ${OPENTHREADS_INCLUDE_DIR} )
        add_definitions( -DUSE_OSG )
        set( SOURCES ${SOURCES} OSGGroupWrapper.cpp )
      else( OPENTHREADS_FOUND )
        unset( OSG_FOUND )
      endif( OPENTHREADS_FOUND )
    else( OSGVIEWER_FOUND )
      unset( OSG_FOUND )
    endif( OSGVIEWER_FOUND )
  else( OSGDB_FOUND )
    unset( OSG_FOUND )
  endif( OSGDB_FOUND )
endif( USE_OSG AND OSG_FOUND )

# setup include directories, compiler flags, and libraries for optional pkgs
if( LIBXML2_FOUND )
  set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${LIBXML2_DEFINITIONS} )
  set( CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG} ${LIBXML2_DEFINITIONS} )
  set( CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${LIBXML2_DEFINITIONS} )
  set( CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} ${LIBXML2_DEFINITIONS} )
  set( CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_MINSIZEREL} ${LIBXML2_DEFINITIONS} )
  include_directories( ${LIBXML2_INCLUDE_DIR} )
endif( LIBXML2_FOUND )
if( GLPK_FOUND )
  add_definitions( -DUSE_GLPK )
  include_directories( ${GLPK_INCLUDE_DIRS} )
endif( GLPK_FOUND )

# setup include directories, compiler flags, and libraries for required pkgs
include_directories( ${BOOST_INCLUDE_DIRS} )
include_directories( ${QHULL_INCLUDE_DIRS} )

# configure checks: optional libraries
#if (USE_PATH)
#  unset (USE_PATH CACHE)
#  CHECK_LIBRARY_EXISTS(path46 MCP_SetInterface "" USE_PATH)
#endif (USE_PATH)
CHECK_LIBRARY_EXISTS( odepack dlsode_ "" HAVE_ODEPACK )

# setup necessary library list
set( REQLIBS qhull )

# build against PATH?
if( USE_PATH )
  add_definitions( -DUSE_PATH )
  set( EXTRA_LIBS ${EXTRA_LIBS} path46 )
  set( SOURCES ${SOURCES} PathLCPSolver.cpp )
endif( USE_PATH )

# build against ODEPACK?
if( HAVE_ODEPACK )
  add_definitions( -DUSE_ODEPACK )
  set( EXTRA_LIBS ${EXTRA_LIBS} odepack )
endif( HAVE_ODEPACK )

set( MOBY_LIBRARIES ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES} ${QHULL_LIBRARIES} ${EXTRA_LIBS} )

# link optional libraries
if( ARBITRARY_PRECISION )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${MPFR_LIBRARIES} )
endif( ARBITRARY_PRECISION )
if( OMP )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${OPENMP_LIBRARIES} )
endif( OMP )
if( LIBXML2_FOUND )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${LIBXML2_LIBRARIES} )
endif( LIBXML2_FOUND )
if( GLPK_FOUND )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${GLPK_LIBRARIES} )
endif( GLPK_FOUND )
if( USE_OSG AND OSG_FOUND )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${OSG_LIBRARIES} )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${OSGVIEWER_LIBRARIES} )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${OSGDB_LIBRARIES} )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${OSGGA_LIBRARIES} )
  set( MOBY_LIBRARIES ${MOBY_LIBRARIES} ${OPENTHREADS_LIBRARY} )
endif( USE_OSG AND OSG_FOUND )

set( MOBY_LIBRARIES ${MOBY_LIBRARIES} /home/james/Moby/build/libMoby.so )

set( MOBY_INCLUDE_DIR /home/james/Moby/include )
#set( MOBY_LIBRARIES /home/james/Moby/build/libMoby.so ${OSG_LIBRARY} ${OSGDB_LIBRARY} ${OSGGA_LIBRARY} ${OSGVIEWER_LIBRARY} ${OPENTHREADS_LIBRARY} )

#message( ${MOBY_INCLUDE_DIR} )
#message( ${MOBY_LIBRARIES} )
