# Searches for an installation of the G3D libraries. On success, it sets the following variables:
#
#   G3D_FOUND              Set to true to indicate the G3D library was found
#   G3D_INCLUDE_DIRS       The directory containing the main G3D header file G3D/G3D.h
#   G3D_LIBRARY_DIRS       The directory containing the G3D libraries (for info only, should not be required)
#   G3D_LIBRARIES          The libraries needed to use G3D (with full paths) (you may also need libzip, libjpeg etc.)
#   GLG3D_FOUND            Set to true to indicate the GLG3D library was found
#   GLG3D_LIBRARIES        The libraries needed to use GLG3D (you also need OpenGL and SDL, which should be found and linked
#                          separately for now)
#
# To specify an additional directory to search, set G3D_ROOT.
#
# Author: Siddhartha Chaudhuri, 2009
#

# Look for the G3D header, first in the user-specified location and then in the system locations
SET(G3D_INCLUDE_DOC "The directory containing the G3D header file G3D/G3D.h")
FIND_PATH(G3D_INCLUDE_DIRS NAMES G3D/G3D.h PATHS ${G3D_ROOT} ${G3D_ROOT}/include DOC ${G3D_INCLUDE_DOC} NO_DEFAULT_PATH)
IF(NOT G3D_INCLUDE_DIRS)  # now look in system locations
  FIND_PATH(G3D_INCLUDE_DIRS NAMES G3D/G3D.h DOC ${G3D_INCLUDE_DOC})
ENDIF(NOT G3D_INCLUDE_DIRS)

SET(G3D_FOUND FALSE)
SET(GLG3D_FOUND FALSE)

IF(G3D_INCLUDE_DIRS)
  SET(G3D_LIBRARY_DIRS ${G3D_INCLUDE_DIRS})

  IF("${G3D_LIBRARY_DIRS}" MATCHES "/include$")
    # Strip off the trailing "/include" in the path.
    GET_FILENAME_COMPONENT(G3D_LIBRARY_DIRS ${G3D_LIBRARY_DIRS} PATH)
  ENDIF("${G3D_LIBRARY_DIRS}" MATCHES "/include$")

  IF(EXISTS "${G3D_LIBRARY_DIRS}/lib")
    SET(G3D_LIBRARY_DIRS ${G3D_LIBRARY_DIRS}/lib)
  ENDIF(EXISTS "${G3D_LIBRARY_DIRS}/lib")

  # Find G3D libraries
  FIND_LIBRARY(G3D_DEBUG_LIBRARY   NAMES G3Dd libG3Dd PATH_SUFFIXES "" Debug   PATHS ${G3D_LIBRARY_DIRS} NO_DEFAULT_PATH)
  FIND_LIBRARY(G3D_RELEASE_LIBRARY NAMES G3D libG3D   PATH_SUFFIXES "" Release PATHS ${G3D_LIBRARY_DIRS} NO_DEFAULT_PATH)

  SET(G3D_LIBRARIES )
  IF(G3D_DEBUG_LIBRARY AND G3D_RELEASE_LIBRARY)
    SET(G3D_LIBRARIES debug ${G3D_DEBUG_LIBRARY} optimized ${G3D_RELEASE_LIBRARY})
  ELSEIF(G3D_DEBUG_LIBRARY)
    SET(G3D_LIBRARIES ${G3D_DEBUG_LIBRARY})
  ELSEIF(G3D_RELEASE_LIBRARY)
    SET(G3D_LIBRARIES ${G3D_RELEASE_LIBRARY})
  ENDIF(G3D_DEBUG_LIBRARY AND G3D_RELEASE_LIBRARY)

  IF(G3D_LIBRARIES)
    SET(G3D_FOUND TRUE)

  # G3D also needs libpng, libjpeg, zlib, libzip

    # Dependency: PNG
    FIND_LIBRARY(PNG_LIBRARIES png PATHS ${G3D_LIBRARY_DIRS} NO_DEFAULT_PATH)
    IF(NOT PNG_LIBRARIES)
      FIND_LIBRARY(PNG_LIBRARIES png)
    ENDIF(NOT PNG_LIBRARIES)
    IF(PNG_LIBRARIES)
      MESSAGE(STATUS "G3D: Found PNG library: ${PNG_LIBRARIES}")
    ELSE(PNG_LIBRARIES)
      MESSAGE(FATAL_ERROR "G3D: PNG library not found")
    ENDIF(PNG_LIBRARIES)

    # Dependency: JPEG
    FIND_LIBRARY(JPEG_LIBRARIES jpeg PATHS ${G3D_LIBRARY_DIRS} NO_DEFAULT_PATH)
    IF(NOT JPEG_LIBRARIES)
      FIND_LIBRARY(JPEG_LIBRARIES jpeg)
    ENDIF(NOT JPEG_LIBRARIES)
    IF(JPEG_LIBRARIES)
      MESSAGE(STATUS "G3D: Found JPEG library: ${JPEG_LIBRARIES}")
    ELSE(JPEG_LIBRARIES)
      MESSAGE(FATAL_ERROR "G3D: JPEG library not found")
    ENDIF(JPEG_LIBRARIES)

    # Dependency: ZIP (must be the one installed by G3D)
    SET(ZIP_LIBRARY_DIRS ${G3D_LIBRARY_DIRS})
    FIND_LIBRARY(ZIP_LIBRARIES zip PATHS ${ZIP_LIBRARY_DIRS} NO_DEFAULT_PATH)
    IF(ZIP_LIBRARIES)
      MESSAGE(STATUS "G3D: Found ZIP library: ${ZIP_LIBRARIES}")
    ELSE(ZIP_LIBRARIES)
      MESSAGE(FATAL_ERROR "G3D: Compatible ZIP library not found")
    ENDIF(ZIP_LIBRARIES)

    # Dependency: ZLIB
    FIND_LIBRARY(ZLIB_LIBRARIES z zlib zdll PATHS ${G3D_LIBRARY_DIRS} NO_DEFAULT_PATH)
    IF(NOT ZLIB_LIBRARIES)
      FIND_LIBRARY(ZLIB_LIBRARIES z zlib zdll)
    ENDIF(NOT ZLIB_LIBRARIES)
    IF(ZLIB_LIBRARIES)
      MESSAGE(STATUS "G3D: Found ZLIB library: ${ZLIB_LIBRARIES}")
    ELSE(ZLIB_LIBRARIES)
      MESSAGE(FATAL_ERROR "G3D: ZLIB library not found")
    ENDIF(ZLIB_LIBRARIES)

    LIST(APPEND G3D_LIBRARIES ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${ZIP_LIBRARIES} ${ZLIB_LIBRARIES})

  ENDIF(G3D_LIBRARIES)

  # Find an associated GLG3D installation
  IF(EXISTS ${G3D_INCLUDE_DIRS}/GLG3D/GLG3D.h)
    FIND_LIBRARY(GLG3D_DEBUG_LIBRARY   NAMES GLG3Dd libGLG3Dd PATH_SUFFIXES "" Debug   PATHS ${G3D_LIBRARY_DIRS}
                 NO_DEFAULT_PATH)
    FIND_LIBRARY(GLG3D_RELEASE_LIBRARY NAMES GLG3D libGLG3D   PATH_SUFFIXES "" Release PATHS ${G3D_LIBRARY_DIRS}
                 NO_DEFAULT_PATH)

    SET(GLG3D_LIBRARIES )
    IF(GLG3D_DEBUG_LIBRARY AND GLG3D_RELEASE_LIBRARY)
      SET(GLG3D_LIBRARIES debug ${GLG3D_DEBUG_LIBRARY} optimized ${GLG3D_RELEASE_LIBRARY})
    ELSEIF(GLG3D_DEBUG_LIBRARY)
      SET(GLG3D_LIBRARIES ${GLG3D_DEBUG_LIBRARY})
    ELSEIF(GLG3D_RELEASE_LIBRARY)
      SET(GLG3D_LIBRARIES ${GLG3D_RELEASE_LIBRARY})
    ENDIF(GLG3D_DEBUG_LIBRARY AND GLG3D_RELEASE_LIBRARY)

    IF(GLG3D_LIBRARIES)
      SET(GLG3D_FOUND TRUE)
    ENDIF(GLG3D_LIBRARIES)
  ENDIF(EXISTS ${G3D_INCLUDE_DIRS}/GLG3D/GLG3D.h)
ENDIF(G3D_INCLUDE_DIRS)

IF(G3D_FOUND)
  IF(NOT G3D_FIND_QUIETLY)
    MESSAGE(STATUS "Found G3D: headers at ${G3D_INCLUDE_DIRS}, libraries at ${G3D_LIBRARY_DIRS}")
  ENDIF(NOT G3D_FIND_QUIETLY)
ELSE(G3D_FOUND)
  IF(G3D_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "G3D not found")
  ENDIF(G3D_FIND_REQUIRED)
ENDIF(G3D_FOUND)

IF(NOT GLG3D_FOUND)
  MESSAGE(STATUS "GLG3D not found")
ENDIF(NOT GLG3D_FOUND)
