
sub lock
{
$mylockfile = shift;
   my $endtime = time + 7;
   if (-e $mylockfile) {
      open (LOCKFILE, $mylockfile);
      my $temp = <LOCKFILE>;
      close (LOCKFILE);
      if ($temp < time) {
         unlink ($mylockfile);
      }
   }
   while (-e $mylockfile && time < $endtime) {
      sleep(1);
   }
   if (-e $mylockfile) {
      &error("Can't obtain file lock for $mylockfile");
   } else {
      open (LOCKFILE, ">$mylockfile") or &error ("Can't obtain file lock for $mylockfile");
      print LOCKFILE (time + 10);
	  close(LOCKFILE);
   }
}

sub unlock
{
$mylockfile = shift;
   # close (LOCKFILE);
   unlink ($mylockfile);
}

sub error
{
   my $msg = shift;
   print "Content-type: text/html\n\n";
   print "<html><head><title>File Lock Error</title></head><body>\n";
   print '<p><a href="javascript:window.location.reload(1);">Click Here</a> to try again.</p>';
   print "\n<p>$msg</p>\n";
   print '<p>This may be caused by a slow server.</p>';
   print '</body></html>';
   exit;
}

1;