#!/usr/local/bin/perl
#
# Archive maker for glimpseHTTP
##############################################

# Location of httpd directory
$GLIMPSEHTTP_HOME = "/usr2/local/glimpsehttp2";

# get the paths for perl and glimpse
$PERL = "/usr/local/bin/perl";
$GLIMPSE_LOC = "/usr/local/bin/glimpse";
$GLIMPSEIDX_LOC = "/usr/local/bin/glimpseindex";

# location for crontab
$CRONTAB = "/usr/bin/crontab";

# make the formats okay
if($PERL =~ /\n$/){
	chop $PERL;
}
if($GLIMPSE_LOC =~ /\n$/){
	chop $GLIMPSE_LOC;
}
if($GLIMPSEIDX_LOC =~ /\n$/){
	chop $GLIMPSEIDX_LOC;
}
if($CRONTAB =~ /\n$/){
	chop $CRONTAB;
}

##############################################
#                                            #
# no configuration is needed below this line #
#                                            #
##############################################

# index template
$MASTERTEMPLATE=$GLIMPSEHTTP_HOME."/ghtemplate.html";
$TEMPLATE=".ghtemplate.html";

# ghindex.html generation command
$GHGEN_GHINDEX="$GLIMPSEHTTP_HOME/ghgenhtml";

# name of the index file
$HTMLINDEX="ghindex.html";

# name of cron file
$CRONFILE = "ghreindex";

# name of config file
$CONFIGFILE = "archive.cfg";

# gif file
$MASTEREYEGIF = $GLIMPSEHTTP_HOME."/gheye.gif";
$EYEGIF = ".gheye.gif";

# removal script
$REMOVE = "rmgharc";

# Glimpse indexing options
# the -t option is for Glimpse v3.5 ONLY
$GLIMPSEIDX_OPTIONS = "-o -t";

# Uncomment the following line for use with Glimpse 3.0 or earlier
# $GLIMPSEIDX_OPTIONS = "-o";

######## defaults ########
$title="Glimpse HTTP Search";
$url="http://www.your.server.name.here/path/to/directory";


# permission information
$umaskval = umask(0022);
# for executables
$xmodval = 0755;


# config is the file to create
$dir = `pwd`;
chop $dir;

# print initial message
print
"This program is the GlimpseHTTP archive maker.\n";
print
"It is part of GlimpseHTTP v2.0.  For documentation,\n";
print
"see http://glimpse.cs.arizona.edu/ghttp.\n\n";

# prompt user for directory to make archive in
$indexdir = &read("Root directory for archive ", $dir);
$config = $indexdir."/".$CONFIGFILE;

# if config exists, don't allow user to continue
if (-e $config){
	print "*ERROR*: Configuration file already exists in that directory.\n";
	print "         Either use $CRONFILE to reindex the files or \n";
	print "         $REMOVE to remove the archive and configuration file.\n";
	exit -1;
}

# prompt user for information
$title =        &read("Archive title ",$title);
$url =  &read("URL for the root archive directory ",$url);
$subindex = &read_bool("Do you want a search page for each subdirectory? ","y");
$indexnow = &read_bool("Index files and create search pages now? ", "y");
$cronnow = &read_bool("Set up nightly reindexing (using crontab) now? ", "y");

# if the url has a trailing '/', get rid of it
if($url=~/\/$/) {
   chop $url;
}

# save the configuration
open(CFG,">$config") ;
print CFG $title,"\t",$url,"\t",$subindex,"\n";
close(CFG);

# copy the eye gif
$myeyegif = $indexdir."/".$EYEGIF;
`cp $MASTEREYEGIF $myeyegif`;
chmod (0644, $myeyegif);

# copy the template; do it this way, so error handling is better than cp
open(MASTER,$MASTERTEMPLATE) ||
    die "Cannot open master template file "."$MASTERTEMPLATE: $!\n";
open(TEMPLATE, ">$indexdir/$TEMPLATE") ||
    die "Cannot open template file "."$TEMPLATE: $!\n";

# copy the master, line by line
while($line = <MASTER>){
	print TEMPLATE "$line";
}
close(MASTER);

# close TEMPLATE
close(TEMPLATE);
chmod (0644, "$indexdir/$TEMPLATE");

# construct the .glimpse_exclude file
eval { open(GEX, ">.glimpse_exclude"); };
if($@){
	warn "Cannot open file .glimpse_exclude; some of the configuration files may be indexed!  $@";
}else{
	# exclude ghindex.html, config file, template file, and cron file
	print GEX "$HTMLINDEX\n";
	print GEX "$CRONFILE\n";
	print GEX "$TEMPLATE\n";
	print GEX "$CONFIGFILE\n";
	close (GEX);
}

# construct the cron file
$mycronfile = $indexdir."/".$CRONFILE;
&make_cron($mycronfile, $indexdir, $title);

# index now if needed
if($indexnow){
	print "Performing indexing (using file $mycronfile)...\n";
	print "  (This function may take a while.)\n";

	# just call the cron file we just created
	print "------------------------ output from indexing ------------------------\n";
	$output = `$mycronfile` || die "Error calling $mycronfile: ";
	print $output;
	print "--------------------- end of output from indexing --------------------\n";
}else{
	# skipping indexing
	print "***No indexing done at this time.***\n";
	print "  If you'd like to manually index later,\n";
	print "  run the script $mycronfile.\n";
}

# perform cron if needed
if($cronnow){
	### TO DO -- more extensive?
	### TO DO -- more error checking?
	print "\n\nCalling crontab to do reindexing every night at 2am...\n";
	`$CRONTAB -l > $indexdir/$CRONFILE.tmp`;

	# append to file
	open (CFILE, ">>$indexdir/$CRONFILE.tmp");
	print CFILE "\n\n# GlimpseHTTP\n";
	print CFILE "0 2 * * * $mycronfile\n\n";
	close CFILE;

	# tell crontab to use the file
	`$CRONTAB $indexdir/$CRONFILE.tmp`;

	print "Cron job successfully added.\n";
	`rm $indexdir/$CRONFILE.tmp`;
	goto end_crontab;

	cron_err:
		print "  ERROR:  Could not properly execute crontab call.\n  Cron job must be submitted later.\n";

	end_crontab:
		;
		
}else{
	# skipping cron call
	print "\n\nSkipping call to crontab.  No automatic nightly reindexing will occur.\n";
}

print "\n\n\nDONE.\n";



##############################################################################
## Subroutines
##############################################################################

sub make_cron {
	local($cronfile, $indexdir,$title) = @_;
	print "\n\nMaking reindexing script $cronfile\n";

	$SHELL = `which sh`;

	open(OUT, ">$cronfile") || die "Cannot open $cronfile: ";
	print OUT "#!$SHELL\n\n";

	print OUT "# CRONFILE FOR ARCHIVE $title IN DIRECTORY $indexdir\n\n";
	print OUT "# Do the indexing\n";
	$command = "$GLIMPSEIDX_LOC -H ".$indexdir." $GLIMPSEIDX_OPTIONS ".
		$indexdir;
	print OUT "$command\n\n";

	print OUT "# now change the directory and set the permissions\n";
	print OUT "cd $indexdir\n";
	print OUT "chmod a+r .glimpse_*\n";

	print OUT "\n\n# Regenerate the index files\n";
	print OUT "$GHGEN_GHINDEX $indexdir\n";

	close (OUT);

	# change the permissions so we can execute it
	chmod ($xmodval, $cronfile);
}

sub read_bool {
	local($prompt,$def) = @_;
	$ans = -1;
	until($ans >= 0){
		print $prompt,"[",$def,"] (y/n):";
		$| = 1;
		$_ = <STDIN>;
		chop;
		$_ = $_ ? $_ : $def;
		$_ = substr($_, 0, 1);
		if ($_ eq "y" || $_ eq "Y"){
			$ans = 1;
		}elsif ($_ eq "n" || $_ eq "N"){
			$ans = 0;
		}else {
			print "Please enter y or n.\n";
		}
	}
	return $ans;
}

sub read {
	local($prompt,$def) = @_;
	print $prompt,"[",$def,"]:";
	$| = 1;
	$_ = <STDIN>;
	chop;
	return $_?$_:$def;
}

