# mylib.pl  This is my perl library.  It must be called with require(mylib.pl)
# Contents:  &get_lines(), &get_count(), &print_html_header(), &read_form_results()


sub webdie{
&begin_html("System Difficulties");
print("$_[0]<p> $@ <p>\n");
print("</body></html>");
exit;
}

# &get_lines: opens text file and returns lines in an array 
# arguments: 1  filename (path)                               
# returns:  an array of lines
sub get_lines{                                                             
my($file, @lines);                                                            
$file = @_[0];                                                              
open (FH, "$file");                                                     
while (<FH>) {
  @lines = (@lines, $_);
}
close (FH);
return(@lines);
}
#End get_lines


                                                                              
# &getcount():  Get the count, increment, write back to file.
# arguments: 1. data file 
# returns:  A number (scalar)
sub get_count{
my($file,$count,$return_count);
$file = @_[0]; 
if (!-e "$file") {			#if $file doesn't exist, create it
   open (OUT, ">$file");
   print (OUT "0");
   close (OUT);
   chmod(0660, "$file");
   }

open (READ, "$file");			#Read stored count
while (<READ>){
   $count = $_;
   last;				#quit after first line.
}
close (READ);

$return_count = $count;
$count++;				#Increment count.

open (OUT, ">$file");			#Print out new count                 
print (OUT "$count");                                                       
close (OUT);
return($return_count);
}					#End &get_count
#End get_count


# begin_html(): Print the HTML header  
# arguments 2: 1. title, 2. first h1 header
# No return.  Prints to the screen.
sub begin_html{                                                          
my($title,$h1);                                                          
$title = $_[0];      
                                
if ($_[1] eq "") { $h1 = $_[0];}
else {$h1 = $_[1];}      

print("Content-type: text/html\n\n");                                       
print("<html>\n <head> <title> $title </title> </head>\n");              
print("<body bgcolor=\"#006699\" text=\"#ffffff\">\n");                                    
print("<center><h1> $h1 </h1></center>\n"); 
}
#End print_html_header



# Accepts STDIN data stream from forms HTML; splits name/value pairs,
# and assigns value to the associative array %entry with name as key   

sub read_form_results {                                                   
    local ($buffer, $name, $value);                                        
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});                        
    foreach (split(/&/, $buffer))                                        
    {                                                                    
        ($name, $value) = split(/=/);                                          
        # Un-Webify plus signs and %-encoding                            
        $value =~ tr/+/ /;                                               
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;     
        $entry{$name} = $value;                                          
    }                                                                      
    1;                                                                   
}                                                                        


1;	#Or return(1); Library must return yes.
#End read_form_results
