#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_survey_string.h"
#include "manhat-lib/shared_survey_util.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_modify.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_survey_read_param.h"


SURVEY_DATA *
read_parameters(int *is_admin, char *key)
{
  char tmp[10];
  SURVEY_DATA *data =0;
  
   cs_get_required_parameter("id", key, MAX_KEY);
   cs_get_optional_parameter("admin", tmp, 9);
   *is_admin = !(*tmp) ? 0 : atoi(tmp);
   
   data = read_title_parameters(get_tag_id()); /* shared_survey_read_param.c */

   return data;
}



void
write_xml(SURVEY_DATA *data, const char *xml_file)
{
   FILE *fp;
   fp = fopen(xml_file, "w");
    xml_header(fp);
   write_title(fp, data);
   xml_close(fp);
   fclose(fp);
}

 



void
record_survey_info(SESSION *user,  SURVEY_INFO *info)
{
    char file_path[MAX_PATH +1];
    char base[MAX_PATH +1];

    get_survey_base(user, base);  /* shared_survey_path_util.c */
    snprintf(file_path, MAX_PATH+1, "%s/%s", base, SURVEY_INFO_FNAME);

   if(!test_file_exist(file_path))
     write_head_info_xml(file_path, info); /* shared_survey_util.h */
   else
     write_survey_info_xml(info, file_path); /* shared_survey_util.h */
}




SURVEY_INFO *
get_survey_info(SURVEY_DATA *data,SESSION *user, const char *survey_id)
{
	char timestring[MAX_TIMESTRING +1];
	time_t now;
	SURVEY_INFO *info;
        char survey_path[MAX_PATH +1];
        get_survey_path(user, survey_path, survey_id);
	now = time(NULL);
        strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
	          localtime (&now));
	info = (SURVEY_INFO *)malloc(sizeof(SURVEY_INFO));
	info = initialize_survey_info(info);
	info->id = malloc_str(survey_id);
	info->title = malloc_str(data->left_cols);
	info->state = malloc_str(SURVEY_STATE_DEV);
	info->create_time = malloc_str(timestring);
	info->finish_time = malloc_str("00:00");
	info->release_time = malloc_str("00:00");
	info->close_time = malloc_str("00:00");
	info->author_id = malloc_str(user->username);
	info->author_name = malloc_str(user->realname);
	info->survey_domain = malloc_str(survey_path);
	return info; 
}




void
do_action( SESSION *user, SURVEY_DATA *data, char *survey_id)
{
   char survey_path[MAX_PATH +1];
   char survey_file_path[MAX_PATH +1];
   SURVEY_INFO *info;

   get_survey_base(user, survey_path); /* shared_survey_path_util.c */

   /* create a directory(username or _admin_ ) under surveys for this user*/ 
    mkdir(survey_path, 0770);
   /* create a directory which is survey id */ 
    get_survey_id(survey_id, survey_path);

   snprintf(survey_file_path, MAX_PATH +1, "%s/%s/%s",
		   survey_path, survey_id, SURVEY_XML_FNAME);
   write_xml(data, survey_file_path); 
   /* write to survey_info.xml  */
  info = get_survey_info( data, user, survey_id);
  record_survey_info(user,  info);
  free_survey_info(info);
}




int 
main()
{
   int  is_admin;
   char key[MAX_KEY +1];
   char survey_id[MAX_PATH +1];
   SURVEY_DATA *data;
   SESSION user;
   int id = 0;

   cs_cgi_init();    /*shared_cs_util.c */
   data = read_parameters(&is_admin, key);

   verify_key_path(&user, key, is_admin);   /* shared_survey_path_util.c */
   
   do_action( &user, data, survey_id);
   
   id = data->id;
   free_survey_data(data);
   printf("Location: %s?admin=%d&id=%s&count=%s&target=%d#%d\n\n", "survey_new", is_admin, key, survey_id, id, id);
   
   cs_cgi_destroy();
   return 0;
}
