#-------------------------------------------------------------------------------
#  cb2bib cmake build script
#  - build cb2bib
#-------------------------------------------------------------------------------

list_source_files(CB2BIB_CPP CB2BIB_H CB2BIB_MOCH)

if(C2B_USE_WEBKIT)
  find_package(Qt4 ${CB2BIB_MINIMUM_REQUIRED_QT} COMPONENTS QtCore QtNetwork QtGui QtWebKit REQUIRED)
  add_definitions(-DC2B_USE_WEBKIT)
else()
  find_package(Qt4 ${CB2BIB_MINIMUM_REQUIRED_QT} COMPONENTS QtCore QtNetwork QtGui REQUIRED)
  list(REMOVE_ITEM CB2BIB_CPP c2bAnnote.cpp)
  list(REMOVE_ITEM CB2BIB_CPP c2bWebBrowser.cpp)
  list(REMOVE_ITEM CB2BIB_H c2bAnnote.h)
  list(REMOVE_ITEM CB2BIB_H c2bWebBrowser.h)
  list(REMOVE_ITEM CB2BIB_MOCH c2bAnnote.h)
  list(REMOVE_ITEM CB2BIB_MOCH c2bWebBrowser.h)
endif()

if(C2B_USE_CBPOLL)
  add_definitions(-DC2B_USE_CBPOLL)
else()
  list(REMOVE_ITEM CB2BIB_CPP clipboardPoll.cpp)
  list(REMOVE_ITEM CB2BIB_H clipboardPoll.h)
  list(REMOVE_ITEM CB2BIB_MOCH clipboardPoll.h)
endif()

qt4_wrap_cpp(CB2BIB_MOC ${CB2BIB_MOCH})

file(GLOB CB2BIB_UI RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.ui")
qt4_wrap_ui(CB2BIB_UIH ${CB2BIB_UI})
qt4_add_resources(CB2BIB_RCC cb2bib.qrc)

list_source_files_at(MODSINGAPP_CPP MODSINGAPP_H MODSINGAPP_MOCH qtsingleapplication/src/)
if(WIN32)
  list(REMOVE_ITEM MODSINGAPP_CPP qtsingleapplication/src/qtlockedfile_unix.cpp)
else()
  list(REMOVE_ITEM MODSINGAPP_CPP qtsingleapplication/src/qtlockedfile_win.cpp)
endif()
qt4_wrap_cpp(MODSINGAPP_MOC ${MODSINGAPP_MOCH})

include(${QT_USE_FILE})
include_directories(.)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(c2b)

if(WIN32)
#  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/c2bicon.o COMMAND windres.exe -I${CMAKE_CURRENT_SOURCE_DIR} 
  add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/c2bicon.o COMMAND windres.exe
                     -i${CMAKE_CURRENT_SOURCE_DIR}/cb2bib.rc -o ${CMAKE_CURRENT_BINARY_DIR}/c2bicon.o )
  list(APPEND CB2BIB_CPP ${CMAKE_CURRENT_BINARY_DIR}/c2bicon.o)
endif(WIN32)

if(APPLE)
  add_executable(cb2bib MACOSX_BUNDLE ${CB2BIB_CPP} ${CB2BIB_MOC} ${CB2BIB_UIH} ${CB2BIB_RCC} ${MODSINGAPP_CPP} ${MODSINGAPP_MOC})
else()
  add_executable(cb2bib WIN32 ${CB2BIB_CPP} ${CB2BIB_MOC} ${CB2BIB_UIH} ${CB2BIB_RCC} ${MODSINGAPP_CPP} ${MODSINGAPP_MOC})
  set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
endif()
if(UNIX AND NOT APPLE)
  if(C2B_USE_LZO)
    target_link_libraries(cb2bib c2b ${LZO_LIBRARIES} ${QT_LIBRARIES} -lX11)
  else()
    target_link_libraries(cb2bib c2b ${QT_LIBRARIES} -lX11)
  endif()
else()
  if(C2B_USE_LZO)
    target_link_libraries(cb2bib c2b ${LZO_LIBRARIES} ${QT_LIBRARIES})
  else()
    target_link_libraries(cb2bib c2b ${QT_LIBRARIES})
  endif()
endif()
if(APPLE)
   install(TARGETS cb2bib BUNDLE DESTINATION ${C2B_INSTALL_BIN_DIR})
else()
   install(TARGETS cb2bib RUNTIME DESTINATION ${C2B_INSTALL_BIN_DIR})
endif()

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