# Provides an option that the user can enable/disable.
# Positional arguments:
#   variable : variable where to store user choice
#   description : help string describing the option
#   value : initial value or boolean expression
#
# ~ Simplified version of corresponding OCV_OPTION macro from OpenCV
macro(RADICAL_OPTION variable description value)
  set(__value ${value})
  if(__value MATCHES ";")
    if(${__value})
      option(${variable} "${description}" ON)
    else()
      option(${variable} "${description}" OFF)
    endif()
  elseif(DEFINED ${__value})
    if(${__value})
      option(${variable} "${description}" ON)
    else()
      option(${variable} "${description}" OFF)
    endif()
  else()
    option(${variable} "${description}" ${__value})
  endif()
  unset(__value)
endmacro()


# Read set of version defines from a header file.
#
# ~ Source code copied from OpenCV
macro(parse_header FILENAME FILE_VAR)
  set(vars_regex "")
  set(__parnet_scope OFF)
  set(__add_cache OFF)
  foreach(name ${ARGN})
    if("${name}" STREQUAL "PARENT_SCOPE")
      set(__parnet_scope ON)
    elseif("${name}" STREQUAL "CACHE")
      set(__add_cache ON)
    elseif(vars_regex)
      set(vars_regex "${vars_regex}|${name}")
    else()
      set(vars_regex "${name}")
    endif()
  endforeach()
  if(EXISTS "${FILENAME}")
    file(STRINGS "${FILENAME}" ${FILE_VAR} REGEX "#define[ \t]+(${vars_regex})[ \t]+[0-9]+" )
  else()
    unset(${FILE_VAR})
  endif()
  foreach(name ${ARGN})
    if(NOT "${name}" STREQUAL "PARENT_SCOPE" AND NOT "${name}" STREQUAL "CACHE")
      if(${FILE_VAR})
        if(${FILE_VAR} MATCHES ".+[ \t]${name}[ \t]+([0-9]+).*")
          string(REGEX REPLACE ".+[ \t]${name}[ \t]+([0-9]+).*" "\\1" ${name} "${${FILE_VAR}}")
        else()
          set(${name} "")
        endif()
        if(__add_cache)
          set(${name} ${${name}} CACHE INTERNAL "${name} parsed from ${FILENAME}" FORCE)
        elseif(__parnet_scope)
          set(${name} "${${name}}" PARENT_SCOPE)
        endif()
      else()
        unset(${name} CACHE)
      endif()
    endif()
  endforeach()
endmacro()
