# Find ARToolKit
#
# This module defines
#  ARToolKit_FOUND
#  ARToolKit_INCLUDE_DIRS
#  ARToolKit_LIBRARIES
#
# Copyright (c) 2016 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
#

INCLUDE (CheckIncludeFiles)

include(FindPkgMacros)
create_search_paths(ARToolKit)

# On a new cmake run, we do not need to be verbose
IF(ARToolKit_INCLUDE_DIRS AND ARToolKit_LIBRARIES)
	SET(ARToolKit_FIND_QUIETLY TRUE)
ENDIF()

# If ARToolKit_ROOT was defined in the environment, use it.
if (NOT ARToolKit_ROOT)
  if(NOT "$ENV{ARToolKit_ROOT}" STREQUAL "")
    set(ARToolKit_ROOT $ENV{ARToolKit_ROOT})
  else()
    set(ARToolKit_ROOT ${ARToolKit_PREFIX_PATH})    
  endif()
endif()

# concat all the search paths
IF (ARToolKit_ROOT)
	SET(ARToolKit_INCLUDE_SEARCH_DIRS
	  ${ARToolKit_INCLUDE_SEARCH_DIRS}
	  ${ARToolKit_ROOT}/include
  )
  SET(ARToolKit_LIBRARY_SEARCH_RELEASE_DIRS
    ${ARToolKit_LIBRARY_SEARCH_DIRS}
    ${ARToolKit_ROOT}/lib/Release
    ${ARToolKit_ROOT}/lib
  )
  SET(ARToolKit_LIBRARY_SEARCH_DEBUG_DIRS
    ${ARToolKit_LIBRARY_SEARCH_DIRS}
    ${ARToolKit_ROOT}/lib/Debug
    ${ARToolKit_ROOT}/lib
  )
ENDIF()

# log message
IF (NOT ARToolKit_FIND_QUIETLY)
	MESSAGE(STATUS "Checking for ARToolKit library")
ENDIF()

# Search for header files
FIND_PATH(ARToolKit_INCLUDE_DIR AR/ar.h
  PATHS ${ARToolKit_INCLUDE_SEARCH_DIRS})

SET(ARToolKit_INCLUDE_DIRS ${ARToolKit_INCLUDE_DIR} CACHE STRING "Directory containing ARToolKit header files")


# Search for libraries files (release mode)
FIND_LIBRARY(ARToolKit_LIBRARY_AR_RELEASE libAR
  PATHS ${ARToolKit_LIBRARY_SEARCH_RELEASE_DIRS})

# Search for libraries files (debug mode)
FIND_LIBRARY(ARToolKit_LIBRARY_AR_DEBUG libARd libAR_d libAR
  PATHS ${ARToolKit_LIBRARY_SEARCH_DEBUG_DIRS})

SET(ARToolKit_AR_LIBRARY debug ${ARToolKit_LIBRARY_AR_DEBUG} optimized ${ARToolKit_LIBRARY_AR_RELEASE})

# Search for libraries files (release mode)
FIND_LIBRARY(ARToolKit_LIBRARY_AR2_RELEASE libAR2
  PATHS ${ARToolKit_LIBRARY_SEARCH_RELEASE_DIRS})

# Search for libraries files (debug mode)
FIND_LIBRARY(ARToolKit_LIBRARY_AR2_DEBUG libAR2d libAR2_d libAR2
  PATHS ${ARToolKit_LIBRARY_SEARCH_DEBUG_DIRS})

SET(ARToolKit_AR2_LIBRARY debug ${ARToolKit_LIBRARY_AR2_DEBUG} optimized ${ARToolKit_LIBRARY_AR2_RELEASE})

# Search for libraries files (release mode)
FIND_LIBRARY(ARToolKit_LIBRARY_ARICP_RELEASE libARICP
  PATHS ${ARToolKit_LIBRARY_SEARCH_RELEASE_DIRS})

# Search for libraries files (debug mode)
FIND_LIBRARY(ARToolKit_LIBRARY_ARICP_DEBUG libARICPd libARICP_d libARICP
  PATHS ${ARToolKit_LIBRARY_SEARCH_DEBUG_DIRS})

SET(ARToolKit_ARICP_LIBRARY debug ${ARToolKit_LIBRARY_ARICP_DEBUG} optimized ${ARToolKit_LIBRARY_ARICP_RELEASE})

# Search for libraries files (release mode)
FIND_LIBRARY(ARToolKit_LIBRARY_KPM_RELEASE libKPM
  PATHS ${ARToolKit_LIBRARY_SEARCH_RELEASE_DIRS})

# Search for libraries files (debug mode)
FIND_LIBRARY(ARToolKit_LIBRARY_KPM_DEBUG libKPMd libKPM_d libKPM
  PATHS ${ARToolKit_LIBRARY_SEARCH_DEBUG_DIRS})

SET(ARToolKit_KPM_LIBRARY debug ${ARToolKit_LIBRARY_KPM_DEBUG} optimized ${ARToolKit_LIBRARY_KPM_RELEASE})

# Search for libraries files (release mode)
FIND_LIBRARY(ARToolKit_ARUTIL_RELEASE libARUtil
  PATHS ${ARToolKit_LIBRARY_SEARCH_RELEASE_DIRS})

# Search for libraries files (debug mode)
FIND_LIBRARY(ARToolKit_ARUTIL_DEBUG libARUtild libARUtil_d libARUtil
  PATHS ${ARToolKit_LIBRARY_SEARCH_DEBUG_DIRS})

SET(ARToolKit_ARUTIL_LIBRARY debug ${ARToolKit_ARUTIL_DEBUG} optimized ${ARToolKit_ARUTIL_RELEASE})

SET(ARToolKit_LIBRARIES ${ARToolKit_KPM_LIBRARY} ${ARToolKit_AR2_LIBRARY} ${ARToolKit_AR_LIBRARY} ${ARToolKit_ARICP_LIBRARY} ${ARToolKit_ARUTIL_LIBRARY} CACHE STRING "ARToolKit libraries files")


IF(ARToolKit_INCLUDE_DIR AND ARToolKit_LIBRARIES)
	SET(ARToolKit_FOUND TRUE)
ENDIF()

# Hide those variables in GUI
SET(ARToolKit_INCLUDE_DIR ${ARToolKit_INCLUDE_DIR} CACHE INTERNAL "")
SET(ARToolKit_LIBRARY_RELEASE ${ARToolKit_LIBRARY_RELEASE} CACHE INTERNAL "")
SET(ARToolKit_LIBRARY_DEBUG ${ARToolKit_LIBRARY_DEBUG} CACHE INTERNAL "")
SET(ARToolKit_LIBRARY ${ARToolKit_LIBRARY} CACHE INTERNAL "")

# log find result
IF(ARToolKit_FOUND)
	IF(NOT ARToolKit_FIND_QUIETLY)
		MESSAGE(STATUS "  libraries: ${ARToolKit_LIBRARIES}")
		MESSAGE(STATUS "  includes: ${ARToolKit_INCLUDE_DIRS}")
	ENDIF()
ELSE(ARToolKit_FOUND)
	IF(NOT ARToolKit_LIBRARIES)
		MESSAGE(STATUS, "ARToolKit library or one of it dependencies could not be found.")
	ENDIF()
	IF(NOT ARToolKit_INCLUDE_DIRS)
		MESSAGE(STATUS "ARToolKit include files could not be found.")
	ENDIF()
ENDIF(ARToolKit_FOUND)