message("\nUNIX non APPLE environment\n")
message("\nIf building as the ${CMAKE_PROJECT_NAME} standalone library,
run the build like this:\n")
message("mkdir build; pushd build; cmake -G \"Unix Makefiles\" -DCMAKE_BUILD_TYPE=Debug ../ ; make -j10 ; popd\n")

set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES /usr/include)
set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES /usr/include)

# This is used throughout all the build system files
set(TARGET ${LOWCASE_PROJECT_NAME})


message("")
if(STANDALONE_BUILD_MODE)
  message(STATUS "${BoldGreen}Building this library in STANDALONE mode${ColourReset}\n")
else()
  message(STATUS "${BoldGreen}Building this library in EMBEDDED mode${ColourReset}\n")
endif(STANDALONE_BUILD_MODE)
message("")


set(LIBNAME massgui)


message(\n${BoldGreen}"Now configuring library lib${LIBNAME}"${ColourReset}\n)

########################################################
# Files
set(${LIBNAME}_SRCS
  #
  ColorSelector.cpp
  IsotopicClusterGeneratorDlg.cpp
  IsotopicClusterShaperDlg.cpp
  IsotopicDataTableView.cpp
  IsotopicDataTableViewModel.cpp
  MassDataClientServerConfigDlg.cpp
  MassPeakShaperConfigDlg.cpp
  MassPeakShaperConfigWidget.cpp)

set(${LIBNAME}_UIS
  ui/IsotopicClusterGeneratorDlg.ui
  ui/MassPeakShaperConfigDlg.ui
  ui/IsotopicClusterShaperDlg.ui
  ui/ElementGroupBoxWidget.ui
  ui/MassDataClientServerConfigDlg.ui
  ui/MassPeakShaperConfigWidget.ui
)

find_package(Qt6 COMPONENTS Widgets Core Gui PrintSupport Svg Xml Sql Concurrent REQUIRED)

qt6_wrap_ui(${LIBNAME}_UIS_H ${${LIBNAME}_UIS})

qt6_add_resources(${TARGET}_QRC_CPP ../libmassgui-ressources.qrc)

# Because the header files are in their own directory and not along the source
# files, we need to have them listed explicitely for automoc to work properly
# below.

# Create a variable to hold the 'includes' directory *relative* to the
# current CMakeLists.txt file. We need to use ${CMAKE_CURRENT_LIST_DIR}/../
# relative path # instead of ${CMAKE_SOURCE_DIR}/ because that variable
# is going to be modified when the libmass standalone build is replaced by a
# build where libmass is embedded in another source tree.

set(INCLUDE_DIR_RELATIVE_PATH "${CMAKE_CURRENT_LIST_DIR}/../includes")

file(GLOB ${LIBNAME}_HEADERS ${INCLUDE_DIR_RELATIVE_PATH}/lib${LIBNAME}/*.hpp)
message(STATUS "Included the header files from ${INCLUDE_DIR_RELATIVE_PATH}/lib${LIBNAME}")
message(STATUS "The header files: ${${LIBNAME}_HEADERS}")

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

###############################################################
# Configuration of the binary to be built
###############################################################

if(STANDALONE_BUILD_MODE)

  message(STATUS "${BoldGreen}Building this library in STANDALONE mode${ColourReset}")

  # We depend on libmass, which we find in its own development directory.

  # We have to include like this: <libmass/header.hpp>

  set(HOME_DEVEL_DIR "$ENV{HOME}/devel")

  set_property(GLOBAL PROPERTY glob_prop_libmass_INCLUDE_DIRS "${HOME_DEVEL_DIR}/libmass/includes")
  set_property(GLOBAL PROPERTY glob_prop_libmass_LIBRARIES "${HOME_DEVEL_DIR}/libmass/libmass.a")

  get_property(libmass_INCLUDE_DIRS GLOBAL PROPERTY glob_prop_libmass_INCLUDE_DIRS)
  get_property(libmass_LIBRARIES GLOBAL PROPERTY glob_prop_libmass_LIBRARIES)

  message(STATUS "Found libmass at: ${libmass_LIBRARIES} with include dir: ${libmass_INCLUDE_DIRS}")

else()

  message(STATUS "${BoldGreen}Building this library in EMBEDDED mode${ColourReset}")

  # Get the lib and include specifs for libmass that has been also built in EMBEDDED mode.

  get_property(libmass_INCLUDE_DIRS GLOBAL PROPERTY glob_prop_libmass_INCLUDE_DIRS)
  get_property(libmass_LIBRARIES GLOBAL PROPERTY glob_prop_libmass_LIBRARIES)

  message(STATUS "Found libmass at: ${libmass_LIBRARIES} with include dir: ${libmass_INCLUDE_DIRS}")

endif()


# Only now can we add the library, because we have defined the sources.

add_library(${LIBNAME} STATIC
  ${${LIBNAME}_HEADERS}
  ${${LIBNAME}_SRCS}
  ${${LIBNAME}_UIS_H}
  ${${TARGET}_QRC_CPP}
  ${PLATFORM_SPECIFIC_SOURCES}
)

target_include_directories(${LIBNAME} PUBLIC
  ${libmass_INCLUDE_DIRS}
  ${INCLUDE_DIR_RELATIVE_PATH}
  ${INCLUDE_DIR_RELATIVE_PATH}/lib${LIBNAME}

  # For the UI compiler output:
  ${CMAKE_CURRENT_BINARY_DIR})

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

set_target_properties(${LIBNAME}
  PROPERTIES OUTPUT_NAME ${LIBNAME}
)

add_custom_command(TARGET ${LIBNAME}
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy
  ${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:${LIBNAME}>
  ${CMAKE_CURRENT_LIST_DIR}/..
)

# Finally actually set the linking dependencies to the library.
target_link_libraries(${LIBNAME}
  ${libmass_LIBRARIES}

  -Wl,--no-as-needed
  PappsoMSpp::Core
  PappsoMSpp::Widget
  -Wl,--as-needed

  Qt6::Widgets
  Qt6::Xml
  Qt6::Svg

  IsoSpec++::IsoSpec++
)

# This is to avoid the include_next(math.h) file not found error.
if(WIN32)
  set_target_properties(${LIBNAME}
  PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
endif()


#############################################################
#############################################################
# Platform-specific CMake configuration


# There are two ways this library might be built for use:

# 1. As a standalone library (call cmake directly to the main source directory)
# 2. As an embedded library, for example inside the massXpert2 or mineXpert2 top
# source directories.

# In both cases we might need to know where the built lib is (along with
# the header files), because it might be needed (as standalone, it is typically
# needed for the test binaries).

# When including lib headers from the embedding project the includes must
# be in the form #include <libmass/header.hpp>

set(temp_lib${LIBNAME}_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../includes/")
# For the UICC generated GUI header files.
list(APPEND temp_lib${LIBNAME}_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../includes/lib${LIBNAME}")
list(APPEND temp_lib${LIBNAME}_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}")

# When including lib headers from the embedding project the includes must
# be in the form #include <libmass/header.hpp>

set_property(GLOBAL PROPERTY glob_prop_lib${LIBNAME}_INCLUDE_DIRS "${temp_lib${LIBNAME}_INCLUDE_DIRS}")
set_property(GLOBAL PROPERTY glob_prop_lib${LIBNAME}_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/../lib${LIBNAME}.a")

get_property(lib${LIBNAME}_INCLUDE_DIRS GLOBAL PROPERTY glob_prop_lib${LIBNAME}_INCLUDE_DIRS)
get_property(lib${LIBNAME}_LIBRARIES GLOBAL PROPERTY glob_prop_lib${LIBNAME}_LIBRARIES)

message("")
message(STATUS "Making lib${LIBNAME} headers available at: ${lib${LIBNAME}_INCLUDE_DIRS}.")
message(STATUS "Making lib${LIBNAME} available at ${lib${LIBNAME}_LIBRARIES}.\n")


message("")
message(STATUS "${BoldGreen}Finished configuring of lib${LIBNAME}.${ColourReset}")
message("")

