if (DEFINED CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build:
    None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
    Debug
    Release
    RelWithDebInfo
    MinSizeRel")
else ()
    SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build:
    None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
    Debug
    Release
    RelWithDebInfo
    MinSizeRel")
endif ()

set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -pipe)

if (APPLE)
    set(CMAKE_OSX_ARCHITECTURES i386;x86_64 CACHE STRING "Architecture")
endif ()

if (NOT APPLE)
    option(USE_AVAHI "Use Avahi for multicast DNS service publishing" ON)
else ()
    set(USE_AVAHI OFF)
endif ()

project(rinput)

cmake_minimum_required(VERSION 2.6)

include(CheckIncludeFiles)
include(CheckLibraryExists)

if (USE_AVAHI)
    include(FindPkgConfig)
endif ()

configure_file(config.h.in config.h)
include_directories(${CMAKE_BINARY_DIR})

find_package(Qt4 REQUIRED)

if (NOT APPLE)
    check_include_files(sasl/sasl.h HAVE_SASL_H)
    check_library_exists(sasl2 sasl_checkpass "" HAVE_LIBSASL2)

    if (NOT HAVE_SASL_H)
        message(FATAL_ERROR "Could not find cyrus-sasl headers")
    endif ()
    if (NOT HAVE_LIBSASL2)
        message(FATAL_ERROR "Could not find libsasl2 library")
    endif ()
endif ()

if (USE_AVAHI)
    pkg_check_modules(AVAHI_CLIENT avahi-client)
    pkg_check_modules(AVAHI_QT4 avahi-qt4)
    
    if (NOT ${AVAHI_CLIENT_FOUND} OR NOT ${AVAHI_QT4_FOUND})
        message(FATAL_ERROR "Could not find Avahi client and/or Avahi Qt4 development packages. Install them or turn off USE_AVAHI in cmake")
    endif ()
endif ()

option(BUILD_TEST "Build rinput-test test client" NO)

add_subdirectory(common)
add_subdirectory(rinputd)

if (BUILD_TEST)
    add_subdirectory(rinput-test)
endif ()
