

package org.apache.tools.ant.taskdefs;



import org.apache.tools.ant.*;

import org.apache.tools.ant.types.*;



import java.util.Vector;

import java.io.File;

import java.io.IOException;



/**

 * Executes a given command, supplying a set of files as arguments. 

 *

 * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 

 * @author <a href="mailto:mariusz@rakiura.org">Mariusz Nowostawski</a> 

 */

public class Wrap extends ExecTask {


    public final static String CVSrcsfile  = "$RCSfile: Wrap.java,v $";
    public final static String CVSrevision = "$Revision: 1.3 $";
    public final static String CVSdate     = "$Date: 2001/09/25 15:50:49 $";
    public final static String CVSauthor   = "$Author: jad $";
    public final static String CVScomment  = "Special version of ExecOn task to handle Oracle wrap";


    protected Vector filesets = new Vector();

    protected String srcFile = null;
    protected String outdir = ".";
    protected String oracle_home = null;
    protected Commandline.Marker srcFilePos = null;



    /**

     * Adds a set of files (nested fileset attribute).

     */

    public void addFileset(FileSet set) {

        filesets.addElement(set);

    }

	
	/**
    * Set the name of the sql file to be wrapped.
    */

	public void setSrc(String srcFile) {
        this.srcFile = srcFile;
    }

	/**
    * Sets the directory where the wrapped files should recide.
    */

        public void setOutdir(String outdir) {
        this.outdir = outdir;
    }


   	/**
    * Sets the oracle_home directory.
    */

	public void setOraclehome(String oraclehome) {
        this.oracle_home = oraclehome;
    }



    /**

     * Marker that indicates where the name of the source file should

     * be put on the command line.

     */

    public Commandline.Marker createSrcfile() {

        if (srcFilePos != null) {

            throw new BuildException(taskType + " doesn\'t support multiple srcfile elements.",

                                     location);

        }

        srcFilePos = cmdl.createMarker();

        return srcFilePos;

    }



    protected void checkConfiguration() {

        if (filesets.size() == 0 && srcFile == null) {

            throw new BuildException("no sourcefile or filesets specified", location);

        }
		if (oracle_home == null) {

            throw new BuildException("oracle_home must be specified", location);

        }
    }



    protected void runExec(Execute exe) throws BuildException {

        try {

			int err = -1;
			String command = new String(oracle_home + "/bin/wrap "); 
			String iname = null;
			String oname = null;
			String slash = System.getProperty("file.separator");	

			if (srcFile != null) {
			    iname = new String("iname=" + srcFile + " "); 
				oname = new String("oname=" + outdir + srcFile.substring(srcFile.lastIndexOf(slash),srcFile.length() - 3) + "plb");
				Commandline cmdl = new Commandline(command + iname + oname);
				exe.setCommandline(cmdl.getCommandline());

                err = exe.execute();

            }

			if (filesets.size() != 0) {										
		       for (int i=0; i<filesets.size(); i++) {

                   Vector v = new Vector();

                   FileSet fs = (FileSet) filesets.elementAt(i);

                   File base = fs.getDir(project);

                   DirectoryScanner ds = fs.getDirectoryScanner(project);

                   String[] sf = getFiles(base, ds);

                   for (int j=0; j<sf.length; j++)

                       v.addElement(sf[j]);

               	   String[] s = new String[v.size()];

                   v.copyInto(s);				  
                   for (int j=0; j<s.length; j++) {
				       srcFile = (new File(base, s[j])).getAbsolutePath();
					   iname = new String("iname=" + srcFile + " "); 
				  	   oname = new String("oname=" + outdir + srcFile.substring(srcFile.lastIndexOf(slash),srcFile.length() - 3) + "plb");
					   Commandline cmdl = new Commandline(command + iname + oname);
					   exe.setCommandline(cmdl.getCommandline());

                	   err = exe.execute();
  				   }
	           }
			} 	

            if (err != 0) {

               if (failOnError) {

                   throw new BuildException("Exec returned: "+err,location);

               } 
			   else {

                   log("Result: " + err, Project.MSG_ERR);

               }

            }

        } 
//		catch (IOException e) {

		catch (Exception e) {
            throw new BuildException("Execute failed: " + e, e, location);

        } 
		finally {

            // close the output file if required

            logFlush();

        }

    }



    /**

     * Return the list of files from this DirectoryScanner that should

     * be included on the command line.

     */

    protected String[] getFiles(File basedir, DirectoryScanner ds) {

        return ds.getIncludedFiles();

    }



    /**

     * Return the list of Directories from this DirectoryScanner that

     * should be included on the command line.

     */

    protected String[] getDirs(File basedir, DirectoryScanner ds) {

        return ds.getIncludedDirectories();

    }



    

}


