#!/usr/local/bin/perl
#
# WWW page-counter program, version 0.7
# rasca, berlin 1995 - published under the GNU GPL
# produces a gif file or an ASCII-string to stdout
#

### configuration ###########################################################

require "/wo_immer/infosystems/www/cgi-bin/perlwww.lib";

$cpath		= "/wo_immer/infosystems/www/logs/CPL";	# directory for saving the counts
$pbmpath	= "/usr/bin/X11";			# path to pbmplus or netpbm tools
$giftrans	= "/usr/local/bin/giftrans";	# the giftrans program

### end of configuration ####################################################

$ENV{'PATH'} = "$ENV{'PATH'}:$pbmpath";

$html = &url_ref();
if ($html eq "") {
	die "no reference!";
}

$html =~ s%http://%%;	# we don't need the 'http:' in the string
$html =~ s/\//%2F/g;	# substitute the '/' char

$file = "$cpath/$html";
umask (0002);

if (&lock_file ($file) == 1) {
	if (! -f $file) {
		$num = 0;
	} else {
		unless (open (FILE, "<$file")) {
			&unlock ($file);
			die "couldn't open $file for reading";
		}
		$num = <FILE>;
		close (FILE);
	}
	$num++;

	unless (open (FILE, ">$file")) {
		&unlock ($file);
		die "coudn't open $file for writing";
	}
	print (FILE $num);
	close (FILE);
	&unlock_file($file);

	if ($ARGV[0] eq "-a") {
		# produce ASCII output, e.g. for the NCSA httpd 'server side includes'
		#
		print ("$num");
		exit (0);
	}
	open (PIPE,
		"pbmtext $num | pnmcrop | ppmtogif -transparent white |"
		) || die "oops, something went wrong :-(";
		#
		# or use something like the following for the PIPE if you have
		# the PBMplus tools instead of the NetPBM package:
		#
		# "pbmtext $num | pnmcrop | ppmtogif | giftrans -B blue -t 1 |"
		# "pbmtext $num | pnmcrop | pnmmargin -white 3 | ppmtogif |"
		#
		print ("Content-Type: image/gif\n\n");
		while (<PIPE>) {
			print;
		}
	close (PIPE);
}

