include (FindPackageHandleStandardArgs)

set(ARNOLD_INC_SUFFIX "include")
set(ARNOLD_LIB_SUFFIX "lib")
set(ARNOLD_PLUGIN_EXTENSION ".so")
set(ARNOLD_AI_LIB ai)
set(ARNOLD_KICK_NAME kick)

if(WIN32)
    set(ARNOLD_PLUGIN_EXTENSION ".dll")
elseif(APPLE)
else()
    set(ARNOLD_LIB_SUFFIX "bin")
endif()

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

if(DEFINED ENV{ARNOLD_ROOT})
    set(ARNOLD_LOCATION $ENV{ARNOLD_ROOT})
endif()

string(REPLACE "\\" "/" ARNOLD_LOCATION ${ARNOLD_LOCATION})
set(ARNOLD_ROOT ${ARNOLD_LOCATION})

set(ARNOLD_INCLUDE_PATH ARNOLD_INCLUDE_PATH-NOTFOUND)
find_path(ARNOLD_INCLUDE_PATH ai.h
        PATHS
        ${ARNOLD_ROOT}
        PATH_SUFFIXES
        "${ARNOLD_INC_SUFFIX}"
        NO_DEFAULT_PATH
        DOC "Arnold include path")

set(ARNOLD_AI_LIBRARY ARNOLD_AI_LIBRARY-NOTFOUND)
find_library(ARNOLD_AI_LIBRARY ai
            PATHS
            ${ARNOLD_ROOT}
            PATH_SUFFIXES
            "${ARNOLD_LIB_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "Arnold ai library")
            
set(ARNOLD_LIBRARY_PATH ARNOLD_LIBRARY_PATH-NOTFOUND)

if(ARNOLD_AI_LIBRARY)
    set(ARNOLD_LIBRARY_PATH "${ARNOLD_ROOT}/${ARNOLD_LIB_SUFFIX}")
endif()

find_program(ARNOLD_KICK
    ${ARNOLD_KICK_NAME}
    PATHS
    "${ARNOLD_ROOT}/bin"
    NO_DEFAULT_PATH
    DOC "Arnold kick Path")

set(ARNOLD_VERSION ARNOLD_VERSION-NOTFOUND)
#if(EXISTS ${ARNOLD_KICK})
#    execute_process(COMMAND "${ARNOLD_KICK}" "-av"
#                    WORKING_DIRECTORY "${ARNOLD_ROOT}/bin"
#                    RESULT_VARIABLE ARNOLD_KICK_RESULT
#                    OUTPUT_VARIABLE ARNOLD_VERSION)
#    if(${ARNOLD_KICK_RESULT} GREATER 0)
#        message(FATAL_ERROR "Fail to get arnold version : ${ARNOLD_KICK_RESULT}")
#    endif()
#endif()
set(ARNOLD_VERSION_HEADER "${ARNOLD_INCLUDE_PATH}/ai_version.h")

if(EXISTS ${ARNOLD_VERSION_HEADER})
    
    file(STRINGS ${ARNOLD_VERSION_HEADER} arnold_version_str REGEX "#define AI_VERSION_ARCH_NUM ")
    string(REGEX REPLACE "#define AI_VERSION_ARCH_NUM" "" AI_VERSION_ARCH_NUM "${arnold_version_str}")
    string(STRIP "${AI_VERSION_ARCH_NUM}" AI_VERSION_ARCH_NUM)
    
    file(STRINGS ${ARNOLD_VERSION_HEADER} arnold_version_str REGEX "#define AI_VERSION_MAJOR_NUM ")
    string(REGEX REPLACE "#define AI_VERSION_MAJOR_NUM" "" AI_VERSION_MAJOR_NUM "${arnold_version_str}")
    string(STRIP "${AI_VERSION_MAJOR_NUM}" AI_VERSION_MAJOR_NUM)
    
    file(STRINGS ${ARNOLD_VERSION_HEADER} arnold_version_str REGEX "#define AI_VERSION_MINOR_NUM ")
    string(REGEX REPLACE "#define AI_VERSION_MINOR_NUM" "" AI_VERSION_MINOR_NUM "${arnold_version_str}")
    string(STRIP "${AI_VERSION_MINOR_NUM}" AI_VERSION_MINOR_NUM)
    
    file(STRINGS ${ARNOLD_VERSION_HEADER} arnold_version_str REGEX "#define AI_VERSION_FIX ")
    string(REGEX REPLACE "#define AI_VERSION_FIX" "" AI_VERSION_FIX "${arnold_version_str}")
    string(STRIP "${AI_VERSION_FIX}" AI_VERSION_FIX)
    string(REGEX REPLACE "\"" "" AI_VERSION_FIX "${AI_VERSION_FIX}")
    
    unset(arnold_version_str)
    set(ARNOLD_VERSION "${AI_VERSION_ARCH_NUM}.${AI_VERSION_MAJOR_NUM}.${AI_VERSION_MINOR_NUM}.${AI_VERSION_FIX}")
    
endif()

message(STATUS "ARNOLD_ROOT : ${ARNOLD_ROOT}")
message(STATUS "ARNOLD_INCLUDE_PATH : ${ARNOLD_INCLUDE_PATH}")
message(STATUS "ARNOLD_LIBRARY_PATH : ${ARNOLD_LIBRARY_PATH}")
message(STATUS "ARNOLD_AI_LIBRARY : ${ARNOLD_AI_LIBRARY}")
message(STATUS "ARNOLD_VERSION : ${ARNOLD_VERSION}")

find_package_handle_standard_args(Arnold 
    REQUIRED_VARS 
        ARNOLD_ROOT
        ARNOLD_INCLUDE_PATH 
        ARNOLD_LIBRARY_PATH
        ARNOLD_AI_LIBRARY
    VERSION_VAR
        ARNOLD_VERSION
    )
