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

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

#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.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_page.h"


void
read_parameters(int *is_admin, char *key,  char *count,  char *course, int *mode)
{
    char tmp[10];

    cs_get_required_parameter("id", key, MAX_KEY);
    cs_get_required_parameter("count", count, MAX_PATH);
    cs_get_optional_parameter("admin", tmp, 9);
//    *is_admin = !(*tmp)? 0: atoi(tmp)? 1 : 0;
    if(strlen(tmp))
       *is_admin = atoi(tmp);
    else
       *is_admin = 0; 

    cs_get_optional_parameter("crs", course, MAX_PATH);

 /* mode is a 'mystery' integer 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", tmp, 9);
    *mode = *tmp? atoi(tmp) : -1;

}
 


void
do_action( SESSION *user, int is_admin, const char *key, const char *count, int mode, const char *course)
{
    SURVEY_LIST  *list;
    char fname[MAX_PATH +1];
    char survey_path[MAX_PATH +1];
    int  domain;
    char *username;

    domain = hdf_get_int_value(global_cgi->hdf, "Query.domain", 0);
    username = hdf_get_value(global_cgi->hdf, "Query.username", 0);

    /* survey came from admin */
   
    /* MODE_TAKE_MYMANHATTAN is in survey.h */
    if( !is_admin && ((domain == 1) || (mode == MODE_TAKE_MYMANHATTAN)) )   /* path of public surveys */
                snprintf(survey_path, MAX_PATH +1, "../%s/_%s_/%s",SURVEY_DIR, ADMIN_DIR, count);
    else if(is_admin)        
    {
      if(!username )	    
        get_survey_path(user, survey_path, count); 
      else if(username && domain != 3)
        snprintf(survey_path, MAX_PATH +1, "../%s/%s/%s",SURVEY_DIR, username, count);
      else if(domain == 3)
        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);
    if(!file_exists (fname))
      printf("Location: %s?count=%s&id=%s\n\n", "survey_clean_up", count, key); 
      
    list = survey_data_parser(fname);
    set_survey_content(list); /* shared_survey_page.c */
    free_survey_data_list(list);
}



int
main()
{
    int is_admin;
    char count[MAX_PATH +1];
    char key[MAX_KEY+1];
    int   save_course_info;
    int   save_record = 0;
    char file[MAX_PATH+1];
    char course[MAX_PATH +1];
    SESSION user;
    CONFIG_STRUCT conf;
    int mode;

    cs_cgi_init();
    read_parameters(&is_admin, key, count,  course, &mode);

    if(!(*course))
      verify_key_path(&user, key, is_admin);
    else
    {
	 read_configuration_file(course, &conf);
         validate_key(key, &user, &conf);
     }

    
    if((mode != MODE_PREVIEW_PRIVATE) && (mode != MODE_PREVIEW_PUBLIC))
     {

	 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))
	      cs_critical_error(ERR_REQUEST_DENIED, file);

	 if(!valid_survey_taker(file,  count, &save_record, &save_course_info))   /* shared_survey_deliver_list.c */
	      cs_critical_error(ERR_REQUEST_DENIED, "");

         if(!save_record)
            cs_set_int_value("allow_submit", 0);
         else 
            cs_set_int_value("allow_submit", 1);
     } 
     
    do_action( &user, is_admin, key,  count, mode, course);
    cs_cgi_display("survey_page", 1);
    cs_cgi_destroy();

    return 0;
}

       
    
	

