# Find VRPN
#
# This module defines
#  VRPN_FOUND
#  VRPN_INCLUDE_DIRS
#  VRPN_LIBRARIES
#
# Copyright (c) 2012 I-maginer
# 
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA, or go to
# http://www.gnu.org/copyleft/lesser.txt
#

# On a new cmake run, we do not need to be verbose
IF(VRPN_INCLUDE_DIR AND VRPN_LIBRARY)
	SET(VRPN_FIND_QUIETLY TRUE)
ENDIF()

include(FindPkgMacros)
findpkg_begin(VRPN)

# construct search paths
set(VRPN_PREFIX_PATH
    ${SCOLDEPS_SOURCE_DIR}/VRPN/VRPN
    ${SCOLDEPS_LIB_DIRECTORY})
create_search_paths(VRPN)

# redo search if prefix path changed
clear_if_changed(VRPN_PREFIX_PATH
  VRPN_LIBRARY_FWK
  VRPN_LIBRARY_REL
  VRPN_LIBRARY_DBG
  VRPN_INCLUDE_DIR
)

set(VRPN_LIBRARY_NAMES vrpn)
get_debug_names(VRPN_LIBRARY_NAMES)

set(VRPN_QUAT_LIBRARY_NAMES quat)
get_debug_names(VRPN_QUAT_LIBRARY_NAMES)

use_pkgconfig(VRPN_PKGC VRPN)
findpkg_framework(VRPN)

# log message
IF (NOT VRPN_FIND_QUIETLY)
	MESSAGE(STATUS "Checking for VRPN library")
ENDIF()

find_path(VRPN_INCLUDE_DIR NAMES vrpn_Tracker.h HINTS ${VRPN_INC_SEARCH_PATH} ${VRPN_PKGC_INCLUDE_DIRS} PATH_SUFFIXES VRPN)
find_library(VRPN_LIBRARY_REL NAMES ${VRPN_LIBRARY_NAMES} HINTS ${VRPN_LIB_SEARCH_PATH} ${VRPN_PKGC_LIBRARY_DIRS} PATH_SUFFIXES "" Release RelWithDebInfo MinSizeRel)
find_library(VRPN_LIBRARY_DBG NAMES ${VRPN_LIBRARY_NAMES_DBG} HINTS ${VRPN_LIB_SEARCH_PATH} ${VRPN_PKGC_LIBRARY_DIRS} PATH_SUFFIXES "" Debug)
make_library_set(VRPN_LIBRARY)

find_library(VRPN_QUAT_LIBRARY_REL NAMES ${VRPN_QUAT_LIBRARY_NAMES} HINTS ${VRPN_LIB_SEARCH_PATH} ${VRPN_PKGC_LIBRARY_DIRS} PATH_SUFFIXES "" Release RelWithDebInfo MinSizeRel)
find_library(VRPN_QUAT_LIBRARY_DBG NAMES ${VRPN_QUAT_LIBRARY_NAMES} HINTS ${VRPN_LIB_SEARCH_PATH} ${VRPN_PKGC_LIBRARY_DIRS} PATH_SUFFIXES "" Debug)
make_library_set(VRPN_QUAT_LIBRARY)

add_parent_dir(VRPN_INCLUDE_DIRS VRPN_INCLUDE_DIR)
findpkg_finish(VRPN)

# Set global variables for VRPN
SET(VRPN_LIBRARIES ${VRPN_LIBRARY} ${VRPN_QUAT_LIBRARY} CACHE STRING "VRPN libraries files" FORCE)
SET(VRPN_INCLUDE_DIRS ${VRPN_INCLUDE_DIRS} CACHE STRING "VRPN libraries files" FORCE)

# Show variables in GUI
SET(VRPN_LIBRARY      ${VRPN_LIBRARY}       CACHE INTERNAL "")
SET(VRPN_QUAT_LIBRARY ${VRPN_QUAT_LIBRARY}  CACHE INTERNAL "")
SET(VRPN_INCLUDE_DIR  ${VRPN_INCLUDE_DIR}   CACHE INTERNAL "")

IF(VRPN_INCLUDE_DIR AND VRPN_LIBRARY)
  SET(VRPN_FOUND TRUE)
ELSE ()
  SET(VRPN_FOUND FALSE)
ENDIF()

# log find result
IF(VRPN_FOUND)
  IF(NOT VRPN_FIND_QUIETLY)
    MESSAGE(STATUS "  libraries: ${VRPN_LIBRARIES}")
    MESSAGE(STATUS "  includes: ${VRPN_INCLUDE_DIRS}")
  ENDIF()
ELSE(VRPN_FOUND)
  IF(NOT VRPN_LIBRARIES)
    MESSAGE(STATUS "  VRPN library or one of it dependencies could not be found.")
  ENDIF()
  IF(NOT VRPN_INCLUDE_DIRS)
    MESSAGE(STATUS "  VRPN include files could not be found.")
  ENDIF()
ENDIF(VRPN_FOUND)