#include <stdio.h>
#include <stdlib.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_record_parser.h"
#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_bar_graph.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_optional_parameter("admin", tmp, 9);
    *is_admin = !(*tmp)? 0 : atoi(tmp)? 1 : 0;

}
   
void
do_action( SESSION *user, const char *count)
{
    char fname[MAX_PATH +1];
    char file_path[MAX_PATH +1];
    char record_fname[MAX_PATH +1];
    struct stat buf;
   
    get_survey_path(user, file_path, count);
    snprintf(fname, MAX_PATH+1, "%s/%s", file_path, SURVEY_XML_FNAME);
    snprintf(record_fname, MAX_PATH+1, "%s/%s", file_path, SURVEY_RESULT_FNAME);
    if(stat(record_fname, &buf))
      cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE , "");
    
    
    /* -1 means to use the number of recipients recorded
    ** in the xml file
    */
    if(!set_survey_graph_results(fname,record_fname,-1))  /* shared_survey_bar_graph.c */
       cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE, "");
    cs_set_value("survey.type", "mymanhattan");
   
}

int
main()
{
    int is_admin;
    char count[MAX_PATH +1];
    char key[MAX_KEY+1];
    SESSION user;

    cs_cgi_init();
    read_parameters(&is_admin, key, count);
    verify_key_path( &user, key, is_admin);
    
    do_action( &user, count);
    
    cs_cgi_display("survey_bar_graph", 1);
    cs_cgi_destroy();
    
    return 0;
}

       
    
	
