#-------------------------------------------------------------------------------
#  cb2bib cmake build script
#-------------------------------------------------------------------------------
include(cmake/modules/c2bUtilities.cmake)

#-------------------------------------------------------------------------------
#  cb2bib cmake options and defaults
#-------------------------------------------------------------------------------
project(cb2bib)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  if(WIN32)
    set(CMAKE_INSTALL_PREFIX "C:\Program Files" CACHE PATH "Default install path prefix" FORCE)
  elseif(APPLE)
    set(CMAKE_INSTALL_PREFIX "/Applications" CACHE PATH "Default install path prefix" FORCE)
  else()
    set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "Default install path prefix" FORCE)
  endif()
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
if(CMAKE_BUILD_TYPE STREQUAL "")
  set(CMAKE_BUILD_TYPE Release)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  add_definitions(-DC2B_DEBUG)
endif()

find_package(Qt5Widgets)

option(C2B_USE_WEBKIT "Link cb2bib against QtWebKit library." ON)
option(C2B_USE_LZO "Link cb2bib against lzo2 library." ON)
if(UNIX AND NOT APPLE)
  option(C2B_USE_CBPOLL "Use clipboardPoll." ON)
  path_option(C2B_INSTALL_BIN_DIR "Executables installation directory [default: CMAKE_INSTALL_PREFIX/bin]." "${CMAKE_INSTALL_PREFIX}/bin")
  path_option(C2B_INSTALL_DATA_DIR "Data installation directory [default: CMAKE_INSTALL_PREFIX/share/cb2bib]." "${CMAKE_INSTALL_PREFIX}/share/cb2bib")
  path_option(C2B_INSTALL_DESKTOP_DIR "Desktop file installation directory [default: CMAKE_INSTALL_PREFIX/share/applications]." "${CMAKE_INSTALL_PREFIX}/share/applications")
  path_option(C2B_INSTALL_ICON_DIR "Icon file directory [default: CMAKE_INSTALL_PREFIX/share/pixmaps]." "${CMAKE_INSTALL_PREFIX}/share/pixmaps")
endif(UNIX AND NOT APPLE)
if(APPLE)
  set(C2B_USE_CBPOLL OFF)
  path_option(C2B_INSTALL_BIN_DIR "Executables installation directory [default: CMAKE_INSTALL_PREFIX/cb2bib.app/Contents/MacOS]." "${CMAKE_INSTALL_PREFIX}/cb2bib.app/Contents/MacOS")
  path_option(C2B_INSTALL_DATA_DIR "Data installation directory [default: CMAKE_INSTALL_PREFIX/cb2bib.app/Contents/Resources]." "${CMAKE_INSTALL_PREFIX}/cb2bib.app/Contents/Resources")
endif(APPLE)
if(WIN32)
  set(C2B_USE_CBPOLL OFF)
  path_option(C2B_INSTALL_BIN_DIR "Executables installation directory [default: CMAKE_INSTALL_PREFIX\cb2bib]." "${CMAKE_INSTALL_PREFIX}\cb2bib")
endif(WIN32)

if(C2B_USE_LZO)
  set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
  find_package(LZO REQUIRED)
  include_directories(${LZO_INCLUDE_DIR})
  add_definitions(-DC2B_USE_LZO)
endif(C2B_USE_LZO)
message("-- cb2bib uses QtWebKit library: ${C2B_USE_WEBKIT}")
message("-- cb2bib uses lzo2 library: ${C2B_USE_LZO}")
message("-- cb2bib executables installation directory: ${C2B_INSTALL_BIN_DIR}")
message("-- cb2bib data installation directory: ${C2B_INSTALL_DATA_DIR}")
if(UNIX AND NOT APPLE)
  message("-- cb2bib desktop file installation directory: ${C2B_INSTALL_DESKTOP_DIR}")
  message("-- cb2bib icon file installation directory: ${C2B_INSTALL_ICON_DIR}")
endif(UNIX AND NOT APPLE)
message("-- Build type: ${CMAKE_BUILD_TYPE}")

configure_file(${CMAKE_SOURCE_DIR}/src/c2b/cb2bib_conf_parameters.h.in ${CMAKE_CURRENT_BINARY_DIR}/cb2bib_conf_parameters.h @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

#-------------------------------------------------------------------------------
#  general settings
#-------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CB2BIB_MINIMUM_REQUIRED_QT "5.0.0")

#-------------------------------------------------------------------------------
#  build tree structure
#-------------------------------------------------------------------------------
if(WIN32)
  subdirs(src/c2b src src/win)
else()
  subdirs(src/c2b src)
endif()

#-------------------------------------------------------------------------------
#  installs
#-------------------------------------------------------------------------------
if(UNIX AND NOT APPLE)
  install(PROGRAMS c2bscripts/c2bciter DESTINATION ${C2B_INSTALL_BIN_DIR})
  install(PROGRAMS c2bscripts/c2bimport DESTINATION ${C2B_INSTALL_BIN_DIR})
  install(FILES AUTHORS COPYRIGHT LICENSE CHANGELOG DESTINATION ${C2B_INSTALL_DATA_DIR})
  install(DIRECTORY data c2btools testPDFImport DESTINATION ${C2B_INSTALL_DATA_DIR})
  install(FILES c2bscripts/cb2bib.desktop c2bscripts/c2bciter.desktop c2bscripts/c2bimport.desktop DESTINATION ${C2B_INSTALL_DESKTOP_DIR})
  install(FILES src/icons/cb2bib.png DESTINATION ${C2B_INSTALL_ICON_DIR})
endif(UNIX AND NOT APPLE)
if(APPLE)
  install(PROGRAMS ./c2bscripts/c2bciter DESTINATION ${C2B_INSTALL_BIN_DIR})
  install(PROGRAMS ./c2bscripts/c2bimport DESTINATION ${C2B_INSTALL_BIN_DIR})
  install(FILES AUTHORS COPYRIGHT LICENSE CHANGELOG DESTINATION ${C2B_INSTALL_DATA_DIR})
  install(DIRECTORY data c2btools testPDFImport DESTINATION ${C2B_INSTALL_DATA_DIR})

  set(MACOSX_BUNDLE_BUNDLE_NAME cb2bib)
  set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.MOLspaces.cb2Bib")
  set(MACOSX_BUNDLE_ICON_FILE src/icons/cb2bib.icns)
  set(MACOSX_BUNDLE_INFO_STRING "A tool for rapidly extracting unformatted bibliographic references")
  set(MACOSX_BUNDLE_SHORT_VERSION_STRING "1.9")
  set(MACOSX_BUNDLE_LONG_VERSION_STRING "1.9.0")
  set(MACOSX_BUNDLE_BUNDLE_VERSION "1.9.0")
  set(MACOSX_BUNDLE_COPYRIGHT "(C) 2004-2015 Pere Constans")

###  ??
###  set_source_files_properties(${ProjectName_RESOURCES} ${ProjectName_TRANSLATIONS} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
##
### According to http://cmake.org/Wiki/CMake:Bundles_And_Frameworks, appl1_aux1 and appl1_aux2 would be c2bciter and c2bimport
###/Applications/
###             appl1.app/
###                      Contents/
###                              Info.plist
###                              MacOS/
###                                   appl1 -> appl1-1
###                                   appl1-1
###                                   appl1_aux1
###                                   appl1_aux2
###                              Resources/
###                                   appl1_res1
###                                   appl1_res2
##
##  # are they set as executable ? how if orig files were executable already ? are they seen as execs from the gui ?
##  set_source_files_properties(c2bscripts/c2bciter c2bscripts/c2bimport PROPERTIES MACOSX_PACKAGE_LOCATION MacOS)
##  # is this correct ? how about data c2btools testPDFImport directories ?
##  set_source_files_properties(AUTHORS COPYRIGHT LICENSE CHANGELOG PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
##  # need this to copy icon ?
##  set_source_files_properties(MACOSX_BUNDLE_ICON_FILE PROPERTIES MACOSX_PACKAGE_LOCATION Resources)

endif(APPLE)
if(WIN32)
endif(WIN32)

#-------------------------------------------------------------------------------