#PROJECT(fslview)
#SET(CMAKE_MODULE_PATH ${FSLVIEW_SOURCE_DIR}/cmake)

FIND_PACKAGE( Qt4 REQUIRED )
FIND_PACKAGE( Qwt REQUIRED )
FIND_PACKAGE( VTK REQUIRED )
FIND_PACKAGE( Boost REQUIRED )

MESSAGE(STATUS, "Setting Qt options from: ${QT_USE_FILE}")

SET(QT_USE_QTXML TRUE)
#SET(QT_USE_QTOPENGL TRUE)
SET(QT_USE_QT3SUPPORT TRUE)
SET(QT_USE_QTASSISTANT FALSE)
SET(QT_USE_QTWEBKIT TRUE)
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS( -DQT3_SUPPORT )
ADD_DEFINITIONS( ${QT_DEFINITIONS} )

INCLUDE_DIRECTORIES( ${QWT_INCLUDE_DIR} )

IF(VTK_DIR)
  INCLUDE( ${VTK_USE_FILE} )
  ADD_DEFINITIONS( -DHAVE_VTK )
ENDIF(VTK_DIR)

INCLUDE_DIRECTORIES( ${BOOST_DIR} )

INCLUDE_DIRECTORIES( ${FSL_DIR}/include )
INCLUDE_DIRECTORIES( ${FSL_DIR}/extras/include/newmat )
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/.. )


SET(utility_srcs
  preferences.cpp
  atlas.cpp
  filemanager.cpp
  cursor.cpp
  lookuptable.cpp
  clusterdata.cpp)

SET(srcs
  main.cpp
  preferences.cpp
  atlas.cpp
  filemanager.cpp
  logger.cpp
  assistantclient.cpp
  splashscreen.cpp 
  version.cpp
  tracker.cpp
  cursor.cpp
  bricon.cpp
  imagegroup.cpp 
  overlaylist.cpp 
  metaimage.cpp 
  imagedata.cpp 
  drawsettings.cpp 
  imagedisplaysetting.cpp 
  lookuptable.cpp
  curvedatalist.cpp 
  imagedatastore.cpp 
  imagebuffer.cpp 
  graphmanager.cpp 
  briconwidget.cpp 
  cursorwidget.cpp 
  drawwidget.cpp 
#  imagewidget.cpp 
  imagewindow.cpp 
  viewwidget.cpp 
  orthowidget.cpp 
  singlewidget.cpp 
#  lightboxwidget.cpp
  lightboxwindow.cpp
  slicewidget.cpp 
  sliceview.cpp 
  timeserieswidget.cpp 
  singleserieswidget.cpp 
  overlaywidget.cpp 
  histogramwidget.cpp 
  clusterbrowser.cpp
  histogramtoolbar.cpp 
  overlayinfodialog.cpp
  meshoptionsdialog.cpp
  atlasoptionsdialog.cpp
  atlasselectiondialog.cpp
  viewoptionsdialog.cpp
  histogramoptionsdialogimpl.cpp 
  properties.cpp 
  propertiesdialogimpl.cpp 
  vtkpropertydialog.cpp 
  vtktoolbar.cpp 
  timeseriestoolbar.cpp
  talairachwidget.cpp  
  shape.cpp 
  rect.cpp 
  command.cpp 
  modelfit.cpp
  clusterdata.cpp
  tsplotcode.cpp 
  maintoolbar.cpp 
  modetoolbar.cpp 
  vtkmeshsurface.cpp
  vtkwidget.cpp
  application.cpp)

QT_WRAP_CPP(ui moc_srcs
  application.h
#  assistantclient.h
  overlayinfodialog.h
  meshoptionsdialog.h
  atlasoptionsdialog.h
  atlasselectiondialog.h
  viewoptionsdialog.h
#  imagewidget.h
  imagewindow.h
  viewwidget.h
  sliceview.h
  orthowidget.h
  singlewidget.h
#  lightboxwidget.h
  lightboxwindow.h
  briconwidget.h
  cursorwidget.h
  drawwidget.h
  slicewidget.h
  splashscreen.h
  timeserieswidget.h
  talairachwidget.h
  clusterbrowser.h
  singleserieswidget.h
  maintoolbar.h
  modetoolbar.h
  histogramwidget.h
  vtkwidget.h
  vtktoolbar.h
  overlaywidget.h)

QT_WRAP_UI(ui ui_hdrs dd
  applicationwindowbase.ui
# Toolbars
  maintoolbarbase.ui 
  modetoolbarbase.ui 
  drawtoolbarbase.ui 
  briconwidgetbase.ui
# Dock windows etc
#  talairachwidgetbase.ui 
  atlasdockbase.ui 
  atlasoptionsdialogbase.ui 
  atlasselectiondialogbase.ui 
#  cursorwidgetbase.ui
  cursordockbase.ui
#  overlaywidgetbase.ui 
  overlaydockbase.ui 
  overlayinfodialogbase.ui 
# View windows
  imagewindowbase.ui
  clusterbrowserbase.ui
  sliceviewbase.ui
  histogramoptionsdialog.ui 
  histogramtoolbarbase.ui 
  viewoptionsdialogbase.ui 
  propertiesdialog.ui 
  createmaskdialog.ui 
  timeseriestoolbarbase.ui 
  smartpendialogbase.ui
  vtktoolbarbase.ui 
  vtkpropertydialogbase.ui 
  meshoptionsdialogbase.ui 
  timeserieswindowbase.ui)

IF(NOT WINDOWS)
  FIND_PATH( FSL_DIR etc/fslconf/fsl.sh
	   /usr/local/fsl )
ENDIF(NOT WINDOWS)

SET(HAS_SYSLOG ON CACHE BOOL "Use syslog found on many Unix systems")

CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h )

#LINK_DIRECTORIES( ${X11_LIBRARY_DIR} )
#LINK_DIRECTORIES( ${VTK_LIBRARY_DIRS} )
#LINK_DIRECTORIES( ${QWT_LIB_DIR} )
#LINK_DIRECTORIES( ${FSL_DIR}/lib ${FSL_DIR}/extras/lib )
#LINK_DIRECTORIES( ${FSLVIEW_BINARY_DIR}/lib )

ADD_LIBRARY( ui ${ui_hdrs} ${moc_srcs} )
ADD_LIBRARY( utilities ${utility_srcs} )

#QT_WRAP_CPP( melview mv_srcs melview.h)
#QT_WRAP_UI( melview mv_hdrs dd melviewbase.ui)
#ADD_EXECUTABLE( melview melview.cpp ${mv_srcs} ${mv_hdrs} ${rcc_srcs})
#TARGET_LINK_LIBRARIES( melview ${QT_LIBRARIES} ${QWT_LIBRARIES} )

ADD_EXECUTABLE( atlasquery atlasquery.cc )
TARGET_LINK_LIBRARIES( atlasquery utilities storage miscmaths newmat utils fslio niftiio znz z )
TARGET_LINK_LIBRARIES( atlasquery ${QT_LIBRARIES} )

SET(rccs fslview.qrc)
QT4_ADD_RESOURCES(rcc_srcs ${rccs})

IF(APPLE)
  SET( MACOSX_BUNDLE_ICON_FILE application.icns )
  INSTALL(FILES application.icns DESTINATION bin/fslview.app/Contents/Resources)
  SET( MACOSX_BUNDLE_COPYRIGHT "Copyright (c) 2005, University of Oxford" )
  
  ADD_EXECUTABLE( fslview MACOSX_BUNDLE ${srcs} ${rcc_srcs})
  INSTALL(TARGETS fslview 
    BUNDLE DESTINATION bin
#    BUNDLE DESTINATION /Applications/MacPorts/FSL
#    RUNTIME DESTINATION bin
	LIBRARY DESTINATION lib
 	ARCHIVE DESTINATION lib 
	)

  FILE( GLOB vtklibs ${VTK_DIR}/*.dylib* )
  FILE( GLOB qwtlibs /opt/local/lib/libqwt*.dylib )
  FILE( GLOB extralibs /opt/local/lib/libpng*.dylib
		/opt/local/lib/libssl*.dylib
		/opt/local/lib/libcrypto*.dylib
		/opt/local/lib/libz*.dylib )
#  FILE( GLOB qtlibs ${QT_INCLUDE_DIR}/../lib/*.dylib* )

  INSTALL(FILES ${vtklibs} ${qwtlibs} ${extralibs} DESTINATION bin/fslview.app/Contents/Frameworks)
#  INSTALL(DIRECTORY $ENV{QTDIR}/bin/assistant.app DESTINATION lib)
ELSE (APPLE)
  ADD_EXECUTABLE( fslview ${srcs} ${rcc_srcs})
  INSTALL(TARGETS fslview
	RUNTIME DESTINATION bin
	LIBRARY DESTINATION lib
	ARCHIVE DESTINATION lib )

  FILE( GLOB vtklibs ${VTK_DIR}/../libvtk*.so* ${VTK_DIR}/../libQVTK.so* )
  FILE( GLOB osmesa /usr/lib*/libOSMesa*.so* )
  FILE( GLOB qwtlibs ${QWT_LIB_DIR}/lib/*.so* )
  FILE( GLOB qtlibs ${QT_QT_LIBRARY}* ${QT_QASSISTANTCLIENT_LIBRARY}* )
  FILE( GLOB assistant ${QT_INCLUDE_DIR}/../bin/assistant )
  #INSTALL(FILES ${vtklibs} ${osmesa} ${qtlibs} ${qwtlibs} DESTINATION lib)
#  INSTALL(FILES ${assistant} DESTINATION lib)
#  INSTALL(TARGETS fslview atlasquery RUNTIME DESTINATION /bin )
  #INSTALL(TARGETS fslview atlasquery 
ENDIF(APPLE)

   
MESSAGE(STATUS, "${VTK_LIBRARIES} ${QT_LIBRARIES} ${QWT_LIBRARIES}")

TARGET_LINK_LIBRARIES( fslview utilities ui storage trace)
TARGET_LINK_LIBRARIES( fslview ${QT_LIBRARIES} fslio niftiio znz z )
TARGET_LINK_LIBRARIES( fslview miscmaths newmat utils )
#TARGET_LINK_LIBRARIES( fslview qassistantclient )
TARGET_LINK_LIBRARIES( fslview ${QWT_LIBRARIES} )
TARGET_LINK_LIBRARIES( fslview vtkHybrid vtkWidgets QVTK )

IF(UNIX)
#  TARGET_LINK_LIBRARIES( fslview ${FSLVIEW_BINARY_DIR}/lib/libfslio.a )
#   INSTALL_FILES ( /lib ${VTK_LIBRARY_DIR}/.*\\.so$ )
ENDIF(UNIX)
IF(WINDOWS)
    LINK_DIRECTORIES( ${FSLVIEW_BINARY_DIR}/lib/Debug )
ENDIF(WINDOWS)

# Now for the test harness

CREATE_TEST_SOURCELIST(Tests
  atlastests.cpp
  atlastst.cpp
  agtest.cpp)

REMOVE(srcs main.cpp)

#ADD_EXECUTABLE( atlastests ${srcs} ${Tests} )
#TARGET_LINK_LIBRARIES(atlastests ui storage
#  fslio niftiio znz z
#  miscmaths newmat utils qassistantclient qwt QVTK vtkHybrid vtkWidgets )

#SET(TestsToRun ${Tests})
#REMOVE(TestsToRun atlastests.cpp)

#FOREACH (test ${TestsToRun})
#  GET_FILENAME_COMPONENT( TName ${test} NAME_WE )
#  ADD_TEST(${TName} ${EXECUTABLE_OUTPUT_PATH}/atlastests ${TName})
#ENDFOREACH (test)

