#!/usr/bin/perl
####################################################################
# URL Search Engine
# Copyright 1996 Techno Trade  http://www.technotrade.com
# Written By : Sammy Afifi   sammy@technotrade.com
# Date Last Modified : Jan 14, 1997
####################################################################
#
# This script is free of charge.
# Please link back to http://technotrade.com, thank you :-)
# PARA SER LLAMADO, UTILIZAR:
#<SCRIPT LANGUAGE="JavaScript" SRC="http://peoresnada.com/cgi-bin/daily/peoresnada.cgi"></SCRIPT>
#######
#
# Jan 14, stops people from entering html tags, converts < to &lt;
#
####################################################################

#  $linktitle, $linkdescrip,  $linkwords, $linkemail, $linkurl
# define some global variables

    $fields = 4;                       # Number of fields in each record
    $filename = "calendario.txt";      # The database text file
    $results = 1000;               # maximum number of results to display

    &getDate;
    
     $searchstring = "2000" . "$mes" . "$mday" . " " . "0000" . "$mes" . "$mday";
#    $searchstring = "$mes" . "$mday";



    &open_file("FILE1","",$filename);

    print "Content-type: text/html\n\n";
    $counter = 0;
print "document.write(\"Hoy es $dateShort\");";
    while (($line = &read_file("FILE1")) && ($counter < $results)) {
         # split the fields at the | character     
         @tabledata = split(/\s*\|\s*/,$line ,$fields);
          &check_record;
          if ($found == 1) {
            $counter++;
            &print_record;
          }

    }
    close(FILE1);
    if ($counter == 0) {
	print "document.write(\"<b>Que tenga un buen dia!</b>\");\n";
    }
    
    

#########################################
#
#  Print the matched record
#
#########################################
sub print_record {
print "document.write(\" y se celebra $tabledata[2]\");";
}


##########################################
#
#  Check to see if record matches search criteria
#
##########################################
sub check_record {
    # get the data from the record read from the file. $tabledata

   $link_id = $tabledata[0];
   $link_date = $tabledata[1];
   $link_label = $tabledata[2];
   $link_desc   = $tabledata[3];   
   #chop($link_date);

    #build the search line with all fields we want to search in
    $searchline = $link_date;


   #search by keywords
   # only perform the keyword search if the length of the search string is greater than 2
   # don't think we want people to search for and  or or etc.
   $sfound = 0;
   $found = 0;
   $notfound = 1; 

   $stlen = length($link_label);
   if ($stlen > 1) {
      
           if ($searchstring =~ $searchline) {
                  $found = 1;
           } 
           else {
                  $found = 0;
            }
         }
    
   
    #if page doesn't have a title then return not found
#    $tlen = length($link_label);
#    if ($tlen < 1) {
#        $found = 0;
#    }
}


############################################
#
#  Add Record
#
############################################

sub parse_form {

   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   if (length($buffer) < 5) {
         $buffer = $ENV{QUERY_STRING};
    }
 
  @pairs = split(/&/, $buffer);
   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
   }
}


sub open_file {

  local ($filevar, $filemode, $filename) = @_;
  
  open ($filevar,$filemode . $filename) ||
     die ("Can't open $filename");
}

sub read_file {

  local ($filevar) = @_;

  <$filevar>;  
}

sub write_file {

  local ($filevar, $line) = @_;

  print $filevar ($line);
}

sub getDate{

	# Days
 	@RealDay = ("Domingo","<a href=http://peoresnada.com/pictures/galeria_humor.php?foto=lunes_monito.jpg target=_top>Lunes</A>","Martes","<a href=http://peoresnada.com/pictures/galeria_humor.php?foto=miercoles_01.jpg target=_top>Miercoles</A>",
         		"Jueves","Viernes","Sabado");
	#months
 	@RealMonth = ("Enero","Febrero","Marzo","Abril","Mayo","Junio",
         		"Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
 	@NumeralMonth = ("01","02","03","04","05","06",
         		"07","08","09","10","11","12");
	($sec,$min, $hour, $mday, $mon, $year, $wday, $yday,
	$isdst) = localtime (time); #local time of day
#century

	#add the century to the year ( good past year 2000 )

	$year += ($year < 90) ? 2000 : 1900;

	# set up stuff for printing

	$mins = (sprintf ("%02d",$min));       # to get "01", "02", etc.
	$daweek = $RealDay[$wday];             # Monday, Tuesday, etc.
	$month = $RealMonth[$mon];             # January, February, etc.
	$am = "AM";
	$am = "PM"  if($hour>12);
	$hour -= 12 if($hour>12);


	$mes = $NumeralMonth[$mon];             # 01, 02, etc.
if ($mday < 10) {$mday = "0" . $mday;}
     
	$dateShort ="$daweek, $mday de $month";
	$currentDate="$daweek $mday de $month de $year, $hour:$mins $am";#  ;)
	$dateString ="$daweek,  $month $mday, $year   $hour:$mins $am";

}