#!/usr/bin/perl

use lib '/home/max/berezin/bin';
use CGI qw(:standard);

#***************************************************************
# Main
#***************************************************************
{
  if ($ENV{'REQUEST_METHOD'} eq "GET")
    { $in = $ENV{'QUERY_STRING'}; } 

  elsif ($ENV{'REQUEST_METHOD'} eq "POST")
    { read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); }

  # split returned data at & or ;
  @in = split(/[&;]/,$in); 

  # begin building web page
  print "Content-Type: text/html\r\n\r\n";
  print "<html> <head>\n";
  print "</head>\n";  print "<body>\n";
  print "<br>GREETINGS and salutations";
  print "<br>$in";

  foreach (@in)
  {
    ($name,$value)=  split( /=/, $_, 2);
    print( "<br>", "$name ", "$value " );

    if( "$name" eq "list" )
    {
        @list = `ls`;
        foreach $file ( @list )
          { print "<br>$file"; }
    } 

    elsif( "$name" eq "link" )
    {
      print "<br>linked by html"; 
      print "<br>$ENV{ 'PATH' }"; 
      print "<br>$ENV{ 'QUERY_STRING' }"; 
    }
        
    elsif( "$name" eq "env" )
    {

        print "<table>";
        foreach my $var (sort( keys %ENV ))
          { print "<tr><td>$var</td><td>$ENV{$var}</td></tr>"; }

        print "</table>";
    } 

    elsif( "$name" eq "errors" )
    {
        print "<br>";
        print( "checing errors" );
        $cur = `pwd`;
        if( chdir( "/var/apps/apache/logs" ))
        {
          @list = `grep z912730 error_log`;
          foreach $file ( @list )
            { print "<br>$file"; }
        }

        else
          { print "<br>dir not open"; }
    } 

    # search for information requested by caller.
    $cntr = 0;
  }

  print "\n";
  print "</body> </html>\n";
  #  end of web page generation
}

