# -*- cmake -*-
# - Find Cortex
#
# Author : Alex Fuller boberfly@gmail.com
#
# This module will define the following variables:
#  Cortex_INCLUDE_DIRS - Location of the openexr includes
#  Cortex_FOUND - true if OPENEXR was found on the system
#  Cortex_LIBRARYDIR - the full set of library directories
#  Cortex_IECORE_LIBRARY
#  Cortex_IECOREPYTHON_LIBRARY
#  Cortex_IECOREGL_LIBRARY
#  Cortex_IECOREALEMBIC_LIBRARY
#  Cortex_IECOREAPPLESEED_LIBRARY
#  Cortex_IECOREARNOLD_LIBRARY

FIND_PACKAGE ( PackageHandleStandardArgs )

FIND_PATH ( CORTEX_LOCATION include/IECore/IECore.h
  ENV CORTEX_ROOT
  NO_DEFAULT_PATH
  NO_SYSTEM_ENVIRONMENT_PATH
  )

FIND_PACKAGE_HANDLE_STANDARD_ARGS ( Cortex
  REQUIRED_VARS CORTEX_LOCATION
  )

IF ( CORTEX_FOUND )

	SET ( IECORE_LIBRARY_NAME IECore )
	SET ( IECOREPYTHON_LIBRARY_NAME IECorePython )
  SET ( IECOREGL_LIBRARY_NAME IECoreGL )
  SET ( IECOREIMAGE_LIBRARY_NAME IECoreImage )
  SET ( IECORESCENE_LIBRARY_NAME IECoreScene )
  SET ( IECOREALEMBIC_LIBRARY_NAME IECoreAlembic )
  SET ( IECOREAPPLESEED_LIBRARY_NAME IECoreAppleseed )
  SET ( IECOREARNOLD_LIBRARY_NAME IECoreArnold )
	
  SET ( Cortex_INCLUDE_DIRS
    ${CORTEX_LOCATION}/include
    CACHE STRING "Cortex include directories")
  SET ( Cortex_LIBRARYDIR ${CORTEX_LOCATION}/lib
    CACHE STRING "Cortex library directories")
  SET ( CORTEX_FOUND TRUE )

  IF (Cortex_USE_STATIC_LIBS)
    IF (APPLE)
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
    ELSEIF (WIN32)
      # Link library
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
    ELSE ()
      # MESSAGE ( "CMAKE_FIND_LIBRARY_SUFFIXES = " ${CMAKE_FIND_LIBRARY_SUFFIXES})
      SET ( ORIGINAL_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
      SET ( CMAKE_FIND_LIBRARY_SUFFIXES ".a")
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      SET ( CMAKE_FIND_LIBRARY_SUFFIXES ${ORIGINAL_CMAKE_FIND_LIBRARY_SUFFIXES})
    ENDIF ()
  ELSE ()
    IF (APPLE)
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib")
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
    ELSEIF (WIN32)
      # Link library
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      # Load library
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      # MUST reset
      SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
    ELSE ()
      FIND_LIBRARY ( Cortex_IECORE_LIBRARY ${IECORE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREPYTHON_LIBRARY ${IECOREPYTHON_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREGL_LIBRARY ${IECOREGL_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREIMAGE_LIBRARY ${IECOREIMAGE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECORESCENE_LIBRARY ${IECORESCENE_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREALEMBIC_LIBRARY ${IECOREALEMBIC_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREAPPLESEED_LIBRARY ${IECOREAPPLESEED_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
      FIND_LIBRARY ( Cortex_IECOREARNOLD_LIBRARY ${IECOREARNOLD_LIBRARY_NAME} PATHS ${Cortex_LIBRARYDIR} )
    ENDIF ()
  ENDIF ()
  
ENDIF ( CORTEX_FOUND )

