#############################################################
# Software configuration
PROJECT (massxpert)
SET (VERSION "2.3.6")

IF (APPLE)
  # If Mac OS X, then capitalize the program name.
  set (PROGNAME massXpert)
ELSE (NOT APPLE)
  set (PROGNAME massxpert)
ENDIF (APPLE)

# Required to install and reference the translation files and to give
# to the executable (in Mac OS X) the proper filename.
set (TARGET ${PROGNAME})

#############################################################
# CMake configuration
CMAKE_MINIMUM_REQUIRED (VERSION 2.6.0)

SET (CMAKE_COLOR_MAKEFILE ON)
SET (CMAKE_VERBOSE_MAKEFILE ON)

#############################################################
# Enable warnings and treat them as errors, on GNU/Linux only
IF (UNIX)
  SET (PEDANTIC TRUE CACHE BOOL "Should we compile with -Wall -Werror.")
  
  IF (PEDANTIC)
    ADD_DEFINITIONS (-Wall -Werror)
  ENDIF (PEDANTIC)
ENDIF (UNIX)

IF (WIN32)
  SET (PEDANTIC TRUE CACHE BOOL "Should we compile with -Wall.")
  
  IF (PEDANTIC)
    ADD_DEFINITIONS (-Wall)
  ENDIF (PEDANTIC)
ENDIF (WIN32)

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

IF (WIN32)
  # ${CMAKE_INSTALL_PREFIX} == "C:/Program Files/massxpert" The
  # CMAKE_INSTALL_PREFIX value can be changed by running
  #
  #'cmake -d CMAKE_INSTALL_PREFIX=C:/Program Files/massxpert'
  #
  #, for example.
  SET (MASSXPERT_BIN_DIR ${CMAKE_INSTALL_PREFIX})
  SET (MASSXPERT_DATA_DIR ${CMAKE_INSTALL_PREFIX}/data)
  SET (MASSXPERT_PLUGIN_DIR ${CMAKE_INSTALL_PREFIX}/plugins)
  SET (MASSXPERT_LOCALE_DIR ${CMAKE_INSTALL_PREFIX}/locales)
  SET (MASSXPERT_DOC_DIR ${CMAKE_INSTALL_PREFIX}/doc)
ENDIF (WIN32)

IF (UNIX AND NOT APPLE)
  SET (MASSXPERT_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin)
  SET (MASSXPERT_DATA_DIR ${CMAKE_INSTALL_PREFIX}/share/massxpert)

  # Plugin-specific stuff, some distros set CMAKE_INSTALL_LIBDIR to
  # /usr/lib64 (Fedora64, for example)
  IF (NOT CMAKE_INSTALL_LIBDIR)
    SET (CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
  ENDIF (NOT CMAKE_INSTALL_LIBDIR)
  SET (MASSXPERT_PLUGIN_DIR ${CMAKE_INSTALL_LIBDIR}/massxpert/plugins)

  SET (MASSXPERT_LOCALE_DIR ${CMAKE_INSTALL_PREFIX}/share/massxpert/locales)
  SET (MASSXPERT_DOC_DIR ${CMAKE_INSTALL_PREFIX}/share/doc/massxpert)
  SET (MASSXPERT_USERMAN_DIR ${MASSXPERT_DOC_DIR}/usermanual)
ENDIF (UNIX AND NOT APPLE)

IF (APPLE)
  SET (BUNDLE_DIR ${CMAKE_CURRENT_BINARY_DIR}/gui/massXpert.app)
  SET (MASSXPERT_BIN_DIR ${BUNDLE_DIR}/Contents/MacOS)
  SET (MASSXPERT_DATA_DIR ${BUNDLE_DIR}/Contents/Resources/data)
  SET (MASSXPERT_PLUGIN_DIR ${BUNDLE_DIR}/Contents/Plugins)
  SET (MASSXPERT_LOCALE_DIR ${BUNDLE_DIR}/Contents/Locales)
  SET (MASSXPERT_DOC_DIR ${BUNDLE_DIR}/Contents/Doc)
ENDIF (APPLE)



#############################################################
#############################################################
# There are two functional subtrees in the source:
#
# 1. The source code generating the massxpert binary, which is
# architecture-dependent;
#
# 2. The chemical data and the documentation (user manual) which are
# architecture independent.
#
# Note that if BUILD_ALL = 1 then all of the BUILD_PROGRAM, BUILD_DATA or
# BUILD_USERMANUAL are defined to 1 and everything is built.

IF (BUILD_ALL)
  SET (BUILD_PROGRAM 1)
  SET (BUILD_DATA 1)
  SET (BUILD_USERMANUAL 1)
ENDIF (BUILD_ALL)

IF (BUILD_RESET)
  SET (BUILD_PROGRAM 0)
  SET (BUILD_DATA 0)
  SET (BUILD_USERMANUAL 0)
ENDIF (BUILD_RESET)

IF (NOT BUILD_PROGRAM AND NOT BUILD_DATA AND NOT BUILD_USERMANUAL)
  SET (BUILD_PROGRAM 1)
  SET (BUILD_DATA 1)
  SET (BUILD_USERMANUAL 0)
ENDIF (NOT BUILD_PROGRAM AND NOT BUILD_DATA AND NOT BUILD_USERMANUAL)


#############################################################
######################
# BUILD OF THE PROGRAM

IF (${BUILD_PROGRAM})

  ################
  # Search for Qt4
  SET (QT_USE_QTXML TRUE)
  SET (QT_USE_QTSVG TRUE)

  SET (QT_MIN_VERSION "4.4.0")
  FIND_PACKAGE (Qt4 REQUIRED)
  MESSAGE (STATUS "QTVERSION:" ${QTVERSION})

  INCLUDE (${QT_USE_FILE})

  # 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)

  ###############
  # install stuff

  # The license file
  INSTALL (FILES COPYING
    DESTINATION ${MASSXPERT_DOC_DIR})

  # The desktop file
  IF (UNIX AND NOT APPLE)
    INSTALL (FILES massxpert.desktop
      DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications)
  ENDIF (UNIX AND NOT APPLE)

  # The manual page
  IF (UNIX AND NOT APPLE)
    INSTALL (FILES massxpert.1
      DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man1)
  ENDIF (UNIX AND NOT APPLE)

  ###############
  # Plugins should not have a soname, so override the CMake default, which gives them one
  # create a shared C++ library but with no soname in the lib file:
  SET(CMAKE_CXX_CREATE_SHARED_MODULE
    "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>  -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  # Compare the line above with
  # /usr/share/cmake-2.6/Modules/CMakeCXXInformation.cmake

  #################
  # process subdirs
  SUBDIRS (lib gui plugins-src i18n)


ENDIF (${BUILD_PROGRAM})


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

IF (${BUILD_DATA})
  
  ###############
  # install stuff
  
  # The license file
  INSTALL (FILES COPYING
    DESTINATION ${MASSXPERT_DOC_DIR})
  
  # The manual pages (data)
  IF (UNIX AND NOT APPLE)
    INSTALL (FILES massxpert-data.7
      DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man7)
  ENDIF (UNIX AND NOT APPLE)
  
  #################
  # process subdirs
  SUBDIRS (data)
  
ENDIF (${BUILD_DATA})


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

IF (${BUILD_USERMANUAL})
  
  # Below : no more in use since started using UseLATEX.cmake
  # Use the configured VERSION to create the 
  # usermanual/massxpert-version.tex file
  #
  #CONFIGURE_FILE (${CMAKE_SOURCE_DIR}/CMakeTemplates/massxpert-version.tex.in
  #  ${CMAKE_SOURCE_DIR}/usermanual/massxpert-version.tex @ONLY)
  
  
  ###############
  # install stuff
  
  # The license file
  INSTALL (FILES COPYING
    DESTINATION ${MASSXPERT_DOC_DIR})
  
  # The manual pages (user manual)
  IF (UNIX AND NOT APPLE)
    INSTALL (FILES massxpert-doc.7
      DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man7)
  ENDIF (UNIX AND NOT APPLE)
  
  
  #################
  # process subdirs
  SUBDIRS (usermanual)

ENDIF (${BUILD_USERMANUAL})


#############################################################
# summary
MESSAGE ("")
MESSAGE ("~~~~~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~~~~~")
MESSAGE (STATUS "BUILD_PROGRAM:" ${BUILD_PROGRAM})
MESSAGE (STATUS "BUILD_DATA:" ${BUILD_DATA})
MESSAGE (STATUS "BUILD_USERMANUAL:" ${BUILD_USERMANUAL})
MESSAGE ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
MESSAGE (STATUS "TARGET: " ${TARGET}  --  "VERSION: " ${VERSION})
MESSAGE (STATUS "CMAKE_INSTALL_PREFIX: " ${CMAKE_INSTALL_PREFIX})
MESSAGE (STATUS "MASSXPERT_BIN_DIR: " ${MASSXPERT_BIN_DIR})
MESSAGE (STATUS "MASSXPERT_DATA_DIR: " ${MASSXPERT_DATA_DIR})
MESSAGE (STATUS "MASSXPERT_PLUGIN_DIR: " ${MASSXPERT_PLUGIN_DIR})
MESSAGE (STATUS "MASSXPERT_LOCALE_DIR: " ${MASSXPERT_LOCALE_DIR})
MESSAGE (STATUS "MASSXPERT_DOC_DIR: " ${MASSXPERT_DOC_DIR})
MESSAGE (STATUS "MASSXPERT_USERMAN_DIR: " ${MASSXPERT_USERMAN_DIR})
MESSAGE ("~~~~~~~~~~~~~~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~~~~~~~~~~~~~~")
MESSAGE ("")
