include(FindZLIB)

if (ZLIB_FOUND)
    FIND_PATH(OPENEXR_INCLUDE_DIR ImfRgbaFile.h PATH_SUFFIXES OpenEXR PATHS $ENV{OPENEXR_PATH}/include $ENV{CPATH})
    FIND_LIBRARY(OPENEXR_HALF_LIBRARY NAMES Half PATHS $ENV{OPENEXR_PATH}/lib $ENV{LIBRARY_PATH})
    FIND_LIBRARY(OPENEXR_IEX_LIBRARY NAMES Iex PATHS $ENV{OPENEXR_PATH}/lib $ENV{LIBRARY_PATH})
    FIND_LIBRARY(OPENEXR_IMATH_LIBRARY NAMES Imath PATHS $ENV{OPENEXR_PATH}/lib $ENV{LIBRARY_PATH})
    FIND_LIBRARY(OPENEXR_ILMIMF_LIBRARY NAMES IlmImf PATHS $ENV{OPENEXR_PATH}/lib $ENV{LIBRARY_PATH})
    FIND_LIBRARY(OPENEXR_ILMTHREAD_LIBRARY NAMES IlmThread PATHS $ENV{OPENEXR_PATH}/lib $ENV{LIBRARY_PATH})

    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(OpenEXR REQUIRED_VARS
            OPENEXR_INCLUDE_DIR
            OPENEXR_HALF_LIBRARY
            OPENEXR_IEX_LIBRARY
            OPENEXR_IMATH_LIBRARY
            OPENEXR_ILMIMF_LIBRARY
            OPENEXR_ILMTHREAD_LIBRARY)
    
    set(OPENEXR_LIBRARIES
        ${OPENEXR_ILMIMF_LIBRARY}
        ${OPENEXR_ILMTHREAD_LIBRARY}
        ${OPENEXR_IMATH_LIBRARY}
        ${OPENEXR_HALF_LIBRARY}
        ${OPENEXR_IEX_LIBRARY}
        ${ZLIB_LIBRARY})
    
    mark_as_advanced(
            OPENEXR_INCLUDE_DIR
            OPENEXR_HALF_LIBRARY
            OPENEXR_IEX_LIBRARY
            OPENEXR_IMATH_LIBRARY
            OPENEXR_ILMIMF_LIBRARY
            OPENEXR_ILMTHREAD_LIBRARY
            OPENEXR_LIBRARIES)
else()
    message(STATUS "OpenEXR not available: Could not find ZLib")
endif()