/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/
package com.nokia.tcf;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import com.nokia.tcf.impl.TCAPIConnection;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends Plugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.nokia.tcf";

	// The shared instance
	private static Activator plugin;
	private IPath debuggerPath = null;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;

		Bundle bundle = Platform.getBundle("com.nokia.carbide.cpp.support"); //$NON-NLS-1$
		if (bundle != null) {
			URL pluginURL = FileLocator.find(bundle, new Path(""), null); //$NON-NLS-1$
			pluginURL = FileLocator.resolve(pluginURL);
			String pluginFilePath = pluginURL.getFile();
			Path pluginPath = new Path(pluginFilePath);

			Location installLocation = Platform.getInstallLocation();
			URL installURL = installLocation.getURL();
			String installFilePath = installURL.getFile();
			Path installPath = new Path(installFilePath);

			if (installPath.isPrefixOf(pluginPath)) { // Release build
				debuggerPath = pluginPath.append("Debugger"); //$NON-NLS-1$
			} else { // Debug build
				IPath basePath = pluginPath.removeLastSegments(3);
				debuggerPath = basePath
						.append("ide").append("Output").append("IDEDebug"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}
		}
		TCAPIConnection api = new TCAPIConnection();
		long err = api.nativeStartServer();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		TCAPIConnection api = new TCAPIConnection();
		long err = api.nativeStopServer();
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	public IPath getDebuggerPath() {
		return debuggerPath;
	}
}
