set(_ENV_BOOST_ROOT "$ENV{BOOST_ROOT}")
if(NOT Boost_ROOT_DIR AND _ENV_BOOST_ROOT)
  set(Boost_ROOT_DIR "$ENV{BOOST_ROOT}")
endif(NOT Boost_ROOT_DIR AND _ENV_BOOST_ROOT)

if(NOT Boost_ROOT_DIR)
  set(_PATHS "")
  set(_BOOST_PATHS "")
  if(ROKKO_SOLVER_ROOT)
    list(APPEND _PATHS ${ROKKO_SOLVER_ROOT})
  endif(ROKKO_SOLVER_ROOT)
  list(APPEND _PATHS ${CMAKE_INSTALL_PREFIX} /opt/rokko /opt/local /opt /opt/alps /opt/nano/alps "$ENV{HOME}/opt/rokko" "$ENV{HOME}/opt")
  foreach(D0 ${_PATHS})
    file(GLOB D1 ${D0}/boost*)
    if(D1)
      list(APPEND _BOOST_PATHS ${D1})
    endif(D1)
  endforeach(D0 ${_PATHS})
  find_path(Boost_ROOT_DIR "libs/program_options/src/cmdline.cpp" PATHS ${_BOOST_PATHS})
endif (NOT Boost_ROOT_DIR)

if(Boost_ROOT_DIR)
  set(Boost_INCLUDE_DIR ${Boost_ROOT_DIR} CACHE PATH "Boost Include Directory")
  set(Boost_SOURCE_DIR ${Boost_ROOT_DIR} CACHE PATH "Boost Source Directory")
endif(Boost_ROOT_DIR)

# check Boost version (from Modules/FindBoost.cmake)
if(Boost_INCLUDE_DIR)
  set(_boost_VERSION 0)
  set(_boost_LIB_VERSION "")
  file(READ "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS)
  string(REGEX REPLACE ".*#define BOOST_VERSION ([0-9]+).*" "\\1" _boost_VERSION "${_boost_VERSION_HPP_CONTENTS}")
  string(REGEX REPLACE ".*#define BOOST_LIB_VERSION \"([0-9_]+)\".*" "\\1" _boost_LIB_VERSION "${_boost_VERSION_HPP_CONTENTS}")
  set(Boost_LIB_VERSION ${_boost_LIB_VERSION} CACHE INTERNAL "The library version string for boost libraries")
  set(Boost_VERSION ${_boost_VERSION} CACHE INTERNAL "The version number for boost libraries")
  MATH(EXPR Boost_MAJOR_VERSION "${Boost_VERSION} / 100000")
  MATH(EXPR Boost_MINOR_VERSION "${Boost_VERSION} / 100 % 1000")
  MATH(EXPR Boost_SUBMINOR_VERSION "${Boost_VERSION} % 100")
endif(Boost_INCLUDE_DIR)

if(Boost_ROOT_DIR)
  message(STATUS "Found Boost Source: ${Boost_ROOT_DIR}")
  message(STATUS "Boost Version: ${Boost_MAJOR_VERSION}_${Boost_MINOR_VERSION}_${Boost_SUBMINOR_VERSION}")
  if(Boost_MAJOR_VERSION LESS 1 OR Boost_MINOR_VERSION LESS 48)
    message(FATAL_ERROR "Boost library version is too old, boost chrono requires boost >= 1.48.0")
  endif(Boost_MAJOR_VERSION LESS 1 OR Boost_MINOR_VERSION LESS 48)
else(Boost_ROOT_DIR)
  find_package(Boost 1.48.0 COMPONENTS system unit_test_framework)
endif(Boost_ROOT_DIR)

# Avoid auto link of Boost library
add_definitions(-DBOOST_ALL_NO_LIB=1)
if(BUILD_SHARED_LIBS)
  add_definitions(-DBOOST_ALL_DYN_LINK=1)
else(BUILD_SHARED_LIBS)
  add_definitions(-DBOOST_ALL_STATIC_LINK=1)
endif(BUILD_SHARED_LIBS)

mark_as_advanced(Boost_INCLUDE_DIR)
mark_as_advanced(Boost_SOURCE_DIR)
