CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(${GINKGO_PROJECT})

MESSAGE(STATUS "GINKGO PROJECT = ${GINKGO_PROJECT}")

SET(SRCS main.cpp)

ADD_GINKGO_DEFINITIONS()

INCLUDE_DIRECTORIES(${CADxCore_INCLUDE_DIR})

IF(LINUX AND NOT CUSTOM_BUILD)
	SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
	SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
	SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/ginkgocadx")
	SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
	LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib/ginkgocadx" isSystemDir)
	IF("${isSystemDir}" STREQUAL "-1")
		SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/ginkgocadx")
	ENDIF()
ENDIF()

ADD_DEFINITIONS(${libcurl_defines})
INCLUDE_DIRECTORIES(${libcurl_INCLUDE_DIR})
LINK_DIRECTORIES(${libcurl_DIR})


IF(USE_CUSTOM_WX)
        ADD_DEFINITIONS(${wxWidgets_defines})
        INCLUDE_DIRECTORIES(${wxWidgets_INCLUDE_DIR})
        LINK_DIRECTORIES(${wxWidgets_DIR})
ELSE()
        INCLUDE(${wxWidgets_USE_FILE})
	#MESSAGE("SYSTEM_WX: VL = ${VTK_DIR}")
        LINK_DIRECTORIES(${wxWidgets_DIR})
ENDIF()

IF (USE_CUSTOM_VTK)
	LINK_DIRECTORIES(${VTK_DIR})
ELSE()
	#MESSAGE("SYSTEM_VTK: VL = ${VTK_DIR}")
	LINK_DIRECTORIES(${VTK_LIBRARY_DIRS})
ENDIF()


IF(USE_CUSTOM_ITK)
	LINK_DIRECTORIES(${ITK_DIR})
ELSE()
	#MESSAGE("SYSTEM_ITK: IL = ${ITK_DIR}")
	LINK_DIRECTORIES(${ITK_DIR})
ENDIF()

IF(USE_CUSTOM_DCMTK)
	ADD_DEFINITIONS(${DCMTK_defines})
	INCLUDE_DIRECTORIES(${DCMTK_INCLUDE_DIR})
	LINK_DIRECTORIES(${DCMTK_DIR})
ELSE()
	ADD_DEFINITIONS(-DHAVE_CONFIG_H)
	INCLUDE_DIRECTORIES(${DCMTK_INCLUDE_DIRS})
	#MESSAGE("DCMTKLIBS = ${DCMTK_LIBRARIES}")
	#MESSAGE("DCMTKINC = ${DCMTK_INCLUDE_DIRS}")
ENDIF()

LINK_DIRECTORIES(${Cairo_DIR})
LINK_DIRECTORIES(${Pixman_DIR})
#LINK_DIRECTORIES(${XmlRpc++_DIR})

IF(MACOS)
	SET(SRCS ${SRCS}
		${CMAKE_CURRENT_SOURCE_DIR}/ginkgocadx.icns
		${CMAKE_CURRENT_SOURCE_DIR}/dcmdoc.icns
		${CMAKE_CURRENT_SOURCE_DIR}/../cadxcore/resources/conformance.xml
	)
	FOREACH(PLUGIN ${GINKGO_PLUGINS})
		EXECUTE_PROCESS(
			COMMAND /bin/bash -c "touch ${CMAKE_CURRENT_BINARY_DIR}/../${PLUGIN}/${PLUGIN}.inf"
		)
		SET(SRCS ${SRCS} ${CMAKE_CURRENT_BINARY_DIR}/../${PLUGIN}/${PLUGIN}.inf)
		SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/../${PLUGIN}/${PLUGIN}.inf PROPERTIES MACOSX_PACKAGE_LOCATION PlugIns)
	ENDFOREACH()
	
	SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/ginkgocadx.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
	SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/dcmdoc.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
	SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/../cadxcore/resources/conformance.xml PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
	SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/PkgInfo PROPERTIES MACOSX_PACKAGE_LOCATION .)
	LINK_LIBRARIES(CADxCore)
ENDIF()

ADD_EXECUTABLE(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ${SRCS})

IF(LINUX)
  TARGET_LINK_LIBRARIES(${PROJECT_NAME} CADxCore pthread dl)
  TARGET_LINK_LIBRARIES(${PROJECT_NAME} visualizator)
  TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${GTK_LIBRARIES})
ENDIF()

IF(USE_CUSTOM_DCMTK)
	MESSAGE(STATUS "DCMTK_LIBS= ${DCMTK_LIBRARIES}" )
	TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${DCMTK_LIBRARIES})
ELSE()
	TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${DCMTK_LIBRARIES} oflog dcmdsig dcmtls)
ENDIF()
IF(LINUX)
	SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
	LINK_INTERFACE_LIBRARIES ""
	CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/ginkgocadx")

	INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin)
ENDIF()

IF (MACOS)
	SET(DIRS
		${wxWidgets_DIR}
		${VTK_DIR}
		${ITK_DIR}
		${DCMTK_DIR}
		CADxCore
		${CMAKE_CURRENT_BINARY_DIR}/../cadxcore
	)
	MESSAGE(STATUS "Directories: ${DIRS}")
	SET(GINKGO_PLUGINS "" CACHE PARENT_SCOPE "")

	SET(APPBUNDLE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.app)
	SET(MACOSX_BUNDLE_GUI_IDENTIFIER "com.metaemotion.Ginkgo_CADx")
	SET(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME})
	SET(MACOSX_BUNDLE_VERSION ${GINKGO_VERSION_STR})
	
	CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/GinkgoCADx-Info.plist.in ${CMAKE_CURRENT_BINARY_DIR}/Info.plist)

	SET(LIBS)

	MESSAGE(STATUS "PLUGINS = ${GINKGO_PLUGINS}")
	FOREACH(PLUGIN ${GINKGO_PLUGINS})
		LIST(APPEND LIBS ${CMAKE_CURRENT_BINARY_DIR}/../${PLUGIN}/lib${PLUGIN}.dylib)
	ENDFOREACH()

	install(CODE 
	        " 
	        include(BundleUtilities) 
	        fixup_bundle(\"${APPBUNDLE}\" \"${LIBS}\" \"${DIRS}\") 
	        " 
	        COMPONENT Runtime 
	    )
		
	SET_TARGET_PROPERTIES(
		"${PROJECT_NAME}"
		PROPERTIES
			MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_BINARY_DIR}/Info.plist
		    LINK_FLAGS "-Wl ${XCODE_LINK_FLAGS}"
	)
ENDIF()

MESSAGE(STATUS "OS=[${OS}], Arch=[${ARCH}], Target=[${CMAKE_BUILD_TYPE}], Project=[${PROJECT_NAME}]")
