#
# Try to find SPPAR library and include path.
# Once done this will define
#
# SPPAR_FOUND
# SPPAR_INCLUDE_DIR
# SPPAR_LIBRARIES
# SPPAR_ROOT
#

set(FIND_SPPAR_PATHS
    ${SPPAR_ROOT}
    $ENV{SPPAR_ROOT}
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt)

if(SPPAR_ROOT)
	SET(SPPAR_INCLUDE_DIR "${SPPAR_ROOT}/include")
#	SET(SPPAR_LIBRARIES "${SPPAR_ROOT}/lib/libSPPAR.a")
	SET(SPPAR_FOUND 1)
endif(SPPAR_ROOT)

find_path(SPPAR_INCLUDE_DIR include/SPPAR.hpp
          PATH_SUFFIXES include
          PATHS ${FIND_SPPAR_PATHS})

#find_library(SPPAR_LIBRARY
#		NAMES SPPAR
#		PATH_SUFFIXES lib
#		PATHS ${FIND_SPPAR_PATHS})

#check the version number
set(SPPAR_VERSION_OK TRUE)
if(SPPAR_FIND_VERSION AND SPPAR_INCLUDE_DIR)
    set(SPPAR_CONFIG_HPP_INPUT "${SPPAR_INCLUDE_DIR}/SPPAR/Config.hpp")
    FILE(READ "${SPPAR_CONFIG_HPP_INPUT}" SPPAR_CONFIG_HPP_CONTENTS)
    STRING(REGEX REPLACE ".*#define SPPAR_VERSION_MAJOR ([0-9]+).*" "\\1" SPPAR_VERSION_MAJOR "${SPPAR_CONFIG_HPP_CONTENTS}")
    STRING(REGEX REPLACE ".*#define SPPAR_VERSION_MINOR ([0-9]+).*" "\\1" SPPAR_VERSION_MINOR "${SPPAR_CONFIG_HPP_CONTENTS}")
    STRING(REGEX REPLACE ".*#define SPPAR_VERSION_PATCH ([0-9]+).*" "\\1" SPPAR_VERSION_PATCH "${SPPAR_CONFIG_HPP_CONTENTS}")
    if (NOT "${SPPAR_VERSION_PATCH}" MATCHES "^[0-9]+$")
        set(SPPAR_VERSION_PATCH 0)
    endif()
    math(EXPR SPPAR_REQUESTED_VERSION "${SPPAR_FIND_VERSION_MAJOR} * 10000 + ${SPPAR_FIND_VERSION_MINOR} * 100 + ${SPPAR_FIND_VERSION_PATCH}")
    if (SPPAR_VERSION_MAJOR)
        math(EXPR SPPAR_VERSION "${SPPAR_VERSION_MAJOR} * 10000 + ${SPPAR_VERSION_MINOR} * 100 + ${SPPAR_VERSION_PATCH}")
        if(SPPAR_VERSION LESS SPPAR_REQUESTED_VERSION)
            set(SPPAR_VERSION_OK FALSE)
        endif()
    else()
        # SPPAR version is < 2.0
        if(SPPAR_REQUESTED_VERSION GREATER 10900)
            set(SPPAR_VERSION_OK FALSE)
            set(SPPAR_VERSION_MAJOR 1)
            set(SPPAR_VERSION_MINOR x)
            set(SPPAR_VERSION_PATCH x)
        endif()
    endif()
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SPPAR "" SPPAR_INCLUDE_DIR)

set(SPPAR_INCLUDE_DIR "${SPPAR_INCLUDE_DIR}")
#set(SPPAR_LIBRARIES "${SPPAR_LIBRARY}")

if(NOT SPPAR_VERSION_OK)
	set(FIND_SPPAR_ERROR "SPPAR found but version too low (requested: ${SPPAR_FIND_VERSION}}, found: ${SPPAR_VERSION_MAJOR}.${SPPAR_VERSION_MINOR}.${SPPAR_VERSION_PATCH})")
	set(SPPAR_FOUND FALSE)
elseif(NOT SPPAR_FOUND)
	set(FIND_SPPAR_ERROR "Could NOT find SPPAR")
	if(SPPAR_FIND_REQUIRED)
		message(FATAL_ERROR ${FIND_SPPAR_ERROR})
	endif()
endif()
if(SPPAR_FOUND)
	message(STATUS "Found SPPAR ${SPPAR_VERSION_MAJOR}.${SPPAR_VERSION_MINOR}.${SPPAR_VERSION_PATCH} in ${SPPAR_INCLUDE_DIR}")
endif(SPPAR_FOUND)
