/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000,2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs.optional.clearcase;

import java.io.File;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.FileSet;


/**
 * A base class for creating tasks for executing commands on ClearCase.
 * <p>
 * The class extends the 'exec' task as it operates by executing the cleartool program
 * supplied with ClearCase. By default the task expects the cleartool executable to be
 * in the path, * you can override this be specifying the cleartooldir attribute.
 * </p>
 * <p>
 * This class provides set and get methods for the 'viewpath' attribute. It
 * also contains constants for the flags that can be passed to cleartool.
 * </p>
 *
 * @author Curtis White
 * @author <a href="mailto:of_jakarta@gmx.net">Oscar Forero</a>
 */
public abstract class ClearCase extends Task {
    private String m_ClearToolDir = "";
    private String m_viewPath = null;
	private FileSet fileset = null;

    protected int verbosity = Project.MSG_VERBOSE;


	public void addFileSet(FileSet fileset) throws BuildException {
		if(this.fileset==null) {
			this.fileset=fileset;
		} else {
			throw new BuildException("Fileset has already been set, only one " +
				"FileSet element is accepted");
		}
	}

	/**
     * Set the directory where the cleartool executable is located.
     *
     * @param dir the directory containing the cleartool executable
     */
    public final void setClearToolDir(String dir) {
        m_ClearToolDir = project.translatePath(dir);
    }

    /**
     * Builds and returns the command string to execute cleartool
     *
     * @return String containing path to the executable
     */
    protected final String getClearToolCommand() {
        String toReturn = m_ClearToolDir;
        if (!toReturn.equals("") && !toReturn.endsWith("/")) {
            toReturn += "/";
        }

        toReturn += CLEARTOOL_EXE;

        return toReturn;
    }

    /**
     * Set the path to the item in a ClearCase view to operate on.
     *
     * @param viewPath Path to the view directory or file
     */
    public final void setViewPath(String viewPath) {
        m_viewPath = viewPath;
    }

    /**
     * Get the path to the item in a clearcase view
     *
     * @return m_viewPath
     */
    public String getViewPath() {
        return m_viewPath;
    }

    /**
     * Used to force listing of all names of files.
     */
    public void setVerbose(boolean verbose) {
        if (verbose) {
            this.verbosity = Project.MSG_INFO;
        } else {
            this.verbosity = Project.MSG_VERBOSE;
        }
    }

    protected int run(Commandline cmd) {
        try {
            Project aProj = getProject();
            Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
            exe.setAntRun(aProj);
            exe.setWorkingDirectory(aProj.getBaseDir());
            exe.setCommandline(cmd.getCommandline());
            return exe.execute();
        } catch (java.io.IOException e) {
            throw new BuildException(e, location);
        }
    }

	
	/**
     * Abstract to be defined by Subclasses it will have to determine the Clear
	 * Case command to execute and the required parameters in every case.
     */
	abstract protected void checkOptions(Commandline cmd);
	
	/**
     * Execute an Clear Case Command for a given file.
     * <p>
     * Builds a command line to execute cleartool and then calls Exec's run method
     * to execute the command line.
     */
	private void execute(String sFile) {
		setViewPath(sFile);

		// build the command line from what we got the format is
		// cleartool checkout [options...] [viewpath ...]
		// as specified in the CLEARTOOL.EXE help
		Commandline commandLine = new Commandline();
		int result = 0;
		commandLine.setExecutable(getClearToolCommand());

		checkOptions(commandLine);

		// For debugging
        log(commandLine.toString(), verbosity);

		result = run(commandLine);
		if (result != 0) {
			String msg = "Failed executing: " + commandLine.toString();
			throw new BuildException(msg, location);
		}
	}

	/**
     * Executes the task.
     * <p>
     * If the task contains FileSet elements will execute the task in all the 
	 * elements identified by they. If not will execute using the ViewPath 
	 * property.
     */
    public void execute() throws BuildException {
		if( fileset==null ) {
			if (getViewPath() == null) {
				execute(getProject().getBaseDir().getPath());
			} else {
				execute(getViewPath());
			}
		} else {
			Enumeration clearObjects = getClearCaseObjects();
			while( clearObjects.hasMoreElements() ) {
				String sName = (String) clearObjects.nextElement();
				execute(sName);			
			}
		}
    }

    protected Enumeration getClearCaseObjects() {
		Vector rFiles = new Vector();
		log("Fileset present, processing files", verbosity);
		
		DirectoryScanner ds = fileset.getDirectoryScanner(project);

		String[] srcFiles = ds.getIncludedFiles();
		for(int i=0 ; i<srcFiles.length ; i++) {
			rFiles.add(fileset.getDir(project).getAbsolutePath() + File.separator + srcFiles[i]);
		}

		String[] srcDirs = ds.getIncludedDirectories();
		for(int i=0 ; i<srcDirs.length ; i++) {
			rFiles.add(fileset.getDir(project).getAbsolutePath() + File.separator + srcDirs[i]);
		}
		
		return rFiles.elements();
	}
	
	
	/**
     * Constant for the thing to execute
     */
    private static final String CLEARTOOL_EXE = "cleartool";

    /**
     * The 'Update' command
     */
    public static final String COMMAND_UPDATE = "update";
    /**
     * The 'Checkout' command
     */
    public static final String COMMAND_CHECKOUT = "checkout";
    /**
     * The 'Checkin' command
     */
    public static final String COMMAND_CHECKIN = "checkin";
    /**
     * The 'UndoCheckout' command
     */
    public static final String COMMAND_UNCHECKOUT = "uncheckout";

}

