# Get the name of the current directory but with no path:
# get_filename_component(TARGET ${CMAKE_CURRENT_BINARY_DIR} NAME)

set(TARGET massxpert)
set(DOCTARGET ${TARGET})
set(MASSXPERT_TARGET ${TARGET})
set(TITLE_TARGET massXpert)

# Name the target module to be able to get its name in the parent directory.
set_directory_properties(PROPERTIES MASSXPERT_TARGET ${MASSXPERT_TARGET})

# On windows, the binary will be with the .exe extension.
if(SYSTEM_UNAME_S MATCHES "^MINGW")
	set(TARGET ${TARGET}.exe)
endif()

if(SYSTEM_UNAME_S MATCHES "Darwin")
	set(TARGET massXpert)
endif()

message("")
message(STATUS "${BoldGreen}Starting configuring of the ${TARGET} submodule${ColourReset}")
message("")

#############################################################
# Software configuration
message(STATUS "CMAKE_CURRENT_BINARY_DIR: " ${CMAKE_CURRENT_BINARY_DIR})

########################################################
# Files
file(GLOB ${TARGET}_nongui_SRCS nongui/*.cpp)
file(GLOB ${TARGET}_gui_SRCS gui/*.cpp)
file(GLOB ${TARGET}_SRCS *.cpp)
file(GLOB ${TARGET}_UIS gui/ui/*.ui)

# The desktop file
if(UNIX AND NOT APPLE)
	install(FILES ${TARGET}.desktop
		DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications)
endif()

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

find_package(Qt5 COMPONENTS Widgets Xml Svg Sql Script)

qt5_wrap_ui(${TARGET}_UIS_H ${${TARGET}_UIS})

qt5_add_resources(${TARGET}QRC_CPP gui/application.qrc)

add_executable(${TARGET}
	${${TARGET}_nongui_SRCS} 
	${${TARGET}_gui_SRCS} 
	${${TARGET}_SRCS} 
	${${TARGET}_UIS_H} 
	${${TARGET}QRC_CPP})

qt5_use_modules(${TARGET} Widgets)
qt5_use_modules(${TARGET} Xml)
qt5_use_modules(${TARGET} Svg)
qt5_use_modules(${TARGET} Sql)
qt5_use_modules(${TARGET} Script)
qt5_use_modules(${TARGET} PrintSupport)

include_directories(
	# For the config.h file generated from config.h.in	
	${CMAKE_BINARY_DIR} 

	# For all the source subdirectories
	${CMAKE_SOURCE_DIR}

	# For the ui_Xyyyyy.h files
	${CMAKE_CURRENT_BINARY_DIR} 

	${CMAKE_BINARY_DIR}/libmassgui

	${Qt5Widgets_INCLUDE_DIRS}
	${Qt5Xml_INCLUDE_DIRS}
	${Qt5Svg_INCLUDE_DIRS}
	${Qt5Sql_INCLUDE_DIRS}
	${Qt5Script_INCLUDE_DIRS}
	${Qt5PrintSupport_INCLUDE_DIRS})


# On GNU/Linux, make sure we have the QCustomPlot library
# installed on the system (on MINGW, we have the
# source files (see above).
if((NOT SYSTEM_UNAME_S MATCHES "^MINGW.*") AND
	(NOT SYSTEM_UNAME_S MATCHES "Darwin"))

	find_package(QCustomPlot)

	if(NOT QCustomPlot_FOUND)
		message(FATAL_ERROR "QCustomPlot not found!")
	else()
		message(STATUS "QCustomPlot found!")
	endif()

endif()


##On MINGW, we need to add the qcustomplot directory
##that contains the header file and the tclap headers.
#if(SYSTEM_UNAME_S MATCHES "^MINGW.*")
#include_directories(${CMAKE_SOURCE_DIR}/qcustomplot)
#include_directories(${CMAKE_SOURCE_DIR}) # for the tclap
#endif()


# Now handle the private libraries for non-gui stuff (libmass.a) and gui stuff
# (libmassgui.a)
if(SYSTEM_UNAME_S MATCHES "Darwin")

	set(MASSLIB "${CMAKE_BINARY_DIR}/libmass/libmass.a")
	set(MASSGUILIB "${CMAKE_BINARY_DIR}/libmassgui/libmassgui.a")

else()

	set(MASSLIB "-Wl,-whole-archive ${CMAKE_BINARY_DIR}/libmass/libmass.a -Wl,-no-whole-archive")
	set(MASSGUILIB "-Wl,-whole-archive ${CMAKE_BINARY_DIR}/libmassgui/libmassgui.a -Wl,-no-whole-archive")

endif()

message(STATUS "libdir: ${CMAKE_BINARY_DIR}/libmass/libmass.a")
message(STATUS "guilibdir: ${CMAKE_BINARY_DIR}/libmassgui/libmassgui.a")


# Use the Widgets module from Qt 5. We craft a variable for this so as to
# transmit it directly to the tests subdirectory that will need it.
# Also, take the opportunity to ask for linking of the libgomp or lomp OPENMP
# library, depending on the compiler.

message(STATUS "Using parallelizing library ${OPENMP_LIBRARY}")

set(required_target_link_libraries
	${MASSLIB}
	${MASSGUILIB}
	${OPENMP_LIBRARY}	
	Qt5::Widgets
	Qt5::Xml
	Qt5::Svg
	Qt5::Sql
	Qt5::Script
	Qt5::PrintSupport)


##############################################################
# libpwiz for reading mass spec data files,
message(STATUS "${BoldYellow}Link against the libpwiz library.${ColourReset}")
if(SYSTEM_UNAME_S MATCHES "^MINGW.*")
	include_directories("/mingw64/include/pwiz")
elseif(SYSTEM_UNAME_S MATCHES "Darwin")
	include_directories("/opt/local/include/pwiz")
	link_directories("/opt/local/lib")
else()
	include_directories("/usr/include/pwiz")
endif()

set(required_target_link_libraries
	${required_target_link_libraries}
	pwiz)

##############################################################
# libboost_* on mingw
# On MINGW.* we are our own provider of the boost libraries.
if(SYSTEM_UNAME_S MATCHES "^MINGW.*")

	message(STATUS "${BoldYellow}Link against the libboost libraries.${ColourReset}")

	include_directories("${CMAKE_SOURCE_DIR}/../../boost/boost_1_56_0") 

	set(required_target_link_libraries
		${required_target_link_libraries}
		boost_chrono
		boost_filesystem
		boost_iostreams
		boost_program_options
		boost_serialization
		boost_system
		boost_thread
		z)

elseif(SYSTEM_UNAME_S MATCHES "Darwin")
	include_directories("/opt/local/include")
	link_directories("/opt/local/lib")

	set(required_target_link_libraries
		${required_target_link_libraries}
		boost_chrono-mt
		boost_filesystem-mt
		boost_iostreams-mt
		boost_program_options-mt
		boost_serialization-mt
		boost_system-mt
		boost_thread-mt
		z)

endif()


# On MINGW.*, we'll need to add the -lstdc++ flag. Don't ask why.
if(SYSTEM_UNAME_S MATCHES "^MINGW.*")

	set(required_target_link_libraries
		${required_target_link_libraries}
		"stdc++")

endif()

# When on UNIX/NOT APPLE we can make use the system-wide qcustomplot library.
if((NOT SYSTEM_UNAME_S MATCHES "^MINGW.*") AND
	(NOT SYSTEM_UNAME_S MATCHES "Darwin"))

	set(required_target_link_libraries
		${required_target_link_libraries}
		"qcustomplot")

endif()


#### Debugging stuff
message(STATUS required_target_link_libraries: ${required_target_link_libraries})

message(STATUS "Now printing all the include directories -I...")

get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "included dir='${dir}'")
endforeach()

message(STATUS "Now printing all the linking directories -L...")

get_property(dirs DIRECTORY PROPERTY LINK_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "link directory dir='${dir}'")
endforeach()

# Finally actually set the linking dependencies to the executable.
target_link_libraries(${TARGET}
	${required_target_link_libraries})


# Add an explicit dependency to libmass and libmassgui so that when invoking
# make from inside the massxpert bin build dir, the dependencies get built.
add_dependencies(${TARGET} mass massgui)


if(PROFILE)
	message(STATUS "Profiling is requested, adding linker -pg flag.")
	set (CMAKE_EXE_LINKER_FLAGS "-pg")
endif()

# We want to install the binary arch-dependent target in 
# specific situations. To have proper control, we define the arch
# component.
install(TARGETS ${TARGET} 
	RUNTIME 
	COMPONENT arch
	DESTINATION ${MSXPERTSUITE_BIN_DIR})


#############################################################
###################
# BUILD OF THE DATA

#################
# process subdirs
add_subdirectory(data)


#############################################################
##########################
# BUILD OF THE USER MANUAL

message(STATUS "${TITLE_TARGET} user manual to be built")
add_subdirectory(user-manual)


message("")
message(STATUS "${BoldGreen}Finished configuring of the ${TARGET} submodule${ColourReset}")
message("")
