# some variable assignment logic behavior and some comments borrowed from SysFera's analogous configuration module

#OMNIORB_NEED_COS - additional COS components configured
#OMNIORB_EXTRA_LIBS - additional nonstandard OMNIORB libraries configured

# Find the omniORB libraries and include dir
#
 
# The following variables are set:
# OMNIORB_FOUND        - When false, don't try to use omniORB
# OMNIORB_INCLUDE_DIRS - Directories to include to use omniORB
# OMNIORB_LIBRARIES    - Files to link against to use omniORB
# OMNIORB_IDL_COMPILER - The idl compiler command (when found)
# OMNIORB_VERSION_STRING - A string of the form X.Y.Z representing the version
# OMNIORB_OMNINAMES_COMMAND - the omniNames ORB server command (when found)

IF(OMNIORB_INCLUDE_DIRS)
  #already in cache
  SET(OMNIORB_FIND_QUIETLY TRUE)
ENDIF(OMNIORB_INCLUDE_DIRS)

FIND_PATH(OMNIORB4_INCLUDE_DIR
  NAMES omniORB.h
  PATHS /usr/include/omniORB4)

FIND_PATH(OMNITHREAD_INCLUDE_DIR
  NAMES omnithread.h
  PATHS /usr/include/omnithread)

if(OMNIORB_NEED_COS)
  FIND_PATH(OMNICOS_INCLUDE_DIR
    NAMES CosTime.hh
    PATHS /usr/include/COS)
else(OMNIORB_NEED_COS)
  set(OMNICOS_INCLUDE_DIR "")
endif(OMNIORB_NEED_COS)

set(OMNIORB_INCLUDE_DIRS ${OMNIORB4_INCLUDE_DIR} ${OMNITHREAD_INCLUDE_DIR} ${OMNICOS_INCLUDE_DIR})

FIND_LIBRARY(OMNIORB_OMNIORB4_LIBRARY
  NAMES omniORB4
  PATHS /usr/lib64 /lib64)

FIND_LIBRARY(OMNIORB_OMNITHREAD_LIBRARY
  NAMES omnithread
  PATHS /usr/lib64 /lib64)

FIND_LIBRARY(OMNIORB_DYNAMIC_LIBRARY
  NAMES omniDynamic4
  PATHS /usr/lib64 /lib64)

if(OMNIORB_NEED_COS)
  FIND_LIBRARY(OMNIORB_COS4_LIBRARY
    NAMES COS4
    PATHS /usr/lib64 /lib64)
  FIND_LIBRARY(OMNIORB_COSDYNAMIC_LIBRARY
    NAMES COSDyanmic4
    PATHS /usr/lib64 /lib64)
else(OMNIORB_NEED_COS)
  set(OMNIORB_COS4_LIBRARY "")
  set(OMNIORB_COSDYNAMIC_LIBRARY "")
endif(OMNIORB_NEED_COS)

if(OMNIORB_EXTRA_LIBS)
  FIND_LIBRARY(OMNIORB_CODESETS_LIBRARY
    NAMES omniCodeSets4
    PATHS /usr/lib64 /lib64)
  FIND_LIBRARY(OMNIORB_CONNECTIONMGMT_LIBRARY
    NAMES omniConnectionMgmt4
    PATHS /usr/lib64 /lib64)
  FIND_LIBRARY(OMNIORB_SSLTP_LIBRARY
    NAMES omnisslTP4
    PATHS /usr/lib64 /lib64)
else(OMNIORB_EXTRA_LIBS)
  set(OMNIORB_CODESETS_LIBRARY "")
  set(OMNIORB_CONNECTIONMGMT_LIBRARY "")
  set(OMNIORB_SSLTP_LIBRARY "")
endif(OMNIORB_EXTRA_LIBS)

set(OMNIORB_LIBRARIES ${OMNIORB_DYNAMIC_LIBRARY} ${OMNIORB_OMNIORB4_LIBRARY} ${OMNIORB_OMNITHREAD_LIBRARY} ${OMNIORB_COS4_LIBRARY} ${OMNIORB_COSDYNAMIC_LIBRARY} ${OMNIORB_CODESETS_LIBRARY} ${OMNIORB_CONNECTIONMGMT_LIBRARY} ${OMNIORB_SSLTP_LIBRARY})

IF(EXISTS "${OMNIORB4_INCLUDE_DIR}/acconfig.h")
    FILE(STRINGS "${OMNIORB4_INCLUDE_DIR}/acconfig.h" OMNIORB_H REGEX "^#define PACKAGE_VERSION \"[^\"]*\"$")
    STRING(REGEX REPLACE "^.*PACKAGE_VERSION \"([0-9]+).*$" "\\1" OMNIORB_VERSION_MAJOR "${OMNIORB_H}")
    STRING(REGEX REPLACE "^.*PACKAGE_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" OMNIORB_VERSION_MINOR  "${OMNIORB_H}")
    STRING(REGEX REPLACE "^.*PACKAGE_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" OMNIORB_VERSION_PATCH "${OMNIORB_H}")
    SET(OMNIORB_VERSION_STRING "${OMNIORB_VERSION_MAJOR}.${OMNIORB_VERSION_MINOR}.${OMNIORB_VERSION_PATCH}")
ENDIF()

FIND_PROGRAM(OMNIORB_IDL_COMPILER
  NAMES omniidl
  PATHS /usr/bin /bin)

#Usage: OMNIIDL_TARGET(targetname inputfile outputfile flags)
#note: reruns during every rebuild for no discernible reason
IF(OMNIORB_IDL_COMPILER)
  MACRO(OMNIIDL_TARGET Name OmniidlInput OmniidlOutput OmniidlFlags)
    SET(OMNIIDL_TARGET_cmdopt "${OmniidlFlags}")
    SET(OMNIIDL_TARGET_outputs "${OmniidlOutput}")
    STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
    STRING(REPLACE "cc" "hh" _fileext ${_fileext})
    STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
        OMNIIDL_${Name}_OUTPUT_HEADER "${ARGV2}")
    LIST(APPEND OMNIIDL_TARGET_outputs "${OMNIIDL_${Name}_OUTPUT_HEADER}")
    FILE(RELATIVE_PATH relpath "${CMAKE_CURRENT_SOURCE_DIR}" "${OmniidlOutput}")
    get_filename_component(outputpath "${relpath}" PATH)

    ADD_CUSTOM_COMMAND(OUTPUT ${OMNIIDL_TARGET_outputs}
      COMMAND ${OMNIORB_IDL_COMPILER} -C${CMAKE_CURRENT_SOURCE_DIR}/${outputpath} ${OMNIIDL_TARGET_cmdopt} ${OmniidlInput}
      MAIN_DEPENDENCY ${OmniidlInput}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      COMMENT "[OMNIIDL][${Name}] Building interface with OmniIDL ${OMNIORB_VERSION_STRING}"
      VERBATIM)

    SET(OMNIIDL_${Name}_DEFINED TRUE)
    SET(OMNIIDL_${Name}_INPUT ${OmniidlInput})
    SET(OMNIIDL_${Name}_OUTPUTS ${OMNIIDL_TARGET_outputs})
    SET(OMNIIDL_${Name}_COMPILE_FLAGS ${OMNIIDL_TARGET_cmdopt})
    SET(OMNIIDL_${Name}_OUTPUT_SOURCE "${OmniidlOutput}")

  ENDMACRO(OMNIIDL_TARGET)
ENDIF(OMNIORB_IDL_COMPILER)

FIND_PROGRAM(OMNIORB_OMNINAMES_COMMAND
  NAMES omniNames
  PATHS /usr/bin /bin)

IF(OMNIORB_INCLUDE_DIRS AND OMNIORB_LIBRARIES AND OMNIORB_IDL_COMPILER)
  SET(OMNIORB_FOUND TRUE)
  SET(OMNIORB_LIBRARY_DIR ${OMNIORB_OMNIORB4_LIBRARY})
ELSE(OMNIORB_INCLUDE_DIRS AND OMNIORB_LIBRARIES AND OMNIORB_IDL_COMPILER)
  SET(OMNIORB_FOUND FALSE)
  SET(OMNIORB_LIBRARY_DIR)
ENDIF(OMNIORB_INCLUDE_DIRS AND OMNIORB_LIBRARIES AND OMNIORB_IDL_COMPILER)

#handle the QUIETLY and REQUIRED arguments
include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OMNIORB DEFAULT_MSG OMNIORB_LIBRARIES OMNIORB_VERSION_STRING)

mark_as_advanced(
  OMNIORB_FOUND
  OMNIORB_INCLUDE_DIRS
  OMNIORB_LIBRARIES
  OMNIORB_IDL_COMPILER
  OMNIORB_OMNINAMES_COMMAND
  OMNIORB_VERSION_STRING)
