#!/usr/bin/perl
# perl_form - a simple illustration of forms and Perl CGI
# Adapted from perl_form by Stephen Bloch, 12/96

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

&ReadParse;

print "<title>Acknowledgment</title><h1>Acknowledgment</h1><hr>";



open (MAIL, "| /usr/sbin/sendmail sbloch\@boethius.adelphi.edu");
print MAIL "Subject: Ostgardr mailing list update\n\n";
print MAIL "Here are all the data of interest:\n\n";

foreach $key ('name','sca_name','sca_titles','offices','address','phone','hours','pager','email','URL','directions') {
    $pname = "pub_$key";
    print MAIL "$key ($in{$pname}): $in{$key}\n";
}
print MAIL "picture_URL: $in{'picture_URL'}\n\n";

if ($in{'send_sample'} eq 'yes') {
    print MAIL "Please send a sample Seahorse!\n\n";
    }

print MAIL "Here's the new Web page entry:\n";

if ( length($in{'picture_URL'}) ) {
    print MAIL '<img src="' . $in{'picture_URL'} . '" alt="" align=right>';
    print MAIL "\n";
    }
if ( PublishOnWeb ('sca_name') ) {
    if ( length($in{'sca_titles'}) ) {
        print MAIL "$in{'sca_titles'} ";
        }
    print MAIL "$in{'sca_name'}";
    if ( PublishOnWeb ('name') ) {
        print MAIL ", mka $in{'name'}";
        if ( length($in{'offices'}) ) {
            print MAIL ", $in{'offices'}";
            }
        }
    print MAIL " <br>\n";
    }
elsif ( PublishOnWeb ('name')) {
    print MAIL "$in{'name'}";
    if ( length($in{'offices'}) ) {
        print MAIL ", $in{'offices'}";
        }
    print MAIL " <br>\n";
    }
if (PublishOnWeb ('address')) {
    print MAIL "$in{'address'} <br>\n";
    }
if (PublishOnWeb ('phone')) {
    print MAIL "Phone $in{'phone'} ";
    if (PublishOnWeb ('hours')) {
	print MAIL "$in{'hours'}";
	}
    print MAIL " <br>\n";
    }
if (PublishOnWeb ('pager')) {
    print MAIL "Pager: $in{'pager'} <br>\n";
    }
if (PublishOnWeb ('email')) {
    print MAIL 'E-mail <a href="mailto:' . $in{'email'} . '">';
    print MAIL "$in{'email'}</a> <br>\n";
    }
if (PublishOnWeb ('URL')) {
    print MAIL 'Web page <a href="' . $in{'URL'} . '">';
    print MAIL "$in{'URL'}</a> <br>\n";
    }

print MAIL "\nAnd here are all the environment variables:\n";
foreach $key (keys %ENV) {
    print MAIL "   $key: $ENV{$key}\n";
    }

print "Your information has been e-mailed to the &Oslash;stgardr Webmaster, ";
print '<a href="mailto:sbloch@boethius.adelphi.edu">Stephen Bloch</a>.';
print '<p><a href="http://www.adelphi.edu/~sbloch/sca/ostgardr/index.html">';
print 'Return to &Oslash;stgardr page</a>';

$agent = $ENV{"HTTP_USER_AGENT"};
print MAIL "\nThe user is using '$agent'.\n";
if ($agent =~ /lynx/i) {
    print MAIL "Text-only browser (lynx).\n";
    }
elsif ($agent =~ /mozilla/i || $agent =~ /netscape/i) {
    print MAIL "Netscape.\n";
    }
else {
    print MAIL "Something else.\n";
    }


close MAIL;



# sub PublishOnWeb takes a key and tells whether that key is publishable
sub PublishOnWeb {
    local ($pname);

    $pname = "pub_$_[0]";
    $_ = $in{$pname};
    if ( /Web/ ) {
        return 1;
        }
    else {
 	return 0;
	}
    }

# Adapted from cgi-lib.pl by S.E.Brenner@bioc.cam.ac.uk
# Copyright 1994 Steven E. Brenner
sub ReadParse {
  local (*in) = @_ if @_;
  local ($i, $key, $val);

  if ( $ENV{'REQUEST_METHOD'} eq "GET" ) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  } else {
        # Added for command line debugging
        # Supply name/value form data as a command line argument
        # Format: name1=value1\&name2=value2\&...
        # (need to escape & for shell)
        # Find the first argument that's not a switch (-)
        $in = ( grep( !/^-/, @ARGV )) [0];
        $in =~ s/\\&/&/g;
  }

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Split into key and value.
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;

    # Associate key and value. \0 is the multiple separator
    $in{$key} .= "\0" if (defined($in{$key}));
    $in{$key} .= $val;
  }
  return length($in);
}
