PROJECT(cr3)
cmake_minimum_required(VERSION 2.6)

#INCLUDE(CPack)

if( ${CMAKE_SYSTEM} MATCHES "Darwin" )
  message("MAC OSX build")
  SET(MAC 1)
endif( ${CMAKE_SYSTEM} MATCHES "Darwin" )

IF(NOT CMAKE_BUILD_TYPE)
  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)

IF(${CMAKE_BUILD_TYPE} STREQUAL Debug)
  ADD_DEFINITIONS( -D_DEBUG=1 -DDEBUG=1 )
ELSE()
  ADD_DEFINITIONS( -DNDEBUG=1 )
ENDIF(${CMAKE_BUILD_TYPE} STREQUAL Debug)

if(MSVC)
  # Force to always compile with W4
  if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
    string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
  # Update if necessary
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic -std=c++11 -fPIC -Wno-misleading-indentation")
endif()

if (NOT DEFINED GUI)
  SET(GUI QT)
  message("GUI type is not specified!")
  message("Using ${GUI} as default")
  message("Add cmake parameter -D GUI={QT|WX|CRGUI_XCB|CRGUI_NANOX|CRGUI_PB|CRGUI_QT|CRGUI_JINKE_PLUGIN|CRGUI_WIN32|FB2PROPS} to use another GUI frontend")
else ()
  message("Using GUI frontend ${GUI}")
endif (NOT DEFINED GUI)



# Determines whether to use external app EDICT for dictionary
if (NOT DEFINED USE_EXTERNAL_EDICT_DICTIONARY)
  SET(USE_EXTERNAL_EDICT_DICTIONARY 0)
else()
  SET(USE_EXTERNAL_EDICT_DICTIONARY ${USE_EXTERNAL_EDICT_DICTIONARY})
endif (NOT DEFINED USE_EXTERNAL_EDICT_DICTIONARY)
message("using USE_EXTERNAL_EDICT_DICTIONARY=${USE_EXTERNAL_EDICT_DICTIONARY}")
ADD_DEFINITIONS( -DUSE_EXTERNAL_EDICT_DICTIONARY=${USE_EXTERNAL_EDICT_DICTIONARY} )


# Document buffer cache / compression settings

#whether to compress data
if (NOT DEFINED DOC_DATA_COMPRESSION_LEVEL)
  message("-D DOC_DATA_COMPRESSION_LEVEL=0|1|2|3|4|5 parameter is not defined: will use default value")
  SET(DOC_DATA_COMPRESSION_LEVEL 1)
else()
  SET(DOC_DATA_COMPRESSION_LEVEL ${DOC_DATA_COMPRESSION_LEVEL})
endif (NOT DEFINED DOC_DATA_COMPRESSION_LEVEL)
message("using TEXT_COMPRESSION_LEVEL=${DOC_DATA_COMPRESSION_LEVEL}")
ADD_DEFINITIONS( -DDOC_DATA_COMPRESSION_LEVEL=${DOC_DATA_COMPRESSION_LEVEL} )

# Total RAM buffers size for document
if (NOT DEFINED DOC_BUFFER_SIZE)
  message("-D DOC_BUFFER_SIZE=N parameter is not defined: will use default value")
  SET(DOC_BUFFER_SIZE 0x400000)
else()
  SET(DOC_BUFFER_SIZE ${DOC_BUFFER_SIZE})
endif (NOT DEFINED DOC_BUFFER_SIZE)
message("using DOC_BUFFER_SIZE=${DOC_BUFFER_SIZE}")
ADD_DEFINITIONS( -DDOC_BUFFER_SIZE=${DOC_BUFFER_SIZE} )

# big page margins support
if (NOT DEFINED BIG_PAGE_MARGINS)
  SET(BIG_PAGE_MARGINS 0)
else()
  SET(BIG_PAGE_MARGINS ${BIG_PAGE_MARGINS})
endif (NOT DEFINED BIG_PAGE_MARGINS)
ADD_DEFINITIONS( -DBIG_PAGE_MARGINS=${BIG_PAGE_MARGINS} )

# big page margins support
if (NOT DEFINED MAX_IMAGE_SCALE_MUL)
  SET(MAX_IMAGE_SCALE_MUL 0)
else()
  SET(MAX_IMAGE_SCALE_MUL ${MAX_IMAGE_SCALE_MUL})
endif (NOT DEFINED MAX_IMAGE_SCALE_MUL)
ADD_DEFINITIONS( -DMAX_IMAGE_SCALE_MUL=${MAX_IMAGE_SCALE_MUL} )
message("using MAX_IMAGE_SCALE_MUL=${MAX_IMAGE_SCALE_MUL}")


if(MAC)
  ADD_DEFINITIONS( -DMAC=1 -DLINUX=1 -D_LINUX=1 -DCR_EMULATE_GETTEXT=1 )
elseif ( WIN32 )
  ADD_DEFINITIONS( -DWIN32=1 -D_WIN32=1 -DCR_EMULATE_GETTEXT=1 )
else()
  ADD_DEFINITIONS( -DLINUX=1 -D_LINUX=1 )
endif(MAC)

# Large files support
ADD_DEFINITIONS( -D_LARGEFILE64_SOURCE=1 -D_FILE_OFFSET_BITS=64 -DLVLONG_FILE_SUPPORT=1 )

if (NOT DEFINED GRAY_BACKBUFFER_BITS)
  SET(GRAY_BACKBUFFER_BITS 2)
endif (NOT DEFINED GRAY_BACKBUFFER_BITS)
if (NOT DEFINED ENABLE_DBUS_VIEWER_EVENTS)
  SET(ENABLE_DBUS_VIEWER_EVENTS 0)
endif (NOT DEFINED ENABLE_DBUS_VIEWER_EVENTS)


SET (CRGUI_DEFS -DCOLOR_BACKBUFFER=0 -DLDOM_USE_OWN_MEM_MAN=0 -DGRAY_BACKBUFFER_BITS=${GRAY_BACKBUFFER_BITS} 
      -DENABLE_DBUS_VIEWER_EVENTS=${ENABLE_DBUS_VIEWER_EVENTS})
SET (DESKTOP_DEFS -DCOLOR_BACKBUFFER=1 -DLDOM_USE_OWN_MEM_MAN=1 -DUSE_FREETYPE=1 )

if( ${GUI} STREQUAL QT )
FIND_PACKAGE( Qt4 REQUIRED )
INCLUDE(${QT_USE_FILE})
SET(QT_3RDPARTY_DIR ${QT_INCLUDE_DIR}/../src/3rdparty)
endif( ${GUI} STREQUAL QT )

#UNRAR support
if(DEFINED USE_UNRAR)
  message("Will build static UNRAR library")
  SET(USE_UNRAR 1)
  SET(UNRAR_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/unrar)
  SET(UNRAR_LIBRARIES unrar)
  ADD_DEFINITIONS(-DUSE_UNRAR=1)
  INCLUDE_DIRECTORIES(${UNRAR_INCLUDE_DIR})
  ADD_SUBDIRECTORY(thirdparty/unrar)
else()
  message("NO_UNRAR parameter specified: Will not support UNRAR")
  SET(UNRAR_INCLUDE_DIR)
  SET(UNRAR_LIBRARIES)
  ADD_DEFINITIONS(-DUSE_UNRAR=0)
  SET(USE_UNRAR 0)
endif(DEFINED USE_UNRAR)

if (MAC)
  message("will use zlib, libpng, libjpeg, freetype from Qt static library")
  SET(ZLIB_INCLUDE_DIR ${QT_3RDPARTY_DIR}/zlib)
  SET(ZLIB_LIBRARIES )
  SET(PNG_INCLUDE_DIR ${QT_3RDPARTY_DIR}/libpng)
  SET(PNG_LIBRARIES )
  SET(FREETYPE_INCLUDE_DIRS ${QT_3RDPARTY_DIR}/freetype/include)
  SET(FREETYPE_LIBRARIES )
  SET(JPEG_INCLUDE_DIR ${QT_3RDPARTY_DIR}/libjpeg)
  SET(JPEG_LIBRARIES )
endif(MAC)

if (NOT MAC)
if (DEFINED USE_QT_ZLIB)
  SET(ZLIB_INCLUDE_DIR ${QT_INCLUDE_DIR}/../src/3rdparty/zlib)
  message("Using QT ZLIB ${USE_QT_ZLIB} include dir: ${ZLIB_INCLUDE_DIR}")
# SET(ZLIB_LIBRARIES "")
else()
if (NOT WIN32 AND NOT DEFINED USE_STATIC_ZLIB)
FIND_PACKAGE(ZLIB)
endif (NOT WIN32 AND NOT DEFINED USE_STATIC_ZLIB)
if (NOT ZLIB_FOUND)
  message("System ZLIB not found, will build local one")
  #to disable #include <qconfig.h>
  ADD_DEFINITIONS(-DCR3_PATCH=1)
  ADD_SUBDIRECTORY(thirdparty/zlib)
  SET(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/zlib)
  SET(ZLIB_LIBRARIES z)
endif (NOT ZLIB_FOUND)
endif(DEFINED USE_QT_ZLIB)
endif(NOT MAC)

if ( NOT ${GUI} STREQUAL FB2PROPS )
if (NOT MAC)
if (NOT WIN32 AND NOT CR3_PNG)
FIND_PACKAGE(PNG)
endif (NOT WIN32 AND NOT CR3_PNG)
if (NOT PNG_FOUND)
  message("System LIBPNG not found, will build local one")
  #to disable #include <qconfig.h>
  ADD_DEFINITIONS(-DCR3_PATCH=1)
  INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
  ADD_SUBDIRECTORY(thirdparty/libpng)
  SET(PNG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libpng)
  SET(PNG_LIBRARIES png)
endif (NOT PNG_FOUND)

if (NOT WIN32 AND NOT CR3_JPEG)
FIND_PACKAGE(JPEG)
endif (NOT WIN32 AND NOT CR3_JPEG)
if (NOT JPEG_FOUND)
  message("System LIBJPEG not found, will build local one")
  ADD_SUBDIRECTORY(thirdparty/libjpeg)
  SET(JPEG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libjpeg)
  SET(JPEG_LIBRARIES jpeg)
endif (NOT JPEG_FOUND)

if (NOT WIN32 AND NOT CR3_FREETYPE)
FIND_PACKAGE(Freetype)
endif (NOT WIN32 AND NOT CR3_FREETYPE)
if (NOT FREETYPE_FOUND)
  message("System FREETYPE not found, will build local one")
  ADD_SUBDIRECTORY(thirdparty/freetype)
  SET(FREETYPE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/freetype/include)
  SET(FREETYPE_LIBRARIES freetype)
endif (NOT FREETYPE_FOUND)
endif (NOT MAC)

#if (ENABLE_CHM)
message("Will build patched LIBCHM library")
ADD_SUBDIRECTORY(thirdparty/chmlib)
SET(CHM_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/chmlib/src)
SET(CHM_LIBRARIES chmlib)
ADD_DEFINITIONS(-DCHM_SUPPORT_ENABLED=1)
#else()
#ADD_DEFINITIONS(-DCHM_SUPPORT_ENABLED=0)
#endif(ENABLE_CHM)

message("Will build patched ANTIWORD library")
ADD_DEFINITIONS(-DCR3_ANTIWORD_PATCH=1)
ADD_SUBDIRECTORY(thirdparty/antiword)
SET(ANTIWORD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/antiword)
SET(ANTIWORD_LIBRARIES antiword)
ADD_DEFINITIONS(-DENABLE_ANTIWORD=1)
INCLUDE_DIRECTORIES( ${ANTIWORD_INCLUDE_DIR} )
endif ( NOT ${GUI} STREQUAL FB2PROPS )


INCLUDE_DIRECTORIES( 
  ${PNG_INCLUDE_DIR} 
  ${JPEG_INCLUDE_DIR} 
  ${ZLIB_INCLUDE_DIR} 
  ${FREETYPE_INCLUDE_DIRS}
  ${HARFBUZZ_INCLUDE_DIRS}
  ${ANTIWORD_INCLUDE_DIR}
  ${CHM_INCLUDE_DIR}
)

if ( ${GUI} STREQUAL FB2PROPS )
SET(STD_LIBS 
  ${ZLIB_LIBRARIES} 
)
else()
SET(STD_LIBS 
  ${JPEG_LIBRARIES} 
  ${FREETYPE_LIBRARIES} 
  ${HARFBUZZ_LIBRARIES}
  ${PNG_LIBRARIES} 
  ${ZLIB_LIBRARIES} 
  ${CHM_LIBRARIES}
  ${ANTIWORD_LIBRARIES}
)
include_directories(${FT_INCLUDE_PATH})
endif ( ${GUI} STREQUAL FB2PROPS )

include_directories(crengine/include)

if ( NOT ${GUI} STREQUAL FB2PROPS )
include_directories(tinydict)
message("Will make tinydict library - .dict format support")
ADD_SUBDIRECTORY(tinydict)
endif ( NOT ${GUI} STREQUAL FB2PROPS )


include(FindPkgConfig)

if ( ${GUI} STREQUAL CRGUI_XCB )
  message("Will make CR3GUI for XCB")
  ADD_DEFINITIONS( -DCR_USE_XCB=1 ${CRGUI_DEFS} )
  ADD_DEFINITIONS( -DCR_INTERNAL_PAGE_ORIENTATION=0 ${CRGUI_DEFS} )
  message("Freetype include dir: ${FREETYPE_INCLUDE_DIRS}")
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3gui)
elseif ( ${GUI} STREQUAL CRGUI_NANOX )
  message("Will make CR3GUI for NanoX")
  if (ENABLE_DBUS_VIEWER_EVENTS)
    message("Will handle DBUS messages")
    pkg_check_modules( DBUS REQUIRED dbus-1 )
    message("dbus include dir: ${DBUS_INCLUDE_DIRS} path ${DBUS_INCLUDE_PATH}")
    include_directories(${DBUS_INCLUDE_DIRS})
  endif (ENABLE_DBUS_VIEWER_EVENTS)
  ADD_DEFINITIONS( -DLBOOK=1 -DWITH_JINKE=1 -DCR_USE_JINKE=1
    -DJINKE_VIEWER ${CRGUI_DEFS} -DENABLE_UPDATE_MODE_SETTING=1 )
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3gui)
elseif ( ${GUI} STREQUAL CRGUI_JINKE_PLUGIN )
  message("Will make CR3GUI plugin for Jinke Viewer")
  ADD_DEFINITIONS( -DLBOOK=1 -DWITH_JINKE=1 -DWITH_JINKE_APP=1
    -DCR_USE_JINKE=1 ${CRGUI_DEFS}
    -DENABLE_UPDATE_MODE_SETTING=1 )
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3gui)
elseif ( ${GUI} STREQUAL FB2PROPS )
  message("Will make LIBFB2PROPS plugin for Jinke Bookshelf")
  ADD_DEFINITIONS( -DLBOOK=1 -DWITH_JINKE=1 -DCR_USE_JINKE=1 -DBUILD_LITE=1 ${CRGUI_DEFS} )
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(fb2props)
elseif ( ${GUI} STREQUAL CRGUI_WIN32 )
  message("Will make CR3GUI for Win32")
  ADD_DEFINITIONS( ${CRGUI_DEFS} )
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3gui)
elseif ( ${GUI} STREQUAL QT )
  message("Will make CR3/QT")
  ADD_DEFINITIONS( ${DESKTOP_DEFS} )
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3qt)
elseif ( ${GUI} STREQUAL WX )
  message("Will make CR3/WX")
  ADD_DEFINITIONS( ${DESKTOP_DEFS} -DCR_WX_SUPPORT=1 )
  SET(wxWidgets_USE_LIBS base core )
  FIND_PACKAGE(wxWidgets)
  IF (wxWidgets_FOUND)
    INCLUDE(${wxWidgets_USE_FILE})
    include_directories( ${wxWidgets_INCLUDE_DIRS} )
    message( "WX include: ${wxWidgets_INCLUDE_DIRS}" )
  ELSE (wxWidgets_FOUND)
    message("wxWidgets library not found!")
  ENDIF (wxWidgets_FOUND)
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3wx)
elseif ( ${GUI} STREQUAL CRGUI_PB )
  message("Will make CR3GUI for PocketBook")
  ADD_DEFINITIONS( -DLBOOK=1 -DCR_POCKETBOOK=1 
   ${CRGUI_DEFS} -DCR_USE_INVERT_FOR_SELECTION_MARKS=1 -DENABLE_UPDATE_MODE_SETTING=1)
  ADD_DEFINITIONS( -DCR_INTERNAL_PAGE_ORIENTATION=0 )
  ADD_SUBDIRECTORY(crengine)  
  ADD_SUBDIRECTORY(cr3gui)
  FIND_PATH (INKVIEW_INCLUDE_DIR inkview.h)
  IF (INKVIEW_INCLUDE_DIR)
    include_directories(${INKVIEW_INCLUDE_DIR})
  ELSE (INKVIEW_INCLUDE_DIR)
    message("inkview.h not found")
  ENDIF (INKVIEW_INCLUDE_DIR)
elseif ( ${GUI} STREQUAL CRGUI_QT )
  message("Will make CR3GUI with Qt backend")
  ADD_DEFINITIONS( -DCR_USE_QT=1 -DCRGUI_QT=1 -DCR_USE_INVERT_FOR_SELECTION_MARKS=1)
  ADD_DEFINITIONS( -DCR_INTERNAL_PAGE_ORIENTATION=0 ${CRGUI_DEFS} )
  ADD_SUBDIRECTORY(crengine)
  ADD_SUBDIRECTORY(cr3gui)
else ( ${GUI} STREQUAL CRGUI_XCB )
  message("Unknown GUI type ${GUI}")
endif ( ${GUI} STREQUAL CRGUI_XCB )
