#!/usr/bin/perl

require "cgi-lib.pl";
require "incrementCounter.pl";
&ReadParse;

$visitors="visitors.db";
$counterfile="counterfile.txt";
$tyme=time;

$username=$in{'username'};

if (length($username)<=0)
{
	$counter=&incrementCounter($counterfile);
	$username="$counter";
}

## need to lock $visitors file right here
$lockfile=$visitors."lock";

## if counter file is locked wait.
while(-e $lockfile)
{
	sleep(1);
}

##lock visitor file
open(FILE,">".$lockfile);
close(FILE);

dbmopen %Aliases,$visitors,0666;
while(($key,$value)= each %Aliases)
{
	$diff=$tyme-$Aliases{$key};
	if ($diff>36)
	{
		delete $Aliases{$key};
	}
}
$Aliases{$username}=$tyme;
dbmclose(%Aliases);

dbmopen %Aliases,$visitors,0666;
while(($key,$value)= each %Aliases)
{
	$data.=",$key";
}
dbmclose(%Aliases);


##unlock counter file
unlink($lockfile);

## print page
print &PrintHeader;
print <<"_P_A_G_E_";
<meta http-equiv="refresh" content="18;url=guests.pl?username=$username">
<SCRIPT LANGUAGE="JavaScript">
parent.saveList();
parent.guests = new Array(0$data);
parent.compareLists();
// -->
</SCRIPT></head><body bgcolor=#000000 text=#990000><FONT FACE="Verdana,Arial,Helvetica" SIZE=2>$data</FONT></body></html>
_P_A_G_E_



