#################################################
#
#  (C) 2012 Serghei Amelian
#  serghei (DOT) amelian (AT) gmail.com
#
#  Improvements and feedback are welcome
#
#  This file is released under GPL >= 2
#
#################################################


##### set project version ########################

include( TDEVersion )
cmake_minimum_required( VERSION ${TDE_CMAKE_MINIMUM_VERSION} )
tde_set_project_version( )


##### general package setup #####################

project( tdemultimedia )

set( PACKAGE tdemultimedia )

enable_testing( )


##### include essential cmake modules ###########

include( FindPkgConfig )
include( CheckIncludeFile )
include( CheckIncludeFileCXX )
include( CheckTypeSize )
include( CheckCSourceCompiles )
include( CheckCXXSourceCompiles )
include( CheckFunctionExists )
include( CheckLibraryExists )
include( CheckSymbolExists )


##### include our cmake modules #################

include( TDEMacros )


##### setup install paths #######################

include( TDESetupPaths )
tde_setup_paths( )


##### optional stuff ############################

option( WITH_ALL_OPTIONS "Enable all optional support" OFF )
option( WITH_ALSA "Enable ALSA support (kscd, kmix)" ON )
option( WITH_ARTS_AKODE "Enable aRts akode plugin" ${WITH_ALL_OPTIONS} )
option( WITH_ARTS_AUDIOFILE "Enable aRts audiofile plugin" ${WITH_ALL_OPTIONS} )
option( WITH_ARTS_MPEGLIB "Enable aRts mpeglib pluing" ${WITH_ALL_OPTIONS} )
option( WITH_ARTS_XINE "Enable aRts xine plugin" ${WITH_ALL_OPTIONS} )
option( WITH_CDPARANOIA "Enable CDParanoia support (mpeglib, tdeioslave-audiocd)" ${WITH_ALL_OPTIONS} )
option( WITH_FLAC "Enable FLAC support (tdeioslave)" ${WITH_ALL_OPTIONS} )
option( WITH_GSTREAMER "Enable gstreamer support (juk)" ${WITH_ALL_OPTIONS} )
option( WITH_KSCD_CDDA "Enable cdda support in kscd" ${WITH_ALL_OPTIONS} )
option( WITH_LAME "Enable lame support (krec)" ${WITH_ALL_OPTIONS} )
option( WITH_MUSICBRAINZ "Enable MusicBrainz support (juk)" OFF )
option( WITH_TAGLIB "Enable taglib support (juk, tdefile-plugins)" ${WITH_ALL_OPTIONS} )
option( WITH_THEORA "Enable Ogg/Theora support (tdefile-plugins)" ${WITH_ALL_OPTIONS} )
option( WITH_VORBIS "Enable Ogg/Vorbis support (krec, mpeglib, oggarts, tdefile-plugins)" ${WITH_ALL_OPTIONS} )
option( WITH_KMIX_EXPERIMENTAL "Enable experimental/unmaintained KMix features" OFF )

##### user requested modules ####################

option( BUILD_ALL "Build all" OFF )
option( BUILD_ARTS "Build aRts" ${BUILD_ALL} )
option( BUILD_DOC "Build documentation" ${BUILD_ALL} )
option( BUILD_JUK "Build Juk player" ${BUILD_ALL} )
option( BUILD_KABOODLE "Build Kaboodle video player" ${BUILD_ALL} )
option( BUILD_KAPPFINDER_DATA "Build data for kappfinder" ${BUILD_ALL} )
option( BUILD_KAUDIOCREATOR "Build KAudio Creator" ${BUILD_ALL} )
option( BUILD_KMIX "Build kmix" ${BUILD_ALL} )
option( BUILD_KREC "Build krec" ${BUILD_ALL} )
option( BUILD_KSCD "Build kscd player" ${BUILD_ALL} )
option( BUILD_LIBKCDDB "Build kcddb library" ${BUILD_ALL} )
option( BUILD_MPEGLIB "Build mpeg library" ${BUILD_ALL} )
option( BUILD_NOATUN "Build noatun player" ${BUILD_ALL} )
option( BUILD_TDEFILE_PLUGINS "Build tdefile plugins" ${BUILD_ALL} )
option( BUILD_TDEIOSLAVE "Build tdeio slave plugins" ${BUILD_ALL} )
option( BUILD_TDEMID "Build tdemid" ${BUILD_ALL} )


##### configure checks ##########################

include( ConfigureChecks.cmake )


###### global compiler settings #################

add_definitions(
  -DHAVE_CONFIG_H
)

set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TQT_CXX_FLAGS}" )
set( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined" )
set( CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined" )


##### activate dependencies #####################


##### tdemultimedia directories ########################

if( BUILD_MPEGLIB ) # must be before mpeglib_artsplug
  if( NOT BUILD_ARTS )
    tde_message_fatal( "mpeglib can not be built without building arts" )
  endif( )
  add_subdirectory( mpeglib )
endif( BUILD_MPEGLIB )
if( BUILD_ARTS )
  add_subdirectory( arts )
  tde_conditional_add_subdirectory( WITH_ARTS_AKODE akode_artsplugin )
  tde_conditional_add_subdirectory( WITH_ARTS_AUDIOFILE audiofile_artsplugin )
  if( WITH_ARTS_MPEGLIB )
    if( NOT BUILD_MPEGLIB )
      tde_message_fatal( "mpeglib aRts plugin can not be built without building mpeglib" )
    endif( )
    add_subdirectory( mpeglib_artsplug )
  else( WITH_ARTS_MPEGLIB )
    tde_conditional_add_subdirectory( WITH_VORBIS oggvorbis_artsplugin )
  endif( WITH_ARTS_MPEGLIB )
  tde_conditional_add_subdirectory( WITH_ARTS_XINE xine_artsplugin )
endif( BUILD_ARTS )
tde_conditional_add_subdirectory( BUILD_DOC doc )
tde_conditional_add_subdirectory( BUILD_JUK juk )
tde_conditional_add_subdirectory( BUILD_KABOODLE kaboodle )
tde_conditional_add_subdirectory( BUILD_KAPPFINDER_DATA kappfinder-data )
if( BUILD_KAUDIOCREATOR )
  if( NOT BUILD_KSCD )
    tde_message_fatal( "kaudiocreator can not be built without building kscd" )
  endif( )
  add_subdirectory( kaudiocreator )
endif( BUILD_KAUDIOCREATOR )
tde_conditional_add_subdirectory( BUILD_KMIX kmix )
if( WITH_KMIX_EXPERIMENTAL )
  if( NOT BUILD_KMIX )
    tde_message_fatal( "KMix experimental features requested, but KMix won't be built" )
  endif( )
endif( WITH_KMIX_EXPERIMENTAL )
if( BUILD_KREC )
  if( NOT BUILD_ARTS )
    tde_message_fatal( "krec can not be built without building arts" )
  endif( )
  add_subdirectory( krec )
endif( BUILD_KREC )
tde_conditional_add_subdirectory( BUILD_KSCD kscd )
tde_conditional_add_subdirectory( BUILD_LIBKCDDB libkcddb )
tde_conditional_add_subdirectory( BUILD_NOATUN noatun )
tde_conditional_add_subdirectory( BUILD_TDEFILE_PLUGINS tdefile-plugins )
tde_conditional_add_subdirectory( BUILD_TDEIOSLAVE tdeioslave )
tde_conditional_add_subdirectory( BUILD_TDEMID tdemid )


##### write configure files #####################

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


##### add apidox targets ############

add_custom_target( apidox
  COMMAND ${CMAKE_COMMAND} -E env
    "TQTDOCDIR=/usr/share/tqt3/doc/html"
    "DOXDATA=${HTML_INSTALL_DIR}/en/common"
    ${CMAKE_SOURCE_DIR}/admin/doxygen.sh
    --no-modulename
    ${CMAKE_SOURCE_DIR}
  COMMAND
    find ${CMAKE_BINARY_DIR}/apidocs -name '*.html' |
    xargs -r sed -i 's|${CMAKE_BINARY_DIR}/apidocs|${HTML_INSTALL_DIR}/en/${PROJECT_NAME}-apidocs|g'
  COMMAND
    find ${CMAKE_BINARY_DIR}/apidocs -name '*.tag' |
    xargs -r sed -i 's|${CMAKE_SOURCE_DIR}/|${PROJECT_NAME}/|g'
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_custom_target( install-apidox
  COMMAND ${CMAKE_COMMAND} -E echo Apidox installed in ${HTML_INSTALL_DIR}/en/${PROJECT_NAME}-apidocs
)

install( CODE "
  if( EXISTS ${CMAKE_BINARY_DIR}/apidocs )
    file(
      INSTALL ${CMAKE_BINARY_DIR}/apidocs/
      DESTINATION ${HTML_INSTALL_DIR}/en/${PROJECT_NAME}-apidocs
      PATTERN doxygen_sqlite3.db EXCLUDE
      PATTERN installdox-slow EXCLUDE
    )
  endif()"
)
