# - Try to find the OggVorbis libraries
# Once done this will define
#
#  OGGVORBIS_FOUND        - system has OggVorbis (i.e. it has the Ogg and Vorbis headers, as well as libogg and libvorbis)
#  OGGVORBIS_VERSION      - set either to 1 or 2
#  OGGVORBIS_INCLUDE_DIRS - the Ogg and Vorbis include directories
#  OGG_INCLUDE_DIR        - the Ogg include directory
#  VORBIS_INCLUDE_DIR     - the Vorbis include directory
#  OGGVORBIS_LIBRARIES    - The libraries needed to use OggVorbis
#  OGG_LIBRARY            - The Ogg library
#  VORBIS_LIBRARY         - The Vorbis library
#  VORBISFILE_LIBRARY     - The VorbisFile library (optional)
#  VORBISENC_LIBRARY      - The VorbisEnc library (optional)
#
# To specify an additional directory to search for Ogg, set Ogg_ROOT.
# To specify an additional directory to search for Vorbis, set Vorbis_ROOT.
#
# Copyright (c) 2006, Richard Laerkaeng, <richard@goteborg.utfors.se>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
# (Copied and slightly modified from the KDE project)
#

INCLUDE(CheckLibraryExists)

FIND_PATH(VORBIS_INCLUDE_DIR vorbis/vorbisfile.h PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/include NO_DEFAULT_PATH)
IF(NOT VORBIS_INCLUDE_DIR)
  FIND_PATH(VORBIS_INCLUDE_DIR vorbis/vorbisfile.h)
ENDIF(NOT VORBIS_INCLUDE_DIR)

FIND_PATH(OGG_INCLUDE_DIR ogg/ogg.h PATHS ${Ogg_ROOT} ${Ogg_ROOT}/include NO_DEFAULT_PATH)
IF(NOT OGG_INCLUDE_DIR)
  FIND_PATH(OGG_INCLUDE_DIR ogg/ogg.h)
ENDIF(NOT OGG_INCLUDE_DIR)

# Debug libraries
FIND_LIBRARY(OGG_DEBUG_LIBRARY NAMES oggd ogg_d liboggd libogg_d PATH_SUFFIXES "" Debug
             PATHS ${Ogg_ROOT} ${Ogg_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT OGG_DEBUG_LIBRARY)  # now look in system locations
  FIND_LIBRARY(OGG_DEBUG_LIBRARY NAMES oggd ogg_d liboggd libogg_d PATH_SUFFIXES "" Debug)
ENDIF(NOT OGG_DEBUG_LIBRARY)

FIND_LIBRARY(VORBIS_DEBUG_LIBRARY NAMES vorbisd vorbis_d libvorbisd libvorbis_d PATH_SUFFIXES "" Debug
             PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT VORBIS_DEBUG_LIBRARY)  # now look in system locations
  FIND_LIBRARY(VORBIS_DEBUG_LIBRARY NAMES vorbisd vorbis_d libvorbisd libvorbis_d PATH_SUFFIXES "" Debug)
ENDIF(NOT VORBIS_DEBUG_LIBRARY)

FIND_LIBRARY(VORBISFILE_DEBUG_LIBRARY NAMES vorbisfiled vorbisfile_d libvorbisfiled libvorbisfile_d PATH_SUFFIXES "" Debug
             PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT VORBISFILE_DEBUG_LIBRARY)  # now look in system locations
  FIND_LIBRARY(VORBISFILE_DEBUG_LIBRARY NAMES vorbisfiled vorbisfile_d libvorbisfiled libvorbisfile_d PATH_SUFFIXES "" Debug)
ENDIF(NOT VORBISFILE_DEBUG_LIBRARY)

FIND_LIBRARY(VORBISENC_DEBUG_LIBRARY NAMES vorbisencd vorbisenc_d libvorbisencd libvorbisenc_d PATH_SUFFIXES "" Debug
             PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT VORBISENC_DEBUG_LIBRARY)  # now look in system locations
  FIND_LIBRARY(VORBISENC_DEBUG_LIBRARY NAMES vorbisencd vorbisenc_d libvorbisencd libvorbisenc_d PATH_SUFFIXES "" Debug)
ENDIF(NOT VORBISENC_DEBUG_LIBRARY)


# Release libraries
FIND_LIBRARY(OGG_RELEASE_LIBRARY NAMES ogg libogg PATH_SUFFIXES "" Release
             PATHS ${Ogg_ROOT} ${Ogg_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT OGG_RELEASE_LIBRARY)  # now look in system locations
  FIND_LIBRARY(OGG_RELEASE_LIBRARY NAMES ogg libogg PATH_SUFFIXES "" Release)
ENDIF(NOT OGG_RELEASE_LIBRARY)

FIND_LIBRARY(VORBIS_RELEASE_LIBRARY NAMES vorbis libvorbis PATH_SUFFIXES "" Release
             PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT VORBIS_RELEASE_LIBRARY)  # now look in system locations
  FIND_LIBRARY(VORBIS_RELEASE_LIBRARY NAMES vorbis libvorbis PATH_SUFFIXES "" Release)
ENDIF(NOT VORBIS_RELEASE_LIBRARY)

FIND_LIBRARY(VORBISFILE_RELEASE_LIBRARY NAMES vorbisfile libvorbisfile PATH_SUFFIXES "" Release
             PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT VORBISFILE_RELEASE_LIBRARY)  # now look in system locations
  FIND_LIBRARY(VORBISFILE_RELEASE_LIBRARY NAMES vorbisfile libvorbisfile PATH_SUFFIXES "" Release)
ENDIF(NOT VORBISFILE_RELEASE_LIBRARY)

FIND_LIBRARY(VORBISENC_RELEASE_LIBRARY NAMES vorbisenc libvorbisenc PATH_SUFFIXES "" Release
             PATHS ${Vorbis_ROOT} ${Vorbis_ROOT}/lib NO_DEFAULT_PATH)
IF(NOT VORBISENC_RELEASE_LIBRARY)  # now look in system locations
  FIND_LIBRARY(VORBISENC_RELEASE_LIBRARY NAMES vorbisenc libvorbisenc PATH_SUFFIXES "" Release)
ENDIF(NOT VORBISENC_RELEASE_LIBRARY)


SET(OGG_LIBRARY)
IF(OGG_DEBUG_LIBRARY AND OGG_RELEASE_LIBRARY)
  SET(OGG_LIBRARY debug ${OGG_DEBUG_LIBRARY} optimized ${OGG_RELEASE_LIBRARY})
ELSEIF(OGG_DEBUG_LIBRARY)
  SET(OGG_LIBRARY ${OGG_DEBUG_LIBRARY})
ELSEIF(OGG_RELEASE_LIBRARY)
  SET(OGG_LIBRARY ${OGG_RELEASE_LIBRARY})
ENDIF(OGG_DEBUG_LIBRARY AND OGG_RELEASE_LIBRARY)

SET(VORBIS_LIBRARY)
IF(VORBIS_DEBUG_LIBRARY AND VORBIS_RELEASE_LIBRARY)
  SET(VORBIS_LIBRARY debug ${VORBIS_DEBUG_LIBRARY} optimized ${VORBIS_RELEASE_LIBRARY})
ELSEIF(VORBIS_DEBUG_LIBRARY)
  SET(VORBIS_LIBRARY ${VORBIS_DEBUG_LIBRARY})
ELSEIF(VORBIS_RELEASE_LIBRARY)
  SET(VORBIS_LIBRARY ${VORBIS_RELEASE_LIBRARY})
ENDIF(VORBIS_DEBUG_LIBRARY AND VORBIS_RELEASE_LIBRARY)

SET(VORBISFILE_LIBRARY)
IF(VORBISFILE_DEBUG_LIBRARY AND VORBISFILE_RELEASE_LIBRARY)
  SET(VORBISFILE_LIBRARY debug ${VORBISFILE_DEBUG_LIBRARY} optimized ${VORBISFILE_RELEASE_LIBRARY})
ELSEIF(VORBISFILE_DEBUG_LIBRARY)
  SET(VORBISFILE_LIBRARY ${VORBISFILE_DEBUG_LIBRARY})
ELSEIF(VORBISFILE_RELEASE_LIBRARY)
  SET(VORBISFILE_LIBRARY ${VORBISFILE_RELEASE_LIBRARY})
ENDIF(VORBISFILE_DEBUG_LIBRARY AND VORBISFILE_RELEASE_LIBRARY)

SET(VORBISENC_LIBRARY)
IF(VORBISENC_DEBUG_LIBRARY AND VORBISENC_RELEASE_LIBRARY)
  SET(VORBISENC_LIBRARY debug ${VORBISENC_DEBUG_LIBRARY} optimized ${VORBISENC_RELEASE_LIBRARY})
ELSEIF(VORBISENC_DEBUG_LIBRARY)
  SET(VORBISENC_LIBRARY ${VORBISENC_DEBUG_LIBRARY})
ELSEIF(VORBISENC_RELEASE_LIBRARY)
  SET(VORBISENC_LIBRARY ${VORBISENC_RELEASE_LIBRARY})
ENDIF(VORBISENC_DEBUG_LIBRARY AND VORBISENC_RELEASE_LIBRARY)

# Should the check also test for the OGG... variables?
IF(OGG_INCLUDE_DIR AND VORBIS_INCLUDE_DIR AND OGG_LIBRARY AND VORBIS_LIBRARY)
   SET(OGGVORBIS_FOUND TRUE)

   SET(OGGVORBIS_INCLUDE_DIRS ${OGG_INCLUDE_DIR} ${VORBIS_INCLUDE_DIR})
   SET(OGGVORBIS_LIBRARIES ${OGG_LIBRARY} ${VORBIS_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBISENC_LIBRARY})

   IF(NOT WIN32)  # not quite sure what to do on Windows yet
     SET(_CMAKE_REQUIRED_LIBRARIES_TMP ${CMAKE_REQUIRED_LIBRARIES})
     SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${OGGVORBIS_LIBRARIES})
     CHECK_LIBRARY_EXISTS(vorbis vorbis_bitrate_addblock "" HAVE_LIBVORBISENC2)
     SET(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_TMP})

     IF(HAVE_LIBVORBISENC2)
        SET(OGGVORBIS_VERSION 2)
     ELSE(HAVE_LIBVORBISENC2)
        SET(OGGVORBIS_VERSION 1)
     ENDIF(HAVE_LIBVORBISENC2)
   ENDIF(NOT WIN32)

ELSE(OGG_INCLUDE_DIR AND VORBIS_INCLUDE_DIR AND OGG_LIBRARY AND VORBIS_LIBRARY)
   SET(OGGVORBIS_VERSION)
   SET(OGGVORBIS_FOUND FALSE)
ENDIF(OGG_INCLUDE_DIR AND VORBIS_INCLUDE_DIR AND OGG_LIBRARY AND VORBIS_LIBRARY)


IF(OGGVORBIS_FOUND)
   IF(NOT OggVorbis_FIND_QUIETLY)
      MESSAGE(STATUS "Found OggVorbis: ${OGGVORBIS_LIBRARIES}")
   ENDIF(NOT OggVorbis_FIND_QUIETLY)
ELSE(OGGVORBIS_FOUND)
   IF(OggVorbis_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find OggVorbis libraries")
   ENDIF(OggVorbis_FIND_REQUIRED)
   IF(NOT OggVorbis_FIND_QUITELY)
      MESSAGE(STATUS "Could not find OggVorbis libraries")
   ENDIF(NOT OggVorbis_FIND_QUITELY)
ENDIF(OGGVORBIS_FOUND)

#CHECK_INCLUDE_FILES(vorbis/vorbisfile.h HAVE_VORBISFILE_H)
#CHECK_LIBRARY_EXISTS(ogg ogg_page_version "" HAVE_LIBOGG)
#CHECK_LIBRARY_EXISTS(vorbis vorbis_info_init "" HAVE_LIBVORBIS)
#CHECK_LIBRARY_EXISTS(vorbisfile ov_open "" HAVE_LIBVORBISFILE)
#CHECK_LIBRARY_EXISTS(vorbisenc vorbis_info_clear "" HAVE_LIBVORBISENC)
#CHECK_LIBRARY_EXISTS(vorbis vorbis_bitrate_addblock "" HAVE_LIBVORBISENC2)

#IF(HAVE_LIBOGG AND HAVE_VORBISFILE_H AND HAVE_LIBVORBIS AND HAVE_LIBVORBISFILE AND HAVE_LIBVORBISENC)
#    MESSAGE(STATUS "Ogg/Vorbis found")
#    SET(VORBIS_LIBS "-lvorbis -logg")
#    SET(VORBISFILE_LIBS "-lvorbisfile")
#    SET(VORBISENC_LIBS "-lvorbisenc")
#    SET(OGGVORBIS_FOUND TRUE)
#    IF(HAVE_LIBVORBISENC2)
#        SET(HAVE_VORBIS 2)
#    ELSE(HAVE_LIBVORBISENC2)
#        SET(HAVE_VORBIS 1)
#    ENDIF(HAVE_LIBVORBISENC2)
#ELSE(HAVE_LIBOGG AND HAVE_VORBISFILE_H AND HAVE_LIBVORBIS AND HAVE_LIBVORBISFILE AND HAVE_LIBVORBISENC)
#    MESSAGE(STATUS "Ogg/Vorbis not found")
#ENDIF(HAVE_LIBOGG AND HAVE_VORBISFILE_H AND HAVE_LIBVORBIS AND HAVE_LIBVORBISFILE AND HAVE_LIBVORBISENC)
