#!/usr/bin/perl

sub ok {
	print "OK\n";
}

sub md5sum($) {
	use Digest::MD5;
        my $string = $_[0];
        my $md5 = Digest::MD5->new;
        $md5->add($string);
        return($md5->hexdigest);
}

sub check_dependencies {
	print "\nChecking Dependencies\n";
	print "Digest::MD5\t";
	use Digest::MD5;
	ok();
	print "Mysql\t\t";
	use Mysql;
	ok();
	print "Time::Local\t";
	use Time::Local;
	ok();
	print "MIME::Base64\t";
	use MIME::Base64;
	ok();
	print "GD\t\t";
	use GD;
	ok();
	print "GD::Graph\t";
	use GD::Graph;
	ok();
	print "Dependency Check Complete\n";
}

sub ask($$) {
	my ($question,$dpath) = @_;
	print "$question [$dpath]:";
	my $answer = <STDIN>;
	chomp($answer);
	if($answer) {
		return $answer;
	} else {
		return $dpath;
	}
}

sub where($$) {
	my ($item,$dpath) = @_;
	my $question = "Where is your $item";
	return ask($question,$dpath);
}

sub filter_replace($$$$) {
	my ($infile,$outfile,$st,$et) = @_;
	my ($buffer,$data);
	#print "$infile -> $outfile, s/$st/$et/g\n";
	open(IN,$infile) || die $!;
	while($buffer = <IN>) {
		$data .= $buffer;
	}
	close(IN);
	$data =~ s/$st/$et/g;
	open(OUT,">$outfile") || die $!;
	print OUT $data;
	close(OUT);
}

sub clean {
	print "\nCleaning Old Files, please ignore errors.\n";
	unlink('db/sql_db');
	unlink('src/modules/WJLCore.pm');
}


print "Web Job Logs Installer\n";
if($ARGV[0] =~ /clean/) {
	clean();
	exit(0);
} else {
	check_dependencies();
	clean();
}
print "\n";
my $cgibin 	= where('cgi-bin','/var/www/cgi-bin/wjl');
my $www		= where('www-root','/var/www/html');
my $perllib 	= where('Perl modules directory','/usr/lib/perl5/5.6.0');
my $confdb	= ask('Configure Database','Yes');
my ($sqlroot,$adminuser,$adminpass);
if($confdb =~ /^Y/i) {
	#$sqlroot	= ask('What is your MySQL Root Password','');
	$adminuser	= ask('What username do you want your WJL admin to have','admin');
	$adminpass	= ask('What password should the WJL admin user have','changeme');
	$adminpass	= md5sum($adminpass);
} else {
	$confdb = 0;
}
my $sqluser	= ask('What name should the Database user have','wjl');
my $sqlpass	= ask('What password should the Database user have','changeme');
my $organisation= ask('What is the name of your organisation','noorg');

print "\nMangling Files\n";
if($confdb) {
	filter_replace('db/sql_db.in','db/sql_db','your_user',$sqluser);
	filter_replace('db/sql_db','db/sql_db','your_password',$sqlpass);
	filter_replace('db/sql_db','db/sql_db','admin_user',$adminuser);
	filter_replace('db/sql_db','db/sql_db','admin_password',$adminpass);
}
filter_replace('src/modules/WJLCore.pm.in','src/modules/WJLCore.pm','your_user',$sqluser);
filter_replace('src/modules/WJLCore.pm','src/modules/WJLCore.pm','your_password',$sqlpass);
filter_replace('src/modules/WJLCore.pm','src/modules/WJLCore.pm','your_organisation',$organisation);
if($ARGV[0] !~ /config/i) {
	if($confdb) {
		print "\nSetting Up MySQL Database\n";
		print "You maybe asked for your root password.\n";
		system('db/inst');
		print "If that worked you won't see any messages\n";
	}
	print "\nInstalling Files\n";
	system('src/inst',$cgibin,$www,$perllib);
}
print "Ok I think we are done\n";
exit(0);
