include(FindPackageHandleStandardArgs)

if(NOT DEFINED MAYA_VERSION)
    set(MAYA_VERSION 2018)
endif()

if(DEFINED ENV{MAYA_VERSION})
    set(MAYA_VERSION $ENV{MAYA_VERSION})
endif()

set(MAYA_BIN_SUFFIX "bin")
set(MAYA_INC_SUFFIX "include")
set(MAYA_LIB_SUFFIX "lib")

if(WIN32)
    set(MAYA_LOCATION "$ENV{ProgramFiles}/Autodesk/Maya${MAYA_VERSION}")
    set(MAYA_PLUGIN_EXT ".mll")
    set(MAYA_COMPILE_FLAGS "/MD /D \"NT_PLUGIN\" /D \"REQUIRE_IOSTREAM\" /D \"_BOOL\"" )
    set(MAYA_LINK_FLAGS " /export:initializePlugin /export:uninitializePlugin " )
elseif(APPLE)
    set(MAYA_LOCATION "/Applications/Autodesk/maya${MAYA_VERSION}")
    set(MAYA_PLUGIN_EXT ".bundle")
    set(MAYA_COMPILE_FLAGS "-DAW_NEW_IOSTREAMS -DCC_GNU_ -DOSMac_ -DOSMacOSX_ -DOSMac_MachO_ -DREQUIRE_IOSTREAM -fno-gnu-keywords -D_LANGUAGE_C_PLUS_PLUS")
    set(MAYA_LINK_FLAGS "-fno-gnu-keywords -framework System -framework SystemConfiguration -framework CoreServices -framework Carbon -framework Cocoa -framework ApplicationServices -framework Quicktime -framework IOKit -bundle -fPIC -L${ALEMBIC_MAYA_LIB_ROOT} -Wl,-executable_path,${ALEMBIC_MAYA_LIB_ROOT}")
else()
    set(MAYA_LOCATION "/usr/autodesk/maya${MAYA_VERSION}")
    set(MAYA_PLUGIN_EXT ".so")
    set(MAYA_COMPILE_FLAGS "-m64 -g -pthread -pipe -D_BOOL -DLINUX -DLINUX_64 -DREQUIRE_IOSTREAM -fPIC -Wno-deprecated -fno-gnu-keywords")
    set(MAYA_LINK_FLAGS "-shared -m64 -g -pthread -pipe -D_BOOL -DLINUX -DLINUX_64 -DREQUIRE_IOSTREAM -fPIC -Wno-deprecated -fno-gnu-keywords -Wl,-Bsymbolic")
endif()

set(MAYA_ROOT ${MAYA_LOCATION})
string(REPLACE "\\" "/" MAYA_ROOT ${MAYA_ROOT})

if(DEFINED ENV{MAYA_LOCATION})
    set(MAYA_ROOT $ENV{MAYA_LOCATION})
endif()

set(MAYA_EXE MAYA_EXE-NOTFOUND)
find_program(MAYA_EXE maya
            PATHS
            ${MAYA_ROOT}
            PATH_SUFFIXES
            "${MAYA_BIN_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "Maya Executable Path")

set(MAYA_PY_EXE MAYA_PY_EXE-NOTFOUND)
find_program(MAYA_PY_EXE mayapy
            PATHS
            ${MAYA_ROOT}
            PATH_SUFFIXES
            "${MAYA_BIN_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "Maya Python Executable Path")
            
set(MAYA_INCLUDE_PATH MAYA_INCLUDE_PATH-NOTFOUND)            
find_path(MAYA_INCLUDE_PATH "maya/MFn.h"
            PATHS
            ${MAYA_ROOT}
            PATH_SUFFIXES
            "${MAYA_INC_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "Maya Include Dir")

set(MAYA_OPENMAYA_LIBRARY MAYA_OPENMAYA_LIBRARY-NOTFOUND)
find_library(MAYA_OPENMAYA_LIBRARY OpenMaya
    PATHS
    "${MAYA_ROOT}/lib"
    DOC "OpenMaya Libray Path")
    
if(MAYA_OPENMAYA_LIBRARY)
    set(MAYA_LIBRARY_PATH "${MAYA_ROOT}/lib")
endif()

set(MAYA_OPENMAYAFX_LIBRARY MAYA_OPENMAYAFX_LIBRARY-NOTFOUND)    
find_library(MAYA_OPENMAYAFX_LIBRARY OpenMayaFX
    PATHS
    "${MAYA_ROOT}/lib"
    DOC "OpenMayaFX Libray Path")
    
set(MAYA_OPENMAYAANIM_LIBRARY MAYA_OPENMAYAANIM_LIBRARY-NOTFOUND)    
find_library(MAYA_OPENMAYAANIM_LIBRARY OpenMayaAnim
    PATHS
    "${MAYA_ROOT}/lib"
    DOC "OpenMayaAnim Libray Path")
    
set(MAYA_OPENMAYAUI_LIBRARY MAYA_OPENMAYAUI_LIBRARY-NOTFOUND)    
find_library(MAYA_OPENMAYAUI_LIBRARY OpenMayaUI
    PATHS
    "${MAYA_ROOT}/lib"
    DOC "OpenMayaUI Libray Path")
    
set(MAYA_OPENMAYARENDER_LIBRARY MAYA_OPENMAYARENDER_LIBRARY-NOTFOUND)    
find_library(MAYA_OPENMAYARENDER_LIBRARY OpenMayaRender
    PATHS
    "${MAYA_ROOT}/lib"
    DOC "OpenMayaRender Libray Path")
    
set(MAYA_FOUNDATION_LIBRARY MAYA_FOUNDATION_LIBRARY-NOTFOUND)    
find_library(MAYA_FOUNDATION_LIBRARY Foundation
    PATHS
    "${MAYA_ROOT}/lib"
    DOC "Foundation Libray Path")

set(XGEN_PLUGIN "plug-ins/xgen")    

set(MAYA_XGEN_ROOT MAYA_XGEN_ROOT-NOTFOUND)
if(EXISTS "${MAYA_ROOT}/plug-ins/xgen")
    set(MAYA_XGEN_ROOT "${MAYA_ROOT}/plug-ins/xgen")
endif()

set(MAYA_XGEN_INCLUDE_PATH MAYA_XGEN_INCLUDE_PATH-NOTFOUND)

find_path(MAYA_XGEN_INCLUDE_PATH "XGen/XgWinExport.h"
            PATHS
            ${MAYA_ROOT}
            PATH_SUFFIXES
            "${XGEN_PLUGIN}/${MAYA_INC_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "Maya XGen Include Dir")

if(MAYA_VERSION GREATER_EQUAL 2020)
    find_path(MAYA_XGEN_INCLUDE_PATH "xgen/src/xgcore/XgConfig.h"
            PATHS
            ${MAYA_ROOT}
            PATH_SUFFIXES
            "${XGEN_PLUGIN}/${MAYA_INC_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "Maya XGen Include Dir")
endif()

set(MAYA_ADSKXGEN_LIBRARY MAYA_ADSKXGEN_LIBRARY-NOTFOUND)
find_library(MAYA_ADSKXGEN_LIBRARY 
    AdskXGen
    libAdskXGen
    PATHS
    "${MAYA_ROOT}/plug-ins/xgen/lib"
    DOC "libAdskXGen Libray Path")
    
set(MAYA_XGEN_LIBRARY_PATH MAYA_XGEN_LIBRARY_PATH-NOTFOUND)
if(MAYA_ADSKXGEN_LIBRARY)
    set(MAYA_XGEN_LIBRARY_PATH "${MAYA_ROOT}/plug-ins/xgen/lib")
endif()

set(MAYA_ADSKXPD_LIBRARY MAYA_ADSKXPD_LIBRARY-NOTFOUND)
find_library(MAYA_ADSKXPD_LIBRARY 
    AdskXpd 
    libAdskXpd
    PATHS
    "${MAYA_ROOT}/plug-ins/xgen/lib"
    DOC "libAdskXpd Libray Path")

set(MAYA_ADSKSEEXPR_LIBRARY MAYA_ADSKSEEXPR_LIBRARY-NOTFOUND)
find_library(MAYA_ADSKSEEXPR_LIBRARY 
    AdskSeExpr 
    libAdskSeExpr
    PATHS
    "${MAYA_ROOT}/plug-ins/xgen/lib"
    DOC "libAdskSeExpr Libray Path")
    
    
set(MAYA_LIBRARIES
    ${MAYA_FOUNDATION_LIBRARY}
    ${MAYA_OPENMAYA_LIBRARY}
    ${MAYA_OPENMAYAANIM_LIBRARY}
    ${MAYA_OPENMAYAFX_LIBRARY}
    ${MAYA_OPENMAYARENDER_LIBRARY}
    ${MAYA_OPENMAYAUI_LIBRARY})
    
set(MAYA_XGEN_LIBRARIES
    ${MAYA_ADSKXGEN_LIBRARY}
    ${MAYA_ADSKXPD_LIBRARY}
    ${MAYA_ADSKSEEXPR_LIBRARY})    
    
message(STATUS "MAYA_ROOT : ${MAYA_ROOT}")
message(STATUS "MAYA_EXE : ${MAYA_EXE}")
message(STATUS "MAYA_PY_EXE : ${MAYA_PY_EXE}")
message(STATUS "MAYA_INCLUDE_PATH : ${MAYA_INCLUDE_PATH}")
message(STATUS "MAYA_LIBRARY_PATH : ${MAYA_LIBRARY_PATH}")
message(STATUS "MAYA_FOUNDATION_LIBRARY : ${MAYA_FOUNDATION_LIBRARY}")
message(STATUS "MAYA_OPENMAYA_LIBRARY : ${MAYA_OPENMAYA_LIBRARY}")
message(STATUS "MAYA_OPENMAYAANIM_LIBRARY : ${MAYA_OPENMAYAANIM_LIBRARY}")
message(STATUS "MAYA_OPENMAYAFX_LIBRARY : ${MAYA_OPENMAYAFX_LIBRARY}")
message(STATUS "MAYA_OPENMAYARENDER_LIBRARY : ${MAYA_OPENMAYARENDER_LIBRARY}")
message(STATUS "MAYA_OPENMAYAUI_LIBRARY : ${MAYA_OPENMAYAUI_LIBRARY}")
message(STATUS "----------------------------------------------")
message(STATUS "Maya XGen SDK")
message(STATUS "----------------------------------------------")
message(STATUS "MAYA_XGEN_ROOT : ${MAYA_XGEN_ROOT}")
message(STATUS "MAYA_XGEN_INCLUDE_PATH : ${MAYA_XGEN_INCLUDE_PATH}")
message(STATUS "MAYA_XGEN_LIBRARY_PATH : ${MAYA_XGEN_LIBRARY_PATH}")
message(STATUS "MAYA_ADSKXGEN_LIBRARY : ${MAYA_ADSKXGEN_LIBRARY}")
message(STATUS "MAYA_ADSKXPD_LIBRARY : ${MAYA_ADSKXPD_LIBRARY}")
message(STATUS "MAYA_ADSKSEEXPR_LIBRARY : ${MAYA_ADSKSEEXPR_LIBRARY}")

find_package_handle_standard_args(Maya 
    REQUIRED_VARS 
        MAYA_ROOT
        MAYA_INCLUDE_PATH 
        MAYA_LIBRARY_PATH
        MAYA_FOUNDATION_LIBRARY
        MAYA_OPENMAYA_LIBRARY 
        MAYA_OPENMAYAANIM_LIBRARY 
        MAYA_OPENMAYAFX_LIBRARY
        MAYA_OPENMAYARENDER_LIBRARY
        MAYA_OPENMAYAUI_LIBRARY
    VERSION_VAR
        MAYA_VERSION
    )
