#!/usr/bin/perl

$date=`/bin/date +"%T (%Z) %D"`;
chomp $date;
$date_hits=`/bin/date \+\"\%m\-\%d\-\%y\"`;
chomp $date_hits;
$mailprog="/usr/bin/sendmail";

$log_file="/home/wilab/public_html/hits/hits.shtml";
$data_file="/home/wilab/public_html/hits/data/$date_hits.dat";


if(!-f "$log_file") {
	open(FILE, ">$log_file");
	print FILE <<EOT;
<html>
<head>
<title>Website Hits Logger</title>
</head>
<body bgcolor="#ffffff" text="#000000" link="blue" vlink="blue" alink="red">
<center>
<table width=80%>
<tr><td width=100%>
<center><font face="Verdana" size=2>
<font size=5 color="#074B85"><b>Website Hits Logger</b></font>
</center>
<br>
<font size=2><b>This system was created on: 01:36:47 (CST) 04/03/02</b></font>
</center>
<br>
<hr size=1>
<br>
<!--edit-->

<br>
<br>
</td></tr>
</table>
</body>
</html>
EOT
	close(FILE);
	$newfile=1;
}

open (FILE,"$data_file");
$NUMB=<FILE>; 
$NUMB++;
close (FILE);
        
open (FILE,">$data_file");
print FILE "$NUMB";
close (FILE); 
                
open (FILE,"$log_file");
@LINES=<FILE>;
close(FILE);

if ($ENV{REMOTE_USER} eq "") {
	$ENV{REMOTE_USER} = "Not Available";
}

open (LOG,">$log_file");   
for (my $i=0; $i <= $#LINES;$i++) {
	if ($LINES[$i] =~ /<!--edit-->/) {
		print LOG <<EOT;
<!--edit-->
<table width=80%>
	<tr>
		<td width=23%>
			<font face="$$CustomConfig{'BodyFontFace'}" size=2>
				<b>Date:</b></td>
		<td width=77%>
			<font face=arial size=2>
				$date</td>
	</tr>
	<tr>
		<td>
			<font face="$$CustomConfig{'BodyFontFace'}" size=2>
				<b>Requested Page:</b></td>
		<td>
			<font face=arial size=2>
				$ENV{'REQUEST_URI'}</td>
	</tr>
	<tr>
		<td>
			<font face=arial size=2>
				<b>IP Address:</b></td>
		<td>
			<font face=arial size=2>
				$ENV{'REMOTE_ADDR'}</td>
	</tr>
	<tr>
		<td>
			<font face=arial size=2>
				<b>Browser/OS:</b></td>
		<td>
			<font face=arial size=2>
				$ENV{'HTTP_USER_AGENT'}</td>
	</tr>
	<tr>
		<td>
			<font face=arial size=2>
				<b>From:</b></td>
		<td>
			<font face=arial size=2>
EOT
		if ($ENV{'HTTP_REFERER'} eq "") {
			print LOG <<EOT;
				Not Available
EOT
		} else {
			print LOG <<EOT;
				<a href="http://www.$ENV{'HTTP_REFERER'}">$ENV{'HTTP_REFERER'}</a>
EOT
		}

		print LOG <<EOT;
</td>
	</tr>
	<tr>
		<td>
			<font face=arial size=2>
				<b>Who:</b></td>
		<td>
			<font face=arial size=2>
				$ENV{REMOTE_USER}</td>
	</tr>
</table>
<hr size=1>
EOT
	} else {
		print LOG $LINES[$i];
	}
}  
close (LOG);
