SET(ILMBASE_VERSION_MAJOR "2")
SET(ILMBASE_VERSION_MINOR "2")
SET(ILMBASE_VERSION_PATCH "0")

SET(ILMBASE_VERSION ${ILMBASE_VERSION_MAJOR}.${ILMBASE_VERSION_MINOR}.${ILMBASE_VERSION_PATCH})
SET(ILMBASE_VERSION_API ${ILMBASE_VERSION_MAJOR}_${ILMBASE_VERSION_MINOR})

# MESSAGE(STATUS "check ${ILMBASE_CONFIG_PATH}")
# MESSAGE(STATUS "check ${ILMBASE_CONFIG_FILE}")
# IF (NOT ${ILMBASE_CONFIG_PATH})
#   SET(ILMBASE_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR}/config)
#   SET(ILMBASE_CONFIG_FILE ${ILMBASE_CONFIG_PATH}/IlmBaseConfig.h)
# ENDIF ()
MESSAGE(STATUS "Generate ${ILMBASE_CONFIG_FILE}")

IF (WIN32)
  FILE ( COPY ${ILMBASE_PATH}/config.windows/IlmBaseConfig.h
    DESTINATION ${ILMBASE_CONFIG_PATH}
    )
ELSE ()
  IF (APPLE)
    FILE ( WRITE  ${ILMBASE_CONFIG_FILE} "#define HAVE_PTHREAD 1\n" )
  ELSE ()
    FILE ( WRITE  ${ILMBASE_CONFIG_FILE} "#define HAVE_PTHREAD 1\n" )
    FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_HAVE_LARGE_STACK  1\n" )
    FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define HAVE_POSIX_SEMAPHORES 1\n" )
    FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_HAVE_CONTROL_REGISTER_SUPPORT 1\n")
  ENDIF ()
ENDIF ()

IF (NAMESPACE_VERSIONING)
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_INTERNAL_NAMESPACE_CUSTOM 1\n")
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define IMATH_INTERNAL_NAMESPACE Imath_${ILMBASE_VERSION_API}\n")
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define IEX_INTERNAL_NAMESPACE Iex_${ILMBASE_VERSION_API}\n")
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMTHREAD_INTERNAL_NAMESPACE IlmThread_${ILMBASE_VERSION_API}\n")
ELSE ()
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_INTERNAL_NAMESPACE_CUSTOM 0\n")
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define IMATH_INTERNAL_NAMESPACE Imath\n")
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define IEX_INTERNAL_NAMESPACE Iex\n")
  FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMTHREAD_INTERNAL_NAMESPACE IlmThread\n")
ENDIF ()

FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define IMATH_NAMESPACE Imath\n")
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define IEX_NAMESPACE Iex\n")
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMTHREAD_NAMESPACE IlmThread\n")
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_VERSION_STRING \"${ILMBASE_VERSION}\"\n" )
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_PACKAGE_STRING \"IlmBase ${ILMBASE_VERSION}\"\n" )
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_VERSION_MAJOR ${ILMBASE_VERSION_MAJOR}\n" )
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_VERSION_MINOR ${ILMBASE_VERSION_MINOR}\n" )
FILE ( APPEND ${ILMBASE_CONFIG_FILE} "#define ILMBASE_VERSION_PATCH ${ILMBASE_VERSION_PATCH}\n" )

FILE ( APPEND ${ILMBASE_CONFIG_FILE} "
// Version as a single hex number, e.g. 0x01000300 == 1.0.3
#define ILMBASE_VERSION_HEX ((ILMBASE_VERSION_MAJOR << 24) | \\
                             (ILMBASE_VERSION_MINOR << 16) | \\
                             (ILMBASE_VERSION_PATCH <<  8))
")
