# Searches for an installation of the OPT++ library (http://software.sandia.gov/opt++/)
#
# Defines:
#
#   OPTPP_FOUND          True if OPT++ was found, else false
#   OPTPP_LIBRARIES      Libraries to link
#   OPTPP_INCLUDE_DIRS   The directories containing the header files
#   OPTPP_LDFLAGS        Extra linker flags
#
# To specify an additional directory to search, set OPTPP_ROOT.
#
# Author: Siddhartha Chaudhuri, 2011
#

SET(OPTPP_FOUND FALSE)

IF(NOT OPTPP_INCLUDE_DIRS)
  # Look for the OPTPP header, first in the user-specified location and then in the system locations
  SET(OPTPP_INCLUDE_DOC "The directory containing the OPT++ include file Opt.h")
  FIND_PATH(OPTPP_INCLUDE_DIRS
            NAMES Opt.h
            PATHS ${OPTPP_ROOT}
            PATH_SUFFIXES "include" "include/OPT++" "include/OPTPP" "include/opt++" "include/optpp"
            DOC ${OPTPP_INCLUDE_DOC}
            NO_DEFAULT_PATH)
  IF(NOT OPTPP_INCLUDE_DIRS)  # now look in system locations
    FIND_PATH(OPTPP_INCLUDE_DIRS NAMES Opt.h PATH_SUFFIXES "OPT++" "OPTPP" "opt++" "optpp" DOC ${OPTPP_INCLUDE_DOC})
  ENDIF(NOT OPTPP_INCLUDE_DIRS)
ENDIF(NOT OPTPP_INCLUDE_DIRS)

# Only look for the library file in the immediate neighbourhood of the include directory
IF(OPTPP_INCLUDE_DIRS)
  SET(OPTPP_LIBRARY_DIRS ${OPTPP_INCLUDE_DIRS})

  IF("${OPTPP_LIBRARY_DIRS}" MATCHES "/(OPT|opt)([+][+]|pp)$")
    # Strip off the trailing "/OPT++" from the path
    GET_FILENAME_COMPONENT(OPTPP_LIBRARY_DIRS ${OPTPP_LIBRARY_DIRS} PATH)
  ENDIF("${OPTPP_LIBRARY_DIRS}" MATCHES "/(OPT|opt)([+][+]|pp)$")

  IF("${OPTPP_LIBRARY_DIRS}" MATCHES "/include$")
    # Strip off the trailing "/include" from the path
    GET_FILENAME_COMPONENT(OPTPP_LIBRARY_DIRS ${OPTPP_LIBRARY_DIRS} PATH)
  ENDIF("${OPTPP_LIBRARY_DIRS}" MATCHES "/include$")

  # Look for libopt
  FIND_LIBRARY(OPTPP_DEBUG_LIBRARY NAMES opt_d libopt_d optd liboptd PATH_SUFFIXES "" Debug
               PATHS ${OPTPP_LIBRARY_DIRS} ${OPTPP_LIBRARY_DIRS}/lib NO_DEFAULT_PATH)

  FIND_LIBRARY(OPTPP_RELEASE_LIBRARY NAMES opt libopt PATH_SUFFIXES "" Release
               PATHS ${OPTPP_LIBRARY_DIRS} ${OPTPP_LIBRARY_DIRS}/lib NO_DEFAULT_PATH)

  SET(OPTPP_LIBRARIES )
  IF(OPTPP_DEBUG_LIBRARY AND OPTPP_RELEASE_LIBRARY)
    SET(OPTPP_LIBRARIES debug ${OPTPP_DEBUG_LIBRARY} optimized ${OPTPP_RELEASE_LIBRARY})
  ELSEIF(OPTPP_DEBUG_LIBRARY)
    SET(OPTPP_LIBRARIES ${OPTPP_DEBUG_LIBRARY})
  ELSEIF(OPTPP_RELEASE_LIBRARY)
    SET(OPTPP_LIBRARIES ${OPTPP_RELEASE_LIBRARY})
  ENDIF(OPTPP_DEBUG_LIBRARY AND OPTPP_RELEASE_LIBRARY)

  # Look for libnewmat
  IF(OPTPP_LIBRARIES)
    FIND_LIBRARY(OPTPP_NEWMAT_DEBUG_LIBRARY NAMES newmat_d libnewmat_d newmatd libnewmatd PATH_SUFFIXES "" Debug
                 PATHS ${OPTPP_LIBRARY_DIRS} ${OPTPP_LIBRARY_DIRS}/lib NO_DEFAULT_PATH)
  
    FIND_LIBRARY(OPTPP_NEWMAT_RELEASE_LIBRARY NAMES newmat libnewmat PATH_SUFFIXES "" Release
                 PATHS ${OPTPP_LIBRARY_DIRS} ${OPTPP_LIBRARY_DIRS}/lib NO_DEFAULT_PATH)

    IF(OPTPP_NEWMAT_DEBUG_LIBRARY AND OPTPP_NEWMAT_RELEASE_LIBRARY)
      SET(OPTPP_LIBRARIES ${OPTPP_LIBRARIES} debug ${OPTPP_NEWMAT_DEBUG_LIBRARY} optimized ${OPTPP_NEWMAT_RELEASE_LIBRARY})
    ELSEIF(OPTPP_NEWMAT_DEBUG_LIBRARY)
      SET(OPTPP_LIBRARIES ${OPTPP_LIBRARIES} ${OPTPP_NEWMAT_DEBUG_LIBRARY})
    ELSEIF(OPTPP_NEWMAT_RELEASE_LIBRARY)
      SET(OPTPP_LIBRARIES ${OPTPP_LIBRARIES} ${OPTPP_NEWMAT_RELEASE_LIBRARY})
    ENDIF(OPTPP_NEWMAT_DEBUG_LIBRARY AND OPTPP_NEWMAT_RELEASE_LIBRARY)
  ENDIF(OPTPP_LIBRARIES)

  # Look for BLAS
  IF(OPTPP_LIBRARIES)
    FIND_PACKAGE(BLAS)
    IF(BLAS_FOUND)
      SET(OPTPP_LIBRARIES ${OPTPP_LIBRARIES} ${BLAS_LIBRARIES})
      SET(OPTPP_LDFLAGS ${BLAS_LINKER_FLAGS})
    ELSE(BLAS_FOUND)
      MESSAGE(STATUS "OPT++: BLAS library not found")
      SET(OPTPP_LIBRARIES )
    ENDIF(BLAS_FOUND)
  ENDIF(OPTPP_LIBRARIES)

  IF(OPTPP_LIBRARIES)
    SET(OPTPP_FOUND TRUE)
  ENDIF(OPTPP_LIBRARIES)
ENDIF(OPTPP_INCLUDE_DIRS)

IF(OPTPP_FOUND)
  IF(NOT OPTPP_FIND_QUIETLY)
    MESSAGE(STATUS "Found OPT++: headers at ${OPTPP_INCLUDE_DIRS}, libraries at ${OPTPP_LIBRARIES}")
  ENDIF(NOT OPTPP_FIND_QUIETLY)
ELSE(OPTPP_FOUND)
  IF(OPTPP_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "OPT++ not found")
  ENDIF(OPTPP_FIND_REQUIRED)
ENDIF(OPTPP_FOUND)
