/**
 * This package provides classes to handle video in Processing. The API is compatible with the built-in video library of Processing. 
 * GSVideo uses the multimedia toolkit GStreamer (http://www.gstreamer.net/)  through the gstreamer-java bindings by Wayne Meissener:
 * http://code.google.com/p/gstreamer-java/ 
 * @author Andres Colubri
 * @version 0.4.5
 *
 * Copyright (c) 2008 Andres Colubri
 *
 * This source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This code 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
 * General Public License for more details.
 * 
 * A copy of the GNU General Public License is available on the World
 * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also
 * obtain it by writing to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package codeanticode.gsvideo;

import org.gstreamer.*;

/**
 * This class contains some basic functions used by the rest of the classes in this library.
 */
public class GSVideo
{	
    public static void init()
    {
        if (INSTANCES_COUNT == 0) 
        {
		    if (OS() == MACOS)
			{
				setOSXPath();
			}
		    if (OS() == WINDOWS)
			{
				setWinPath();
			}			
		    
		    if (!gstreamerBinPath.equals(""))
		    {
                System.setProperty("jna.library.path", gstreamerBinPath);
		    }
		    		    
		    if ((OS() == WINDOWS) && !useGlobalGstreamer)
		    {
	    	    JnaLibLoaderChain loader = JnaLibLoaderChain.getInstance();
		    }

		    if ((OS() == MACOS) && !useGlobalGstreamer)
		    {
		    	/*
		    	CLibrary clib = CLibrary.INSTANCE;
		    	
		    	String s = clib.getenv("DYLD_LIBRARY_PATH");
		    	System.out.println("DYLD_LIBRARY_PATH after = " + s);
		    	
		    	clib.unsetenv("DYLD_LIBRARY_PATH");
		    	
		    	//int n = clib.setenv("DYLD_LIBRARY_PATH", "jajaja", 1);
		    	//System.out.println("Result of executing setenv for DYLD_LIBRARY_PATH: " + n);
		    	
		    	
		    	s = clib.getenv("DYLD_LIBRARY_PATH");
		    	System.out.println("DYLD_LIBRARY_PATH before = " + s);
		    	*/
		    	
		    	System.out.println("jna.library.path: " + System.getProperty("jna.library.path"));   
		    	System.out.println("java.library.path: " + System.getProperty("java.library.path"));
		    	/*
	            try {
	                Runtime rt = Runtime.getRuntime();
	                
	                Process pr = rt.exec("export DYLD_LIBRARY_PATH=" + gstreamerBinPath);
	                //Process pr = rt.exec("echo $DYLD_LIBRARY_PATH");
	                
	                int exitVal = pr.waitFor();
	                System.out.println("Exited with error code "+exitVal);
	 
	            } catch(Exception e) {
	                System.out.println(e.toString());
	                e.printStackTrace();
	            }
		    	*/
		    	
	    	    JnaLibLoaderChain loader = JnaLibLoaderChain.getInstance();
		    }
		    
            String[] args = { "" };
            Gst.init("GSVideo", args);

		    if (!gstreamerPluginsPath.equals(""))
            {
        	    Registry reg = Registry.getDefault();
        	    boolean res;
        	    res = reg.scanPath(gstreamerPluginsPath);
        	    if (!res) System.err.println("Cannot load GStreamer plugins from " + gstreamerPluginsPath);
            }
        	
        }
        INSTANCES_COUNT++;	    
    }
	
    public static int OS() 
    {
        String os = System.getProperty("os.name").toUpperCase();
        if (os.indexOf("LINUX") != -1) 
        {
            return LINUX;
        }
        else if (os.indexOf("MAC") != -1) 
        {
            return MACOS;
        }
        else if (os.indexOf("WINDOWS") != -1) 
        {
            return WINDOWS;
        }	  
        else 
        {
            return UNKNOWN;
        }
    }

	public static void setOSXPath()
	{
		if (useGlobalGstreamer)
		{
		    gstreamerBinPath = "/opt/local/lib:/usr/local/lib";
		    gstreamerPluginsPath = "";	
		}
		else
		{
		    LibraryPath libPath = new LibraryPath();
		
	        String path = libPath.get();

		    gstreamerBinPath = path;
		    gstreamerPluginsPath = path + "/gstreamerOSX/gst-plugins";
		}
    }	

	public static void setWinPath()
	{
		if (useGlobalGstreamer)
		{
		    gstreamerBinPath = "";
		    gstreamerPluginsPath = "";	
		}
		else
		{
	        LibraryPath libPath = new LibraryPath();
			
     	    String path = libPath.get();
	    
		    gstreamerBinPath = path + "\\gstreamer\\base";
		    gstreamerPluginsPath = path + "\\gstreamer\\plugins";	
		}
    }	

    public static final int LINUX = 0;
    public static final int MACOS = 1;
    public static final int WINDOWS = 2;
    public static final int UNKNOWN = -1;
    static long INSTANCES_COUNT = 0;
	
    static String gstreamerBinPath = "";
    static String gstreamerPluginsPath = "";
    
    public static boolean useGlobalGstreamer = true;
    
    //private static final CLibrary clib = CLibrary.INSTANCE;
 }
