string(REPLACE "." ";" GLEW_VERSION_LIST ${ADD_PACKAGE_VERSION})
list(GET GLEW_VERSION_LIST 0 GLEW_VERSION_MAJOR)
list(GET GLEW_VERSION_LIST 1 GLEW_VERSION_MINOR)
list(GET GLEW_VERSION_LIST 2 GLEW_VERSION_PATCH)

if(WIN32)
	set(GLEW_ZIP_FOLDER "glew-${ADD_PACKAGE_VERSION}")
	set(GLEW_ZIP_FILE "glew-${ADD_PACKAGE_VERSION}-win32.zip")
	set(GLEW_ZIP_URL "https://sourceforge.net/projects/glew/files/glew/${ADD_PACKAGE_VERSION}/${GLEW_ZIP_FILE}/download")

	set(GLEW_LIB "glew32.lib")
	set(GLEW_LOCAL_LIB_DIR ${DEPENDENCY_PACKAGE_INSTALL_DIR}/lib)
	
	#check if library exists, if not download, unzip and build as needed
	if(NOT EXISTS ${GLEW_LOCAL_LIB_DIR}/${GLEW_LIB})

		set(GLEW_LOCAL_ZIP ${DEPENDENCY_PACKAGE_INSTALL_DIR}/${GLEW_ZIP_FILE})
		#if no zip, download
		if(NOT EXISTS ${GLEW_LOCAL_ZIP})
			message(STATUS "Downloading GLEW Precompiled")
			file(DOWNLOAD ${GLEW_ZIP_URL} ${GLEW_LOCAL_ZIP})
		endif()

		message(STATUS "Unzipping GLEW Precompiled ${GLEW_ZIP_FOLDER}")

		set(GLEW_ZIP_INCLUDE "${GLEW_LOCAL_ZIP}/${GLEW_ZIP_FOLDER}/include/")
		set(GLEW_ZIP_LIB "${GLEW_LOCAL_ZIP}/${GLEW_ZIP_FOLDER}/lib/Release/x64/")
		set(GLEW_ZIP_BIN "${GLEW_LOCAL_ZIP}/${GLEW_ZIP_FOLDER}/bin")

		set(GLEW_INCLUDE_DIR "${DEPENDENCY_PACKAGE_INSTALL_DIR}/include")
		set(GLEW_LIB_DIR "${DEPENDENCY_PACKAGE_INSTALL_DIR}/lib")
		set(GLEW_BIN_DIR "${DEPENDENCY_PACKAGE_INSTALL_DIR}/bin")

		#command only works with .Net 4.5+
		execute_process(
			COMMAND powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('${GLEW_LOCAL_ZIP}', '${DEPENDENCY_PACKAGE_INSTALL_DIR}'); }"
			WORKING_DIRECTORY ${DEPENDENCY_PACKAGE_INSTALL_DIR}
		)

		if(EXISTS ${DEPENDENCY_PACKAGE_INSTALL_DIR}/${GLEW_ZIP_FOLDER})
			file(RENAME ${DEPENDENCY_PACKAGE_INSTALL_DIR}/${GLEW_ZIP_FOLDER}/include ${DEPENDENCY_PACKAGE_INSTALL_DIR}/include)
			file(RENAME ${DEPENDENCY_PACKAGE_INSTALL_DIR}/${GLEW_ZIP_FOLDER}/lib/Release/x64 ${DEPENDENCY_PACKAGE_INSTALL_DIR}/lib)
			file(RENAME ${DEPENDENCY_PACKAGE_INSTALL_DIR}/${GLEW_ZIP_FOLDER}/bin/Release/x64 ${DEPENDENCY_PACKAGE_INSTALL_DIR}/bin)
			file(REMOVE_RECURSE ${DEPENDENCY_PACKAGE_INSTALL_DIR}/${GLEW_ZIP_FOLDER})
		endif()

		if(EXISTS ${GLEW_LOCAL_ZIP})
			file(REMOVE ${GLEW_LOCAL_ZIP})
		endif()
	endif()
		
	set(${DEPENDENCY_PACKAGE_NAME}_INCLUDE_DIRS "${DEPENDENCY_PACKAGE_INSTALL_DIR}/include" PARENT_SCOPE)
	set(${DEPENDENCY_PACKAGE_NAME}_BIN_DIRS "${DEPENDENCY_PACKAGE_INSTALL_DIR}/bin" PARENT_SCOPE)
	set(${DEPENDENCY_PACKAGE_NAME}_LIBRARIES ${DEPENDENCY_PACKAGE_INSTALL_DIR}/lib/${GLEW_LIB} PARENT_SCOPE)

	message(STATUS "${DEPENDENCY_PACKAGE_NAME} include: ${${DEPENDENCY_PACKAGE_NAME}_INCLUDE_DIRS}")
	
endif()