/*
 * Pwd.java
 *
 * Created on March 9, 2001, 11:45 AM
 */

package com.sns.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import java.io.*;

/**
 * Sets a property equal to the directory in which the current antfile lives.
 *
 *Usage:
 *<pre>
 *  &lt;pwd property="dir"/&gt;
 *</pre>
 *
 * @author  <a href="mailto:John.D.Casey@mail.sprint.com">John Casey</a>
 * @version 1.0
 */
public class Pwd extends org.apache.tools.ant.Task {
    private String property;
    
    /**
     * Sets the property name to use.
     *
     *@param prop The property name.
     */
    public void setProperty(String prop){
        this.property = prop;
    }
    
    /**
     * Executes the task.
     */
    public void execute() throws BuildException{
        if(property == null){property = ".";}
        String path = project.getProperty("ant.file");
        int pos = path.lastIndexOf("/");
        if(pos < 0){
            pos = path.lastIndexOf("\\");
        }
        if(pos < 0){
            throw new BuildException("Couldn't resolve the current path.");
        }
        else{
            path = path.substring(0, pos);
            project.setProperty(property, path);
        }
    }
}
