include(FindPackageHandleStandardArgs)

if(NOT DEFINED MTOA_MAYA_VERSION)
    set(MTOA_MAYA_VERSION 2018)
    if(DEFINED MAYA_VERSION)
        set(MTOA_MAYA_VERSION ${MAYA_VERSION})
    endif()
    if(DEFINED ENV{MAYA_VERSION})
        set(MTOA_MAYA_VERSION $ENV{MAYA_VERSION})
    endif()
endif()

set(MTOA_INC_SUFFIX "include")
set(MTOA_LIB_SUFFIX "lib")
set(MTOA_PLUGIN_EXTENSION ".so")
set(MTOA_API_NAME mtoa_api)
set(MTOA_KICK_NAME kick)

if(WIN32)
    set(MTOA_LOCATION "C:/solidangle/mtoadeploy/${MTOA_MAYA_VERSION}")
    if(${MTOA_MAYA_VERSION} GREATER 2019)
        set(MTOA_LOCATION "C:/Program Files/Autodesk/Arnold/maya${MTOA_MAYA_VERSION}")
    endif()
    set(MTOA_KICK_NAME "kick.exe")
    set(MTOA_PLUGIN_EXTENSION ".dll")
elseif(APPLE)
    if(${MTOA_MAYA_VERSION} GREATER 2019)
        set(MTOA_LOCATION "/Applications/Autodesk/Arnold/maya${MTOA_MAYA_VERSION}")
    endif()
else()
    set(MTOA_LOCATION "/opt/solidangle/mtoa/${MTOA_MAYA_VERSION}")
    if(${MTOA_MAYA_VERSION} GREATER 2019)
        set(MTOA_LOCATION "/usr/autodesk/arnold/maya${MTOA_MAYA_VERSION}")
    endif()
    set(MTOA_LIB_SUFFIX "bin")
endif()

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

message(STATUS "MAYA_VERSION : $ENV{MAYA_VERSION}")
message(STATUS "MTOA_MAYA_VERSION : ${MTOA_MAYA_VERSION}")
message(STATUS "MTOA_LOCATION : ${MTOA_LOCATION}")

set(MTOA_ROOT ${MTOA_LOCATION})

if(DEFINED ENV{MTOA_LOCATION})
    set(MTOA_ROOT $ENV{MTOA_LOCATION})
endif()

set(MTOA_INCLUDE_PATH MTOA_INCLUDE_PATH-NOTFOUND)

find_path(MTOA_INCLUDE_PATH extension/Extension.h
            PATHS
            ${MTOA_LOCATION}
            PATH_SUFFIXES
            "${MTOA_INC_SUFFIX}/mtoa"
            NO_DEFAULT_PATH
            DOC "Mtoa Include Dir")
          
set(MTOA_VERSION_HEADER "${MTOA_INCLUDE_PATH}/utils/Version.h")

if(EXISTS ${MTOA_VERSION_HEADER})
    file(STRINGS ${MTOA_VERSION_HEADER} SRC_CONTENT)
    foreach(SRC_LINE ${SRC_CONTENT})
        if(${SRC_LINE} MATCHES "^#define MTOA_ARCH_VERSION_NUM")
            set(MTOA_VERSION_TYPE MTOA_ARCH_VERSION)
        elseif(${SRC_LINE} MATCHES "^#define MTOA_MAJOR_VERSION_NUM")
            set(MTOA_VERSION_TYPE MTOA_MAJOR_VERSION_NUM)
        elseif(${SRC_LINE} MATCHES "^#define MTOA_MINOR_VERSION_NUM")
            set(MTOA_VERSION_TYPE MTOA_MINOR_VERSION_NUM)
        elseif(${SRC_LINE} MATCHES "^#define MTOA_FIX_VERSION")
            set(MTOA_VERSION_TYPE MTOA_FIX_VERSION)
        else()
            
        endif()
        # message(${SRC_LINE})
        string(STRIP "${SRC_LINE}" LINE_BUFFER)
        string(REPLACE " " ";" LINE_BUFFER_LIST ${LINE_BUFFER})
        list(LENGTH LINE_BUFFER_LIST _LINE_LENGTH)
        list(GET LINE_BUFFER_LIST -1 "${MTOA_VERSION_TYPE}")
        # message("## ${LINE_BUFFER_LIST} - ${MTOA_VERSION_TYPE} : ${_LINE_LENGTH}")
    endforeach()
    
    if(DEFINED MTOA_FIX_VERSION)
        string(SUBSTRING "${MTOA_FIX_VERSION}" 1 1 MTOA_FIX_VERSION)
        set(MTOA_VERSION "${MTOA_ARCH_VERSION}.${MTOA_MAJOR_VERSION_NUM}.${MTOA_MINOR_VERSION_NUM}.${MTOA_FIX_VERSION}")
    else()
        set(MTOA_VERSION "${MTOA_ARCH_VERSION}.${MTOA_MAJOR_VERSION_NUM}.${MTOA_MINOR_VERSION_NUM}")
    endif()
else()
    message(FATAL_ERROR "Could not find version header : ${MTOA_VERSION_HEADER}")    
endif()

if(NOT DEFINED MTOA_VERSION)
    message(FATAL_ERROR "Could not find MTOA_VERSION")
endif()

set(MTOA_API_LIBRARY MTOA_API_LIBRARY-NOTFOUND)

find_library(MTOA_API_LIBRARY ${MTOA_API_NAME}
    PATHS
    "${MTOA_LOCATION}/${MTOA_LIB_SUFFIX}"
    DOC "mtoa_api Libray Path")
        
if(MTOA_API_LIBRARY)
    set(MTOA_LIBRARY_PATH "${MTOA_LOCATION}/${MTOA_LIB_SUFFIX}")
endif()

find_program(MTOA_KICK
    ${MTOA_KICK_NAME}
    PATHS
    "${MTOA_LOCATION}/bin"
    NO_DEFAULT_PATH
    DOC "mtoa kick Path")

if(EXISTS ${MTOA_KICK})
    execute_process(COMMAND "${MTOA_KICK}" "-av"
                    WORKING_DIRECTORY "${MTOA_LOCATION}/bin"
                    RESULT_VARIABLE MTOA_KICK_RESULT
                    OUTPUT_VARIABLE MTOA_ARNOLD_VERSION)
    if(${MTOA_KICK_RESULT} GREATER 0)
        message(FATAL_ERROR "Fail to get arnold version : ${MTOA_KICK_RESULT}")
    endif()
endif()    

message(STATUS "MTOA_ROOT : ${MTOA_ROOT}")
message(STATUS "MTOA_VERSION : ${MTOA_VERSION}")
message(STATUS "MTOA_ARNOLD_VERSION : ${MTOA_ARNOLD_VERSION}")
message(STATUS "MTOA_INCLUDE_PATH : ${MTOA_INCLUDE_PATH}")
message(STATUS "MTOA_LIBRARY_PATH : ${MTOA_LIBRARY_PATH}")
message(STATUS "MTOA_API : ${MTOA_API_LIBRARY}")
message(STATUS "MTOA_KICK : ${MTOA_KICK}")

find_package_handle_standard_args(Mtoa 
    REQUIRED_VARS 
        MTOA_INCLUDE_PATH 
        MTOA_LIBRARY_PATH
        MTOA_API_LIBRARY
    VERSION_VAR
        MTOA_VERSION
    )
