#!/usr/bin/perl
# configure script for yesiwill
# nickie halflinger & the yes men, november 2001
# GNU GPL, 2001
# Version: $Name: beta20011115c $
# run this with './configure' or 'perl configure'.
#
# $Id: configure,v 1.11 2001/11/15 12:16:37 codemonkey Exp $
#
############################################################

use strict;
# use Data::Dumper;
use File::Copy;
use Getopt::Std;

my %opts;

getopts('dh', \%opts);
if(defined $opts{h}) {
	print "usage: ./configure [-d] [-h]\n";
	print "\t-d\trun non-interatively, all default values.\n";
	print "\t-h\tdisplay this help message.\n";
	exit;
}

my $config_template = 'iwill.conf.tmpl';
my $config_target = 'cgi-bin/iwill.conf';
my $script_target = 'cgi-bin/yesiwill.pl';
my $perl = `which perl`;
my $host = `hostname`;
my $pwd = `pwd`;
chomp $host;
chomp $pwd;
chomp $perl;

# this is an array of hashrefs of tokennames keyed to defaults and question texts.
my @configs = (
	{'token' 	=>	'perl',
	 'question' 	=>	'Where is perl?',
	 'default'	=>	$perl, },
	 
	 {'token' 	=>	'givemedomain',
	 'question' 	=>	'What site (domain) would you like to appropriate?',	
	 'default'	=>	'www.wto.org', },
	 
	 {'token' 	=>	'givemealsogreedy',
	 'question' 	=>	"Other domains of hosts found linked from the target domain, to also appropriate as needed?\n(Subdomains--xxx.domain.com--will also be appropriated.)",	
	 'default'	=>	'wto.org, wto-ministerial.org', },
	
# could try to take a look at $host, remove the .com or whatever, 
#    and see if it occurs in $pwd (looking from the end).... 
	 {'token' 	=>	'putithere',
	 'question' 	=>	'Web URL where your mirrored site will show up:',	
	 'default'	=>	$host . $pwd, },
	 	 
	 {'token' 	=>	'cgi-bin',
	 'question' 	=>	'CGI directory: In what directory would you like to install the site-copying program itself?',	
	 'default'	=>	'./cgi-bin', },
	 
	 {'token' 	=>	'cgiurl',
	 'question' 	=>	'CGI url: URL to directory where program will be: ',	
	 'default'	=>	'cgi-bin', },
	 	 
	 {'token' 	=>	'basepath',
	 'question' 	=>	'Optional: Unix path to your web directory, if your cgi directory is somewhere different:',	
	 'default'	=>	'', },
	 	 
	 {'token' 	=>	'sublist',
	 'question' 	=>	'Name of file containing substitution list?',	
	 'default'	=>	'substitutions.txt', },
	 
	 {'token' 	=>	'smartsubstitutions',
	 'question' 	=>	'"Smart" substitution: Change plurals, past tense, etc? (enter 0 for no, 1 for yes)',
	 'default'	=>	'1', },
	 
	 {'token' 	=>	'cachedir',
	 'question' 	=>	'Relative path to directory where you\'d like to store cache files: ',	
	 'default'	=>	'stolen', },
	 
	 {'token' 	=>	'killtime',
	 'question' 	=>	'Request timeout in seconds:',	
	 'default'	=>	'40', },
	
	 {'token' 	=>	'cachetime',
	 'question' 	=>	'Cached files will be used for this many minutes without checking for new ones:',	
	 'default'	=>	'10', },
	 
#	 {'token' 	=>	'dbgfile',
#	 'question' 	=>	'Debug log: ',	
#	 'default'	=>	'dbg', },
	 
#	 {'token' 	=>	'slashfile',
#	 'question' 	=>	'Cache URLs ending in "/" in this file:',	
#	 'default'	=>	'slashfile', },
	 
	 
);

print "\n\nHello website fan! Welcome to the YesIWill configure tool. Please 
answer the following questions.   The defaults are usually okay if you're 
unsure.  After running this you can make extra changes to the configuration by 
editing the iwill.conf file.\n";

my (%varhash,$answer);
# loop through these and ask for values to them.
foreach my $i (@configs) {
	print "\n", $i->{question}, " [$i->{default}]: ";
	unless($opts{d}) {
		$answer = <STDIN>; chomp $answer;
		# print "\t\tanswer was '$answer'.\n";		# debugging..
	}
	$varhash{$i->{token}} = $answer !~ /^[\n\r]*$/ ? $answer : $i->{default};
	print "\n";
}

# now parse through the config file template and fill in the values.
open(F, $config_template) || die "can't read configuration template: $!\n";
open(OUT, ">$config_target") || die "can't write to configuration file: $!\n";
	
while(<F>) {
	s/\[\[\[(.+)\]\]\]/$varhash{$1}/ig;
	print OUT;
}
close OUT;
close F;
	
print "configuration file created.\n\n";

# do other special things based on some of the answers.
# first, change the perl path if neccesary
unless($varhash{perl} eq $configs[0]->{default}) {
	open(F, $script_target) || die "ERROR: couldn't read script: $!\n";
	my @lines = <F>;
	close F;
	$lines[0] = "$varhash{perl}\n";
	open(F, ">$script_target") || die "ERROR: couldn't write script: $!\n";
	print F @lines;
	close F;
	print "perl path set in $script_target.\n";
}

# now change the RewriteBase in the .htaccess file.
# we figure this out from the "putithere" variable.
# rewritebase is unneccesary if the URL is the root of the site.
my $rewritebase;
my($domain, @path) = split '/', $varhash{putithere};

if(@path) {
	$rewritebase = "RewriteBase	/" . join '/', @path;
	$rewritebase =~ s#([^/])$#$1/#;    # make sure slash at end
	# this is to fix the "trailing slash" problem.
	$rewritebase .= "\nRewriteRule\t^$path[$#path]\$	$path[$#path]/\t[R]";
} else {
	$rewritebase = "# RewriteBase	/foo/bar/";
}

# make sure cgi url is correct. no http, no slash at end.
$varhash{cgiurl} =~ s#^http://##i;
$varhash{cgiurl} =~ s#/$##;

open(TMPL, "htaccess.tmpl") || die "ERROR: couldn't read htaccess.tmpl: $!\n";
my @lines = <TMPL>;
close TMPL;
open(F, ">.htaccess") || die "ERROR: couldn't write to .htaccess: $!\n";
foreach (@lines) {
	s/\[\[\[rewritebase\]\]\]/$rewritebase/ig;
	s/\[\[\[cgiurl\]\]\]/$varhash{cgiurl}/ig;
	print F;
}
close F;
print ".htaccess has been prepared.\n";

# now copy the script and the conf file to the cgi directory, if different
# from the default.
unless($varhash{'cgi-bin'} =~ m#^\.?/?cgi-bin/?#) {
	copy($config_target, "$varhash{'cgi-bin'}/iwill.conf")  
		|| die "trouble copying config file to $varhash{'cgi-bin'}/iwill.conf: $!\n";
	copy($script_target, "$varhash{'cgi-bin'}/yesiwill.pl") 
		|| die "trouble copying script to $varhash{'cgi-bin'}/yesiwill.pl: $!\n";
	print "CGI files copied to $varhash{'cgi-bin'}\n";
}

$varhash{basepath} =~ s#([^/])$#$1/#;     # make sure slash at end
# if basepath is different than where we currently are, move the other files there.
if($varhash{basepath} && ($varhash{basepath} !~ m#^$pwd/?$#)) {	
	my @files = ('mime.types', '.htaccess',  $varhash{sublist});
	foreach (@files) {
		copy($_, $varhash{basepath} . $_) || die "trouble copying $_:$!\n";
	}
	
	print join ',', @files, " copied to $varhash{basepath}.\n";
}

# make cache directory if it's not there already.
# (for some reason my version of perl requires the second parameter for mkdir--codemonkey)
mkdir $varhash{basepath} . $varhash{cachedir}, 0777 
	|| warn $varhash{basepath} . $varhash{cachedir} . " $!\n";
chmod 0777, $varhash{basepath} . $varhash{cachedir}
	|| warn "could not chmod $varhash{basepath}$varhash{cachedir}: $!\n";
print "created and chmoded cachedir '$varhash{basepath}$varhash{cachedir}'.\n";
# print Dumper(%varhash);  # debugging

print "\nReady to go! Again, make sure you have rewrite privileges for your web directory\n(see readme.txt for details).\nIf you're brave, make additional changes to $varhash{'cgi-bin'}/iwill.conf.\n\n";

 
	 
	 
	 
