: # *-*-perl-*-*
    eval 'exec perl -S $0 "$@"'
    if $running_under_some_shell;  
#
#  CreateBroker - Generates a new Broker based on user input.
#  
#  Usage: CreateBroker [skeleton-tree [destination]]
#
#  	For example, CreateBroker /usr/local/harvest/brokers/skeleton
#
#  Darren Hardy, hardy@cs.colorado.edu, October 1994
#
#  $Id: CreateBroker,v 1.19 1995/03/22 06:35:13 hardy Exp $
#
#######################################################################
#
#  Copyright (c) 1994.  All rights reserved.
#          Mic Bowman of Transarc Corporation.
#          Peter Danzig of the University of Southern California.
#          Darren R. Hardy of the University of Colorado at Boulder.
#          Udi Manber of the University of Arizona.
#          Michael F. Schwartz of the University of Colorado at Boulder.
#          Duane Wessels of the University of Colorado at Boulder.
#  
#  This copyright notice applies to all code in Harvest other than subsystems
#  developed elsewhere, which contain other copyright notices in their source
#  text.
#  
#  The Harvest software was developed by the Internet Research Task Force
#  Research Group on Resource Discovery (IRTF-RD).  The Harvest software may
#  be used for academic, research, government, and internal business purposes
#  without charge.  The Harvest software may not be sold or distributed to
#  commercial clients or partners without explicit permission from the
#  copyright holders.
#  
#  The Harvest software is provided "as is", without express or implied
#  warranty, and with no support nor obligation to assist in its use,
#  correction, modification or enhancement.  We assume no liability with
#  respect to the infringement of copyrights, trade secrets, or any patents,
#  and are not responsible for consequential damages.  Proper use of the
#  Harvest software is entirely the responsibility of the user.
#  
#  For those who are using Harvest for non-commercial purposes, you may make
#  derivative works, subject to the following constraints:
#          1. You must include the above copyright notice and these
#             accompanying paragraphs in all forms of derivative works, 
#             and any documentation and other materials related to such
#             distribution and use acknowledge that the software was
#             developed at the above institutions.
#          2. You must notify IRTF-RD regarding your distribution of the
#             derivative work.
#          3. You must clearly notify users that your are distributing a
#             modified version and not the original Harvest software.
#          4. Any derivative product is also subject to the restrictions of
#             the copyright, including distribution and use limitations.
#
$ENV{'HARVEST_HOME'} = "/usr/local/harvest" if (!defined($ENV{'HARVEST_HOME'}));
$ENV{'PATH'} = $ENV{'PATH'} . ":" . "$ENV{'HARVEST_HOME'}/bin" .
			      ":" . "$ENV{'HARVEST_HOME'}/lib/broker". 
			      ":" . "$ENV{'HARVEST_HOME'}/lib";
$whoami_cmd = "whoami";
$whoami_cmd = "/bin/whoami" if (-x "/bin/whoami");
$whoami_cmd = "/usr/bin/whoami" if (-x "/usr/bin/whoami");
$whoami_cmd = "/usr/ucb/whoami" if (-x "/usr/ucb/whoami");
chop($this_person = &grab_cmd_output("$whoami_cmd"));
$hostname_cmd = "hostname";
$hostname_cmd = "/usr/bin/hostname" if (-x "/usr/bin/hostname");
$hostname_cmd = "/usr/ucb/hostname" if (-x "/usr/ucb/hostname");
$hostname_cmd = "/bin/hostname" if (-x "/bin/hostname");
chop($this_host = &grab_cmd_output("$hostname_cmd"));
($this_host_full, @x) = gethostbyname($this_host);
undef @x;

print <<EOM;

  ----------------------------------------------------------------------
                           Welcome to Harvest!

   This program will create and configure a Harvest Broker.  First, you 
   will be asked a few questions about how you want this new Broker to 
   look.  Second, this program will create the Broker and configure it.  
   Finally, this program will generate a script that can be used to run 
   the new Broker.  If you have any questions or problems, please refer 
   to http://harvest.cs.colorado.edu/support.html.
  ----------------------------------------------------------------------

EOM

while (1) {
	&init_vars();
	&get_user_input();
	&print_info();

	$vals{"YorN"} = "yes";
	&get_ans("Is this information correct?", "YorN");
	last if ($vals{"YorN"} =~ /^y/io);
}

&build_broker();
print <<EOM;

  Harvest Brokers can collect indexing information from Gatherers or 
  other Brokers.  The list of collection points for your Broker is in 
  $vals{'Destination-Directory'}/admin/Collection.conf.

EOM
$vals{"YorN"} = "yes";
while (1) {
	print "\n";
	&get_ans("Would you like to add a collection point to the Broker now?", "YorN");
	last if ($vals{"YorN"} =~ /^n/io);
	&add_collection_point("$vals{'Destination-Directory'}/admin/Collection.conf");
	$vals{"YorN"} = "no";
}
print <<EOM;

  ----------------------------------------------------------------------
  The new Broker was successfully built.  To start the new Broker, run

        $vals{'Destination-Directory'}/RunBroker 

  The new Broker's home page is:

        http://$vals{'HTTP-Host'}/Harvest/brokers/$vals{'Broker-Short-Name'}/query.html

  The new Broker's administrative interface is:

        http://$vals{'HTTP-Host'}/Harvest/brokers/$vals{'Broker-Short-Name'}/admin/admin.html

  Please register your Broker with the Harvest Server Registry:

    http://harvest.cs.colorado.edu/Harvest/brokers/hsr/register-with-hsr.html

  ----------------------------------------------------------------------

EOM
exit 0;		# END OF PROGRAM

sub get_ans {
	local($prompt, $tag) = @_;

	$default_value = "";
	$default_value = $vals{$tag} if (defined($vals{$tag}));
	print $prompt, " [$default_value]: ";
	$in = <STDIN>;
	chop($in);
	$vals{$tag} = $in if ($in !~ /^\s*$/io);
}

sub print_info {
	print<<EOM;

  ----------------------------------------------------------------------
EOM
	foreach $key (sort keys %vals) {
		next if ($key eq "YorN");
	   	next if ($key eq "Skeleton-Directory");
		next if ($key =~ /glimpse/io && $INDEXER ne "glimpse");
		next if ($key =~ /wais/io && $INDEXER ne "wais");
		printf "%21s: ", $key;
		if ($vals{$key} eq "No_Default") {
			print "none";
		} else {
			print $vals{$key};
		}
		print "\n";
	}
	print<<EOM;
  ----------------------------------------------------------------------
EOM
	print "\n";
}

sub init_vars {
	print <<EOM;

  CreateBroker will try to set the default answers as correctly as possible.
  Hold on a moment, trying to guess your configuration...
EOM
	$vals{"Broker-Host"} = $this_host_full;
	$vals{"HTTP-Host"} = $vals{"Broker-Host"};
	$vals{"Broker-Name"} = "No_Default";
	$vals{"Broker-Short-Name"} = "No_Default";
	$vals{"Broker-Port"} = "8501";
	$vals{"Broker-Password"} = "";
	$vals{"Contact-Email"} = "$this_person\@$this_host_full";
	$vals{"Contact-Message"} = "";
	$vals{"glimpse-Command"} = "$ENV{'HARVEST_HOME'}/lib/broker/glimpse";
	$vals{"glimpseindex-Command"} = "$vals{'glimpse-Command'}index";
	$vals{"glimpseserver-Command"} = "$vals{'glimpse-Command'}server";
	$vals{'Broker-WAIS-Host'} = $vals{'Broker-Host'};
	$vals{'Broker-WAIS-Port'} = $vals{'Broker-Port'} + 1;
	$vals{'Broker-Glimpse-Host'} = $vals{'Broker-Host'};
	$vals{'Broker-Glimpse-Port'} = $vals{'Broker-Port'} + 1;
	$vals{'Broker-Description-Tag'} = "description";
	$vals{'Broker-Replicas'} = "";
	$INDEXER = "glimpse";
	srand(time);
	$vals{'Broker-Collect'} = sprintf("%02d:%02d", rand(8), rand(59));
	
	foreach $key ("waisindex", "waissearch", "waisserver", "waisparse", "gunzip") {
		chop($vals{"$key-Command"} = &grab_cmd_output("which $key"));
		$vals{"$key-Command"} = "No_Default" 
			if ($vals{"$key-Command"} !~ /^\//o);
	}
	$vals{"Skeleton-Directory"} = "$ENV{'HARVEST_HOME'}/brokers/skeleton";
}


sub get_user_input {
print "\n\n";
&get_ans("Enter a one-line descriptive name for your Broker",
	   "Broker-Name");
&get_ans("Enter a one-word name for your Broker",
	   "Broker-Short-Name");
if ($vals{"Broker-Short-Name"} ne "No_Default") {
	$vals{"Destination-Directory"} = "$ENV{'HARVEST_HOME'}/brokers/" .  $vals{"Broker-Short-Name"};
}
&get_ans("Enter the hostname on which the broker will run", 
	   "Broker-Host");
&get_ans("Enter the port number on which the broker will run",
	   "Broker-Port");
&get_ans("Enter the password to the broker administrative interface", 
	   "Broker-Password");
&get_ans("Enter the hostname for your HTTP server.  If you want to use\na different port number than 80, append it to the hostname\nwith a colon (e.g., www.cs.colorado.edu:9000)", 
	   "HTTP-Host");
&get_ans("Enter your email address",
	   "Contact-Email");
$vals{"Contact-Message"} = "Please send questions and comments to $vals{'Contact-Email'}" if ($vals{'Contact-Email'} ne "");
&get_ans("Enter the name of the attribute that will be displayed to the\nuser as one-line object description in search results",
	   "Broker-Description-Tag");
$vals{"Indexer"} = "glimpse";
&get_ans("Do you want to use Glimpse or WAIS as an Indexer",
	   "Indexer");
$INDEXER = "glimpse";
if ($vals{"Indexer"} =~ /wais/io) {
	$INDEXER = "wais";
} 

if ($vals{"Indexer"} =~ /glimpse/io) {
	&get_ans("Enter the pathname to the glimpse command", 
	   	"glimpse-Command");
	&get_ans("Enter the pathname to the glimpseindex command", 
	   	"glimpseindex-Command");
	&get_ans("Enter the pathname to the glimpseserver command", 
	   	"glimpseserver-Command");
	&get_ans("Enter the host on which to run glimpseserver", 
	   	"Broker-Glimpse-Host");
	&get_ans("Enter the port on which to run glimpseserver", 
	   	"Broker-Glimpse-Port");
} else {
	$vals{'Broker-WAIS-Host'} = $vals{'Broker-Host'};
	$vals{'Broker-WAIS-Port'} = $vals{'Broker-Port'} + 1;
	$vals{'WAIS-Flavor'} = "WAIS";
	$vals{"YorN"} = "no";
	&get_ans("Are you using the commericial version of WAIS?", "YorN");
	if ($vals{"YorN"} =~ /^y/io) {
		&get_ans("Enter the pathname to the waisparse command", 
	   		"waisparse-Command");
		$vals{'WAIS-Flavor'} = "Commerical-WAIS";
	} else {
		$vals{'waisparse-Command'} = "none";
	}
	&get_ans("Enter the host on which to run the WAIS server", 
	   	"Broker-WAIS-Host");
	&get_ans("Enter the port on which to run the WAIS server", 
	   	"Broker-WAIS-Port");
	&get_ans("Enter the pathname to the waisindex command", 
	   	"waisindex-Command");
	&get_ans("Enter the pathname to the waissearch command", 
	   	"waissearch-Command");
	&get_ans("Enter the pathname to the waisserver command", 
	   	"waisserver-Command");
}
	
&get_ans("Enter the destination Broker directory",
   	"Destination-Directory");
if (! -d $vals{"Skeleton-Directory"}) {
	print "Couldn't find the Broker skeleton directory.\n";
	&get_ans("Enter the Broker skeleton directory",
   		"Skeleton-Directory");
}
}	# end of get_user_input

sub build_broker {
	print <<EOM;

  ----------------------------------------------------------------------
  CreateBroker will now perform the following tasks:
      1. Create the directory $vals{'Destination-Directory'}
         in which the new Broker will live.
      2. Build the Broker configuration files from the skeleton Broker 
         tree in $vals{'Skeleton-Directory'}
      3. Create the program $vals{'Destination-Directory'}/RunBroker
  ----------------------------------------------------------------------

EOM
	$vals{"YorN"} = "yes";
	&get_ans("Should I build this Broker now?", "YorN");
	return if ($vals{"YorN"} !~ /^y/io);

	$vals{'Broker-Indexer-Type'} = "Glimpse";
	$vals{'Broker-Indexer-Type'} = "WAIS" if ($INDEXER =~ /wais/io);
	#  Make the broker directory
	mkdir($vals{'Destination-Directory'}, 0755) || die 
		"Cannot make $vals{'Destination-Directory'}: $!\n";
	mkdir("$vals{'Destination-Directory'}/admin", 0755) || die 
		"Cannot make $vals{'Destination-Directory'}/admin: $!\n";

	@files = ("Makefile", "RunBroker", "Query.cgi", "query-$INDEXER.html",
		"admin/Collection.conf", "admin/broker.conf",
		"admin/admin.html");

	foreach $file (@files) {
		open(IN, "< $vals{'Skeleton-Directory'}/$file.in") || die
		"Cannot read $vals{'Skeleton-Directory'}/$file.in: $!\n";
		open(OUT, "> $vals{'Destination-Directory'}/$file") || die
		"Cannot create $vals{'Destination-Directory'}/$file: $!\n";
		print "Creating $vals{'Destination-Directory'}/$file\n";
		while (<IN>) {
	s/\@THIS_HARVEST_HOME\@/$ENV{'HARVEST_HOME'}/g;
	s/\@THIS_HTTP_HOST\@/$vals{'HTTP-Host'}/g;
	s/\@CONTACT_EMAIL\@/$vals{'Contact-Email'}/g;
	s/\@CONTACT_MESSAGE\@/$vals{'Contact-Message'}/g;
	s/\@THIS_BROKER_COLLECT\@/$vals{'Broker-Collect'}/g;
	s/\@THIS_BROKER_DIR\@/$vals{'Destination-Directory'}/g;
	s/\@THIS_BROKER_NAME\@/$vals{'Broker-Name'}/g;
	s/\@THIS_BROKER_SHORT_NAME\@/$vals{'Broker-Short-Name'}/g;
	s/\@THIS_BROKER_PORT\@/$vals{'Broker-Port'}/g;
	s/\@THIS_BROKER_PASSWORD\@/$vals{'Broker-Password'}/g;
	s/\@THIS_BROKER_HOST\@/$vals{'Broker-Host'}/g;
	s/\@THIS_BROKER_GLIMPSE\@/$vals{'glimpse-Command'}/g;
	s/\@THIS_BROKER_GLIMPSEINDEX\@/$vals{'glimpseindex-Command'}/g;
	s/\@THIS_BROKER_GLIMPSESERVER\@/$vals{'glimpseserver-Command'}/g;
	s/\@THIS_BROKER_GLIMPSE_HOST\@/$vals{'Broker-Glimpse-Host'}/g;
	s/\@THIS_BROKER_GLIMPSE_PORT\@/$vals{'Broker-Glimpse-Port'}/g;
	s/\@THIS_BROKER_INDEXER_TYPE\@/$vals{'Broker-Indexer-Type'}/g;
	s/\@THIS_BROKER_WAIS_FLAVOR\@/$vals{'WAIS-Flavor'}/g;
	s/\@THIS_BROKER_WAISINDEX\@/$vals{'waisindex-Command'}/g;
	s/\@THIS_BROKER_WAISPARSE\@/$vals{'waisparse-Command'}/g;
	s/\@THIS_BROKER_WAISSEARCH\@/$vals{'waissearch-Command'}/g;
	s/\@THIS_BROKER_WAISSERVER\@/$vals{'waisserver-Command'}/g;
	s/\@THIS_BROKER_WAIS_HOST\@/$vals{'Broker-WAIS-Host'}/g;
	s/\@THIS_BROKER_WAIS_PORT\@/$vals{'Broker-WAIS-Port'}/g;
	s/\@THIS_BROKER_DESCRIPTION\@/$vals{'Broker-Description-Tag'}/g;
	s/\@REPLICAS\@/$vals{'Broker-Replicas'}/g;
	s#\@THIS_BROKER_TOPDIR\@#$ENV{'HARVEST_HOME'}#g;
	s#\@THIS_BROKER_GUNZIP\@#$vals{'gunzip-Command'}#g;
			print OUT;
		}
		close(IN);
		close(OUT);
	}
	chmod(0755, "$vals{'Destination-Directory'}/RunBroker");
	chmod(0755, "$vals{'Destination-Directory'}/Query.cgi");
	system("ln -s query-$INDEXER.html $vals{'Destination-Directory'}/query.html");
	system("ln -s query.html $vals{'Destination-Directory'}/index.html");

	print "Finished.\n\n";
}


sub add_collection_point {
	local($file) = @_;

	$vals{'Collection-Host'} = $this_host_full;
	$vals{'Collection-Port'} = 8000;
	$vals{'Collection-Type'} = 3;
	$vals{'Collection-Query'} = "";

	&get_ans("Enter the host from which to collect",
		   "Collection-Host");
	&get_ans("Enter the port from which to collect",
		   "Collection-Port");
	print <<EOM;
The Broker supports various types of collections:
    0 - From Gatherer: Full collection each time,    without data compression.
    1 - From Gatherer: Incremental collections,      without data compression.
    2 - From Gatherer: Full collection each time,    with    data compression.
    3 - From Gatherer: Incremental collections,      with    data compression.
    4 - From Broker:   Full collection each time,    without data compression.
    5 - From Broker:   Incremental collections,      without data compression.
    6 - From Broker:   Collection based on a query,  without data compression.
    7 - From Broker:   Incremental based on a query, without data compression.
EOM
	&get_ans("Enter the collection type",
		   "Collection-Type");
	if ($vals{"Collection-Type"} == "6") {
		&get_ans("Enter the query filter", "Collection-Query");
	}
	if ($vals{"Collection-Type"} == "7") {
		&get_ans("Enter the query filter", "Collection-Query");
	}

	print <<EOM;

  ----------------------------------------------------------------------
EOM
	foreach $key (	"Collection-Host", 
			"Collection-Type",
			"Collection-Port", 
			"Collection-Query") {
		printf "%21s: ", $key;
		print $vals{$key}, "\n";
	}
	print <<EOM;
  ----------------------------------------------------------------------
EOM
	$vals{"YorN"} = "yes";
	&get_ans("Is this information correct?", "YorN");
	&add_collection_point($file), return if ($vals{"YorN"} =~ /^n/io);

	open(OUT, ">> $file") || die "Cannot append to $file: $!\n";
	print OUT $vals{'Collection-Host'}, " ";
	print OUT $vals{'Collection-Port'}, " ";
	print OUT $vals{'Collection-Type'}, " ";
	print OUT "--";
	print OUT $vals{'Collection-Query'}, "\n";
	close(OUT);
	print "Ok, the collection point was successfully added.\n\n";
}

#
#  This is an ugly hack so that it works with Perl 4.036 on Solaris 2.3.
#  The backticks (`) don't work on Solaris like they should. -Darren.
#
sub grab_cmd_output {
	local($the_cmd) = @_;
	undef $the_var;
	unlink("/tmp/cmdoutput.$$");
	system("$the_cmd > /tmp/cmdoutput.$$");
	open(CMDOUT, "< /tmp/cmdoutput.$$") || return "none";
	$the_var = <CMDOUT>;
	close(CMDOUT);
	unlink("/tmp/cmdoutput.$$");
	return $the_var;
}
