find_package(ALSA)
find_package(OSS)

if (FQTERM_USE_STATIC_QT)
  ADD_DEFINITIONS(-DFQTERM_USE_STATIC_QT)
endif(FQTERM_USE_STATIC_QT)

if (ALSA_FOUND)
  ADD_DEFINITIONS(-DAUDIO_ALSA)
  SET(AUDIO_INCLUDE_DIR "${ALSA_INCLUDE_DIR}")
  SET(AUDIO_LIBRARY "${ALSA_LIBRARY}")
else (ALSA_FOUND)
  if (OSS_FOUND)
	ADD_DEFINITIONS(-DAUDIO_OSS)
	SET(AUDIO_INCLUDE_DIR "${OSS_INCLUDE_DIR}")
	SET(AUDIO_LIBRARY)
  endif (OSS_FOUND)
endif(ALSA_FOUND)

ADD_DEFINITIONS(-Wall)
ADD_DEFINITIONS(-D_OS_LINUX_)
ADD_DEFINITIONS(--std=c++11)

SET(WITH_ASAN OFF CACHE PATH "Use address sanitizer")

ADD_EXECUTABLE(fqterm.bin
  src/fqterm/main.cpp
)

IF (WITH_ASAN)
    target_compile_options(fqterm.bin PUBLIC -fsanitize=address -fno-omit-frame-pointer)
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
ENDIF(WITH_ASAN)

IF(USE_QT5)
  find_package(Qt5Core REQUIRED)
  find_package(Qt5Widgets REQUIRED)
  find_package(Qt5Network REQUIRED)
  find_package(Qt5Script REQUIRED)
  find_package(Qt5Multimedia REQUIRED)
  find_package(Qt5Xml REQUIRED)
ELSE(USE_QT5)
ENDIF(USE_QT5)

IF(NOT USE_QT5)
  include_directories(
    ${QT_INCLUDE_DIR}
    ${QT_QTCORE_INCLUDE_DIR}
    ${QT_QTGUI_INCLUDE_DIR}
    )
ENDIF(NOT USE_QT5)
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}/src/common
  ${CMAKE_CURRENT_SOURCE_DIR}/src/protocol
  ${CMAKE_CURRENT_SOURCE_DIR}/src/terminal
  ${CMAKE_CURRENT_SOURCE_DIR}/src/utilities
  ${CMAKE_CURRENT_SOURCE_DIR}/src/ui
  ${AUDIO_INCLUDE_DIR}
)

link_directories(
  ${QT_PLUGINS_DIR}/codecs
  ${QT_PLUGINS_DIR}/imageformats
)

if (FQTERM_USE_STATIC_QT)
  set(FQ_QT_STATIC_PLUGINS
  ${QT_PLUGINS_DIR}/imageformats/libqgif.a
  ${QT_PLUGINS_DIR}/imageformats/libqjpeg.a
  ${QT_PLUGINS_DIR}/imageformats/libqmng.a
  ${QT_PLUGINS_DIR}/codecs/libqcncodecs.a
  ${QT_PLUGINS_DIR}/codecs/libqjpcodecs.a
  ${QT_PLUGINS_DIR}/codecs/libqkrcodecs.a
  ${QT_PLUGINS_DIR}/codecs/libqtwcodecs.a
  jpeg
  )
endif(FQTERM_USE_STATIC_QT)

target_link_libraries(fqterm.bin
  ${FQTERM_LIBS}
  ${QT_QTNETWORK_LIBRARIES}
  ${QT_QTXML_LIBRARIES}
  ${QT_QTSCRIPT_LIBRARIES}
  ${FQ_QT_STATIC_PLUGINS}
  ${QT_LIBRARIES}
  ${AUDIO_LIBRARY}
  ${PYTHON_LIB}
  ${OPENSSL_CRYPTO_LIBRARY}
  X11
)

IF(USE_QT5)
  target_link_libraries(fqterm.bin Qt5::Core Qt5::Widgets)
ENDIF(USE_QT5)

add_dependencies(fqterm.bin
  fqterm_main
  fqterm_common
  fqterm_terminal
  fqterm_protocol
  fqterm_ui
  fqterm_utilities
  fqterm_qm_target
)

add_custom_target(love
COMMAND FQTERM_RESOURCE=${CMAKE_SOURCE_DIR}/res ${CMAKE_BINARY_DIR}/fqterm.bin
DEPENDS ${CMAKE_BINARY_DIR}/fqterm.bin
)

INSTALL(
  FILES ${CMAKE_BINARY_DIR}/fqterm.bin
  DESTINATION bin/
  PERMISSIONS
      OWNER_EXECUTE OWNER_WRITE OWNER_READ
      GROUP_EXECUTE GROUP_READ
      WORLD_EXECUTE WORLD_READ
)

INSTALL(
  FILES ${CMAKE_SOURCE_DIR}/res/fqterm.sh
  DESTINATION bin/
  PERMISSIONS
      OWNER_EXECUTE OWNER_WRITE OWNER_READ
      GROUP_EXECUTE GROUP_READ
      WORLD_EXECUTE WORLD_READ
  RENAME fqterm
)

INSTALL(
  DIRECTORY ${CMAKE_SOURCE_DIR}/res/
  DESTINATION share/FQTerm/
  DIRECTORY_PERMISSIONS
              OWNER_EXECUTE OWNER_WRITE OWNER_READ
              GROUP_EXECUTE GROUP_READ
              WORLD_EXECUTE WORLD_READ
  FILE_PERMISSIONS
              OWNER_WRITE OWNER_READ
              GROUP_READ
              WORLD_READ
  PATTERN "*.svn*" EXCLUDE
  PATTERN "fqterm.desktop" EXCLUDE
)

INSTALL(
	FILES ${CMAKE_SOURCE_DIR}/res/pic/fqterm_256x256.png
	DESTINATION share/pixmaps/
	PERMISSIONS
		OWNER_WRITE OWNER_READ
		GROUP_READ
		WORLD_READ
	RENAME fqterm.png
)

INSTALL(
  FILES ${fqterm_qm_files}
  DESTINATION share/FQTerm/dict/
  PERMISSIONS
      OWNER_WRITE OWNER_READ
      GROUP_READ
      WORLD_READ
)

INSTALL(
  FILES res/fqterm.desktop
  DESTINATION share/applications
)

SET(CPACK_PACKAGE_CONTACT FireLakeWalker@gmail.com)
SET(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
SET(CPACK_SYSTEM_NAME Linux-i386)
SET(CPACK_PACKAGE_VERSION ${FQTERM_VERSION})
SET(CPACK_DEBIAN_PACKAGE_VERSION ${FQTERM_VERSION}+i386)
SET(CPACK_DEBIAN_PACKAGE_SECTION Network)
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "This package is generated by FQTerm team, to get a formal version, please visit http://packages.debian.org/sid/main/fqterm.")

SET(CPACK_GENERATOR DEB)
INCLUDE(CPack)
# vim:ft=cmake:
