option(WITH_GMX "Build gromacs reader/writer, disabling leads to reduced functionality!" ON)
option(WITH_GMX_DEVEL "Build against gromacs devel version, very experimental!" OFF)
option(GMX_DOUBLE "Use double precision version of gromacs" OFF)

file(GLOB GMX_SOURCES modules/io/gmx*.cc modules/io/gro*.cc modules/io/pdbtopology*.cc)
file(GLOB NOT_GMX_SOURCES modules/io/gmx_print_version.cc modules/io/gmx_version_nb.cc)
list(REMOVE_ITEM GMX_SOURCES ${NOT_GMX_SOURCES})

file(GLOB IO_SOURCES modules/io/*.cc)
file(GLOB NOT_IO_SOURCES modules/io/mdp*.cc)
list(REMOVE_ITEM IO_SOURCES ${GMX_SOURCES} ${NOT_GMX_SOURCES} ${NOT_IO_SOURCES})

if (WITH_GMX)
  if (GMX_DOUBLE)
    set(GMX_SUFFIX "_d")
  endif(GMX_DOUBLE)
  if (WITH_GMX_DEVEL)
    message(WARNING " Using gromacs-5.0, this is very experimental, hope you know what you are doing")
    set(LIBGMX "libgromacs") 
  else(WITH_GMX_DEVEL)
    set(LIBGMX "libgmx") 
  endif(WITH_GMX_DEVEL)
  find_package(GROMACS COMPONENTS "${LIBGMX}${GMX_SUFFIX}")
  if (NOT GROMACS_FOUND)
    message(FATAL_ERROR "gromacs not found,  make sure you have installed at least the gromacs-4.0.7 and it's dev package. If the gromacs module was not found above, make sure you have sourced GMXRC or set PKG_CONFIG_PATH yourself. If you have a double precision version of gromacs enable to build against it with -DGMX_DOUBLE=ON. If you have gromacs-5.0 installed enable to build against it with -DWITH_GMX_DEVEL=ON. Gromacs support can be disable it with -DWITH_GMX=OFF. ")
  endif(NOT GROMACS_FOUND)
  #general to a gmx builds
  add_definitions(${GROMACS_DEFINITIONS})
  include_directories(${GROMACS_INCLUDE_DIRS})
  set(GMX ${GROMACS_VERSION})
  add_executable(gmx_print_version modules/io/gmx_print_version.cc)
  target_link_libraries(gmx_print_version ${GROMACS_LIBRARIES})
  add_custom_command(OUTPUT gmx_libs_version.h COMMAND gmx_print_version > gmx_libs_version.h DEPENDS gmx_print_version) 
  list(APPEND GMX_SOURCES gmx_libs_version.h)
else(WITH_GMX)
  set(GMX)
  set(GMX_SOURCES)
endif(WITH_GMX)

configure_file(votca_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/votca_config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_custom_target(hgversion COMMAND ${CMAKE_COMMAND} -DTOP_SOURCE_DIR="${CMAKE_SOURCE_DIR}" -P ${CMAKE_MODULE_PATH}/hgversion.cmake)
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES hgversion.h)

file(GLOB VOTCA_SOURCES *.cc)
file(GLOB NOT_VOTCA_SOURCES version_nb.cc test.cc)
list(REMOVE_ITEM VOTCA_SOURCES ${NOT_VOTCA_SOURCES})
add_library(votca_csg ${VOTCA_SOURCES} ${GMX_SOURCES} ${IO_SOURCES})
add_dependencies(votca_csg hgversion)
set_target_properties(votca_csg PROPERTIES SOVERSION ${SOVERSION})
target_link_libraries(votca_csg ${VOTCA_TOOLS_LIBRARIES} ${GROMACS_LIBRARIES} ${BOOST_LIBRARIES} ${THREAD_LIBRARIES})
install(TARGETS votca_csg LIBRARY DESTINATION ${LIB} ARCHIVE DESTINATION ${LIB})

configure_file(libvotca_csg.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libvotca_csg.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libvotca_csg.pc DESTINATION ${LIB}/pkgconfig)
