###| 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
	TerminalCharacterDecoder.cpp
	KeyboardTranslator.cpp 
	Screen.cpp History.cpp BlockArray.cpp konsole_wcwidth.cpp 
	ScreenWindow.cpp 
	Emulation.cpp 
	Vt102Emulation.cpp TerminalDisplay.cpp Filter.cpp 
	Pty.cpp kpty.cpp k3process.cpp k3processcontroller.cpp 
	Session.cpp ShellCommand.cpp 
	qtermwidget.cpp
)

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

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

#| Library Output
#|	CMake supports out-of-source builds, so the binary dir is not
#|	necessarily the same as the source directory
set ( LIBRARY_OUTPUT_PATH
	${PROJECT_BINARY_DIR}/../.
)

#| Library Versioning
#|	Libraries are not versioned by default
set ( qtermwidget_VERSION_MAJOR "0" )
set ( qtermwidget_VERSION_MINOR "1" )
set ( qtermwidget_VERSION_PATCH "0" )
set ( qtermwidget_VERSION 
	"${qtermwidget_VERSION_MAJOR}.${qtermwidget_VERSION_MINOR}.${qtermwidget_VERSION_PATCH}"
)

#| 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}\\"" )

#| Defines
add_definitions ( -DHAVE_POSIX_OPENPT )
#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" )


#
# 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}
    )

    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 ${QT_PLUGINS_DIR}/designer )
    endif (APPLE)
    
    install( TARGETS qtermwidgetplugin DESTINATION ${QT_PLUGINS_DIR}/designer )

endif (BUILD_DESIGNER_PLUGIN)
