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

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

#include "manhat-lib/shared_survey_record_html_parser.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_survey_table.h"
#include "manhat-lib/shared_survey_www_util.h"
#include "manhat-lib/shared_survey_xml_parser.h"
#include "manhat-lib/shared_cs_util.h"

void
read_parameters(char *count, int *csv, char *key, int *crs, int *password)
{

    char is_edit[20];

    cs_get_required_parameter("id", count, MAX_PATH);
    cs_get_optional_parameter("key", key, MAX_KEY);
    cs_get_optional_parameter("crs", is_edit, 19);
    if(strlen(is_edit))
         *crs = atoi(is_edit);
    else
       *crs = -1;

    cs_get_optional_parameter ("pass", is_edit, 19);
    if(strlen(is_edit))
       *password = atoi(is_edit);
    else
      *password =0;

    cs_get_optional_parameter ("csv", is_edit, 19);
    if(strcmp(is_edit, "yes") ==0)
     *csv = 1;
    else
      *csv = 0;
}

void
set_urls( const char *count, int password, int crs, const char *key)
{
  char url[MAX_PATH +1];

  if(strlen(key) && crs ==1)
      snprintf(url, MAX_PATH +1, "%s?id=%s&amp;csv=yes&amp;crs=%d&amp;key=%s&amp;ext=.csv",
            "survey_www_table",  count, crs, key);
  else
    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;csv=yes&amp;pass=%d&amp;ext=.csv",
            "survey_www_table",  count, password);
  cs_set_value("csv_url", url);
}

     
   
void
do_action( const char *count, int csv, int crs, const char *key, int password)
{
    char fname[MAX_PATH +1];
    char survey_path[MAX_PATH +1];
    RECORD_LIST *list;
    int total = 0;
    SURVEY_LIST *survey_list;
    
    snprintf(survey_path, MAX_PATH +1, "../%s/%s/%s", SURVEY_DIR, SURVEY_WWW_DIR, count);

    snprintf(fname, MAX_PATH +1, "%s/%s",survey_path, SURVEY_RESULT_FNAME);

    list = record_html_parser(fname);
    if(list->head)
    {
      if(csv)
      {
              snprintf(fname, MAX_PATH +1, "%s/%s",survey_path, SURVEY_XML_FNAME);
	      survey_list = survey_data_parser(fname);
	      send_survey_results_csv(list, -2, "web_accessible", survey_list);   /* shared_survey_table.c */
	      free_survey_data_list(survey_list);

      }  
     else
      {

       set_urls( count, password, crs, key);
       list->current = list->head;
       set_survey_table_heading(list->current, -2);
       while(list->current)
       {
          set_survey_table_one_record(list->current, -2, total);   /* shared_survey_table.c */
          list->current = list->current->next;
	  total++;
	
       }
       set_survey_table_basic_data(list, total, -2);    /* shared_survey_table.c */
       cs_set_value("survey_type", "www_accessible");
       cs_cgi_display("survey_table_data", 1);

      }
    }
    else
      cs_critical_error(ERR_SURVEY_RESULT_NOT_AVAILABLE, "");
    free_record_html_list(list);

}

int
main()
{
    char count[MAX_PATH +1];
    char key[MAX_KEY +1];
    int csv, password, crs, realpass;
    SESSION user;
    
    cs_cgi_init();

    read_parameters (count, &csv, key, &crs, &password);

    realpass = read_password(count);  /* shared_survey_www_util.h */
    if(password == 0 && strlen(key) && crs ==1)
    {
       validate_super_key(key, &user);
       do_action( count, csv, crs, key, password);
    }
    else if(password == 0 && !strlen(key) && crs ==-1)    /* protected result print access code form */
    {
	  cs_set_int_value("display", 0);
          cs_cgi_display("survey_www_access_form", 1);
    }
    else if(password == realpass)
    {
	do_action( count, csv, crs, key, password);
       //cs_cgi_display("survey_table_data", 1);
    }
    else
    {
	  cs_set_int_value("display", 0);
          cs_cgi_display("survey_www_access_form", 1);
    }
    cs_cgi_destroy();
    return 0;
}

       
    
	
