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

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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_survey_util.h"
#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_page.h"
#include "manhat-lib/shared_cs_util.h"


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

    cs_get_required_parameter("id", key, MAX_KEY);
    cs_get_required_parameter("count", count, MAX_PATH);
    cs_get_required_parameter("admin", tmp, 9);

   *is_admin = !(*tmp)? 0: atoi(tmp);
}






void
set_data( SESSION *user,  const char *key, const char* count)
{
     char filename[MAX_PATH +1];
     char survey_path[MAX_PATH +1];
     SURVEY_LIST *list ; 
     int has_clipboard;
     
     get_survey_path(user, survey_path, count);          /* shared_survey_path_util.c */


     snprintf(filename, MAX_PATH +1, "%s/%s",survey_path, SURVEY_CLIPBOARD_FNAME);
     has_clipboard = file_exists(filename);  /* shared_file_util.c */
     if(has_clipboard)
	cs_set_int_value("has_clipboard", 1);

     snprintf(filename, MAX_PATH +1, "%s/%s",survey_path, SURVEY_XML_FNAME);
     list = survey_data_parser(filename);

     set_survey_content(list);                 /* shared_survey_page.c */

     free_survey_data_list(list);
}





int 
main()
{
   int is_admin;
   char key[MAX_KEY +1];
   char count[MAX_PATH +1];
   SESSION user;
   
   cs_cgi_init();   				/* shared_cs_util.c */
   read_parameters(&is_admin, key,count);

   verify_key_path( &user, key, is_admin);    	/* shared_survey_path_util.c */

   set_data(&user, key, count); 

   cs_cgi_display("survey_new", 1); 		/* shared_cs_util.c */
   cs_cgi_destroy();
   return 0;
}
