
SET(LUA_SEARCH_PATHS
	${POLYCODE_RELEASE_DIR}/Framework/Core/Dependencies/lib
	${POLYCODE_RELEASE_DIR}/Framework/Core/Dependencies/include
	${POLYCODE_RELEASE_DIR}/Framework/Core/Dependencies/include/lua5.1
	${POLYCODE_RELEASE_DIR}/Framework/Modules/Dependencies/lib
	${POLYCODE_RELEASE_DIR}/Framework/Tools/Dependencies/lib
)

SET(MSVC_YEAR_NAME)
IF (MSVC_VERSION GREATER 1599)		# >= 1600
	SET(MSVC_YEAR_NAME VS2010)
ELSEIF(MSVC_VERSION GREATER 1499)	# >= 1500
	SET(MSVC_YEAR_NAME VS2008)
ELSEIF(MSVC_VERSION GREATER 1399)	# >= 1400
	SET(MSVC_YEAR_NAME VS2005)
ELSEIF(MSVC_VERSION GREATER 1299)	# >= 1300
	SET(MSVC_YEAR_NAME VS2003)
ELSEIF(MSVC_VERSION GREATER 1199)	# >= 1200
	SET(MSVC_YEAR_NAME VS6)
ENDIF()

FIND_PATH(LUA_INCLUDE_DIR
	NAMES lua.h 
	HINTS
	$ENV{LUADIR}
	$ENV{LUA_PATH}
	PATH_SUFFIXES include include/lua5.1 lua5.1
	PATHS ${LUA_SEARCH_PATHS}
)

FIND_LIBRARY(LUA_LIBRARY 
	NAMES lua5.1 liblua5.1
	HINTS
	NO_DEFAULT_PATH
	NO_CMAKE_ENVIRONMENT_PATH
	NO_CMAKE_SYSTEM_PATH
	NO_SYSTEM_ENVIRONMENT_PATH
	NO_CMAKE_PATH
	CMAKE_FIND_FRAMEWORK NEVER
	$ENV{LUADIR}
	$ENV{LUA_PATH}
	PATH_SUFFIXES lib lib64 win32/Dynamic_Release "Win32/${MSVC_YEAR_NAME}/x64/Release" "Win32/${MSVC_YEAR_NAME}/Win32/Release"
	PATHS ${LUA_SEARCH_PATHS}
)

# First search for d-suffixed libs
FIND_LIBRARY(LUA_LIBRARY_DEBUG 
	NAMES lua5.1d liblua5.1d
	HINTS
	NO_DEFAULT_PATH
	NO_CMAKE_ENVIRONMENT_PATH
	NO_CMAKE_SYSTEM_PATH
	NO_SYSTEM_ENVIRONMENT_PATH
	NO_CMAKE_PATH
	CMAKE_FIND_FRAMEWORK NEVER
	$ENV{LUADIR}
	$ENV{LUA_PATH}
	PATH_SUFFIXES lib lib64 win32/Dynamic_Debug "Win32/${MSVC_YEAR_NAME}/x64/Debug" "Win32/${MSVC_YEAR_NAME}/Win32/Debug"
	PATHS ${LUA_SEARCH_PATHS}
)

IF(NOT LUA_LIBRARY_DEBUG)
	# Then search for non suffixed libs if necessary, but only in debug dirs
	FIND_LIBRARY(LUA_LIBRARY_DEBUG 
		NAMES lua liblua libliblua
		HINTS
		NO_DEFAULT_PATH
		NO_CMAKE_ENVIRONMENT_PATH
		NO_CMAKE_SYSTEM_PATH
		NO_SYSTEM_ENVIRONMENT_PATH
		NO_CMAKE_PATH
		CMAKE_FIND_FRAMEWORK NEVER
		$ENV{LUADIR}
		$ENV{LUA_PATH}
		PATH_SUFFIXES win32/Dynamic_Debug "Win32/${MSVC_YEAR_NAME}/x64/Debug" "Win32/${MSVC_YEAR_NAME}/Win32/Debug"
		PATHS ${LUA_SEARCH_PATHS}
	)
ENDIF()


IF(LUA_LIBRARY)
	IF(LUA_LIBRARY_DEBUG)
		SET(LUA_LIBRARIES optimized "${LUA_LIBRARY}" debug "${LUA_LIBRARY_DEBUG}")
	ELSE()
		SET(LUA_LIBRARIES "${LUA_LIBRARY}")		# Could add "general" keyword, but it is optional
	ENDIF()
ENDIF()

# handle the QUIETLY and REQUIRED arguments and set XXX_FOUND to TRUE if all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LUA DEFAULT_MSG LUA_LIBRARIES LUA_INCLUDE_DIR)
