#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
##############################################################################
##############################################################################
# StellarStuff.com                                                           # 
# All acCounted 4                                                            #
# Copyright 2000 StellarStuff.com                                            #
# Created 3/1/2001                                                           #
# Available at http://www.stellarstuff.com                                   #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 2001 StellarStuff.com   All Rights Reserved.                     #
#                                                                            #
# This script can be used\modified FREE of charge as long as you have a      #
# registered version.  To register, contact sales@stellarstuff.com.  By      #
# using this script you agree to indemnify me from any liability that might  #
# arise from its use.                                                        #
#                                                                            #
# Redistributing\selling the code for this program without prior written     #
# consent is expressly forbidden.                                            #
##############################################################################

print "Content-type: text/html\n\n";

### Open File and Read All Data
open (FILE, "counts.txt");
@cnts = <FILE>;
close(FILE);

### Delete Old File Before ReWriting it with New Data
$filename = "counts.txt";
if (-e $filename) { unlink("$filename"); }

### Create and Open New File ReInput Old Data While Updating New
$found = 0;
open (NEWFILE,">counts.txt") || die $!;
foreach $cnts(@cnts) {
	($page,$cnt,$blk) = split(/\|/, $cnts);
	if ($page eq $ENV{'DOCUMENT_URI'}) {
		$cnt++;
		$showcnt = $cnt;
		print NEWFILE "$page|$cnt| \n";
		$found = 1;
		}
	else {
		print NEWFILE "$cnts";
		}
	}
### First Time Counting the New Page
if ($found == 0) {
	if ($ENV{'DOCUMENT_URI'} ne "") {
		print NEWFILE "$ENV{'DOCUMENT_URI'}|1| \n";
		$showcnt = 1;
		}
	}

### Print the Count on the Page
print "$showcnt";
