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

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

#include "manhat-lib/shared_time.h"
#include "manhat-lib/shared_cs_course_search.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_cs_util.h"



static void
read_parameters(char *key, char *count, CENTRAL_SURVEY_RECORD *data, 
		COURSE_SEARCH_PARAMS *params,  SESSION *user)
{
   char tmp[25];
   int is_admin;
    
    cs_get_optional_parameter ("admin", tmp, 24);
    is_admin = !(*tmp)? 0 : atoi(tmp)? 1: 0;
    cs_set_int_value("admin", is_admin);

    cs_get_required_parameter ("id", key, MAX_KEY);
    cs_set_value("id", key);

    verify_key_path(user, key, is_admin);    /* shared_survey_path_util.c */
    
    cs_get_required_parameter ("count", count, MAX_PATH);
    cs_set_value("count", count);
    strncpy(data->record.id, count, MAX_PATH +1);
    
    snprintf(data->record.title, MAX_SURVEY_TITLE + 1, "unknown");
    
    cs_get_required_parameter ("taker", tmp, 24);
    if(strcmp(tmp, "all") == 0)
	data->takers = ALL;
    else if(strcmp(tmp, "students")==0)
	data->takers = STUDENTS;
    else if(strcmp(tmp, "students_only")==0)
	data->takers = STUDENTS_ONLY;
    else 
	data->takers = TEACHERS;


    cs_get_required_parameter ("save_course", tmp, 24);
    data->record.save_courseinfo = atoi(tmp);

    data->record.release_time = get_time("release");     /* shared_time.c */
    data->record.unrelease_time = get_time("unrelease"); /* shared_time.c */
    data->record.results_time = get_time("result"); 	 /* shared_time.c */

    if(data->record.unrelease_time <= data->record.release_time)
	  cs_critical_error(ERR_SURVEY_UNRELEASE_TIME_WRONG , "");
    if(data->record.results_time <  data->record.release_time)
	  cs_critical_error(ERR_SURVEY_RESULT_TIME , "");

    cs_read_course_search_params( user->username, params); /* shared_cs_course_search.c */
   
}





static void
write_info(CENTRAL_SURVEY_RECORD *data, char *file)
{
    FILE *fp;
    time_t sent_time;
    
    
    sent_time = time(NULL);
    data->record.sent_time = sent_time;
    data->record.course_count = 0;

    fp = fopen (file, "w");
    if (!fp)
        cs_critical_error (ERR_FOPEN_WRITE_FAILED, file);
    if (fwrite (data, sizeof (CENTRAL_SURVEY_RECORD), 1, fp) != 1)
          cs_critical_error (ERR_FWRITE_FAILED, file);
      fclose (fp);
}








int main()
{
   char key[MAX_KEY + 1];
   char count[MAX_PATH + 1];
   char info_file[MAX_PATH + 1];
   char survey_path[MAX_PATH + 1];
   SESSION user;
   CENTRAL_SURVEY_RECORD data;
   COURSE_SEARCH_NODE *course_head;   /* shared_course_search.h */
   COURSE_SEARCH_PARAMS params;       /* shared_course_search.h */
  
   cs_cgi_init();   

   read_parameters(key, count, &data, &params, &user);
   course_head = build_course_search_list(&params); /* shared_course_search.c */   
   
   get_survey_path(&user, survey_path, count);      /* shared_survey_path_util.c */
   snprintf(info_file, MAX_PATH + 1, "%s/%s",survey_path, SURVEY_DELIVER_INFO_FNAME);

   cs_set_searched_courses (key, course_head, &params, ""); /* shared_cs_course_search.h */
   cs_set_value("bin_alias", ALIAS);

   write_info(&data, info_file);
   cs_cgi_display("survey_deliver_course_list", 1);
   free_subdir_list(params.subdir_head);  /* shared_course_search.c */
   cs_cgi_destroy();
  
  return 0;
}

