#!/usr/local/bin/perl
#
#  essence - semantic indexer, main user interface
#
#  Usage: essence [-i] [-noindex] [-n index-name] [-s stoplist] [-t tmpdir] 
#         [-v] ( -f files datadir | datadir directory ... )
#
#  This program generates uses semantic indexing to build a small,
#  content-based WAIS index of the given files.  datadir will hold 
#  the summary files and WAIS index, and the given directory...
#  are the filenames/directories to index.
#
#  Version 1.1, July 1993, Darren R. Hardy, hardy@cs.colorado.edu
#

$doindex = 1;
$verbose = $interactive = 0;
$winame = "Essence";			# WAIS Index name
chop($curdir = `pwd`);
$tmpdir = "/tmp";
undef $filelist;

# 
#  Parse arguments 
#
do usage() if ($#ARGV < 1);
while ($_ = shift(@ARGV)) {
        last if (!/^-/o);
        $interactive = 1                if (/^-i$/o);
        $verbose = 1                    if (/^-v$/o);
        $doindex = 0                    if (/^-noindex$/o);
	$winame = shift(@ARGV)		if (/^-n$/o);
        $tmpdir = shift(@ARGV)      	if (/^-t$/o);
        $stoplistfn = shift(@ARGV)  	if (/^-s$/o);
        $filelist = shift(@ARGV)  	if (/^-f$/o);
}
$engine_files = "$tmpdir/engine.files.$$";
$engine_outfile = "$tmpdir/engine.out.$$";
$datadir = $_;
$datadir = $curdir . "/" . $datadir if ($datadir !~ /^\//o);
print "Using $datadir\n" if ($verbose);
do usage() if ($#ARGV < 0 && !defined($filelist));
$interactive = 0 if (defined($filelist));
$| = 1 if ($verbose);

#
#  We use find to generate a list of filenames to feed to the Essence engine.
#
#  EssenceInterface is the file which waisserver always returns so that
#  users will always have a document in front of them which tells them
#  how to use Essence.
#
if (!defined($filelist)) {
	chop($find_args = `which EssenceInterface`);
	while ($_ = shift(@ARGV)) {
		$_ = $curdir . "/" . $_ if (! /^\//o);
		$find_args .= " " . $_;
	}
	$find_cmd = "find " . $find_args . " -print";
	print "Using $find_cmd > $engine_files\n" if ($verbose);
	die "Problem with running $find_cmd" if (system("$find_cmd > $engine_files"));
	$filelist = $engine_files;
}

#
#  Setup for the engine run
#
mkdir("$tmpdir", 0755) if (! -d $tmpdir);
mkdir("$datadir", 0755) if (! -d $datadir);
chdir("$datadir");
print "Cleaning out old summary files, if any.\n" if ($verbose);
system("rm -rf sums");		# clean out old summary files
mkdir("sums", 0755);

#
#  Run the Essence engine to generate summary files
#
$engine_command = "engine";
$engine_command .= " -v" if ($verbose);
$engine_command .= " -t $tmpdir" if (defined($tmpdir));
$engine_command .= " -s $stoplistfn" if (defined($stoplistfn));
$engine_command .= " $datadir";
$engine_command .= " < $filelist";
$engine_command .= " > $engine_outfile 2>&1";

print "Running $engine_command\n" if ($verbose);
system($engine_command);

#
#  Add user-comments
#
if ($interactive) {
	print "\n\n***********************************\n\n";
	print "If you would like to add any keywords to the summary files,\n";
	print "do it now.  If so, use ^Z to exit to the shell, and then use\n";
	print "the 'fg' command to return.  $datadir/Map contains\n";
	print "information about the contents of the summary files.\n";
	print "\n  PRESS RETURN TO CONTINUE\n";
	print "\n\n***********************************\n\n";
	$blah = <STDIN>;
	print "\nContinuing...\n";
}

#
#  Once we have all of the summary files, build a WAIS index.
#
if ($doindex) {
	$wi_command = "waisindex -t essence -d $winame -r sums";
	print "Running $wi_command\n" if ($verbose);
	system($wi_command);
}

exit(0);

sub usage {
	print STDERR "Usage: essence [-i] [-noindex] [-n index-name] [-s stoplist] [-t tmpdir] [-v] ( -f files datadir | datadir directory ... )\n";
	exit(1);
}
