# - Find an RITK installation or build tree.

# When RITK is found, the RITKConfig.cmake file is sourced to setup the
# location and configuration of RITK.  Please read this file, or
# RITKConfig.cmake.in from the RITK source tree for the full list of
# definitions.  Of particular interest is RITK_USE_FILE, a CMake source file
# that can be included to set the include directories, library directories,
# and preprocessor macros.  In addition to the variables read from
# RITKConfig.cmake, this find module also defines
#
#  RITK_FOUND    - Whether RITK was found. If this is true, RITK_DIR is okay.
#
#  RITK_DIR      - The directory containing RITKConfig.cmake.  
#                  This is the only cache entry.
#  RITK_USE_FILE - The full path to the UseRITK.cmake file.
#

SET(RITK_DIR_STRING "directory containing RITKConfig.cmake.")

# Search only if the location is not already known.
IF(NOT RITK_DIR)
  # Get the system search path as a list.
  IF(UNIX)
    STRING(REGEX MATCHALL "[^:]+" RITK_DIR_SEARCH1 "$ENV{PATH}")
  ELSE(UNIX)
    STRING(REGEX REPLACE "\\\\" "/" RITK_DIR_SEARCH1 "$ENV{PATH}")
  ENDIF(UNIX)
  STRING(REGEX REPLACE "/;" ";" RITK_DIR_SEARCH2 ${RITK_DIR_SEARCH1})

  # Construct a set of paths relative to the system search path.
  SET(RITK_DIR_SEARCH "")
  FOREACH(dir ${RITK_DIR_SEARCH2})
    SET(RITK_DIR_SEARCH ${RITK_DIR_SEARCH} "${dir}/..")
  ENDFOREACH(dir)

  #
  # Look for an installation or build tree.
  #
  FIND_PATH(RITK_DIR RITKConfig.cmake
    # Look for an environment variable RITK_DIR.
    $ENV{RITK_DIR}

    # Look in places relative to the system executable search path.
    ${RITK_DIR_SEARCH}

    # Look in standard UNIX install locations.
    /usr/local/lib/RITK
    /usr/lib/RITK

    # Read from the CMakeSetup registry entries.  It is likely that
    # RITK will have been recently built.
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild1]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild2]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild3]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild4]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild5]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild6]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild7]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild8]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild9]
    [HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild10]

    # Help the user find it if we cannot.
    DOC "The ${RITK_DIR_STRING}"
  )
ENDIF(NOT RITK_DIR)

# If RITK was found, load the configuration file to get the rest of the settings.
IF(RITK_DIR)
  SET(RITK_FOUND "1")
  MESSAGE(FATAL_ERROR "RITK found...")
  INCLUDE("${RITK_DIR}/RITKConfig.cmake")
ELSE(RITK_DIR)
  SET(RITK_FOUND "0")
  IF(RITK_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "Please set RITK_DIR to the ${RITK_DIR_STRING}")
  ENDIF(RITK_FIND_REQUIRED)
ENDIF(RITK_DIR)
