# /www/httpd/cgi-bin/drf_lib.pl    drf5n 1/17/98
# this file is intended to replace /www/httpd/cgi-bin/wang_lib.pl
# below are descriptions of the function calls
#
# to use this, put the following in your code:
#!/usr/local/bin/perl5 -swI/www/httpd/cgi-bin
# require "/www/httpd/cgi-bin/drf_lib.pl";


#----------------------------------------

# $Html_String=&Html_Escape("unescaped text")
#$today=&today();  Returns January 1 19xx
#&last_update($filename); Updates "Last update: xxxxx" in filename
# sort &by_lower_case (@array); 
# sort &by_lower_case_d (@array);
# &delete_block($filename, $begin_mark, $end_mark); 
# &insert($filename, $begin_mark, $end_mark, $new_entry, $sort);
# &insert($filename, $begin_mark, $end_mark, $new_entry, $sort);
# @content=&content($filename, $begin_mark, $end_mark);
# &replace($filename, $begin_mark, $end_mark, $new_entry);



#usage
# $Html_String=&Html_Escape("unescaped text")
#         drf5n 1/19/98

sub Html_Escape {
local ($instring)= @_;
$instring =~ s/&/&amp;/gm;  # HTML Escape character
$instring =~ s/</&lt;/gm;   # HTML Markup Tags
$instring =~ s/>/&gt;/gm;   # HTML Markup Tags
return ($instring); 
}
#----------------------------------------
#usage
#$today=&today();

sub today {
local(@Month)=("January", "February", "March","April","May","June","July",
"August","September","October","November","December");
local(@Week)=("Sunday","Monday", "Tuesday",
"Wednesday","Thursday","Friday","Saturday");

local($sec, $min, $hour, $mday, $mon, $year, $wday,
$yday,$isdst)=localtime(time);
local($date)= $Month[$mon]." ".$mday.", 19".$year;
return $date;
}

#----------------------------------------
#usage
#&last_update($filename);

sub last_update {
  
  open(HOMEPAGE, "$_[0]") 
   || print STDOUT "<b>ERROR: </b>Cannot open $_[0]<br>";
  local(@homepage)=<HOMEPAGE>;
  close(HOMEPAGE);

  local($new_homepage)=join("\r", @homepage); 

local($today)=&today();
local($entry);

$new_homepage=~ s/Last update:.*<\/strong>/Last update: $today.<\/strong>/g;

   @homepage=split(/\r/, $new_homepage); 
  

    open (TEMP,">$_[0]") 
    || print STDOUT "<b>ERROR: </b>Cannot write to $_[0]<br>";
    foreach  $entry (@homepage){
       print TEMP  $entry;
    }
    close (TEMP);
}

 


#----------------------------------------

# usage: 
# sort &by_lower_case (@array);
sub by_lower_case{
"\L$a" cmp "\L$b";
}

#----------------------------------------

# usage: 
# sort &by_lower_case_d (@array);
sub by_lower_case_d{
"\L$b" cmp "\L$a";
}


#----------------------------------------------------
# usage: 
# &delete_block($filename, $begin_mark, $end_mark);
sub delete_block {
  local($filename)=$_[0];
  local($begin_mark)=$_[1];
  local($end_mark)=$_[2];

  open(LIST, "$filename") || print "<b>ERROR: </b>Cannot write to $filename<br>"; 
  local(@list)=<LIST>;
  close(LIST);
  $length=@list;
    local($count)=0;
    local($begin,$end)=(-1,-1);
    local($entry);
    foreach  $entry (@list){
       if($entry =~ /$begin_mark/) {$begin=$count;}
       if($entry =~ /$end_mark/) {$end=$count;  last;}
       $count++;
    } 

    if($begin==-1)
      {print STDOUT "<b>$begin_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}

    if($end==-1)
      {print STDOUT "<b>$end_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}



    for($times=0;$times<=$begin;$times++) {$shiftout[$times]=shift(@list); }
    for($times=0;$times<=$length-$end-1;$times++) {$popout[$times]=pop(@list); }


    @list="";


    for($times=$begin;$times>=0;$times--){unshift(@list,$shiftout[$times]); }
    for($times=$length-$end-1;$times>=0;$times--){push(@list,$popout[$times]); }

    open (TEMP,">$filename") || print "<b>ERROR: </b>Cannot write to $filename<br>";
    foreach  $entry (@list){
       print TEMP  $entry;
    }
    close (TEMP);
}


#----------------------------------------------------
# usage: 
# &insert($filename, $begin_mark, $end_mark, $new_entry, $sort);
#$sort=1 => sort in increasing order
#$sort=2 => sort in decreasing order

sub insert {
  local($filename)=$_[0];
  local($begin_mark)=$_[1];
  local($end_mark)=$_[2];
  local($new_entry)=$_[3];
  local($sort)=$_[4];

  open(LIST, "$filename") || print "<b>ERROR: </b>Cannot write to $filename<br>"; 
  @list=<LIST>;
  close(LIST);
  $length=@list;
    local($count)=0;
    local($begin,$end)=(-1,-1);
    local($entry);
    foreach  $entry (@list){
       if($entry =~ /$begin_mark/) {$begin=$count;}
       if($entry =~ /$end_mark/) {$end=$count;  last;}
       $count++;
    } 

    if($begin==-1)
      {print STDOUT "<b>$begin_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}

    if($end==-1)
      {print STDOUT "<b>$end_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}


    for($times=0;$times<=$begin;$times++) {$shiftout[$times]=shift(@list); }
    for($times=0;$times<=$length-$end-1;$times++) {$popout[$times]=pop(@list); }


    push(@list,$new_entry);

    if($sort==1) { #sort in increasing order
    @list= sort  by_lower_case (@list);
    }
   
    if($sort==2) { #sort in decreasing order
    @list= sort  by_lower_case_d (@list);
    }
   

    for($times=$begin;$times>=0;$times--){unshift(@list,$shiftout[$times]); }
    for($times=$length-$end-1;$times>=0;$times--){push(@list,$popout[$times]); }


    open (TEMP,">$filename") || print "<b>ERROR: </b>Cannot write to $filename<br>";
    foreach  $entry (@list){
       print TEMP  $entry;
    }
    close (TEMP);
}

#---------------------------------------------------

# usage: 
# $content=&content($filename, $begin_mark, $end_mark);
# return value:
# an array @array

sub content {
  local($filename)=$_[0];
  local($begin_mark)=$_[1];
  local($end_mark)=$_[2];

  open(LIST, "$filename") || print  "<b>ERROR: </b>Cannot write to $filename<br>"; 
  local(@list)=<LIST>;
  close(LIST);
  $length=@list;
    local($count)=0;
    local($begin,$end)=(-1,-1);
    local($entry);
    foreach  $entry (@list){
       if($entry =~ /$begin_mark/) {$begin=$count;}
       if($entry =~ /$end_mark/) {$end=$count;  last;}
       $count++;
    } 

    if($begin==-1)
      {print STDOUT "Begin Mark<b>$begin_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}

    if($end==-1)
      {print STDOUT "<b>End mark $end_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}



    for($times=0;$times<=$begin;$times++) {$shiftout[$times]=shift(@list); }
    for($times=0;$times<=$length-$end-1;$times++) {$popout[$times]=pop(@list); }

#    push(@list,"\r");    
#    push(@list,"\r");    

    $content=join("\r", @list);

}





#----------------------------------------------------
#function
#replace in bulk
# usage: 
# &replace($filename, $begin_mark, $end_mark, $new_entry);
sub replace {

  local($filename)=$_[0];
  local($begin_mark)=$_[1];
  local($end_mark)=$_[2];
  local($entry)=$_[3];
  local(@new_entry)=split(/\r/, $entry); 

  open(LIST, "$filename") || print  "<b>ERROR: </b>Cannot write to $filename<br>"; 
  local(@list)=<LIST>;
  close(LIST);
  $length=@list;
    local($begin,$end)=(-1,-1);
    local($count)=0;
    local($entry);
    foreach  $entry (@list){
       if($entry =~ /$begin_mark/) {$begin=$count;}
       if($entry =~ /$end_mark/) {$end=$count;}
       $count++;
    } 

    if($begin==-1)
      {print STDOUT "<b>$begin_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}

    if($end==-1)
      {print STDOUT "<b>$end_mark</b> is missing in <b>$filename</b>.<br>"; 
       print STDOUT "Work not done.<br>";
       return 0;}


for($times=0;$times<=$begin;$times++) 
  {$shiftout[$times]=shift(@list); }
for($times=0;$times<=$length-$end-1;$times++) 
  {$popout[$times]=pop(@list); }



    for($times=$begin;$times>=0;$times--)
       {unshift(@new_entry,$shiftout[$times]); }
    for($times=$length-$end-1;$times>=0;$times--)
       {push(@new_entry,$popout[$times]); }



    open (TEMP,">$filename") || print "<b>ERROR: </b>Cannot write to $filename.<br>";
    foreach  $entry (@new_entry){
       print TEMP  $entry;
    }
    close (TEMP);
}


1; #return true




