# Find Vuzix
#
# This module defines
#  Vuzix_FOUND
#  Vuzix_INCLUDE_DIRS
#  Vuzix_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(Vuzix_INCLUDE_DIR AND Vuzix_LIBRARY)
	SET(Vuzix_FIND_QUIETLY TRUE)
ENDIF()

# If Vuzix_ROOT was defined in the environment, use it.
if (NOT Vuzix_ROOT)
  if(NOT "$ENV{VUZIX_ROOT}" STREQUAL "")
  	set(Vuzix_ROOT $ENV{VUZIX_ROOT})
  else()
  	set(Vuzix_ROOT "$ENV{PROGRAMFILES(X86)}/Vuzix Corporation/VuzixSDK")
  endif()
endif()

# concat all the search paths
IF(Vuzix_ROOT)
	SET(Vuzix_INCLUDE_SEARCH_DIRS
	  ${Vuzix_INCLUDE_SEARCH_DIRS}
	  ${Vuzix_ROOT}/inc)
  IF(WIN32 AND (WIN64 OR (CMAKE_SIZEOF_VOID_P EQUAL 8)))
  SET(Vuzix_LIBRARY_SEARCH_DIRS 
    ${Vuzix_LIBRARY_SEARCH_DIRS}
    ${Vuzix_ROOT}/lib/x64)
  ELSE()
  SET(Vuzix_LIBRARY_SEARCH_DIRS 
    ${Vuzix_LIBRARY_SEARCH_DIRS}
    ${Vuzix_ROOT}/lib)
  ENDIF()
ENDIF()

# log message
IF (NOT Vuzix_FIND_QUIETLY)
	MESSAGE(STATUS "Checking for Vuzix library")
ENDIF()

# Search for header files
FIND_PATH(Vuzix_INCLUDE_DIR iWearSDK.h
  PATHS ${Vuzix_INCLUDE_SEARCH_DIRS})

# Search for libraries files
FIND_LIBRARY(Vuzix_IWEAR_LIBRARY iweardrv
  PATHS ${Vuzix_LIBRARY_SEARCH_DIRS})
FIND_LIBRARY(Vuzix_IWEARSTEREO_LIBRARY iwrstdrv
  PATHS ${Vuzix_LIBRARY_SEARCH_DIRS})

# Configure libraries
SET(Vuzix_INCLUDE_DIRS ${Vuzix_INCLUDE_DIR} CACHE STRING "Directory containing Vuzix header files")
SET(Vuzix_LIBRARIES ${Vuzix_IWEAR_LIBRARY} ${Vuzix_IWEARSTEREO_LIBRARY} CACHE STRING "Vuzix libraries files")

# Hide those variables in GUI
SET(Vuzix_INCLUDE_DIR ${Vuzix_INCLUDE_DIR} CACHE INTERNAL "")
SET(Vuzix_IWEAR_LIBRARY ${Vuzix_IWEAR_LIBRARY} CACHE INTERNAL "")
SET(Vuzix_IWEARSTEREO_LIBRARY ${Vuzix_IWEARSTEREO_LIBRARY} CACHE INTERNAL "")

IF(Vuzix_INCLUDE_DIR AND Vuzix_IWEAR_LIBRARY AND Vuzix_IWEARSTEREO_LIBRARY)
	SET(Vuzix_FOUND TRUE)
ENDIF()

# log find result
IF(Vuzix_FOUND)
	IF(NOT Vuzix_FIND_QUIETLY)
		MESSAGE(STATUS "  libraries: ${Vuzix_LIBRARIES}")
		MESSAGE(STATUS "  includes: ${Vuzix_INCLUDE_DIRS}")
	ENDIF()
ELSE(Vuzix_FOUND)
	IF(NOT Vuzix_LIBRARIES)
		MESSAGE(STATUS, "Vuzix library could not be found.")
	ENDIF()
	IF(NOT Vuzix_INCLUDE_DIRS)
		MESSAGE(STATUS "Vuzix include files could not be found.")
	ENDIF()
ENDIF(Vuzix_FOUND)