#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "global.h"
#include "survey.h"


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_survey_string.h"
#include "manhat-lib/shared_survey_submit.h"
#include "manhat-lib/shared_survey_take_verify.h" 
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_system_data.h"


void
read_parameters(char *course, char *key, char *sender, char *info_fname)
{
   cs_get_required_parameter ("crs", course, MAX_PATH);
   cs_get_required_parameter ("id", key, MAX_KEY);
   cs_get_required_parameter ("sender", sender, MAX_USERNAME);
   cs_get_required_parameter ("inf", info_fname, MAX_FILENAME);
}

    
void
write_record( SESSION *user, const char *course,
		const char *sender, const char *info_fname, 
		const char *survey_dir,  CONFIG_STRUCT *conf)
{
    char fname[MAX_PATH +1];
    char survey_path[MAX_PATH +1];
    FILE *fp;
    time_t now;
    char timestring[MAX_TIMESTRING + 1];
    SURVEY_LIST *list_survey;
    NAME_VALUE_LIST *list;

    now = time (NULL);
    strftime (timestring, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
		                   localtime (&now));
    snprintf(survey_path, MAX_PATH + 1, "%s%s/%s/%s/%s",
                     conf->course_path,
                     PEOPLE_DIR,
                     sender,
                     SURVEY_DIR,
                     survey_dir);

    snprintf(fname, MAX_PATH+1, "%s/%s", survey_path, SURVEY_XML_FNAME);
    list_survey = survey_data_parser(fname);
    list = build_name_value_list(list_survey); /* shared_survey_submit.c */

    snprintf(fname, MAX_PATH+1, "%s/%s", survey_path, SURVEY_RESULT_FNAME);
    fp = fopen(fname, "r+");
    if(!fp)
	 cs_critical_error( ERR_FOPEN_APPEND_FAILED, "");
    get_exclusive_lock(fileno(fp), 1);
    write_result_file(fp, timestring, course,  list);
    release_lock(fileno(fp));
    fclose(fp);
    free_survey_data_list(list_survey);
    free_name_value_list(list);

}    
    





static
void mark_as_read(SESSION *user, CONFIG_STRUCT *conf, const char *info_fname, 
		NEWS_INFO *infofile_info, const char *key, const char *crs)
{
 CENTRAL_INBOX_RECORD msg;
 time_t fake_time = 1000;     /* some time in January, 1970 */

   
   /* build a fake CENTRAL_INBOX_RECORD */
   strncpy(msg.info_fname, info_fname, MAX_FILENAME + 1);
   msg.info = *infofile_info;   
   
   /* set the status to 'normal' and not protected */
   SET_BASIC_STATUS(msg.status, NORMAL);
   CLEAR_PROTECTED(msg.status);
   
   set_discussion_names (conf->course_path, user->username, SURVEYS);   /* shared_news_util.c */
   mark_msg_as_read_at_time(&msg, conf, user, fake_time);   /* shared_news_util.c */ 


}

    


int
main()
{

   char course[MAX_PATH + 1];            /* from crs=? command line */
   char key[MAX_KEY + 1];                /* from id=? command line */
   char sender[MAX_USERNAME + 1];        /* from sender=? command line - which teacher posted this exam? */
   char info_fname[MAX_FILENAME + 1];    /* from inf=? cmd line */
   SESSION user;
   CONFIG_STRUCT conf;           /* the configuration read from config file */
   int already_taken;            /* did user already take this survey? */
   SURVEY_RELEASE_RECORD release;
   char survey_dir[MAX_FILENAME + 1];    /* randomly generated name of dir where survey is stored */
   int allow_submit;                  /* should this user be allowed to submit the survey? */
   NEWS_INFO infofile_info;

    cs_cgi_init();
    read_parameters(course, key, sender, info_fname);

    read_configuration_file (course, &conf);    /* shared_util.c */
    validate_key (key, &user, &conf);   /* shared_util.c */

    /* shared_survey_take_verify.c */
    read_survey_release_data(&conf, sender, info_fname,  &user,
                     &already_taken, survey_dir, &release, &infofile_info);
    allow_submit = (user.group != FACULTY) && !already_taken;
    if(allow_submit)
      {
        write_record(&user, course, sender, info_fname, survey_dir, &conf);
        mark_as_read(&user, &conf, info_fname, &infofile_info, key, course);
      } 
   cs_set_int_value("close_win", -1); 
   cs_set_value("success_code", SURVEY_SUBMIT_CLASS_CODE); 
   cs_cgi_display("survey_success", 1);
   cs_cgi_destroy(); 
    return 0;
}

	  

