project(QtCurve)

## Temporary
function(translate_add_sources)
endfunction()

## Setup
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}"
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

## Version
set(QTCURVE_VERSION_MAJOR "1")
set(QTCURVE_VERSION_MINOR "8")
set(QTCURVE_VERSION_PATCH "17")
set(QTCURVE_VERSION "${QTCURVE_VERSION_MAJOR}.${QTCURVE_VERSION_MINOR}")
set(QTCURVE_VERSION_FULL "${QTCURVE_VERSION}.${QTCURVE_VERSION_PATCH}")

## Options
function(def_compat_option new_opt old_opt)
  if("${ARGC}" GREATER 2 AND "${ARGV2}")
    set(reverse True)
  else()
    set(reverse False)
  endif()
  if(DEFINED "${old_opt}" AND NOT DEFINED "${new_opt}")
    if(reverse)
      if("${${old_opt}}")
        set("${new_opt}" Off PARENT_SCOPE)
      else()
        set("${new_opt}" On PARENT_SCOPE)
      endif()
    else()
      set("${new_opt}" "${${old_opt}}" PARENT_SCOPE)
    endif()
  endif()
endfunction()

option(ENABLE_QT4 "Building Qt4 theme." On)
option(ENABLE_QT5 "Building Qt5 theme." On)
option(ENABLE_GTK2 "Building Gtk2 theme." On)
option(QTC_ENABLE_X11 "Enable X11" On)
option(QTC_INSTALL_PO "Install translation files." On)

def_compat_option(QTC_QT4_ENABLE_KDE QTC_QT_ONLY True)
option(QTC_QT4_ENABLE_KDE "Building Qt4 style with KDE4 support." On)
# option(QTC_QT5_ENABLE_KDE "Building Qt5 style with KDE5 support." Off)

def_compat_option(QTC_QT4_OLD_NVIDIA_ARROW_FIX QTC_OLD_NVIDIA_ARROW_FIX)
option(QTC_QT4_OLD_NVIDIA_ARROW_FIX "Qt4 old nvidia arrow fix." Off)
option(QTC_QT5_OLD_NVIDIA_ARROW_FIX "Qt5 old nvidia arrow fix." Off)

def_compat_option(QTC_QT4_STYLE_SUPPORT QTC_STYLE_SUPPORT)
option(QTC_QT4_STYLE_SUPPORT
  "Qt4 support for creating a style for each QtCurve theme." Off)
def_compat_option(QTC_QT4_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT
  QTC_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT)
option(QTC_QT4_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT
  "Qt4 support for the 'fix parentless dialogs'." Off)
option(QTC_QT5_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT
  "Qt5 support for the 'fix parentless dialogs'." Off)

def_compat_option(QTC_GTK2_MODIFY_MOZILLA QTC_MODIFY_MOZILLA)
option(QTC_GTK2_MODIFY_MOZILLA "Modify Mozilla Gtk2 with js and css." Off)
def_compat_option(QTC_GTK2_OLD_MOZILLA QTC_OLD_MOZILLA)
option(QTC_GTK2_OLD_MOZILLA
  "Defaulting to treating Firefox(Gtk2) as Firefox < 3.0." Off)
def_compat_option(QTC_GTK2_USE_CAIRO_FOR_ARROWS QTC_USE_CAIRO_FOR_ARROWS)
option(QTC_GTK2_USE_CAIRO_FOR_ARROWS "Using ciaro to draw arrows." Off)
option(QTC_GTK2_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT
  "Gtk2 support for the 'fix parentless dialogs'." Off)

## Common settings.
# Only try to figure out what kde prefix is if not specified at command line.
if(NOT DEFINED QTC_KDE4_PREFIX)
  find_package(KDE4)
  find_program(KDE4_KDECONFIG_EXECUTABLE NAMES kde4-config
    PATHS "${CMAKE_INSTALL_PREFIX}/bin" ${_KDEDIRS} /opt/kde4/bin
    NO_DEFAULT_PATH)
  if(NOT KDE4_KDECONFIG_EXECUTABLE)
    find_program(KDE4_KDECONFIG_EXECUTABLE NAMES kde4-config)
  endif()
  if(KDE4_KDECONFIG_EXECUTABLE)
    execute_process(COMMAND "${KDE4_KDECONFIG_EXECUTABLE}" --prefix
      OUTPUT_VARIABLE QTC_KDE4_PREFIX)
    string(STRIP "${QTC_KDE4_PREFIX}" QTC_KDE4_PREFIX)
    execute_process(COMMAND "${KDE4_KDECONFIG_EXECUTABLE}" --expandvars
      --install icon
      OUTPUT_VARIABLE QTC_KDE4_ICONS_PREFIX)
    string(STRIP "${QTC_KDE4_ICONS_PREFIX}" QTC_KDE4_ICONS_PREFIX)
  else()
    set(QTC_KDE4_PREFIX "${CMAKE_INSTALL_PREFIX}")
  endif()
endif()

if(NOT DEFINED QTC_KDE4_ICONS_PREFIX)
  set(QTC_KDE4_ICONS_PREFIX "${QTC_KDE4_PREFIX}/share/icons")
endif()

## Handle options
macro(qtc_check_x11)
  find_package(PkgConfig)
  if(NOT PKG_CONFIG_FOUND)
    message(WARNING "Cannot find pkg-conig, disable x11 support.")
    set(QTC_ENABLE_X11 Off)
    return()
  endif()
  pkg_check_modules(QTC_X11 xcb xcb-image x11-xcb)
  if(NOT QTC_X11_FOUND)
    message(WARNING
      "Cannot find required modules for x11, disable x11 support.")
    set(QTC_ENABLE_X11 Off)
    return()
  endif()
endmacro()
if(QTC_ENABLE_X11)
  qtc_check_x11()
endif()

if(NOT ENABLE_QT4)
  set(QTC_QT4_ENABLE_KDE Off)
  set(QTC_QT4_ENABLE_KWIN Off)
endif()

if(NOT ENABLE_QT5)
  set(QTC_QT5_ENABLE_KDE Off)
  set(QTC_QT5_ENABLE_KWIN Off)
endif()

if(NOT QTC_ENABLE_X11 AND ENABLE_GTK2)
  message(WARNING "Gtk2 style require QTC_ENABLE_X11 to be enabled.")
  set(ENABLE_GTK2 Off)
endif()

if(ENABLE_GTK2)
  find_package(PkgConfig REQUIRED)
  pkg_check_modules(GTK2 REQUIRED gtk+-2.0)
  if(GTK2_THEME_PREFIX)
    set(GTK2_PREIX "${GTK2_THEME_PREFIX}")
  elseif(NOT GTK2_PREFIX)
    set(GTK2_PREFIX "${CMAKE_INSTALL_PREFIX}")
  endif()
  if(NOT GTK2_LIBDIR)
    set(GTK2_LIBDIR "${GTK2_PREFIX}/lib")
  endif()
  _pkgconfig_invoke(gtk+-2.0 GTK2 BIN_VERSION "" --variable=gtk_binary_version)
  set(GTK2_THEME_DIR ${GTK2_PREFIX}/share/themes/QtCurve)
endif()

configure_file(config.h.in config.h @ONLY)

include_directories(
  "${CMAKE_CURRENT_SOURCE_DIR}"
  "${CMAKE_CURRENT_BINARY_DIR}")
set(CMAKE_C_FLAGS "-Wall -Wextra ${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "-Wall -Wextra ${CMAKE_CXX_FLAGS} -std=c++0x")
set(CMAKE_SHARED_LINKER_FLAGS
  "-Wl,--as-needed -Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS
  "-Wl,--as-needed -Wl,--no-undefined ${CMAKE_MODULE_LINKER_FLAGS}")
add_definitions("-D_GNU_SOURCE -pthread")

if(NOT DEFINED LIB_INSTALL_DIR)
  set(LIB_INSTALL_DIR lib)
endif()

add_subdirectory(qtcurve-utils)
add_subdirectory(tools)
add_subdirectory(po)
add_subdirectory(qt4)
add_subdirectory(qt5)
add_subdirectory(gtk2)

if((ENABLE_QT4 AND QTC_QT4_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT) OR
    (ENABLE_QT5 AND QTC_QT5_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT) OR
    (ENABLE_GTK2 AND QTC_GTK2_ENABLE_PARENTLESS_DIALOG_FIX_SUPPORT))
  message(WARNING
    "You have enabled support for the 'fix parentless dialogs' option. "
    "This is known to BREAK some applications. Please "
    "DO NOT report errors to application authors when you have "
    "this enabled. Please reconsider DISABLING this option.")
endif()

if(ENABLE_GTK2 AND QTC_GTK2_OLD_MOZILLA)
  message(WARNING
    "Defaulting to treating Firefox as Firefox < 3.0. "
    "This option is deprecated and will be removed soon.")
endif()

if(ENABLE_GTK2 AND QTC_GTK2_MODIFY_MOZILLA)
  message(WARNING
    "You have enabled the option for QtCurve to modify the configuration "
    "files of Mozilla applications (Firefox, Thunderbird, Seamonkey). "
    "Note: This sometimes causes issues with newer versions of these "
    "applications. Should such issues occur, you should recompile QtCurve "
    "without this option - and you will have to remove BY HAND the changes "
    "that QtCurve has made. This will entail removing lines from "
    "userChrom.css that indicate 'Added by QtCurve' userChrome.css is usually "
    "in a folder such as: \n"
    "`~/.mozilla/firefox/xxxxxxx.default/chrome/userChrome.css` or \n"
    "`~/.thunderbird/xxxxxxx.default/chrome/userChrome.css`. \n"
    "where xxxxxxx is a series of random characters.")
endif()

if(ENABLE_GTK2 AND QTC_GTK2_USE_CAIRO_FOR_ARROWS)
  message(WARNING
    "Using cairo to draw arrows - this has been reported to fail with intel "
    "Xorg driver > 2.8")
endif()
