#!/usr/bin/perl

system("clear");

print "\n\nTHINGS TO DO BEFORE RUNNING THIS SCRIPT:\n";
print "\t* Edit your halfStats.cfg before running this file\n";
print "\t  (If you have already installed, copy your OLD halfStats.cfg here)\n";
print "\t* Make sure your half-life server is logging stats\n";
print "\t  (add 'mp_logfile 1;log on' to your server.cfg)\n";
print "\t* Make sure you have permission to make directories that do not exist\n";
print "\t* Install a web server if you dont have one :)\n";

print "\nHave you completed all the above tasks [y/N]: ";
$yesNo = lc(my $yesNo = <STDIN>);
chomp($yesNo);

# check to see if we are root
my $login = getlogin();

if($yesNo eq "y")
{
    my $htmlDirLine = `cat ./halfStats.cfg | grep -v '#' | grep 'htmlDir'`;
    chomp($htmlDirLine);
    
    my ($tag, $path) = split("=", $htmlDirLine);
    my @paths = split("/", $path);
    my $dir = "";
    my $checkPath = "";
    my $exists = "/";

    print "* Checking html paths\n";
    foreach $dir (@paths)
    {
	if($dir ne "")
	{
	    $checkPath = `ls -1 $exists | grep '$dir'`;
	    chomp($checkPath);
	    if($exists ne "/"){$exists = "$exists/$dir";}
	    else{$exists = "/$dir";}
	    if($checkPath eq "")
	    {
		print "* Making directory $exists\n";
		system("mkdir $exists");
	    }
	}
    }

    $checkPath = `ls -1 $exists | grep players`;
    chomp($checkPath);
    if($checkPath eq "")
    {
	print "* Making players directory\n";
	system("mkdir $exists/players");
    }

    my $logDir = `cat ./halfStats.cfg | grep -v '#' | grep 'logDir'`;
    ($tag, $path) = split("=", $logDir);
    chomp($path);
    print "* Changing permissions on $path\n";
    system("chmod o+rx $path");

    print "* Copying halfStat files to $exists\n";
    system("cp -R -f ./* $exists");

    print "* Making symbolic link\n";
    system("ln -s $exists ~/halfStats");

    print "* Completed.. hopefully all went well\n";
    print "* Type 'cd ~/halfStats;./halfStats' to run halfStats\n\n";

}
else
{
    system("more README");
}
