#!/bin/perl
# 
# The Nutch command script (in Perl)
#	Perl Conversion by Nick Jacobsen
#	Original Shell Script by the Nutch project
# Environment Variables
#
#   NUTCH_JAVA_HOME The java implementation to use.  Overrides JAVA_HOME.
#
#   NUTCH_HEAPSIZE  The maximum amount of heap to use, in MB. 
#                   Default is 1000.
#
#   NUTCH_OPTS      Extra Java runtime options.
#
use File::Basename;
use Env::Path;

########################################
###		FUNCTIONS				  ###
########################################
sub usage() {
	print "Usage: nutch COMMAND\n";
	print "where COMMAND is one of:\n";
	print "  crawl             one-step crawler for intranets\n";
	print "  admin             database administration, including creation\n";
	print "  inject            inject new urls into the database\n";
	print "  generate          generate new segments to fetch\n";
	print "  fetchlist         print the fetchlist of a segment\n";
	print "  fetch             fetch a segment's pages\n";
	print "  parse             parse a segment's pages\n";
	print "  index             run the indexer on a segment's fetcher output\n";
	print "  merge             merge several segment indexes\n";
	print "  dedup             remove duplicates from a set of segment indexes\n";
	print "  updatedb          update db from segments after fetching\n";
	print "  updatesegs        update segments with link data from the db\n";
	print "  mergesegs         merge multiple segments into a single segment\n";
	print "  readdb            examine arbitrary fields of the database\n";
	print "  analyze           adjust database link-analysis scoring\n";
	print "  prune             prune segment index(es) of unwanted content\n";
	print "  segread           read, fix and dump segment data\n";
	print "  segslice          append, join and slice segment data\n";
	print "  server            run a search server\n";
	print "  namenode          run the NDFS namenode\n";
	print "  datanode          run an NDFS datanode\n";
	print "  ndfs              run an NDFS admin client\n";
	print "  jobtracker        run the MapReduce job Tracker node\n";
	print "  tasktracker       run a MapReduce task Tracker node\n";
	print " or\n";
	print "  CLASSNAME         run the class named CLASSNAME\n";
	print "Most commands print help when invoked w/o parameters.\n";	
}

########################################
###	Code Entry Point			  ###
########################################

#determine path seperator, and path slash
if ( $^O eq "MSWin32") {
	$slash = "\\";
	$sep = ";";
} else {
	$slash = "/";
	$sep = ":";
}
$script_name = $0;
#if ($script_name =~ /(([A-Za-z]\:\\\\|\/).*(\\|\/))/) {
$script_name =~ /((?:[A-Za-z]\:\\|\/)(?:.*)(?:\/|\\))((?:.*?)(?:\/|\\))/;
$script_dir = "$1$2";
$nutch_home = "$1";
$java_home = $ENV{'JAVA_HOME'};
$nutch_java_home = $ENV{'NUTCH_JAVA_HOME'};
$nutch_heapsize = $ENV{'NUTCH_HEAPSIZE'};
$nutch_conf_dir = $ENV{'NUTCH_CONF_DIR'};
$nutch_opts = $ENV{'NUTCH_OPTS'};

# if no arguments, print usage and exit
if ($#ARGV < 0) {
	usage();
	exit(0);
}

# get arguments
$command=shift(@ARGV);

# some directories
#($name,$path,$suffix) = fileparse($fullname,@suffixlist)
#fileparse_set_fstype($os_string);
#$basename = basename($fullname,@suffixlist);
#$dirname = dirname($fullname);

# some Java parameters
if ($nutch_java_home) {
	print "run java in $nutch_java_home\n";
	$java_home=$nutch_java_home;
}
if (!$java_home) {
	print "Error: JAVA_HOME is not set.\n";
	exit(1);
}

$java = $java_home . $slash . "bin" . $slash . "java";
$java_heap_max=-Xmx1000m;

# check envvars which might override default args
if ($nutch_heapsize) {
	print "run with heapsize $nutch_heapsize";
	$java_heap_max = "-Xmx" . $nutch_heap_size . "m";
	print $java_heap_max;
}

# CLASSPATH initially contains $NUTCH_CONF_DIR, or defaults to $NUTCH_HOME/conf
if (!$nutch_conf_dir) {
	$nutch_conf_dir = $nutch_home . $slash . "conf";
}
$classpath = $nutch_conf_dir;

# for developers, add Nutch classes to CLASSPATH
if (-d "$nutch_home" . $slash . "build" . $slash . "classes") {
	$classpath .= $sep . $nutch_home . $slash . "build" . $slash . "classes";
}
if (-d $nutch_home . $slash . "build" . $slash . "plugins") {
	$classpath .= $sep . $nutch_home . $slash . "build";
}
if (-d $nutch_home . $slash . "build" . $slash . "test" . $slash . "classes") {
	$classpath .= $sep . $nutch_home .  $slash . "build" . $slash . "test" . $slash . "classes";
}

# for releases, add Nutch jar to CLASSPATH
opendir(DIR, "$nutch_home");
@files = grep(/\.jar$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
   $classpath .= $sep . $nutch_home . $slash . $file;
}

# add plugins to classpath
if (-d $nutch_home . $slash . "plugins") {
	$classpath .= $sep . $nutch_home;
}

# add libs to CLASSPATH
opendir(DIR, $nutch_home . $slash . "lib");
@files = grep(/\.jar$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
   $classpath .= $sep . $nutch_home . $slash . "lib" . $slash . $file;
}
opendir(DIR, $nutch_home . $slash . "lib" . $slash . "jettyext");
@files = grep(/\.jar$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
   $classpath .= ":" . $nutch_home . $slash . "lib" . $slash . "jettyext" . $slash . $file;
}

# figure out which class to run
if ("$command" eq "crawl")		{$class="org.apache.nutch.tools.CrawlTool";}
elsif ("$command" eq "admin")		{$class="org.apache.nutch.tools.WebDBAdminTool";}
elsif ("$command" eq "inject")		{$class="org.apache.nutch.db.WebDBInjector";}
elsif ("$command" eq "generate")	{$class="org.apache.nutch.tools.FetchListTool";}
elsif ("$command" eq "fetchlist")	{$class="org.apache.nutch.pagedb.FetchListEntry";}
elsif ("$command" eq "fetch")		{$class="org.apache.nutch.fetcher.Fetcher";}
elsif ("$command" eq "parse")		{$class="org.apache.nutch.tools.ParseSegment";}
elsif ("$command" eq "index" )		{$class="org.apache.nutch.indexer.IndexSegment";}
elsif ("$command" eq "merge" )		{$class="org.apache.nutch.indexer.IndexMerger";}
elsif ("$command" eq "dedup" )		{$class="org.apache.nutch.indexer.DeleteDuplicates";}
elsif ("$command" eq "updatedb")	{$class="org.apache.nutch.tools.UpdateDatabaseTool";}
elsif ("$command" eq "updatesegs")	{$class="org.apache.nutch.tools.UpdateSegmentsFromDb";}
elsif ("$command" eq "mergesegs")	{$class="org.apache.nutch.tools.SegmentMergeTool";}
elsif ("$command" eq "readdb")		{$class="org.apache.nutch.db.WebDBReader";}
elsif ("$command" eq "prune")		{$class="org.apache.nutch.tools.PruneIndexTool";}
elsif ("$command" eq "segread")		{$class="org.apache.nutch.segment.SegmentReader";}
elsif ("$command" eq "segslice")	{$class="org.apache.nutch.segment.SegmentSlicer";}
elsif ("$command" eq "analyze")		{$class="org.apache.nutch.tools.LinkAnalysisTool";}
elsif ("$command" eq "server")		{$class="org.apache.nutch.searcher.DistributedSearch\$Server";}
elsif ("$command" eq "namenode")	{$class="org.apache.nutch.ndfs.NDFS\$NameNode";}
elsif ("$command" eq "datanode")	{$class="org.apache.nutch.ndfs.NDFS\$DataNode";}
elsif ("$command" eq "ndfs")		{$class="org.apache.nutch.fs.TestClient";}
elsif ("$command" eq "jobtracker")	{$class="org.apache.nutch.mapReduce.JobTracker";}
elsif ("$command" eq "tasktracker")	{$class="org.apache.nutch.mapReduce.TaskTracker";}
else {$class=$command}

# run it
print "$java $java_heap_max $nutch_opts -classpath \"$classpath\" $class @ARGV";
system("$java","$java_heap_max $nutch_opts -classpath \"$classpath\" $class @ARGV");
#exec "$JAVA" $JAVA_HEAP_MAX $NUTCH_OPTS -classpath "$CLASSPATH" $CLASS "@ARGV"

