# CMake script for PAPPSOms++ library
# Author: Olivier Langella
# Created: 03/03/2015

# Global parameters
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(pappsomspp CXX)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

IF(NOT USEQT4)
  IF(NOT USEQT5)
    SET (USEQT5 1)
  ENDIF(NOT USEQT5)
ENDIF(NOT USEQT4)

IF(USEQT5)
    IF(NOT PAPPSOMSPPWIDGET)
        SET (PAPPSOMSPPWIDGET 1)
    ENDIF(NOT PAPPSOMSPPWIDGET)
ENDIF(USEQT5)

IF(NOT CMAKE_BUILD_TYPE)
#  SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
#      "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
#      FORCE)
  SET(CMAKE_BUILD_TYPE Release CACHE STRING
      "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
      FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)

MESSAGE("CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")

IF(CMAKE_BUILD_TYPE MATCHES "Release")
  MESSAGE("compiling as release version")
  ADD_DEFINITIONS("-DQT_NO_DEBUG_OUTPUT")
ENDIF( CMAKE_BUILD_TYPE MATCHES "Release" )

IF(CMAKE_BUILD_TYPE MATCHES "Debug")
  MESSAGE("compiling as debug version")
  set (MAKE_TEST 1)
ENDIF( CMAKE_BUILD_TYPE MATCHES "Debug" )

IF(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
  MESSAGE("compiling as release with debug info version")
ENDIF( CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo" )

#SET(CMAKE_CXX_FLAGS "-Wall -Weffc++ -Wshadow -Wconversion")
if (CMAKE_VERSION VERSION_LESS "3.1")
    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
      set (CMAKE_CXX_FLAGS "-std=gnu++11 ${CMAKE_CXX_FLAGS}")
    endif ()
else ()
    set (CMAKE_CXX_STANDARD 11)
endif ()

SET(PAPPSOMSPP_VERSION_MAJOR "0")
SET(PAPPSOMSPP_VERSION_MINOR "4")
SET(PAPPSOMSPP_VERSION_PATCH "3")
SET(PAPPSOMSPP_VERSION "${PAPPSOMSPP_VERSION_MAJOR}.${PAPPSOMSPP_VERSION_MINOR}.${PAPPSOMSPP_VERSION_PATCH}")

# Set the CMAKE_PREFIX_PATH for the find_library fonction when using non
# standard install location
IF(CMAKE_INSTALL_PREFIX)
  SET(CMAKE_PREFIX_PATH "${CMAKE_INSTALL_PREFIX}" ${CMAKE_PREFIX_PATH})
ENDIF(CMAKE_INSTALL_PREFIX)

# Subdirectories
ADD_SUBDIRECTORY(doc)
ADD_SUBDIRECTORY(src)

# Doxygen
FIND_PACKAGE(Doxygen)
IF (DOXYGEN_FOUND)
  ADD_CUSTOM_TARGET (apidoc cp Doxyfile ${CMAKE_BINARY_DIR}/Doxyfile-build
    COMMAND echo "OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}" >> ${CMAKE_BINARY_DIR}/Doxyfile-build
    COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile-build
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  ADD_CUSTOM_TARGET (apidoc-stable cp Doxyfile ${CMAKE_BINARY_DIR}/Doxyfile-stable
    COMMAND echo "OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}" >> ${CMAKE_BINARY_DIR}/Doxyfile-stable
    COMMAND echo "HTML_HEADER=header.html" >> ${CMAKE_BINARY_DIR}/Doxyfile-stable
    COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile-stable
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
ENDIF (DOXYGEN_FOUND)

# Packager
SET(CPACK_PACKAGE_NAME "libpappsomspp")
SET(CPACK_PACKAGE_VENDOR "PAPPSO Development Team")
SET(CPACK_PACKAGE_VERSION "${PAPPSOMSPP_VERSION}")
SET(CPACK_PACKAGE_VERSION_MAJOR "${PAPPSOMSPP_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${PAPPSOMSPP_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${PAPPSOMSPP_VERSION_PATCH}")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The PAPPSO C++ library for mass spectrometry")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
SET(CPACK_RESOURCE_FILE_AUTHORS "${CMAKE_SOURCE_DIR}/AUTHORS")
SET(CPACK_RESOURCE_FILE_INSTALL "${CMAKE_SOURCE_DIR}/INSTALL")
SET(CPACK_SOURCE_GENERATOR "TGZ")
SET(CPACK_SOURCE_IGNORE_FILES
 "CMakeFiles"
 "Makefile"
 "_CPack_Packages"
 "CMakeCache.txt"
 ".*\\\\.git"
 ".*\\\\.gz"
 ".*\\\\.deb"
 ".*\\\\.rpm"
 ".*\\\\.dmg"
 ".*\\\\..*\\\\.swp"
 "src/\\\\..*"
 "src/libbpp*"
 "debian/tmp"
 "debian/libbpp.*/"
 "debian/libbpp.*\\\\.so.*"
 "debian/libbpp.*\\\\.a"
 "debian/libbpp.*\\\\.substvars"
 "debian/libbpp.*\\\\.debhelper"
 "debian/debhelper\\\\.log"
 "html"
 "Core.tag"
 "Testing"
 "build/"
 "buildwin64/"
 "build-stamp"
 "install_manifest.txt"
 "DartConfiguration.tcl"
 ${CPACK_SOURCE_IGNORE_FILES}
)



#dch -Dstretch "message"
#cmake ..
#make deb
# scp libpappsomspp* proteus.moulon.inra.fr:/var/www/apt/incoming
# reprepro -Vb /var/www/apt processincoming default
# apt-get remove libpappsomspp*
#
#  debuild -S -sa
# dput -f olivier-langella *changes


#dpkg-deb -x libpappsomspp-qt4_0.0.4-1_amd64.deb libpappsomspp-qt4_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt4 -Plibpappsomspp-qt4_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt4.symbols


#dpkg-deb -x libpappsomspp-qt5_0.0.4-1_amd64.deb libpappsomspp-qt5_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt5 -Plibpappsomspp-qt5_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt5.symbols


IF (MACOS)
  SET(CPACK_GENERATOR "Bundle")
ENDIF()

SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET(CPACK_DEBSOURCE_PACKAGE_FILE_NAME "lib${CMAKE_PROJECT_NAME}_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}.orig")
INCLUDE(CPack)

IF (UNIX)
#This creates deb packages:



	add_custom_target(targz
		cpack -G TGZ --config CPackSourceConfig.cmake && tar xvfz pappsomspp-${PAPPSOMSPP_VERSION}.tar.gz
		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
		COMMENT "Creating .tar.gz" VERBATIM
	)


	add_custom_target(deb
		cd pappsomspp-${PAPPSOMSPP_VERSION} && dpkg-buildpackage
		DEPENDS targz
		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
		COMMENT "Creating Debian package" VERBATIM
	)

	file(GLOB changesdebs "${CMAKE_BINARY_DIR}/pappsomspp_*.dsc")

	foreach(libfile ${changesdebs})
	    SET(changesdeb "${libfile}")
	endforeach(libfile)

	message ("changes debian file : ${changesdeb}")
	#lintian -IEi --pedantic tandem-mass_2013.06.15-1_amd64.changes
	add_custom_target(lintian
		lintian -IEi --pedantic ${changesdeb}
		DEPENDS deb
		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
		COMMENT "lintian check" VERBATIM
	)
	
	
	
#dpkg-deb -x libpappsomspp-qt4_0.0.4-1_amd64.deb libpappsomspp-qt4_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt4 -Plibpappsomspp-qt4_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt4.symbols


#dpkg-deb -x libpappsomspp-qt5_0.0.4-1_amd64.deb libpappsomspp-qt5_0.0.4-1
#dpkg-gensymbols -v0 -plibpappsomspp-qt5 -Plibpappsomspp-qt5_0.0.4-1 -Osymbols
#cp symbols ../debian/libpappsomspp-qt5.symbols

IF(MAKE_TEST)
  ENABLE_TESTING()
  INCLUDE(CTest)
  IF (BUILD_TESTING)
    ADD_SUBDIRECTORY(test)
  ENDIF(BUILD_TESTING)
ENDIF(MAKE_TEST)

# Install cmake module
INSTALL(FILES cmake_modules/FindPappsomspp.cmake DESTINATION share/cmake/Modules)

# Install cmake config
configure_file (${CMAKE_SOURCE_DIR}/cmake_modules/PappsomsppConfig.cmake ${CMAKE_SOURCE_DIR}/PappsomsppConfig.cmake)
INSTALL(FILES ${CMAKE_SOURCE_DIR}/PappsomsppConfig.cmake DESTINATION lib${LIB_SUFFIX}/cmake/pappsomspp)



ENDIF(UNIX)

