package com.mooo.aimmac23.node.jna;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.seleniumhq.jetty7.util.log.Log;

import com.mooo.aimmac23.node.servlet.VideoRecordingControlServlet;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

public class JnaLibraryLoader {
	
	private static final Logger log = Logger.getLogger(JnaLibraryLoader.class.getName());

	
	static private LibVPX libVPX;
	static private EncoderInterface encoder;
	static private YUVLib yuvLib;
	
	private static void addNativePath(String path) {
		NativeLibrary.addSearchPath("vpx", path);
		NativeLibrary.addSearchPath("yuv", path);
		NativeLibrary.addSearchPath("interface", path);
	}
	
	private static void extractJNABinariesIfAvailable() {
		InputStream zipStream = JnaLibraryLoader.class.getClassLoader().getResourceAsStream("native.zip");
		if(zipStream == null) {
			return;
		}
		
		String targetDirectory = System.getProperty("java.io.tmpdir") + File.separator + "nativeCode" + File.separator;
		
		File target = new File(targetDirectory);
		if(target.exists()) {
			target.delete();
		}
		
		target.mkdir();
		
		try {
			ZipInputStream zipInputStream = new ZipInputStream(zipStream);
			ZipEntry entry = null;
			while((entry = zipInputStream.getNextEntry()) != null){
				FileOutputStream outputStream = new FileOutputStream(targetDirectory + entry.getName());
				byte[] buffer = new byte[1024];
				int size = 0;
                while((size = zipInputStream.read(buffer)) != -1){
                	outputStream.write(buffer, 0 , size);
                }
                outputStream.flush();
                outputStream.close();
			}
			zipInputStream.close();
			
			addNativePath(targetDirectory);
				
		} catch(IOException e) {
			log.info("Caught IOException");
			e.printStackTrace();
		}
		
		
		
	}
	
	public static void init() {
		
		extractJNABinariesIfAvailable();
		
		// this makes things work in Eclipse
		String classpath = System.getProperty("java.class.path");
		String[] classpathEntries = classpath.split(File.pathSeparator);
		for(String entry : classpathEntries) {
			addNativePath(entry);
		}
		
		libVPX = (LibVPX) Native.loadLibrary("vpx", LibVPX.class);
		yuvLib = (YUVLib) Native.loadLibrary("yuv", YUVLib.class);
		encoder = (EncoderInterface) Native.loadLibrary("interface", EncoderInterface.class);
	}
	
	public static EncoderInterface getEncoder() {
		return encoder;
	}
	
	public static YUVLib getYuvLib() {
		return yuvLib;
	}
	
	public static LibVPX getLibVPX() {
		return libVPX;
	}

}

@@ -246,120 +246,8 @@
 m;%0A%0A
-import org.seleniumhq.jetty7.util.log.Log;%0A%0Aimport com.mooo.aimmac23.node.servlet.VideoRecordingControlServlet;%0A
 impo
@@ -269,16 +269,16 @@
 Native;%0A
+
 import c
@@ -1021,17 +1021,47 @@
 tiveCode
-%22
+-%22 + System.currentTimeMillis()
  + File.
@@ -1743,16 +1743,38 @@
 lose();%0A
+%09%09%09zipStream.close();%0A
 %09%09%09%0A%09%09%09a
@@ -1899,21 +1899,12 @@
 ();%0A
+
 %09%09%7D%0A
-%09%09%0A%09%09%0A%09%09%0A
 %09%7D%0A%09

Add a fix for a segfault, which may have involved truncation of native libraries or something