project(kid3)

set(WITH_TAGLIB ON CACHE BOOL "build with TagLib")
set(WITH_MP4V2 ON CACHE BOOL "build with mp4v2")
set(WITH_ID3LIB ON CACHE BOOL "build with id3lib")
set(WITH_VORBIS ON CACHE BOOL "build with Ogg/Vorbis")
set(WITH_FLAC ON CACHE BOOL "build with FLAC")
set(WITH_TUNEPIMP ON CACHE BOOL "build with TunePimp")
set(WITH_KDE ON CACHE BOOL "build with KDE libraries")

find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include (MacroEnsureVersion)
include (CheckLibraryExists)

if(WITH_KDE)
  set(CONFIG_USE_KDE 1)
  set(BASIC_LIBRARIES ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS})
else(WITH_KDE)
  set(BASIC_LIBRARIES ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTNETWORK_LIBRARY} ${QT_QTXML_LIBRARY})
endif(WITH_KDE)

### Check for taglib
set(TAGLIB_LIBRARIES)
set(TAGLIB_CFLAGS)
if(WITH_TAGLIB)
  find_program(TAGLIBCONFIG_EXECUTABLE NAMES taglib-config PATHS /usr/bin /usr/local/bin ${BIN_INSTALL_DIR})
  if(TAGLIBCONFIG_EXECUTABLE)
    exec_program(${TAGLIBCONFIG_EXECUTABLE} ARGS --libs RETURN_VALUE _return_VALUE OUTPUT_VARIABLE TAGLIB_LIBRARIES)
    exec_program(${TAGLIBCONFIG_EXECUTABLE} ARGS --cflags RETURN_VALUE _return_VALUE OUTPUT_VARIABLE TAGLIB_CFLAGS)
    exec_program(${TAGLIBCONFIG_EXECUTABLE} ARGS --version RETURN_VALUE _return_VALUE OUTPUT_VARIABLE TAGLIB_VERSION)
    if(TAGLIB_LIBRARIES AND TAGLIB_CFLAGS AND TAGLIB_VERSION)
      MACRO_ENSURE_VERSION2("1.4" ${TAGLIB_VERSION} TAGLIB_INSTALLED_VERSION_OK)
      if(TAGLIB_INSTALLED_VERSION_OK)
        set(HAVE_TAGLIB 1)
        message(STATUS "TagLib found: ${TAGLIB_LIBRARIES}")
      endif(TAGLIB_INSTALLED_VERSION_OK)
    endif(TAGLIB_LIBRARIES AND TAGLIB_CFLAGS AND TAGLIB_VERSION)
  endif(TAGLIBCONFIG_EXECUTABLE)
  if (NOT HAVE_TAGLIB)
    message(FATAL_ERROR "Could not find Taglib")
  endif (NOT HAVE_TAGLIB)
endif(WITH_TAGLIB)

### Check for mp4v2
set(MP4V2_LIBRARIES)
if(WITH_MP4V2)
  find_path(MP4V2_INCLUDE_DIR mp4.h
   /usr/include/
   /usr/local/include/
  )

  find_library(MP4V2_LIBRARIES NAMES mp4v2
   PATHS
   /usr/lib
   /usr/local/lib
  )

  if (MP4V2_INCLUDE_DIR AND MP4V2_LIBRARIES)
    message(STATUS "mp4v2 found: ${MP4V2_LIBRARIES}")
    set(HAVE_MP4V2 1)
    set(MP4V2_FOUND TRUE)

    set(_CMAKE_REQUIRED_LIBRARIES_TMP ${CMAKE_REQUIRED_LIBRARIES})
    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${MP4V2_LIBRARIES})
    CHECK_CXX_SOURCE_COMPILES("#include <mp4.h>\nint main() {\n  MP4FileHandle hFile;\n  u_int32_t index;\n  char* ppName;\n  u_int8_t* ppValue;\n  u_int32_t pValueSize;\n  MP4GetMetadataByIndex(hFile, index, &ppName, &ppValue, &pValueSize);\n  return 0;\n}\n" HAVE_MP4V2_MP4GETMETADATABYINDEX_CHARPP_ARG)
    set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_TMP})

  else (MP4V2_INCLUDE_DIR AND MP4V2_LIBRARIES)
    message(STATUS "Could not find mp4v2")
  endif (MP4V2_INCLUDE_DIR AND MP4V2_LIBRARIES)
endif(WITH_MP4V2)

### Check for id3lib
set(ID3LIB_LIBRARIES)
if(WITH_ID3LIB)
  find_path(ID3LIB_INCLUDE_DIR id3/tag.h)
  find_library(ID3LIB_LIBRARIES NAMES id3)
  if(ID3LIB_INCLUDE_DIR AND ID3LIB_LIBRARIES)
    message(STATUS "id3lib found: ${ID3LIB_LIBRARIES}")
    set(HAVE_ID3LIB 1)
  
    set(_CMAKE_REQUIRED_LIBRARIES_TMP ${CMAKE_REQUIRED_LIBRARIES})
    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${ID3LIB_LIBRARIES} -lz -lstdc++)
    CHECK_CXX_SOURCE_COMPILES("#include <id3/globals.h>\nint main() {\n  Mp3_Headerinfo info;\n  info.vbr_bitrate = 0;\n  return 0;\n}\n" ID3LIB_VBR_SUPPORT_FOUND)
    set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_TMP})
  
    if(NOT ID3LIB_VBR_SUPPORT_FOUND)
      set(HAVE_NO_ID3LIB_VBR 1)
    endif(NOT ID3LIB_VBR_SUPPORT_FOUND)
  else(ID3LIB_INCLUDE_DIR AND ID3LIB_LIBRARIES)
    message(FATAL_ERROR "Could not find id3lib")
  endif(ID3LIB_INCLUDE_DIR AND ID3LIB_LIBRARIES)
endif(WITH_ID3LIB)

### Check for TunePimp
set(TUNEPIMP_LIBRARIES)
set(HAVE_TUNEPIMP 0)
if(WITH_TUNEPIMP)
  find_path(TUNEPIMP_INCLUDE_DIR tunepimp/tp_c.h)
  if(NOT TUNEPIMP_INCLUDE_DIR)
    find_path(TUNEPIMP_INCLUDE_DIR tunepimp-0.5/tp_c.h)
  endif(NOT TUNEPIMP_INCLUDE_DIR)
  find_library(TUNEPIMP_LIBRARIES NAMES tunepimp)
  if(TUNEPIMP_INCLUDE_DIR AND TUNEPIMP_LIBRARIES)
    message(STATUS "TunePimp found: ${TUNEPIMP_LIBRARIES}")
    CHECK_LIBRARY_EXISTS(${TUNEPIMP_LIBRARIES} tp_SetFileNameEncoding "" TUNEPIMP_FOUND_VERSION_4)
    CHECK_LIBRARY_EXISTS(${TUNEPIMP_LIBRARIES} tr_GetPUID "" TUNEPIMP_FOUND_VERSION_5)
    if(TUNEPIMP_FOUND_VERSION_5)
      set(HAVE_TUNEPIMP 5)
    elseif(TUNEPIMP_FOUND_VERSION_4)
      set(HAVE_TUNEPIMP 4)
    else(TUNEPIMP_FOUND_VERSION_5)
      set(HAVE_TUNEPIMP 3)
    endif(TUNEPIMP_FOUND_VERSION_5)
  else(TUNEPIMP_INCLUDE_DIR AND TUNEPIMP_LIBRARIES)
    message(FATAL_ERROR "Could not find TunePimp")
  endif(TUNEPIMP_INCLUDE_DIR AND TUNEPIMP_LIBRARIES)
endif(WITH_TUNEPIMP)

### Check for Vorbis
set(VORBIS_LIBRARIES)
if(WITH_VORBIS)
  find_path(VORBIS_INCLUDE_DIR vorbis/vorbisfile.h)
  find_library(VORBIS_LIBRARY NAMES vorbis)
  find_library(VORBISFILE_LIBRARY NAMES vorbisfile)
  if(VORBIS_INCLUDE_DIR AND VORBIS_LIBRARY AND VORBISFILE_LIBRARY)
    set(VORBIS_LIBRARIES ${VORBIS_LIBRARY} ${VORBISFILE_LIBRARY})
    message(STATUS "Vorbis found: ${VORBIS_LIBRARIES}")
    set(HAVE_VORBIS 1)
  else(VORBIS_INCLUDE_DIR AND VORBIS_LIBRARY AND VORBISFILE_LIBRARY)
    message(FATAL_ERROR "Could not find Vorbis")
  endif(VORBIS_INCLUDE_DIR AND VORBIS_LIBRARY AND VORBISFILE_LIBRARY)
endif(WITH_VORBIS)

### Check for FLAC++
set(FLAC_LIBRARIES)
if(WITH_FLAC)
  find_path(FLACPP_INCLUDE_DIR FLAC++/metadata.h)
  find_library(FLAC_LIBRARY NAMES FLAC)
  find_library(FLACPP_LIBRARY NAMES FLAC++)
  if(FLACPP_INCLUDE_DIR AND FLAC_LIBRARY AND FLACPP_LIBRARY)
    set(FLAC_LIBRARIES ${FLACPP_LIBRARY} ${FLAC_LIBRARY})
    message(STATUS "FLAC++ found: ${FLAC_LIBRARIES}")
    set(HAVE_FLAC 1)
  
    set(_CMAKE_REQUIRED_LIBRARIES_TMP ${CMAKE_REQUIRED_LIBRARIES})
    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${FLAC_LIBRARIES} ${FLACPP_LIBRARIES})
    CHECK_CXX_SOURCE_COMPILES("#include <FLAC++/metadata.h>\nint main() {\n  FLAC::Metadata::VorbisComment vc;\n  const ::FLAC__StreamMetadata* fsmd = vc;\n  return 0;\n}\n" FLAC_STREAMMETADATA_OPERATOR_FOUND)
    set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_TMP})
  
    if(NOT FLAC_STREAMMETADATA_OPERATOR_FOUND)
      set(HAVE_NO_FLAC_STREAMMETADATA_OPERATOR 1)
    endif(NOT FLAC_STREAMMETADATA_OPERATOR_FOUND)
  else(FLACPP_INCLUDE_DIR AND FLAC_LIBRARY AND FLACPP_LIBRARY)
    message(FATAL_ERROR "Could not find FLAC++")
  endif(FLACPP_INCLUDE_DIR AND FLAC_LIBRARY AND FLACPP_LIBRARY)
endif(WITH_FLAC)

add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS} ${TAGLIB_CFLAGS} -DHAVE_CONFIG_H=1 -DKDE_NO_COMPAT)
include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDE4_INCLUDES} ${QT_INCLUDES})

configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)

add_subdirectory(kid3)
add_subdirectory(doc)
add_subdirectory(po)
