/*************************************************************************/
/*                                                                     
    S_SRCH.CGI     July 2, 1997 
    by Nannette Thacker

   gcc s_srch.c cgi_lib.c str_lib.c -o s_srch.c


*/
/*************************************************************************/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "cgi_lib.h"  
#include "str_lib.c"


#define TRUE 1
#define FALSE 0
#define OR 1
#define AND 2 
#define NITE 2

void printValues(char *, char *, char *, char*, char*, char*, char*, char*, char*, char*, char*);
void convertArray(char **,char *, char *, char *, char*, char*, char*, char*, char*, char*, char*, char*);
void printSubhead(void);
void printUrl(void);

int GetResult(char *, char *, char *, char*, char*, char*, char*, char*, char*, char*, char*);
int DoSort(char *, char *, char *, char*, char*, char*, char*, char*, char*, char*, char*);


int andor1, andor2, daynite ;



/*************************************************************************/
void printSubhead()
{

/* print the subheader */
//printf("<B>                               SEATS    TIMES</B>\n");
//printf("<B> COURSE      DESCRIPTION    HR LEFT  START  END  MEETS SECT BLD-RM# INSTRUCTOR</B>\n");

printf("<B>                                    SEATS    TIMES</B>\n");
printf("<B>SECT COURSE      DESCRIPTION     HR LEFT  START  END  MEETS  BLD-RM# INSTRUCTOR</B>\n");


return ;

}


/*************************************************************************/
void printUrl()
{

// close out the HTML file

printf("<P><HR>\n");
printf("<P><A HREF=\"http://www.twsu.edu/~regis/html/schsearch.html\">Another Search</A><P>");


printf("<P>Write down the SECTION NUMBERS and use our <A HREF=\"http://www.twsu.edu/~regis/html/tnrworksheet.html\">TOUCH 'N ROLL WORKSHEET!</A><P>");

printFooter();

return ;

}



/*************************************************************************/

void convertArray(char** cgivars, char * filename,char * search1,char * search2,char * search3,char * bldg,char * instr,char * time,char * week,char * credit, char * dept, char * level)
{

// convert the array to actual field names for ease of comparison
// and also, if the field elements in the HTML form change, then the
// array storage location will also change

int i ;
    for (i=0; cgivars[i]; i+= 2)
    {
      if (strncmp(cgivars[i], "FILE",strlen("FILE")) == 0)
		strcpy(filename,cgivars[i+1]);
	else // for all the rest, convert search string to UPPER
	      cgivars[i+1] = str_toupper(cgivars[i+1]);
      
      if (strncmp(cgivars[i], "SRCH1",strlen("SRCH1")) == 0)
		strcpy(search1,cgivars[i+1]);
      else if (strncmp(cgivars[i], "SRCH2",strlen("SRCH2")) == 0)
		strcpy(search2,cgivars[i+1]);
      else if (strncmp(cgivars[i], "SRCH3",strlen("SRCH3")) == 0)
		strcpy(search3,cgivars[i+1]);
      else if (strncmp(cgivars[i], "ANDOR1",strlen("ANDOR1")) == 0)
		andor1 = atoi(cgivars[i+1]);
      else if (strncmp(cgivars[i], "ANDOR2",strlen("ANDOR2")) == 0)
		andor2 = atoi(cgivars[i+1]);
      else if (strncmp(cgivars[i], "BLDG",strlen("BLDG")) == 0)
		strcpy(bldg,cgivars[i+1]);
      else if (strncmp(cgivars[i], "INSTR",strlen("INSTR")) == 0)
		strcpy(instr,cgivars[i+1]);
      else if (strncmp(cgivars[i], "TIME",strlen("TIME")) == 0)
		strcpy(time,cgivars[i+1]);
      else if (strncmp(cgivars[i], "WEEK",strlen("WEEK")) == 0)
		strcpy(week,cgivars[i+1]);
      else if (strncmp(cgivars[i], "CREDIT",strlen("CREDIT")) == 0)
		strcpy(credit,cgivars[i+1]);
      else if (strncmp(cgivars[i], "LEVEL",strlen("LEVEL")) == 0)
		strcpy(level,cgivars[i+1]);
      else if (strncmp(cgivars[i], "DAYNITE",strlen("DAYNITE")) == 0)
		daynite = atoi(cgivars[i+1]);
      else if (strncmp(cgivars[i], "DEPT",strlen("DEPT")) == 0)
		strcpy(dept,cgivars[i+1]);
    }    

return ;

}

/*************************************************************************/

void printValues(char * filename,char * search1,char * search2,char * search3,char * bldg,char * instr,char * time,char * week,char * credit, char * dept, char * level)
{
char year[3];

printf("<H2>Your Search Criteria:</H2>\n") ;
printf("<ul>\n") ;

//printf("<li>[%s] = [%s]\n", cgivars[i], cgivars[i+1]) ;

if(strstr(filename,"fl"))
  printf("<li>Fall ");
else if(strstr(filename,"su"))
  printf("<li>Summer ");
else if(strstr(filename,"sp"))
  printf("<li>Spring ");

strncpy(year, &filename[2],2);
year[3] = '\0';
printf("%s\n",year);

if (dept[0] !=  '\0') // not empty
	printf("<li>Department: %s",dept);

if (search1[0] !=  '\0') // not empty
{	printf("<li>Course/Description: %s",search1);

	if (search2[0] !=  '\0') // not empty
	{
	  if(andor1 == OR)
	    printf(" OR ");
	  else
	    printf(" AND ");
	}
	// print parenthesis if 2 & 3 are filled in
	if (search2[0] !=  '\0' &&
	   search3[0] !=  '\0')
	  printf(" ( ");
	if (search2[0] !=  '\0') // not empty
	  printf("%s",search2);
	if (search2[0] !=  '\0' &&
	   search3[0] !=  '\0') // not empty
	{
	  if(andor2 == OR)
	    printf(" OR ");
	  else
	    printf(" AND ");
	  printf("%s",search3);
	}
	// print parenthesis if 2 & 3 are filled in
	if (search2[0] !=  '\0' &&
	   search3[0] !=  '\0')
	  printf(" ) ");
}

if (bldg[0] !=  '\0') // not empty
	printf("<li>Building: %s",bldg);

if (instr[0] !=  '\0') // not empty
	printf("<li>Instructor: %s",instr);

if(!strstr(time,"00"))
  printf("<li>Time: %s",time);

if(!strstr(week,"ANY"))
  printf("<li>Days of Week: %s",week);

if(!strstr(credit,"00"))
  printf("<li>Credit Hours: %s",credit);

if(daynite == 1)
  printf("<li>Days");
else if(daynite == 2)
  printf("<li>Evenings");


if (level[0] !=  '\0') // not empty
{  printf("<li>Course Level: ");
   if (level[0] == '0')
     printf("Non-credit");
   else if (level[0] == '1' || level[0] == '2')
     printf("Lower Division Undergraduate (Levels 100, 200)");
   else if (level[0] == '3' || level[0] == '4')
     printf("Upper Division Undergraduate (Levels 300, 400)");
   else if (level[0] == '5' || level[0] == '6' || level[0] == '7')
     printf("Dual -- Undergraduate or Graduate (Levels 500, 600, 700)");
   else if (level[0] == '8' || level[0] == '9')
     printf("Graduate Only (Levels 800, 900)");
}


printf("</ul>\n") ;


}

/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/* main */
void main() {

char **cgivars ;
int i ;

// VALUES passed:
char filename[14]; // one additional char for NULL throughout
char dept[6];
char search1[21];
char search2[21];
char search3[21];
char bldg[3];
char instr[21];
char time[3];   // 00
char week[6];   // ANY
char credit[3]; // 00
char level[2]; 

/** First, get the CGI variables into a list of strings         **/
cgivars=getcgivars() ;
    
/* turn off the stdout buffering 
     to make sure that the web server gets the 
     CGI output as soon as it is
     printed to stdout */
setvbuf (stdout, NULL, _IONBF, 0);

printHeader("WSU Class Listings");


convertArray(cgivars,filename,search1,search2,search3,bldg,instr,time,week,credit,dept,level);

// strip the quotes out of the phrases
stripquote(search1);
stripquote(search2);
stripquote(search3);
stripquote(bldg);
stripquote(instr);


printValues(filename,search1,search2,search3,bldg,instr,time,week,credit,dept,level);

htmlPre(); // send the PRE HTML command to the screen
printSubhead();

/* get the requested records based on the search */
GetResult(filename,search1,search2,search3,bldg,instr,time,week,credit,dept,level);

htmlPreOff();
printUrl();

/** Free anything that needs to be freed **/
    for (i=0; cgivars[i]; i++) free(cgivars[i]) ;
    free(cgivars) ;

exit(0) ;

}


/*************************************************************************/
/* GetResult */

int GetResult(char * filename,char * search1,char * search2,char * search3,char * bldg,char * instr,char * time,char * week,char * credit, char * dept, char * level)
{
  FILE *filepointer ;
  char Line[256];
  int i ;
  int ctr ;
  double hdctr ;
  int printed ;
  int noresult ;

/* open data file, read only */
//  if (!(filepointer = fopen("fl97schd.data", "r")))

if (!(filepointer = fopen(filename, "r")))
{
 printf("Unable to open the File Schedule!<BR>\n\n");
 return -1 ;
}

noresult = TRUE ;
hdctr = 0 ; // count past the header before comparison

/* Find the requested records */
while (fgets(Line, sizeof(Line), filepointer))
{

   if(hdctr > 20) // get rid of 1st 20 lines of courses for header
   { 

      if(strstr(Line, " COURSE      DESCRIPTION    HR SEATS START  END  MEETS "))
         ;  // do nothing
      else if(strstr(Line, "        ")) 
            // get rid of blank lines, will show when no search criteria entered otherwise
         ;   // do nothing again
      else if (Line[0] ==  '\0')
         ;   // don't do anything if blank line      
      else if (Line[0] ==  '\n')
         ;   // don't do anything if return on line
      else  // once we get rid of the junk, start the sort criteria
      { 
         printed = FALSE ; // check if any are printed
         printed = DoSort(search1, search2, search3, bldg, instr, time, week, credit, dept, level, Line);
         if(noresult == TRUE && printed == TRUE)
	     noresult = FALSE ;
      }
   }
   else if (hdctr < 21) // increment counter until reaches 21
      hdctr ++ ;
}

fclose(filepointer); // close the file

// if NONE were printed
if (noresult == TRUE)
  printf("No results found on that search!<BR>\n\n");

return(0);

}

/*************************************************************************/
int DoSort(char * search1,char * search2,char * search3,char * bldg,char * instr,char * time,char * week,char * credit, char * dept, char * level, char* Line)
{

int next ;
int dohowmany = 0 ;
char CheckLine[256]; // compare the value only according to the section involved

// *****************************************************************
// DEPARTMENT SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if (dept[0] !=  '\0') // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[0],5);  
  CheckLine[5] = '\0';

trim( dept );
trim( CheckLine ); // trim both strings for exact comparison

if(strcmp(CheckLine,dept) != 0)     // if don't match
    next = FALSE ;

}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;


// *****************************************************************
// COURSE DESCRIPTION SEARCH

// copy the course/description to the CheckLine
strncpy(CheckLine, &Line[0],27); // start at beginning to the 27th character
CheckLine[27] = '\0';

// check how many phrases to do
if (search1[0] !=  '\0' && search2[0] !=  '\0' && search3[0] !=  '\0') // not empty
   dohowmany = 3 ;
else if (search1[0] !=  '\0' && search2[0] !=  '\0') // not empty
   dohowmany = 2 ;
else if (search1[0] !=  '\0') // not empty
   dohowmany = 1 ;

// do the string search to see if the phrases are found
next = FALSE ;

if(dohowmany == 0) // continue
  next = TRUE ;
else if(dohowmany == 1 && strstr(CheckLine, search1))    // one phrase
  next = TRUE ;
else if(dohowmany == 2)        // two phrases
{
   if (andor1 == OR )
   {   if (strstr(CheckLine, search1) || strstr(CheckLine, search2))
	   next = TRUE ;
   }
   else
   {   if (strstr(CheckLine, search1) && strstr(CheckLine, search2))
	   next = TRUE ;
   }   
}
else if(dohowmany == 3)   // three phrases
{
   if (andor1 == OR )
   {   
	if (andor2 == OR )
   	{   
         if (strstr(CheckLine, search1) || strstr(CheckLine, search2)  || strstr(CheckLine, search3))
	       next = TRUE ;
      }
      else
   	{   
         if ( strstr(CheckLine, search1) || (strstr(CheckLine, search2)  && strstr(CheckLine, search3)))
	       next = TRUE ;
      }
   }
   else // andor1 == AND
   {
	if (andor2 == OR )
   	{   
         if (strstr(CheckLine, search1) && (strstr(CheckLine, search2)  || strstr(CheckLine, search3)))
	       next = TRUE ;
      }
      else
   	{   
         if ( strstr(CheckLine, search1) && strstr(CheckLine, search2)  && strstr(CheckLine, search3) )
	       next = TRUE ;
      }
   }
}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return(FALSE) ;


// *****************************************************************
// BUILDING SEARCH

next = TRUE ; // reset 

if (bldg[0] !=  '\0') // not empty
{
  // copy the building to the CheckLine
  strncpy(CheckLine, &Line[60],2); // start at beginning to the 60th char
  CheckLine[2] = '\0';

  if(!strstr(CheckLine, bldg))
     next = FALSE ;
}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;


// *****************************************************************
// INSTRUCTOR SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if (instr[0] !=  '\0') // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[68],20);  
  CheckLine[20] = '\0';

  if(!strstr(CheckLine, instr))
     next = FALSE ;
}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;

// *****************************************************************
// TIME SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if(!strstr(time,"00")) // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[37],2);  
  CheckLine[2] = '\0';

  if(!strstr(CheckLine, time))
     next = FALSE ;
}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;

// *****************************************************************
// WEEKDAYS SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if(!strstr(week,"ANY")) // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[49],5);  
  CheckLine[5] = '\0';

trim( week );
trim( CheckLine ); // trim both strings for exact comparison

// get rid of "N" Evening character
if(CheckLine[4] == 'N')
{ 
// printf("\nb4 Checkline: %s   Week: %s   %d\n", CheckLine, week, strcmp(CheckLine,week) ); 
 strncpy(CheckLine, &Line[49],4);  
 CheckLine[4] = '\0';
// printf("\naf Checkline: %s   Week: %s   %d\n", CheckLine, week, strcmp(CheckLine,week) ); 
 trim( CheckLine ); // trim again

}

if(strcmp(CheckLine,week) != 0)     // if don't match, include LENGTH of week 
								  // (get rid of "N" for night on long strings)
    next = FALSE ;


}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;

// *****************************************************************
// CREDIT HOURS SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if(!strstr(credit,"00")) // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[28],2);  
  CheckLine[2] = '\0';

  if(!strstr(CheckLine, credit))
     next = FALSE ;
}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;


// *****************************************************************
// DAYNITE SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if( daynite != 0 ) // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[53],1);  
  CheckLine[1] = '\0';

  if(daynite == 2 && !strstr(CheckLine, "N"))	// if Nite selected and flag not "N", don't print
     next = FALSE ;
  else if (daynite == 1 && strstr(CheckLine,"N")) // if day selected and flat is "N", don't print
     next = FALSE ;

}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;


// *****************************************************************
// COURSE LEVEL SEARCH

next = TRUE ; // reset  -- ONLY halt the print if the search is not empty!

if (level[0] !=  '\0') // if not empty
{
  // copy to the CheckLine
  strncpy(CheckLine, &Line[5],1);  
  CheckLine[1] = '\0';


   if (level[0] == '0' && CheckLine[0] == '0')
      ;
   else if ( (level[0] == '1' || level[0] == '2') 
           && (CheckLine[0] == '1' || CheckLine[0] == '2'))
      ;
   else if ( (level[0] == '3' || level[0] == '4') 
           && (CheckLine[0] == '3' || CheckLine[0] == '4'))
      ;
   else if ( (level[0] == '5' || level[0] == '6' || level[0] == '7') 
           && (CheckLine[0] == '5' || CheckLine[0] == '6' || CheckLine[0] == '7'))
      ;
   else if ( (level[0] == '8' || level[0] == '8')
           && (CheckLine[0] == '8' || CheckLine[0] == '9'))
      ;
   else
     next = FALSE ;

}

if(next != TRUE) // if the phrases weren't found according to the criteria, then return without printing
  return (FALSE) ;

// *****************************************************************
// *****************************************************************

// if passes all the tests, print the line

strncpy(CheckLine, &Line[55],4); // copy first 54 chars up to section
CheckLine[4] = '\0';
printf("<B>%s</B> ", CheckLine); // print section number in bold

strncpy(CheckLine, &Line[0],55); // copy first 54 chars up to section
CheckLine[55] = '\0';
printf("%s", CheckLine); // print the line and make next section bold

strncpy(CheckLine, &Line[59],30); // copy first 54 chars up to section
CheckLine[30] = '\0';
printf("%s", CheckLine); 

// printf("%s", Line); // print the line!

return(TRUE);

}

// *****************************************************************

