#!/usr/bin/perl

require "cgi-lib.pl";
if (&ReadParse(*input) == 0) {
  &CgiError("No Parameters Provided");
  die "Bad Parameters";
}

$remoteuser = $ENV{'REMOTE_HOST'};
$remoteuser = $ENV{'REMOTE_IDENT'} if $remoteuser eq "";

@filenames = ("exclaim.words","name.words","utter.words","diddle.words",
			  "insert.words", "adjective.words","concave.words","convex.words");

@wordtypes = ("Exclamation", "Name", "Utter verb", "Diddle verb",
			  "Insertion verb", "Adjective", "Concave object", "Convex object");

#
# load input words into $word[0..7]
#
$word[0] = $input{"exclaim"};
$word[1] = $input{"person"};
$word[2] = $input{"uttered"};
$word[3] = $input{"diddled"};
$word[4] = $input{"inserted"};
$word[5] = $input{"adj"};
$word[6] = $input{"concave"};
$word[7] = $input{"convex"};

#
# reject illegal words and report results to $result[0..7]
#
# don't allow numbers, all upper case, etc...
#
foreach $i (0..7) 
{
	if ($word[$i] eq "") 
	{
		$result[$i] = 1;
	}
}

#
# add words to file
#

foreach $i (0..7) 
{
	if ($result[$i] == 0) 
	{
		$result[$i] = &AddWordToFile($filenames[$i],$word[$i]);
		$totalwords++ if ($result[$i] == 0);
	}
}

#
# report beginning results (echo contents of success1.html)
#
print &PrintHeader;

&EchoFile("success1.html");

#
# report added words via $result[0..7]
#
print "<b><center>\n";

if ($totalwords == 0) {
    print "No words added to database";
}
else {
	foreach $i (0..7) 
	{
		if ($result[$i] == 0) {
			printf("%s \"%s\" successfully added to database<br>\n",
					$wordtypes[$i], $word[$i]);
		}
	}
}

print "<br><br>\n";

foreach $i (0..7) 
{
	if ($result[$i] == 1) {
#		printf("%s not provided<br>\n",
#				$wordtypes[$i]);
	}
	elsif ($result[$i] == 2) {
		printf("%s \"%s\" not added - illegal word<br>\n",
				$wordtypes[$i], $word[$i]);
	}
	elsif ($result[$i] == 3) {
		printf("%s \"%s\" not added - already in database<br>\n",
				$wordtypes[$i], $word[$i]);
	}
}

print "</b></center>\n";
print "<br><br><br><br><br>\n";

#
# report end results (echo contents of success2.html)
#

&EchoFile("success2.html");

#
# return success
#

# end of script

#
# subroutine to add word to file
#
# a) lock our file-based semaphore (wait for unlock and lock)
# scan the file into a list
# search the list for our word (case and space insensitive)
# 
# if a match occurs, mark that word type  $result[0..7]
#

sub AddWordToFile {
  local ($filename,$word) = @_;
  $filename = "pornodir/" . $filename;
  open(INFILE, $filename) || die "Cannot open $filename for reading.";
  while (<INFILE>) {
	chop;
# !!! remove non-alpha chars spaces and case
	if ($_ eq $word) {
		return 3;
	}
  }
  close(INFILE);
	# wait for file to unlock
  
  do { $res = open(OUTFILE, ">>" . $filename) } while ($res == 0);
#  sysopen(OUTFILE, $filename, O_RDWR | O_CREAT, 0644);
#  flock(OUTFILE, 2);
#  seek(OUTFILE,0,2);
	print OUTFILE $word . "\n";
	close(OUTFILE);

	# log it
  do { $res = open(OUTFILE, ">>" . "pornodir/newwords.doc") } while ($res == 0);
#  sysopen(OUTFILE, "pornodir/newwords.doc", O_RDWR | O_CREAT, 0644);
#  flock(OUTFILE, 2);
#  seek(OUTFILE,0,2);
	print OUTFILE $word . " --> " . $filename . " by " . $remoteuser . "\n";
	close(OUTFILE);

  return 0;
}

sub EchoFile {
  local ($filename) = @_;
  open(INFILE, "pornodir/" . $filename) || die "Cannot open $filename for reading.";
  while (<INFILE>) {
  	print;
  }
  close(INFILE);
}
