/*
 * Copyright 2009 OW2 Chameleon
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ow2.chameleon.core;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

/**
 * The Debug Activator launches ShellTUI if enabled.
 * @author <a href="mailto:chameleon-dev@ow2.org">Chameleon Project Team</a>
 */
public class DebugActivator implements BundleActivator {

    /**
     * Shell prefix.
     */
    public static final String SHELL = "org.apache.felix.shell";

    /**
     * Gogo bundle prefix.
     */
    public static final String GOGO = "org.apache.felix.gogo";

    /**
     * Arch prefix.
     */
    public static final String ARCH_SHELL = "org.apache.felix.ipojo.arch-";

    /**
     * Arch Gogo prefix.
     */
    public static final String ARCH_GOGO = "org.apache.felix.ipojo.arch.gogo-";

    /**
     * The core directory.
     */
    private File directory;

    /**
     * Initializes the activator.
     * @param dir the core directory.
     */
    public DebugActivator(File dir) {
        directory = dir;
    }

    /**
     * Starts and Installs the Shell.
     * If Gogo bundles are there, it use Gogo.
     * If Shell-TUI is here, use Shell-TUI (old Felix Shell)
     * @param bc the bundle context
     * @throws Exception if the shell tui cannot be installed correctly.
     * @see org.osgi.framework.BundleActivator#
     *  start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext bc) throws Exception {
        File[] files = directory.listFiles();
        List installed = new ArrayList(); // List of installed bundles.
        // First detect if we're using Gogo or the Shell-TUI.
        boolean gogo = isGogo(files);

        for (int i = 0; i < files.length; i++) {
        	if (gogo) {
        		// Gogo
        		if (StringUtils.contains(files[i].getName(), GOGO)
        				|| StringUtils.contains(files[i].getName(), ARCH_GOGO)) {
        			Bundle bundle = bc.installBundle(
                		"reference:" + files[i].toURI().toURL().toExternalForm());
        			installed.add(bundle);
        		}
	        } else {
	        	// Shell-TUI.
	        	if (StringUtils.contains(files[i].getName(), SHELL)
	        			|| StringUtils.contains(files[i].getName(), ARCH_SHELL)) {
	                Bundle bundle = bc.installBundle(
	                		"reference:" + files[i].toURI().toURL().toExternalForm());
	                installed.add(bundle);
	            }
	        }
        }

        for (int i = 0; i < installed.size(); i++) {
        	Bundle bundle = (Bundle) installed.get(i);
        	bundle.start();
        }
    }

    /**
     * Checks whether Gogo bundles are available.
     * @param files the core directory files
     * @return <code>true</code> if the core directory contains
     * Gogo bundles, <code>false</code> otherwise.
     */
    private boolean isGogo(File[] files) {
		for (int i = 0; i < files.length; i++) {
			if (StringUtils.contains(files[i].getName(), GOGO)) {
				return true;
			}
		}
		return false;
	}

	/**
     * Does nothing.
     * @param bc the bundle context
     * @throws Exception cannot be raised.
     * @see org.osgi.framework.BundleActivator#
     *  stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext bc) throws Exception {
        // Nothing to do.
    }

}
