/*----------------------------------------------------------------------*/
/* Richard Roy                                                          
   The Interactive Exam Generator Version 1.4  3/2/95  

   Copyright Richard Roy                             
   Modified by Erich Schneider (erich@bush.cs.tamu.edu)

   This program reads a formatted input file to generate a correctable exam  
 in HTML format to stdout.  Should be called tiegen/input-file.  
 Input-file should be an absolute pathname.
*/
/*----------------------------------------------------------------------*/  

/* defines are max_answers:   the max answers per question.
	       max_line:      the maximum length of any field in the input file.
                              a field being any text between two tags.
               int_line:      the length of lines with expected integer values.
                              currently defined for turning numbering on and off.
*/

#define MAX_ANSWERS 10
#define MAX_LINE 8000

/* End user configurable items */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cgiutils.h"

int mycmp(char *input,char *pattern)
{
  int i,plength;
  int retval = 1 ;

  plength=strlen(pattern);

  for (i=0 ; retval && input[i] != '\0' ; i++) {
    if (pattern[i] != '\0') {
      if (toupper(pattern[i])!=toupper(input[i])) 
	retval = 0 ;
    }
    else {
      if (!isspace(input[i]))
	retval = 0 ;
    }
  }  
  return (retval);
}

int main()
{
  int i,j,k,quest,length,count,index,result,max;
  int state,ans,new,done;
  int question_num=1,answer_num=1,short_ans=0;
  char *filename;
  char input_str[MAX_LINE] ;
  char question[MAX_LINE],answer[MAX_ANSWERS][MAX_LINE];
  char *buffer,include_top[MAX_LINE],include_middle[MAX_LINE];
  char include_bottom[MAX_LINE],footer[MAX_LINE];
  FILE *infile;
  char *restricted_questions ;

  filename=getenv("PATH_INFO");

  restricted_questions = getenv("QUERY_STRING") ;

  return_header("text/html") ;
 
  if ((infile = fopen(filename,"r"))==NULL) {
    printf("Error cannot open input file!!!<p>"); 
    if (filename != NULL) printf("%s",filename);
    return(0);
  }

  /* at this point we have good inputs, and can start parsing the input */

  /* string initializations */

  quest=0;
  new=0;
  question[0]='\0';
  include_top[0]='\0';
  include_middle[0]='\0';
  include_bottom[0]='\0';
  for (i=0 ; i<MAX_ANSWERS ; i++) 
    answer[i][0]='\0';
  footer[0]='\0';
  done=0;

  /* end string initializations */

  do {

    if (!feof(infile)) {
      fgets(input_str,MAX_LINE,infile);

      length=strlen(input_str);
      if (input_str[length-1] == '\n') {
	input_str[length-1] = '\0' ;
	length-- ;
      }
      if (input_str[length-1] == (char)13) {
	input_str[length-1] = '\0' ;
      }

      if (mycmp(input_str,"QUESTION:")) {
	buffer=question;
	new=1;
      }
      else if (mycmp(input_str,"ANS:")) {
	buffer=answer[ans];
	ans++;
      }
      else if (mycmp(input_str,"CORRECT_ANSWER:")) {
	buffer=NULL; /* gets thrown away by tiegen, used by tie */
      }
      else if (mycmp(input_str,"INCLUDE_BOTTOM:")) {
	buffer=include_bottom;
      }
      else if (mycmp(input_str,"INCLUDE_MIDDLE:")) {
	buffer=include_middle;
      }
      else if (mycmp(input_str,"TITLE:") || mycmp(input_str,"HEADER:") ||
	       mycmp(input_str,"INCLUDE_TOP:")) {
	buffer=include_top;
      }
      else if (mycmp(input_str,"FOOTER:")) {
	buffer=footer;
      }
      else if (mycmp(input_str,"QUESTION_NUM:")) {
	!(question_num) ;
      }
      else if (mycmp(input_str,"ANSWER_NUM:")) {
	!(answer_num) ;
      } 
      else if (mycmp(input_str,"SHORT_ANS:")) {
	short_ans=1;
      }
      else if (buffer != NULL)
	strcat(buffer, input_str) ;

    }
    else /* end of file */ {
      new=1;
      done=1;
    }

    if (new) {
      if (quest == 0) {
	printf("%s",include_top);
	printf("<FORM METHOD=POST ACTION=\"/FLORA/cgi/tie%s\">\n",filename) ;
    
	question[0]='\0';
	include_top[0]='\0';
	include_middle[0]='\0';
	include_bottom[0]='\0';
	for (i=0;i<MAX_ANSWERS;i++) 
	  answer[i][0]='\0';
	new=0;
	ans=0;
	short_ans=0;
      }
      else {
	char s[10] ;
	sprintf(s, "_%d_", quest) ;

	if (!nonempty(restricted_questions) ||
	    strstr(restricted_questions, s) != NULL) {
	  printf("%s",include_top);
	  if (question_num) printf("%i)",quest);
	  printf("  %s<p>\n",question);
	  if (!short_ans) printf("<ol>\n");
	  printf("%s",include_middle);
	
	  if (!short_ans) {
	    for (j=0;j<ans;j++) {
	      printf("<li><input type=\"radio\" name=\"%d\" value=\"%d\"> %s\n",quest,j+1,answer[j]);
	    }
	    printf("</ol>\n");
	  }
	  else {
	    char prename[MAX_LINE] ="<b>Your Answer:</b><br><textarea name=";
	    char postname[MAX_LINE]=" rows=2 cols=60></textarea><p>";
	    printf("<b>Your Answer: </b><br>") ;
	    printf("<textarea name=\"%d\" rows=2 cols=60></textarea><p>",
		   quest) ;
	    short_ans=0;
	  }
	
	  printf("%s",include_bottom);  
	}
	question[0]='\0';
	include_top[0]='\0';
	include_middle[0]='\0';
	include_bottom[0]='\0';
	for (i=0;i<MAX_ANSWERS;i++) 
	  answer[i][0]='\0';
	new=0;
	ans=0;     
      }
      quest++ ;
    }
  } while (!done) ;

  printf("%s",footer);

  printf("<hr>Press to <input TYPE=\"submit\" Value=\"SUBMIT \"> or");
  printf("<input TYPE=\"reset\" VALUE=\" CLEAR \"> to redo the exam</FORM><HR>");

  return(0);
}
