#!/usr/bin/perl

require 'MyCGI.pl';
$LOCK_EX = 2;			
$LOCK_SH = 1;			

$HOME = '/home/mjd/public_html/perl/Interpolation/';
$FILE = "$HOME/ideas.txt";
($ME = $0) =~ s:.*/::;

%chars = ('<' => 'lt', '>' => 'gt',
	  '&' => 'amp', '"' => 'quot', 
	  "\0" => 'nbsp',
	 );

$chars = join '', keys %chars;
$pat = "[\Q$chars\E]";

if ($ENV{REQUEST_METHOD} eq POST) {
  %in = ReadParse();
  esc(@in{'name','email','what','desc'});

  open F, ">> $FILE"
    or die "Couldn't open file `$FILE': $!; aborting";
  flock F, $LOCK_EX;
  print F join("\0", @in{'name','email','what','desc'}, '');
  close F;
}


print <<EOM;
Content-type: text/html

<html>
<head><title>Interpolation ideas</title></head>

<body bgcolor=white>

<h1><tt>Interpolation</tt> Ideas</h1>

<p>Sorry, this application is being redesigned.  Check back some other time.  (22 June 1998)</p>

</body>
</html>

EOM

exit 0;


print <<EOM;
Content-type: text/html

<html>
<head><title>Interpolation ideas</title></head>

<body bgcolor=white>

<h1><tt>Interpolation</tt> Ideas</h1>

<p>Jan Krynicky says:</p>

<blockquote>
    Mark-Jason didn\'t you think about creating a library of
    such <tt>Interpolation</tt> tricks. You could use some Guestbook code
    for establishing it.
</blockquote>

<p>Hey, that\'s a pretty good idea.</p>

<hr>

<form method=POST action="$ME">
Your name: <input type="text" name=name> <br>
Your email address: <input type="text" name=email> (Of course it's optional.  Everything is optional.)<br>
One-line title: <input type="text" name=what size=80> <br>
<textarea rows=8 cols=80 name=desc>
Explanation and code samples here.
</textarea>
<input type=submit value="Submit Brilliant Idea">
</form>

<hr>

EOM

foreach $item (get_file()) {
  my ($n, $e, $w, $d) = @$item;
  $n ||= 'Anonymous';
  my $NAME = $e ? "$n (<tt>$e</tt>)" : $n;
  $d =~ s.\r?\n(\r?\n)+.</P>\n\n<P>.gsm;
  print <<EOM;
<h2>$w</h2>
<p>From: <b>$NAME</b></p>

<p>$d</p>
EOM
}

print <<EOM;
<hr>
<p>Return to: 
<a href="../../">Universe of Discourse main page</a> |
<a href="../../whatsnew.html">What's new page</a> |
<a href="../">Perl Paraphernalia</a> |
<a href="./index.html"><tt>Interpolation.pm</tt></a>
</p>

<p><a href="mailto:mjd-perl-interpolation\@plover.com">mjd-perl-interpolation\@plover.com</a></p>

    </BODY>

    </HTML>
EOM

exit 0;

sub get_file {
  open F, "< $FILE" 
    or die "Couldn't open file `$FILE': $!; aborting";
  flock F, $LOCK_SH;
  my @f;
  local $/ = "\0";

  for (;;) {
    my $i;
    my @p;
    for $i (1..4) {
      my $s = <F>;
      unless (defined $s) {
	close F;
	return @f;
      }
      chomp $s;
      push @p, $s;
    }
    push @f, \@p;
  }
}


sub esc {
  local $_;
  foreach (@_) {
    $_ =~ s/($pat)/\&$chars{$1}\;/go;
  }
  @_;
}
