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

#include "global.h"
#include "custom.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_update_info.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_super_list.h"
#include "manhat-lib/shared_survey_deliver_list.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_submit.h"
#include "manhat-lib/shared_cs_util.h"




void
read_parameters(int *is_admin, char *key, char *count, int *mode)
{
   char temp[12];

   cs_get_optional_parameter ("admin", temp, 11);
   *is_admin = !(*temp)? 0 : atoi(temp)? 1 : 0;

   cs_get_required_parameter ("id", key, MAX_KEY);
   cs_get_required_parameter ("count", count, MAX_PATH);
   
   
 /* mode is a 'mystery' number somewhat cleared up in 
 ** survey.h  -  look for 
 **     #define MODE_TAKE_MYMANHATTAN   1
 **     #define MODE_TAKE_WEB_DELIVERED 2
 **    and so on.
 ** 
 */

   cs_get_optional_parameter ("mode", temp, 11);
   *mode = *temp? atoi(temp) : -1;
}







char *
get_all_course_string(const char *username, const char *count)
{
    COURSE_LIST_NODE *head, *ptr;
    char file[MAX_PATH +1];
    FILE *fp;
    char *course_string;

    course_string = (char*)malloc(sizeof(char)*(MAX_PATH *3));
    course_string[0] = '\0';

    snprintf(file, MAX_PATH +1, "../%s/%c/%s/%s/%s_txt",
		    USERS_DIR, sub_dir_name(username), username, SURVEY_DIR, count);
    fp = fopen(file, "r");
    if(!fp)
       cs_critical_error( ERR_FOPEN_APPEND_FAILED, "");
    head = build_course_list(fp);
    fclose(fp);
    for(ptr = head; ptr; ptr=ptr->next)
    {
	strcat(course_string, ptr->crs);
	strcat(course_string, ";");
    }
    super_free_course_list(head);
    return  course_string;
}







NAME_VALUE_LIST *
get_submit_value(SESSION *user, const char *count, int mode, int is_admin)
{

    char fname[MAX_PATH +1];
    char survey_path[MAX_PATH +1];
    SURVEY_LIST *list_survey;
    NAME_VALUE_LIST *list =0;

    if( (mode == MODE_TAKE_MYMANHATTAN) && !is_admin)
	 snprintf(survey_path, MAX_PATH +1, "../%s/_%s_/%s",
			SURVEY_DIR, ADMIN_DIR, count);
    else 
       get_survey_path(user, survey_path, count); 
    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 */
    free_survey_data_list(list_survey);
    return list;
}




void
write_record( SESSION *user, 
		const char *count, int save_course_info, int mode, int is_admin)
{

    char fname[MAX_PATH + 1];
    char survey_path[MAX_PATH + 1];
    FILE *fp;
    time_t now;
    char timestring[MAX_TIMESTRING + 1];
    char *course_string;
    NAME_VALUE_LIST *list = 0;

    now = time (NULL);
    strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
		                   localtime (&now));
    if(save_course_info)
       course_string = get_all_course_string(user->username,  count);
    else
    {
       course_string = (char*)malloc(sizeof(char)*MAX_PATH+1);
       snprintf(course_string, MAX_PATH+1, "no course");
    }

    list = get_submit_value(user, count,  mode,  is_admin);
    if( (mode == MODE_TAKE_MYMANHATTAN) && !is_admin)
	 snprintf(survey_path, MAX_PATH +1, "../%s/_%s_/%s",
			SURVEY_DIR, ADMIN_DIR, count);
    else 
       get_survey_path(user, survey_path, count); 

    snprintf(fname, MAX_PATH+1, "%s/%s", survey_path, SURVEY_RESULT_FNAME);
    fp = fopen(fname, "r+");
    if(!fp)
	 cs_critical_error( ERR_FOPEN_WRITE_FAILED, "");

    get_exclusive_lock(fileno(fp), 1);
    write_result_file(fp, timestring,course_string,  list); /* shared_survey_submit.c */
    release_lock(fileno(fp));
    fclose(fp);
    free(course_string);
    free_name_value_list(list); /* shared_survey_submit.c */
}




int
main()
{
    int is_admin;
    char count[MAX_PATH +1];
    char key[MAX_KEY +1];
    char file[MAX_PATH +1];
    char url[MAX_PATH +1];
    SESSION user;
    int  mode, save_course_info =-1;

    cs_cgi_init();

    read_parameters(&is_admin, key, count, &mode);
    verify_key_path(&user, key, is_admin);
    
    if( (mode !=  MODE_PREVIEW_PRIVATE) && (mode != MODE_PREVIEW_PUBLIC) && !is_admin)
    {
	snprintf(file, MAX_PATH +1, "../%s/%c/%s/%s/%s",
            USERS_DIR, sub_dir_name(user.username),
                user.username, SURVEY_DIR, SURVEY_DELIVER_INFO_FNAME);
        if(!file_exists (file) || !valid_survey_taker(file,  count, &mode, &save_course_info) )  /* shared_survey_deliver_list.c */
	      cs_critical_error(ERR_RESUBMIT_ERROR, file);
    }
    
    snprintf(url, MAX_PATH +1, "%s?id=%s",  "mymanhattan", key);
    cs_set_value("back_url", url);
    if(mode == MODE_TAKE_MYMANHATTAN)
    { 
        write_record( &user, count, save_course_info, mode, is_admin);
        remove_this_survey_from_deliver_info(user.username,  count);
	cs_set_value("success_code", SURVEY_SUBMIT_CODE);
	cs_set_int_value("close_win", -1);
        cs_cgi_display("survey_success", 1);
    }
    else
    {
        remove_this_survey_from_deliver_info(user.username,  count);
	cs_set_int_value("close_win", -1);
	cs_set_value("success_code", SURVEY_SUBMIT_CODE);
        cs_cgi_display("survey_success", 1);
    }
    cs_cgi_destroy();
    return 0;
}

	  
	   
