/* ========================================================================
 * PROJECT: UART
 * ========================================================================
 * Portions of this work are built on top of VRPN which was developed by
 *   Russell Taylor
 *   University of North Carolina
 * http://www.cs.unc.edu/Research/vrpn/
 *
 * We acknowledge the CISMM project at the University of North Carolina at Chapel Hill, supported by NIH/NCRR
 * and NIH/NIBIB award #2P41EB002025, for their ongoing  * support and maintenance of VRPN.
 *
 * Portions of this work are also built on top of the VideoWrapper,
 * a BSD licensed video access library for MacOSX and Windows.
 * VideoWrapper is available at SourceForge via 
 * http://sourceforge.net/projects/videowrapper/
 *
 * Copyright of VideoWrapper is
 *     (C) 2003-2010 Georgia Tech Research Corportation
 *
 * Copyright of the new and derived portions of this work
 *     (C) 2010 Georgia Tech Research Corporation
 *
 * This software released under the Boost Software License 1.0 (BSL1.0), so as to be 
 * compatible with the VRPN software distribution:
 *
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy 
 * of the software and accompanying documentation covered by this license (the "Software") to use, 
 * reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative 
 * works of the Software, and to permit third-parties to whom the Software is furnished to do so,
 * all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including the above license grant,
 * this restriction and the following disclaimer, must be included in all copies of the Software, in
 * whole or in part, and all derivative works of the Software, unless such copies or derivative works
 * are solely in the form of machine-executable object code generated by a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * For further information regarding UART, please contact 
 *   Blair MacIntyre
 *   <blair@cc.gatech.edu>
 *   Georgia Tech, School of Interactive Computing
 *   85 5th Street NW
 *   Atlanta, GA 30308
 *
 * For further information regarding VRPN, please contact 
 *   Russell M. Taylor II
 *   <taylor@cs.unc.edu>
 *   University of North Carolina, 
 *   CB #3175, Sitterson Hall,
 *   Chapel Hill, NC 27599-3175
 *
 * ========================================================================
 ** @author   Alex Hill (ahill@gatech.edu)
 *
 * ========================================================================
 *
 * Plugins.cs
 *
 * Usage: Add this script to the main camera in loading scene and all others
 * 
 *
 * Notes:
 *
 * ========================================================================*/
 
using UnityEngine;
using System;
using System.IO;
using System.Runtime.InteropServices;

public class Plugins : MonoBehaviour {

    public string[] PluginNames = {"VideoWrapper","TrackerWrapper","VRPNWrapper"};
    
	// Use this for initialization
	void Awake () {
		AddToPATH("", true);
        for (int i=0; i<PluginNames.Length; i++) 
			AddToPATH(PluginNames[i], true);	
	}
		
	void AddToPATH(String subpath, bool useProjectDir)
	{	

		//.NET 1.1 has no string.Contains method or System.Environment class
		#if !UNITY_IPHONE

		string cwd = ApplicationDataPath();
		Debug.Log("[CWD]"+cwd);
		string dataDirName = "Plugins";
		String dataDirectory = Path.Combine(cwd,dataDirName);
			
		string separator;
		string environVar;
		if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
		{
			environVar = "PATH";
			separator = ";";
		}
		else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
		{
			environVar = "DYLD_LIBRARY_PATH";
			separator = ":";
		}
		else
		{
			Debug.Log("Not using native platform app");
			return;
		}
					
		string oldPath = System.Environment.GetEnvironmentVariable(environVar);
		Debug.Log("[Old Path]"+oldPath);
		String pluginPath;
		if (useProjectDir)
			pluginPath = Path.Combine(dataDirectory,subpath);
		else
			pluginPath = subpath;

		if(oldPath == null || !oldPath.Contains(pluginPath))
		{
			string newPath = oldPath + separator + pluginPath +"/";
			Debug.Log("[New Path]"+newPath);
			System.Environment.SetEnvironmentVariable(environVar,newPath);
			System.Environment.SetEnvironmentVariable("PATH",newPath);
		}else{
			Debug.Log("Path already contains plugin dir");
		}
				
		#endif
	}

	public static string ApplicationDataPath()
	{    
		return Application.dataPath;
	}
	
	public static string CreateFileFromAsset(string filename)
	{
		TextAsset textAsset = (TextAsset)UnityEngine.Resources.Load(filename);
		string filepath = ApplicationDataPath() + "/" + filename;
		BinaryWriter binWriter = new BinaryWriter(File.Open(filepath, FileMode.Create));
		binWriter.Write(textAsset.bytes);
		binWriter.Close();
		return filepath;
	}
	
}
