###| CMAKE qtermwidget/lib |###

#| 2.6 is out, but most distros only have 2.4
cmake_minimum_required ( VERSION 2.4 )
include(CheckFunctionExists)

#| Project
project ( qtermwidget )


IF (NOT LIB_SUFFIX)
    MESSAGE(STATUS "")
    MESSAGE(STATUS "LIB_SUFFIX variable is not defined. It will be autodetected now.")
    MESSAGE(STATUS "You can set it manually with -DLIB_SUFFIX=<value> (64 for example)")
    IF (NOT APPLE)
        # check 64 bit
        if (CMAKE_SIZEOF_VOID_P EQUAL 4)
            SET (LIB_SUFFIX "")
        else (CMAKE_SIZEOF_VOID_P EQUAL 4)
            set (LIB_SUFFIX "64")
            MESSAGE(STATUS "   LIB_SUFFIX is set to '${LIB_SUFFIX}'")
        endif (CMAKE_SIZEOF_VOID_P EQUAL 4)
    ELSE (NOT APPLE)
        message(STATUS "LIB_SUFFIX is disabled for APPLE: ${CMAKE_SYSTEM_NAME}")
        SET (LIB_SUFFIX "")
    ENDIF (NOT APPLE)
    # BSD does not use lib64 for 64bit libs
    IF (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
        message(STATUS "LIB_SUFFIX is disabled for *BSD: ${CMAKE_SYSTEM_NAME}")
        SET (LIB_SUFFIX "")
    ENDIF()
    #
    MESSAGE(STATUS "")
ENDIF (NOT LIB_SUFFIX)




#| Sources
set ( SRCS
    BlockArray.cpp
    ColorScheme.cpp
    Emulation.cpp
    Filter.cpp
    History.cpp
    KeyboardTranslator.cpp
    konsole_wcwidth.cpp
    kprocess.cpp
    kpty.cpp
    kptydevice.cpp
    kptyprocess.cpp
    Pty.cpp
    qtermwidget.cpp
    Screen.cpp
    ScreenWindow.cpp
    Session.cpp
    ShellCommand.cpp
    TerminalCharacterDecoder.cpp
    TerminalDisplay.cpp
    tools.cpp
    Vt102Emulation.cpp
)

#| Headers
#| 	Only the Headers that need to be moc'd go here
set ( HDRS
    Emulation.h
    Filter.h
    kprocess.h
    kptydevice.h
    kptyprocess.h
    Pty.h
    qtermwidget.h
    ScreenWindow.h
    Session.h
    TerminalDisplay.h
    Vt102Emulation.h
)

set ( HDRS_DISTRIB
	${CMAKE_SOURCE_DIR}/lib/qtermwidget.h )


#| Qt4 Required Options
add_definitions ( -Wall )
find_package ( Qt4 REQUIRED ) # Finds Qt4 on the system
include ( ${QT_USE_FILE} ) # Includes Qt4 headers and libraries (the above command is needed first)
QT4_WRAP_CPP ( MOC_SRCS ${HDRS} ) # Moc's the headers
#include ( ${CMAKE_BINARY_DIR} ) # For including the heades generated by ui files

#| qtermwidget specific
include_directories ( ${PROJECT_SOURCE_DIR} ) # You mark some of the headers as global, so I just add the source directory to the includes


# dirs
set ( SHARE_DIR ${CMAKE_INSTALL_PREFIX}/share )
message ( STATUS "Share dire: ${SHARE_DIR}" )

set ( KB_LAYOUT_DIR ${CMAKE_INSTALL_PREFIX}/share/qtermwidget/kb-layouts/ )
message ( STATUS "Keyboard layouts will be installed in: ${KB_LAYOUT_DIR}" )
add_definitions( -DKB_LAYOUT_DIR="\\"${KB_LAYOUT_DIR}\\"" )

set ( COLORSCHEMES_DIR ${CMAKE_INSTALL_PREFIX}/share/qtermwidget/color-schemes/ )
message ( STATUS "Color schemes will be insatlled in: ${COLORSCHEMES_DIR}" )
add_definitions( -DCOLORSCHEMES_DIR="\\"${COLORSCHEMES_DIR}\\"" )

#| Defines
add_definitions ( -DHAVE_POSIX_OPENPT -DHAVE_SYS_TIME_H )
#add_definitions( -DHAVE_GETPT )
if (APPLE)
    add_definitions( -DHAVE_UTMPX -D_UTMPX_COMPAT )
endif (APPLE)

CHECK_FUNCTION_EXISTS(updwtmpx HAVE_UPDWTMPX)
if (HAVE_UPDWTMPX)
    add_definitions( -DHAVE_UPDWTMPX )
endif (HAVE_UPDWTMPX)


#| Create the Library
#add_library( qtermwidget STATIC ${SRCS} ${MOC_SRCS} )
add_library ( qtermwidget SHARED ${SRCS} ${MOC_SRCS} )

if (APPLE)
    # this is a must to load the lib correctly
    set_target_properties( qtermwidget PROPERTIES
                            INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX} )
endif (APPLE)


#| Set Build Type
if (NOT CMAKE_BUILD_TYPE)
    set ( CMAKE_BUILD_TYPE
	#"release"
	"relwithdebinfo" # Default
	#"debug"
	#"debugfull"
    )
endif (NOT CMAKE_BUILD_TYPE)

#| Library Properties
set_target_properties ( qtermwidget PROPERTIES
	#OUTPUT_NAME "alternateName"
	#PREFIX "lib"
	SOVERSION ${qtermwidget_VERSION_MAJOR}
	#SUFFIX "so"
	VERSION ${qtermwidget_VERSION}
)

#| Link Qt4 Libraries
target_link_libraries ( qtermwidget ${QT_LIBRARIES} )

install(TARGETS qtermwidget DESTINATION lib${LIB_SUFFIX})
install(FILES ${HDRS_DISTRIB} DESTINATION include)

# keyboard layouts
install(DIRECTORY kb-layouts/ DESTINATION ${KB_LAYOUT_DIR} FILES_MATCHING PATTERN "*.keytab" )
# color schemes
install(DIRECTORY color-schemes/ DESTINATION ${COLORSCHEMES_DIR} FILES_MATCHING PATTERN "*.*schem*")


#
# Qt4 designer plugin
#
if (BUILD_DESIGNER_PLUGIN)
    message(STATUS "Building Qt designer plugin")

    include_directories(designer
                        ${QT_QTDESIGNER_INCLUDE_DIR})

    set(DESIGNER_SRC designer/qtermwidgetplugin.cpp)
    QT4_WRAP_CPP( DESIGNER_MOC	designer/qtermwidgetplugin.h)
    QT4_ADD_RESOURCES( DESIGNER_QRC designer/qtermwidgetplugin.qrc)

    link_directories(${CMAKE_BINARY_DIR})
    add_library( qtermwidgetplugin SHARED
        ${DESIGNER_MOC}
        ${DESIGNER_QRC}
        ${DESIGNER_SRC}
    )
    add_dependencies(qtermwidgetplugin qtermwidget)

    target_link_libraries( qtermwidgetplugin
        ${QT_QTCORE_LIBRARY}
        ${QT_QTDESIGNER_LIBRARY}
        ${QT_QTDESIGNERCOMPONENTS_LIBRARY}
        qtermwidget
    )

    if (APPLE)
        # this is a must to load the lib correctly
        set_target_properties( qtermwidgetplugin PROPERTIES
                                INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/qt4/plugins/designer )
    endif (APPLE)

    #install( TARGETS qtermwidgetplugin DESTINATION ${QT_PLUGINS_DIR}/designer )
    install( TARGETS qtermwidgetplugin DESTINATION ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/qt4/plugins/designer )

endif (BUILD_DESIGNER_PLUGIN)
