# We want to use some colors for the message output.
string(ASCII 27 Esc)
set(ColourReset "${Esc}[m")
set(ColourBold  "${Esc}[1m")
set(Red         "${Esc}[31m")
set(Green       "${Esc}[32m")
set(Yellow      "${Esc}[33m")
set(Blue        "${Esc}[34m")
set(Magenta     "${Esc}[35m")
set(Cyan        "${Esc}[36m")
set(White       "${Esc}[37m")
set(BoldRed     "${Esc}[1;31m")
set(BoldGreen   "${Esc}[1;32m")
set(BoldYellow  "${Esc}[1;33m")
set(BoldBlue    "${Esc}[1;34m")
set(BoldMagenta "${Esc}[1;35m")
set(BoldCyan    "${Esc}[1;36m")
set(BoldWhite   "${Esc}[1;37m")

# Ask that uname -s be performed and store the value in SYSTEM_UNAME_S for
# later reference.
execute_process(COMMAND uname -s OUTPUT_VARIABLE SYSTEM_UNAME_S) 

message("")
message(STATUS "${BoldGreen}Starting configuration of msXpertSuite${ColourReset}")
message("")

#############################################################
# Software configuration
project(msXpertSuite)
set(VERSION "3.7.0")

#############################################################
# CMake configuration
cmake_minimum_required(VERSION 3.2.2)

set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_VERBOSE_MAKEFILE ON)

# This export will allow using the flags to be used by
# youcompleteme (vim plugin).
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

# We want C++14
ADD_DEFINITIONS(-std=c++14 -Wno-unknown-pragmas)

# No need to specify position independent code flag on MING64_NT
# because that flag is set automatically and it triggers an error when -Werror
# is set.
if(NOT SYSTEM_UNAME_S MATCHES "^MINGW.*")
  ADD_DEFINITIONS(-fPIC)
endif()


########################################################
# Source tree preparation
# On windows, some libraries are added to the source tree
# while on Debian GNU/Linux there libraries are packaged.
########################################################

if(SYSTEM_UNAME_S MATCHES "^MINGW.*")

  # This include must come before all the others
  # It must include the config-generated config.h file
  # before the others.
  include_directories(${CMAKE_BINARY_DIR})

  # Copy the qcustomplot directory to the source tree
  execute_process(COMMAND cp -rpL ${CMAKE_SOURCE_DIR}/../deps-libs/qcustomplot
    ${CMAKE_SOURCE_DIR}
    WORKING_DIRECTORY	${CMAKE_SOURCE_DIR})
  set(QCUSTOMPLOT_SRC_DIR ${CMAKE_SOURCE_DIR}/qcustomplot)
  include_directories(${QCUSTOMPLOT_SRC_DIR})

  # Copy the tclap directory to the source tree
  execute_process(COMMAND cp -rpL ${CMAKE_SOURCE_DIR}/../deps-libs/tclap
    ${CMAKE_SOURCE_DIR}
    WORKING_DIRECTORY	${CMAKE_SOURCE_DIR})
  set(TCLAP_SRC_DIR ${CMAKE_SOURCE_DIR}/tclap)
  include_directories(${CMAKE_SOURCE_DIR})

endif()



#############################################################
# On MINGW.* we provide a number of libraries by our own, that we
# put, depending on the platform in mingw{32,64}/usr/local/lib
if(SYSTEM_UNAME_S MATCHES "^MINGW.*")

  if(SYSTEM_UNAME_S MATCHES "^MING32.*")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L/mingw32/usr/local/lib") 
  else()
    if(SYSTEM_UNAME_S MATCHES "^MING64.*")
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L/mingw64/usr/local/lib") 
    endif()
  endif()

endif()


#############################################################
# Enable warnings and possibly treat them as errors
add_definitions(-Wall)

if(WARNASERR)
  message(STATUS "${BoldYellow}Warnings treated as errors.${ColourReset}")
  add_definitions(-Werror)
endif()

# On MINGW64_NT, we want to build as release, otherwise build as debug.
if(SYSTEM_UNAME_S MATCHES "^MINGW.*")
  set(CMAKE_BUILD_TYPE "release")
else()
  if(CMAKE_BUILD_TYPE STREQUAL "debug")
    set(CMAKE_BUILD_TYPE "debug")
  else()
    set(CMAKE_BUILD_TYPE "release")
  endif()
endif()

# But the user may want something easier to set: -DDEBUG
if(DEBUG)
  set(CMAKE_BUILD_TYPE "debug")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "debug")
  add_definitions(-g -O0)
  message(STATUS "${BoldYellow}Debug support requested. Adding -g -O0.${ColourReset}")
else()
  add_definitions(-O3)
  message(STATUS "${BoldYellow}Debug support NOT requested. Adding -O3.${ColourReset}")
endif()

if(OPENMP)
  message(STATUS "${BoldYellow}OpenMP support is requested, adding -fopenmp flag.${ColourReset}")
  add_definitions(-fopenmp)

  # Depending on the compiler, if we need the omp.h header file, 
  # we'll have to look for it in different locations.

  message(STATUS "${BoldRed}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")
  message(STATUS "${BoldRed}CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}${ColourReset}")

  if(CMAKE_CXX_COMPILER MATCHES ".*clang.*")
    message(STATUS "${BoldRed}Using clang++${ColourReset}")
    include_directories("${CMAKE_SOURCE_DIR}/clang-openmp")
    link_directories("${CMAKE_SOURCE_DIR}/clang-openmp")
    set(OPENMP_LIBRARY omp)
  else()
    message(STATUS "${BoldRed}Not using clang++${ColourReset}")
    set(OPENMP_LIBRARY gomp)
  endif()

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

if(PROFILE)
  message(STATUS "${BoldYellow}Profiling is requested, adding -pg flag.${ColourReset}")
  add_definitions(-pg)
endif()

#############################################################
#############################################################
# Installation directories depending on the platform.

if(SYSTEM_UNAME_S MATCHES "^MINGW.*")

  if(CMAKE_CXX_COMPILER MATCHES ".*mingw64.*")
    message(STATUS "${BoldYellow}Compiler name matches mingw64 on platform ${SYSTEM_UNAME_S}${ColourReset}")

    if(SYSTEM_UNAME_S MATCHES ".*MINGW64.*")
      #message(STATUS "System uname matches MINGW64")
      set(CMAKE_INSTALL_PREFIX "C:/Program Files/msXpertSuite")
    endif()
    if(SYSTEM_UNAME_S MATCHES "^MINGW32.*")
      #message(STATUS "System uname matches MINGW32")
      set(CMAKE_INSTALL_PREFIX "C:/Program Files (x86)/msXpertSuite")
    endif()
  endif()
  if(CMAKE_CXX_COMPILER MATCHES ".*mingw32.*")
    message(STATUS "${BoldYellow}Compiler name matches mingw32 on platform ${SYSTEM_UNAME_S}${ColourReset}")

    if(SYSTEM_UNAME_S MATCHES "^MINGW32.*")
      #message(STATUS "System uname matches MINGW32")
      set(CMAKE_INSTALL_PREFIX "C:/Program Files/msXpertSuite")
    endif()
  endif()

  message(STATUS "${BoldYellow}Detected MINGW variant. Install prefix set to ${CMAKE_INSTALL_PREFIX}${ColourReset}")

  set(MSXPERTSUITE_BIN_DIR ${CMAKE_INSTALL_PREFIX})
  set(MSXPERTSUITE_DATA_DIR ${CMAKE_INSTALL_PREFIX}/data)
  set(MSXPERTSUITE_DOC_DIR ${CMAKE_INSTALL_PREFIX}/doc)
endif()

if(UNIX AND NOT APPLE)
  message(STATUS "${BoldYellow}Detected UNIX or APPLE$${ColourReset}")
  #set(CMAKE_INSTALL_PREFIX "/usr/local")
  set(MSXPERTSUITE_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin)
  set(MSXPERTSUITE_DATA_DIR ${CMAKE_INSTALL_PREFIX}/share/msxpertsuite)
  set(MSXPERTSUITE_DOC_DIR ${CMAKE_INSTALL_PREFIX}/share/doc/msxpertsuite)
endif(UNIX AND NOT APPLE)


message(STATUS "${BoldYellow}Main CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}${ColourReset}")
message(STATUS "${BoldYellow}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")

# For the config.h file.
set(CMAKE_INCLUDE_CURRENT_DIR ON)

add_subdirectory(globals)
add_subdirectory(libmass)
add_subdirectory(libmassgui)

# It is essential to run these config steps before delving into the massxpert
# and minexpert directories, because at that moment they will need what are
# going to prepare right now.
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/CMakeTemplates/splashscreen.svg.in
  ${CMAKE_SOURCE_DIR}/images/splashscreen.svg @ONLY)

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/CMakeTemplates/msxpertsuite-macros.tex.in
  ${CMAKE_SOURCE_DIR}/user-manual-common/texfiles/msxpertsuite-macros.tex @ONLY)

# Make the conversion of the svg file into a png, but only on GNU/Linux
if(UNIX AND NOT APPLE)
  execute_process(COMMAND convert ${CMAKE_SOURCE_DIR}/images/splashscreen.svg
    ${CMAKE_SOURCE_DIR}/images/splashscreen.png) 
endif(UNIX AND NOT APPLE)

add_subdirectory(massxpert)
get_directory_property(MASSXPERT_TARGET
  DIRECTORY massxpert
  MASSXPERT_TARGET)
get_directory_property(MASSXPERT_INCLUDE_DIRECTORIES
  DIRECTORY massxpert
  INCLUDE_DIRECTORIES)

add_subdirectory(minexpert)
get_directory_property(MINEXPERT_TARGET
  DIRECTORY minexpert
  MINEXPERT_TARGET)
get_directory_property(MINEXPERT_INCLUDE_DIRECTORIES
  DIRECTORY minexpert
  INCLUDE_DIRECTORIES)

add_subdirectory(doc)

add_subdirectory(devdoc)

if(TESTS)
  add_subdirectory(tests)
endif()

# Use all the configured paths to create the config.h file.
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/CMakeTemplates/config.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/CMakeTemplates/msxpertsuite-mingw64-win7+.iss.in
  ${CMAKE_SOURCE_DIR}/winInstaller/msxpertsuite-mingw64-win7+.iss @ONLY)

#############################################################
# summary
message("")
message(STATUS "${BoldRed}~~~~~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~~~~~${ColourReset}")
message(STATUS "${BoldRed}   General configuration of the msXpertSuite project ${ColourReset}")
message("")
message(STATUS "${BoldYellow}System is detected by CMake as ${SYSTEM_UNAME_S}${ColourReset}")
message(STATUS "${BoldYellow}CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}${ColourReset}")
message(STATUS "${BoldYellow}MSXPERTSUITE_BIN_DIR: ${MSXPERTSUITE_BIN_DIR}${ColourReset}")
message(STATUS "${BoldYellow}MSXPERTSUITE_DATA_DIR: ${MSXPERTSUITE_DATA_DIR}${ColourReset}")
message(STATUS "${BoldYellow}MSXPERTSUITE_DOC_DIR: ${MSXPERTSUITE_DOC_DIR}${ColourReset}")

if(PROFILE)
  message(STATUS "${BoldYellow}Profiling is requested (-DPROFILE=1)${ColourReset}")
else()
  message(STATUS "${BoldYellow}Profiling is NOT requested (-DPROFILE=0)${ColourReset}")
endif()

if(OPENMP)
  message(STATUS "${BoldYellow}OpenMP support is requested (-DOPENMP=1)${ColourReset}")
else()
  message(STATUS "${BoldYellow}OpenMP support is NOT requested (-DOPENMP=0)${ColourReset}")
endif()

if(WARNASERR)
  message(STATUS "${BoldYellow}Warnings treated as errors (-DWARNASERR=1)${ColourReset}")
else()
  message(STATUS "${BoldYellow}Warnings NOT treated as errors (-DWARNASERR=0)${ColourReset}")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "debug")
  add_definitions(-g -O0)
  message(STATUS "${BoldYellow}Debug support requested (-DDEBUG=1)${ColourReset}")
else()
  add_definitions(-O3)
  message(STATUS "${BoldYellow}Debug support NOT requested (-DDEBUG=0)${ColourReset}")
endif()

message(STATUS "${BoldYellow}CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}${ColourReset}")
message("")
message(STATUS "${BoldYellow}You can ask for clang++ by issuing the following command:${ColourReset}")
message(STATUS "${BoldYellow}$ CXX=clang++ cmake -DOPENMP=1 -DDEBUG=1 ../development/${ColourReset}")
message(STATUS "${BoldYellow}$ The typical cmake invocation on mingw64 would be: \
cmake -DOPENMP=1 -DDEBUG=0 ../../development \
-DCMAKE_CXX_COMPILER=/mingw64/bin/g++.exe ${ColourReset}")
message(STATUS "${BoldYellow}$ The typical cmake invocation on Debian GNU/Linux would be: \
cmake -DOPENMP=1 -DDEBUG=1 ../../development${ColourReset}")



message("")
message(STATUS "${BoldRed}~~~~~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~~~~~${ColourReset}")
message("")
