#!/usr/local/bin/perl -i

chop ($WorkingDirectory = `pwd`);
print "\rPlease specify veronica root directory: \n(/usr/local/veronica) ";
chop ($veronicaRoot = <STDIN>);
if (!$veronicaRoot) {$veronicaRoot = '/usr/local/veronica';}
if (!-e $veronicaRoot)
{
	print "veronica root directory does not exist.  Create it? (y/n) ";
	chop ($Answer = <STDIN>);
	if ($Answer =~ /y/i)
	{
		`mkdir $veronicaRoot`;
	}
}

print "\nPlease specify where the stop list will be located: \n($veronicaRoot/etc/stoplist) ";
chop ($stoplist = <STDIN>);
if (!$stoplist) {$stoplist = "$veronicaRoot/etc/stoplist";}

print "\nPlease specify where to create the log [\"none\" to turn logging off\]: \n($veronicaRoot/etc/veronica.log) ";
chop ($Log = <STDIN>);
if (!$Log) {$Log = "$veronicaRoot/etc/veronica.log";}
if ($Log =~ /none/i) {$Log = '';}

while (1)
{
	print "\nPlease specify the full name of this machine including domain: ";
	chop ($veronicaServer = <STDIN>);
	last if ($veronicaServer =~ /^\w+\.(\w+){1,3}/);
}

while (1)
{
	print "\nPlease specify the domain to index: ";
	chop ($Domain = <STDIN>);
	last if ($Domain =~ /^(\w+){1,3}/);
}

print "\nPlease specify the port where this script will run: (2347) ";
chop ($veronicaPort = <STDIN>);
if (!$veronicaPort) {$veronicaPort = "2347";}

chdir ($veronicaRoot) || die "Can't change to veronica root: $!";

if (!-e "bin")     {`mkdir bin`;}
if (!-e "Data")    {`mkdir Data`;}
if (!-e "etc")     {`mkdir etc`;}
if (!-e "NewData") {`mkdir NewData`;}

@BinFiles = ("look", "make-dbm", "make-go", "reset", "tree.local",
             "ver-server-0.6b.pl", "veronica.indexer");

foreach $File (@BinFiles)
{
	open (File, "$WorkingDirectory/$File");
	if (-e "bin/$File") {rename ("bin/$File", "bin/$File.old");}
	open (OUT, "> bin/$File");
	while (<File>)
	{
		if (/###\$EtcDir/)
		{
			print OUT "\$EtcDir = '$veronicaRoot/etc';\n";
		}
		elsif (/###\$NewDataDir/)
		{
			print OUT "\$DataDir = '$veronicaRoot/NewData';\n";
		}
		elsif (/###\$DataDir/)
		{
			print OUT "\$DataDir = '$veronicaRoot/Data';\n";
		}
		elsif (/###\$veronicaRoot/)
		{
			print OUT "\$veronicaRoot = '$veronicaRoot';\n";
		}
		elsif (/###\$StopList/)
		{
			print OUT "\$StopList = '$stoplist';\n";
		}
		elsif (/###\$Log/)
		{
			print OUT "\$Log = '$Log';\n";
		}
		elsif (/###\$veronicaServer/)
		{
			print OUT "\$veronicaServer = '$veronicaServer';\n";
		}
		elsif (/###\$veronicaPort/)
		{
			print OUT "\$veronicaPort = '$veronicaPort';\n";
		}
		elsif (/###\$Domain/)
		{
			print OUT "\$Domain = '$Domain';\n";
		}
	
		else
		{
			print OUT $_;
		}
	}
	close (OUT);
}

`cp $WorkingDirectory/stoplist $stoplist`;
`chmod -R 755 bin`;
