#!/usr/local/bin/perl
# The installation script for Glimpse HTTP

##############################################################################
# variables to obtain:
# Location of httpd directory
$GLIMPSEHTTP_HOME="/usr/local/glimpsehttp";
#
# This is URL path where the scripts are located...
$CGIBIN = "cgi-bin";
# 
# get the paths for perl and glimpse
$PERL = `which perl`;
$GLIMPSE_LOC = `which glimpse`;
$GLIMPSEIDX_LOC = `which glimpseindex`;
$gunzip = `which gunzip`;
# remove the '\n'
chop $PERL;
if(!(-e $PERL)){
	$PERL="";
}
chop $GLIMPSE_LOC;
if(!(-e $GLIMPSE_LOC)){
	$GLIMPSE_LOC="";
}
chop $GLIMPSEIDX_LOC;
if(!(-e $GLIMPSEIDX_LOC)){
	$GLIMPSEIDX_LOC="";
}
chop $gunzip;
if(!(-e $gunzip)){
	$gunzip="";
}
# 
# location for crontab
$CRONTAB = "/usr/bin/crontab";
##############################################################################

# defaults
$CGIBIN_PWD = "/usr/local/etc/httpd/cgi-bin";

$tempfile = "tmp.install";
$thisdir = `pwd`;

# print initial message
print
"This program is the GlimpseHTTP v2.0 installation script.  For documentation,\n";
print
"see http://glimpse.cs.arizona.edu/ghttp.\n\n";
print
"Note that you *must* have glimpse installed for Glimpse HTTP to work.\n";
print
"For more information on glimpse, see http://glimpse.cs.arizona.edu.\n\n";
print
"When asked for, directories should *not* have a trailing '/'s.\n\n";

# find the executables
if($PERL eq ""){
	$PERL = &read("I cannot find perl!  Where is it located?","");
}

if($GLIMPSE_LOC eq ""){
	$GLIMPSE_LOC = &read("I cannot find glimpse!  Where is it located?","");
}

if(&check_version("$GLIMPSE_LOC -V", 'version 3\.5')){
	$GLIMPSE_LOC = &read("I cannot find glimpse version 3.5!  Where is it located?","");
}

if($GLIMPSEIDX_LOC eq ""){
	$GLIMPSEIDX_LOC = &read("I cannot find glimpseindex!  Where is it located?","");
}

if(&check_version("$GLIMPSEIDX_LOC -V", 'version 3\.5')){
	$GLIMPSEIDX_LOC = &read("I cannot find glimpseindex version 3.5!  Where is it located?","");
}

if($CRONTAB eq ""){
	$CRONTAB = &read("I cannot find crontab using 'which'!  Where is it located?","");
}

if($gunzip eq ""){
	$gunzip = &read("I cannot find gunzip using 'which'!  Where is it located?","");
}

# prompt user for directory to make archive in
$GLIMPSEHTTP_HOME = 
	&read("Which directory should I copy the Glimpse HTTP *binaries* to?\n(This directory should be created if it doesn't already exist)\n", 
	$GLIMPSEHTTP_HOME);
$CGIBIN_PWD = 
	&read("Which directory should I copy the Glimpse HTTP cgi-bin scripts to?\n(This directory should be created if it doesn't already exist)\n",
	$CGIBIN_PWD);

# ask for the settings
$CGIBIN = 
	&read("What is the cgi-bin url *path* (no machine name or initial '/')\nfor the Glimpse HTTP cgi-bin scripts\n",
	$CGIBIN);

# format the paths correctly
# there should be no trailing \n's
# NO trailing '/'s
if($PERL=~/\/$/) {
	chop $PERL;
}
if($GLIMPSE_LOC=~/\/$/) {
	chop $GLIMPSE_LOC;
}
if($GLIMPSEIDX_LOC=~/\/$/) {
	chop $GLIMPSEIDX_LOC;
}
if($CRONTAB=~/\/$/) {
	chop $CRONTAB;
}
if($gunzip=~/\/$/) {
	chop $gunzip;
}
if($GLIMPSEHTTP_HOME=~/\/$/) {
	chop $GLIMPSEHTTP_HOME;
}
if($CGIBIN_PWD=~/\/$/) {
	chop $CGIBIN_PWD;
}
if($CGIBIN=~/\/$/) {
	chop $CGIBIN;
}

# no beginning '/' on cgibin_url
if($CGIBIN=~/^\//) {
	$CGIBIN=substr($CGIBIN,1);
}
	

# prompt for correctness
print "-----------------------------------------------------\n";
print "perl: $PERL\n";
print "glimpse: $GLIMPSE_LOC\n";
print "glimpseindex: $GLIMPSEIDX_LOC\n";
print "crontab: $CRONTAB\n";
print "gunzip: $gunzip\n";
print "GlimpseHTTP binaries directory: $GLIMPSEHTTP_HOME\n";
print "cgi-bin directory: $CGIBIN_PWD\n";
print "cgi-bin *relative* URL: $CGIBIN\n";
print "-----------------------------------------------------\n";

print "To continue with these settings, press RETURN.  Otherwise, press ^C to abort.\n";
$_=<STDIN>;

# change the settings in the files
# for the files: 
@changefiles=("cgi-bin/aglimpse", 
				"cgi-bin/mfs",
				"getfile",
				"ghgenhtml",
				"makegharc",
				"rmgharc");

# make sure cgi-bin directory is readable and executable by everyone
if(chmod (0755, "cgi-bin")==0){
	die "Cannot change permissions of cgi-bin directory.  Aborting install.\n";
}

foreach $file ( @changefiles ){
	# make sure we can overwrite the file
	if(chmod (0755, $file)==0){
		die "Cannot change permissions of $file.  Aborting install.\n";
	}

	# open the file
	open(FILE, $file) || die "Aborting install.  Cannot open file $file: ";

	# open a temp file 
	open(TEMP, ">$tempfile") 
		||die "Aborting install.  Cannot open temporary file: ";
	
	while(<FILE>){
		if(/^\$PERL *=/){
			print TEMP "\$PERL = \"$PERL\";\n";
		}elsif(/^\$GLIMPSE_LOC *=/){
			print TEMP "\$GLIMPSE_LOC = \"$GLIMPSE_LOC\";\n";
		}elsif(/^\$GLIMPSEIDX_LOC *=/){
			print TEMP "\$GLIMPSEIDX_LOC = \"$GLIMPSEIDX_LOC\";\n";
		}elsif(/^\$CRONTAB *=/){
			print TEMP "\$CRONTAB = \"$CRONTAB\";\n";
		}elsif(/^\$GLIMPSEHTTP_HOME *=/){
			print TEMP "\$GLIMPSEHTTP_HOME = \"$GLIMPSEHTTP_HOME\";\n";
		}elsif(/^\$CGIBIN *=/){
			print TEMP "\$CGIBIN = \"$CGIBIN\";\n";
		}elsif(/^\$gunzip *=/){
			print TEMP "\$gunzip = \"$gunzip\";\n";
		}elsif(/^#!\/usr\/local\/bin\/perl$/){
			print TEMP "#!$PERL\n";
		}else{
			print TEMP $_;
		}
	}
	close (TEMP);
	close (FILE);

	# overwrite $file with $tempfile
	if(rename($tempfile, $file)==0){
		die "Cannot rename $tempfile to $file.\n";
	}

	# make sure everyone can read and execute the file
	if(chmod (0755, $file)==0){
		die "Cannot change permissions of $file.  Aborting install.\n";
	}
}

# make sure GLIMPSEHTTP_HOME  and CGIBIN_PWD directories exist
if(!(-e $GLIMPSEHTTP_HOME)){
	### TO DO -- make the directory
	# for right now, die
	print "The directory $GLIMPSEHTTP_HOME must exist.\n";
	print "Please create this directory then rerun this installation program.\n";
	exit(1);
}
if(!(-e $CGIBIN_PWD)){
	### TO DO -- make the directory
	# for right now, die
	print "The directory $CGIBIN_PWD must exist.\n";
	print "Please create this directory then rerun this installation program.\n";
	exit(1);
}

# make sure GLIMPSEHTTP_HOME  and CGIBIN_PWD directories have permissions
chmod (0755, $GLIMPSEHTTP_HOME);
chmod (0755, $CGIBIN_PWD);

# copy the files
### TO DO
@cgibinfiles=("aglimpse", 
				"mfs");
@glimpsebins=("getfile",
				"ghgenhtml",
				"makegharc",
				"rmgharc",
				"gheye.gif",
				"ghtemplate.html");

# copy the cgi-bins into the directory
chmod (0755, @cgibinfiles);
chmod (0755, "$CGIBIN_PWD");
foreach $file (@cgibinfiles){
	`mv cgi-bin/$file $CGIBIN_PWD/$file`;
}

# copy the binaries into the directory
chmod (0755, @glimpsebins);
# these aren't +x
chmod (0644, "gheye.gif","ghtemplate.html");

chmod (0755, $GLIMPSEHTTP_HOME);
foreach $file (@glimpsebins){
	`mv $file $GLIMPSEHTTP_HOME/$file`;
}

print
"\n\nDONE with GlimpseHTTP installation.\n\n";
print
"The cgi-bin directory $CGIBIN_PWD will need to be added\n";
print
" to your server configuration file as a cgi-bin directory.\n\n";
print
"See http://glimpse.cs.arizona.edu/ghttp for more\n";
print
"information on GlimpseHTTP.\n";


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

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 "\n",$prompt,"[",$def,"]:";
	$| = 1;
	$_ = <STDIN>;
	chop;
	return $_?$_:$def;
}

# courtesy of edo@lava.net
sub check_version {
	local($cmd, $ver) = @_;
	if(`$cmd` =~ "$ver"){
		$ans=0;
	}else{
		$ans = 1;
	}
	return $ans;
}
