/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   */
/* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
package net.nutch.ext.ant;

import java.io.File;
import java.util.StringTokenizer;
import net.nutch.tools.CrawlTool;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.DataType;

/**
 * Ant task for <code>CrawlTool</code> utility.<p>
 * Used nutch command:
 * <pre>bin/nutch crawl urls -dir crawl.test -depth 3 >& crawl.log</pre>
 * Now ant script:
 * <pre>
 * &lt;taskdef name="crawl" classpath="${basedir}/classes" classname="net.nutch.ext.ant.CrawlTask"/&gt;
 * ...
 * &lt;task name="webdb.init"&gt;
 *      &lt;crawl ndfs="[local|host:port]" threads="10" depth="3" showThread="yes"&gt;
 *           &lt;seed file="${seed.dir}/seeds.txt" /&gt;
 *           &lt;dest dir ="${dest.dir}" /&gt;
 *           &lt;log  file="${log.file}" /&gt; 
 *      &lt;/crawl&gt;
 * &lt;/task&gt;
 * ...
 * </pre>
 * 
 * @author jack.tang
 */
public class CrawlTask extends Task 
{
	private String ndfs;
	private int threads = -1;
	private int depth = -1;
	private boolean showThread = false;
	
	//- nested element
	private SeedFile seed;
	private DestDir dest;
	private LogFile log;
	
	
	//~ Constructors
	// ======================================================
	public CrawlTask()
	{}
	
	
	/**
	 * Gets arguments for CrawlTool and calls the main method.
	 * @see
	 * @throws BuildException
	 */
	public void execute() throws BuildException
	{
		if ( null == seed )
			throw new BuildException("No seed-url file specified.");
		if ( null == dest )
			throw new BuildException("No destination dir specified.");
		
		
		StringBuffer sb = new StringBuffer();
		int i = 0;
		
		if ("local".equalsIgnoreCase(ndfs))
			sb.append("-local ");
		else
			sb.append("-ndfs ").append(ndfs);
		
		sb.append(seed.file.getAbsoluteFile());
		sb.append(" -dir ").append(dest.dir.getAbsoluteFile());
		
		if ( -1 != threads )
			sb.append(" -threads ").append(threads);
		if ( -1 != depth )
			sb.append(" -depth ").append(depth);
		if ( showThread )
			sb.append(" -showThreadID");
		
		System.out.println(sb.toString());
		
		StringTokenizer st = new StringTokenizer(sb.toString());
		String[] args = new String[st.countTokens()];
		
		while(st.hasMoreTokens())
			args[i++] = st.nextToken().toString();
		
		
		for (int j=0; j<args.length; j++)
			System.out.println(args[j]);
		
		try 
		{
			CrawlTool.main(args);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			throw new BuildException("Exceptions in CrawlTool: " + e.getMessage());
			
		} 
		
	}
	
	//~ Nested elements
	// ==========================================================================
	
	public SeedFile createSeed()
	{
		SeedFile s = new SeedFile();
		this.seed = s;
		return s;
	}
	
	public DestDir createDest()
	{
		DestDir d = new DestDir();
		this.dest = d;
		return d;
	}
	
	public LogFile createLog()
	{
		LogFile l = new LogFile();
		this.log = l;
		return l;
	}
	
	
	public class SeedFile extends DataType
	{
		private File file;
		
		public SeedFile()
		{}
		
		
		public void setFile(File seed)
		{
			file = seed;
		}
		
	}
	
	public class DestDir extends DataType
	{
		private File dir;
		
		public DestDir()
		{}
		
		public void setDir(File dir)
		{
			this.dir = dir;
		}
	}
	
	public class LogFile extends DataType
	{
		private File file;
		
		public LogFile()
		{}
		
		public void setFile(File log)
		{
			this.file = log;
		}
	}
	
	//~ Attributes
	// =======================================================
	
	public void setNdfs(String ndfs)
	{
		this.ndfs = ndfs;
	}
	
	public void setThreads(int threads)
	{
		this.threads = threads;
	}
	
	public void setDepth(int depth)
	{
		this.depth = depth;
	}
	
	public void setShowThread(boolean showThread)
	{
		this.showThread = showThread;
	}
}
