SET(EXTENSION_LIST)
SET(IDL_LIST)
SET(CURRENT_EXTENSION "no-extension")

MACRO(DECLARE_EXTENSION name)
  IF (NOT CURRENT_EXTENSION STREQUAL "no-extension")
    MESSAGE(FATAL_ERROR "Nesting DECLARE_EXTENSION(${name}) in DECLARE_EXTENSION(${CURRENT_EXTENSION}) is invalid.")
  ENDIF()
  SET(CURRENT_EXTENSION ${name})
  SET(IDL_LIST_${CURRENT_EXTENSION})
  SET(IDL_DEPS_${CURRENT_EXTENSION})
  LIST(APPEND EXTENSION_LIST ${name})
ENDMACRO(DECLARE_EXTENSION)

MACRO(DECLARE_EXTENSION_END name)
  IF (NOT ${CURRENT_EXTENSION} STREQUAL ${name})
    MESSAGE(FATAL_ERROR "Matching DECLARE_EXTENSION_END(${name}) with DECLARE_EXTENSION(${CURRENT_EXTENSION}) is invalid.")
  ENDIF()
  SET(CURRENT_EXTENSION "no-extension")
ENDMACRO(DECLARE_EXTENSION_END)

MACRO(DECLARE_IDL name)
  LIST(APPEND IDL_LIST ${name})
  LIST(APPEND IDL_LIST_${CURRENT_EXTENSION} ${name})
  FILE(READ interfaces/${name}.idl IDLSOURCE)
  STRING(REGEX MATCH "[\r\n]+[ \t\r\n]*module[ \t\r\n]+([A-Z|a-z|0-9|_]+)" ${name}_NAMESPACE ${IDLSOURCE})
  STRING(REGEX REPLACE "[\r\n]+[ \t\r\n]*module[ \t\r\n]+([A-Z|a-z|0-9|_]+)" "\\1" "${name}_NAMESPACE" "${${name}_NAMESPACE}")
  STRING(REGEX MATCHALL "[\r\n]+[ \t\r\n]*interface[ \t\r\n]+([A-Z|a-z|0-9|_]+)[ \t\r\n]*[:{]" ${name}_INTERFACES ${IDLSOURCE})
  STRING(REGEX REPLACE "[\r\n]+[ \t\r\n]*interface[ \t\r\n]+([A-Z|a-z|0-9|_]+)[ \t\r\n]*[:{]" "\\1;" ${name}_INTERFACES ${${name}_INTERFACES})
  STRING(REGEX MATCHALL "[\r\n]+[ \t\r\n]*exception[ \t\r\n]+([A-Z|a-z|0-9|_]+)[ \t\r\n]*[:{]" ${name}_EXCEPTIONS ${IDLSOURCE})
  STRING(REGEX REPLACE "[\r\n]+[ \t\r\n]*exception[ \t\r\n]+([A-Z|a-z|0-9|_]+)[ \t\r\n]*[:{]" "\\1;" "${name}_EXCEPTIONS" "${${name}_EXCEPTIONS}")
  STRING(REGEX MATCHALL "[\r\n]+  enum[ \t\r\n]+([A-Z|a-z|0-9|_]+)[ \t\r\n]*[:{]" ${name}_ENUMS ${IDLSOURCE})
  STRING(REGEX REPLACE "[\r\n]+  enum[ \t\r\n]+([A-Z|a-z|0-9|_]+)[ \t\r\n]*[:{]" "\\1;" "${name}_ENUMS" "${${name}_ENUMS}")
ENDMACRO(DECLARE_IDL)

MACRO(DECLARE_IDL_DEPENDENCY name)
  LIST(APPEND IDL_DEPS_${CURRENT_EXTENSION} ${name})
ENDMACRO(DECLARE_IDL_DEPENDENCY)

#FIND_PROGRAM(OMNIIDL omniidl
#    HINTS /hpc/cmiss/opencmissextras/cellml/packages/omniORB/omniORB-4.1.6/bin
#    DOC "The path to the omniidl program (note: omniidl is part of omniORB: http://omniorb.sourceforge.net/)")
#IF(CHECK_BUILD AND OMNIIDL STREQUAL "OMNIIDL-NOTFOUND")
#  MESSAGE(FATAL_ERROR "omniidl was not found. To override the pre-build checks and manually fix any problems, pass -DCHECK_BUILD:BOOL=OFF to CMake.")
#ENDIF()
#INCLUDE_DIRECTORIES(interfaces)
