#!/usr/bin/perl
#
# Perlfect Search Setup Utility
# (C)1999 Perlfect Solutions
# http://www.perlfect.com
#
# $rcs = ' $Id: setup.pl,v 1.8 2000/04/11 15:03:14 perlfect Exp $ ' ;	

my $VERSION = '3.05';

print "
Perlfect Search $VERSION Setup Utility

WARNING: There is no guarantee implicit or explicit that this program 
will work correctly. By using this program you automatically indemnify 
Perlfect Solutions of any damage or harm directly or indirectly caused 
by its operation. Use it at your own risk!

";
exit if(input("Are you sure you want to continue? [y/n]", "y", ["y","n"]) eq "n");

my $os = input("What operating system is your site running on? [unix/nt]", "unix", ["unix", "nt"]);

my $perl;
if($os eq 'unix')
  {
    $perl = `which perl`;
    chomp $perl;
    $perl='/usr/bin/perl' unless ($perl);
    $perl = input("Where is perl? [$perl]", "$perl");
    print "Ok, I will configure the scripts to run with $perl\n";
    set_perl_path("search.pl", $perl);
    set_perl_path("indexer.pl", $perl);
  }
print "\n";

my $baseurl = input("What is the base URL of your site? (e.g. http://www.perlfect.com/)");
my $cgiurl = input("And is the URL of your cgi-bin directory? (e.g. http://www.perlfect.com/cgi-bin/");

my $basedir = input("Which directory is your site in? (e.g. /home/httpd/html/)");
my $cgidir = input("Where is your cgi-bin directory? (e.g. /home/httpd/cgi-bin/)");

foreach ($basedir, $baseurl, $cgidir, $cgiurl)
  {
    $_=~s/\/$//;
    $_.='/';
  }
my $instdir=$cgidir."perlfect/search/";
my $insturl=$cgiurl."perlfect/search/";

print "
I will now install Perlfect Search $VERSION in
$instdir

";
install_dir($cgidir."perlfect", "0755");
install_dir($cgidir."perlfect/search", "0755");
install_file("search.pl", $instdir."search.pl", "0755");
install_file("indexer.pl", $instdir."indexer.pl", "0755");
install_file("conf.pl", $instdir."conf.pl", "0755");
install_dir($instdir."data", "0755");
install_dir($instdir."conf", "0755");
install_file("conf/stopwords.txt", $instdir."conf/stopwords.txt", "0755", keep);
install_dir($instdir."templates", "0755");
install_file("templates/search.html", $instdir."templates/search.html", "0755", keep);
install_dir($instdir."Perlfect", "0755");
install_file("Perlfect/Template.pm", $instdir."Perlfect/Template.pm", "0755");


print "
Now I need you to tell me what file types will be indexed by Perlfect Search.

";
my @filetypes;
push(@filetypes, '"htm"') if(input("Do you want to index .htm files? [y/n]", "y", ['y','n']));
push(@filetypes, '"html"') if(input("Do you want to index .html files? [y/n]", "y", ['y','n']));
push(@filetypes, '"shtml"') if(input("Do you want to index .shtml files? [y/n]", "y", ['y','n']));
push(@filetypes, '"stm"') if(input("Do you want to index .stm files? [y/n]", "y", ['y','n']));
push(@filetypes, '"asp"') if(input("Do you want to index .asp files? [y/n]", "n", ['y','n']));
push(@filetypes, '"stx"') if(input("Do you want to index .stx files? [y/n]", "n", ['y','n']));
push(@filetypes, '"txt"') if(input("Do you want to index .txt files? [y/n]", "n", ['y','n']));
my $other_filetypes = input("Any other filetypes? (enter a comma-separated list)");
foreach (split(/,/, $other_filetypes))
  {
    $_ =~ s/\s//g;
    $_ =~ s/\'//g;
    $_ =~ s/^\.//;
    $_ =~ "\"$_\"";
    push(@filetypes, $_);
  }
my $filetype_list = join(",", @filetypes);


print "Configuring Perlfect Search...\n";
open(CONF, ">$instdir/conf.pl")
  or die "\n** Could not open $cgidir/conf/conf.pl: $!\n";
print CONF "
# Configuration for Perlfect Search $VERSION

#Where you want the indexer to start
\$DOCUMENT_ROOT = '$basedir';

#The base url of your site
\$BASE_URL = '$baseurl';

#The url in which perlfect search is located(usually somewhere in cgi-bin/)
\$CGIBIN   = '$insturl';

#The full-path of the directory where perlfect search is installed
\$INSTALL_DIR = '$instdir';

#The largest acceptable word size. Reducing this saves space but decreases result accuracy. Setting it to zero ignores stemming altogether.
\$STEMCHARS = 0;

#The minimum length of a word. Any word of smaller size is not indexed.
\$MINLENGTH = 3;

#How many words should be used from the <BODY> of an html document as a description
#for the document in case there is no <META description> tag available.
\$DESC_WORDS = 25;

# Do you want to index numbers? If so set $INDEX_NUMBER to 1
\$INDEX_NUMBERS = 0;

#You shouldn't have to edit anything below this line.
\$DATA_DIR = \$INSTALL_DIR.'data/';
\$INV_INDEX_DB_FILE = \$DATA_DIR.'inv_index';
\$DOCS_DB_FILE  = \$DATA_DIR.'docs';
\$TERMS_DB_FILE  = \$DATA_DIR.'terms';
\$DF_DB_FILE  = \$DATA_DIR.'df';
\$TF_DB_FILE  = \$DATA_DIR.'tf';
\$DESC_DB_FILE  = \$DATA_DIR.'desc';
\$TITLES_DB_FILE  = \$DATA_DIR.'titles';

\$INV_INDEX_TMP_DB_FILE = \$DATA_DIR.'inv_index_tmp';
\$DOCS_TMP_DB_FILE      = \$DATA_DIR.'docs_tmp';
\$TERMS_TMP_DB_FILE     = \$DATA_DIR.'terms_tmp';
\$DESC_TMP_DB_FILE      = \$DATA_DIR.'desc_tmp';
\$TITLES_TMP_DB_FILE    = \$DATA_DIR.'titles_tmp';

\$CONF_DIR = \$INSTALL_DIR.'conf/';
\$STOPWORDS_FILE = \$CONF_DIR.'stopwords.txt';
\$NO_INDEX_FILE = \$CONF_DIR.'no_index.txt';

\@EXT = ($filetype_list);

\$TEMPLATE_DIR = \$INSTALL_DIR.'templates/';
\$SEARCH = 'search.pl';
\$SEARCH_URL = \$CGIBIN.\$SEARCH;
\$SEARCH_TEMPLATE = \$TEMPLATE_DIR.'search.html';
\$RESULTS_PER_PAGE = 5;

1;
";
close CONF;



unless(-e "$instdir/conf/no_index.txt")
  {
    open(EXCLUDE, ">$instdir/conf/no_index.txt")
      or die "Cannot open $instdir/conf/no_index.txt: $!\n";
    print EXCLUDE "$cgidir";
    close EXCLUDE;
  }

print "
Perlfect Search $VERSION has been installed on your system.
";
input("-- press enter to continue --");
system('clear') if($os eq 'unix');

if($os eq 'unix')
  {
    if(input("Do you want me to test the indexer? (this might take some time) [y/n]", "y", ["y", "n"]) eq "y")
      {
	my $rv = system("cd $instdir; ./indexer.pl");
	if($rv)
	  {
	    print "
It seems the indexer failed to index the site. Please refer to
http://www.perlfect.com/freescripts/search/faq.shtml for 
troubleshooting information.
";
	    exit;
	  }
      }
    input("-- press enter to continue --");
    system('clear') if($os eq 'unix');
    my $sendmail;
    my $whereis = `whereis sendmail`;
    if($whereis=~m/^sendmail: (\S+)/)
      {
	$sendmail = $1;
	print "
We would like to know the people that use Perlfect Search so that we
may notify them by email when upgrades become available. I can send
your registration email automatically if you are online right now.
It will only take a few seconds and it will ensure you are up to
date with the development of Perlfect Search.
";
	if(input("Do you want to register by email now? [y/n]", "y", ["y", "n"]) eq "y")
	  {
	    my $name = input("What is you name?");
	    my $email = input("What is your email address?");
	    my $date = localtime(time());
	    if(open (MAIL, "|$sendmail -t"))
	      {
		print MAIL "From: $email\n";
		print MAIL "To: search-registration\@perlfect.com\n";
		print MAIL "Subject: REGISTRATION - PERLFECT SEARCH\n";
		print MAIL "Content-type: text/plain\n\n";
		print MAIL "Name      $name\n";
		print MAIL "Email     $email\n";
		print MAIL "Version   $VERSION\n";
		print MAIL "Site      $baseurl\n";
		print MAIL "Date      $date\n";
		close MAIL;
		print "
Thank you. Your registration email has been sent!
";
		input("-- press enter to continue --");
		system('clear');
	      }
	    else
	      {
		print "
There was an error using your sendmail program. IF you want, you can register
by going to Perlfect Search's website http://www.perlfect.com/freescripts/search/
and following the instructions there to subscribe to our announcements mailing
list.
";
	      }
	  }
      }
  }

print "
To find out how to index your site and integrate Perlfect Search with 
your pages, please refer to the FAQ which can be found at
http://www.perlfect.com/freescripts/search/faq.shtml

If you think you have found a bug, or have a comment/suggestion to
make that is not covered in the FAQ, please contact us by email
at search\@perlfect.com We receive a lot of email every day regarding
Perlfect Search so please be patient with us.

Thanks for choosing Perlfect Search for your site.
Perlfect Solutions - http://www.perlfect.com

";



sub install_dir
  {
    my ($dir, $permissions) = @_;
    print "Setting up $dir\n";
    if(-e $dir)
      {
	if(-d $dir)
	  {
	    print "**Directory already exists.\n";
	  }
	else
	  {
	    print "**$dir is not a directory.\n";
	    print "**I'd better not touch it then\n**Aborting!\n";
	    exit(1);
	  }
      }
    else
      {
	unless(mkdir($dir, oct($permissions)))
	  {
	    print "**Cannot create $dir\n";
	    print "**Aborting!\n";
	    exit(1);
	  }
      }
    unless(chmod(oct($permissions), $dir))
      {
	print "**Cannot set permissions!\n**Aborting.\n";
	exit(1);
      }
  }



sub install_file
  {
    my ($source, $destination, $permissions, $keepold) = @_;
    print "Installing $source to $destination\n";
    my $uninstall;
    if(-e $destination)
      {
	print "**$destination already exists.\n";
	if($keepold)
	  {
	    print "**Preserving existing $destination\n";
	    return;
	  }
	if(rename("$destination", "$destination.bak"))
	  {
	    print "**I kept a backup in $destination.bak\n";
	  }
	else
	  {
	    print "**Cannot backup the file\n**Aborting.\n";
	    exit(1);
	  }
      }
    if(system("cp $source $destination"))
      {
	print "**Cannot copy $source to $destination\n**Aborting.";
	exit(1);
      }
    unless(chmod(oct($permissions), $destination))
      {
	print "**Cannot set permissions!\n**Aborting.\n";
	exit(1);
      }
  }


sub set_perl_path
  {
    my ($file, $path) = @_;
    print "Setting perl path in $file.\n";
    my $source = "#!$path\n";
    open(SRC, "$file") or die "Cannot open $file: $!\n";
    my $dump = <SRC>;
    while(<SRC>)
      {
	$source .= $_;
      }
    close SRC;
    open(DST, ">$file") or die "Cannot open $file: $!\n";
    print DST $source;
    close DST;
  }


sub input
  {
    my ($question, $default, $options) = @_;
    print "$question\n: ";
    my $answer = <>;
    chomp $answer;
    print "\n";
    $answer = $default unless $answer;
    unless(scalar(@$options)==0 or member($options, $answer))
      {
	print "Invalid option. Please select one of: ";
	print join(",", @$options)."\n";
	return input($question, $default, $options);
      }
    return $answer;
  }


sub member
  {
    my ($ary, $el) = @_;
    foreach (@$ary)
      {
	return 1 if($_ eq $el);
      }
    return 0;
  }
