/*
 *    GetPropertyValue.java
 *    A task to get the value of a given property from a given properties file
 *
 *    Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.greenstone.anttasks;

import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import java.util.*;
import java.io.*;
import java.util.regex.*;

/**
 * Gets looks up a property in a properties file and returns its value
 */
public class GetPropertyValue extends Task {

	private File propertiesFile = null;
	private String propertyName = null;
	private String outputProperty = null;
	

	/**
	 * for testing

	public static void main( String[] args ) {

		GetPropertyValue gpv = new GetPropertyValue();
		gpv.setPropertiesFile( new File(args[0]) );
		gpv.setPropertyName( args[1] );
		gpv.setOutputProperty( args[2] );
		gpv.execute();

	}
	 */

	public void execute() {

		if ( propertiesFile == null ) {
			throw new BuildException( "Error - No properties file specified !!" );
		}

		if ( propertyName == null ) {
			throw new BuildException( "Error - No property name specified !!" );
		}

		if ( outputProperty == null ) {
			throw new BuildException( "Error - No output property specified !!" );
		}

		//compile the regex we're looking for
		//System.err.println( "compile the regex we're looking for" );
		String regex = "^" + propertyName.replaceAll("\\.","\\\\.") + "[:=]\\s*(.*)$";
		//System.err.println( "regex: " + regex );
		Pattern wantedLine = Pattern.compile( regex );

		//create the input stream
		//System.err.println( "create the input stream" );
		BufferedReader in = null;
		try {
			in = new BufferedReader(new FileReader( propertiesFile ));
		} catch ( Exception e ) {
			throw new BuildException( "Error - couldn't open the properties file " + propertiesFile );
		}

		//System.err.println( "pass over the file" );
		//pass over the file
		String line, value = null;

		try {
			
			boolean found = false;
			while ( (line = in.readLine()) != null && !found ) {

				Matcher matcher = wantedLine.matcher( line );
				if ( matcher.matches() ) {
					//found the property
					//System.err.println( "found the property" );
					value = matcher.group(1);
					value = value.replaceAll( "#.*", "" ).trim();
					found = true;
				}


			}

			//close the input stream
			//System.err.println( "close the input stream" );
			in.close();

		} catch ( Exception e ) {
			throw new BuildException( "Error - couldn't read from the properties file" );
		}

		//set the property in ant
		if ( value != null ) {
			//System.err.println( "set the property in ant" );
			this.getProject().setProperty(outputProperty, value);
		}

		//System.err.println( "done" );

	}

   
	public void setPropertiesFile(File file) {
		this.propertiesFile = file;
	}

	public void setPropertyName(String name) {
		this.propertyName = name;
	}

	public void setOutputProperty(String property) {
		this.outputProperty = property;
	}

}
