#include <stdlib.h>
#include <stdio.h>
#include "../global.h"
#include "../survey.h"
#include "shared_survey_update_info.h"
#include "shared_survey_info_list_parser.h"
#include "shared_survey_util.h"
#include "shared_survey_string.h"
#include "shared_lock.h"
#include "shared_survey_path_util.h"
#include "shared_survey_log.h"
#include "shared_cs_util.h"

void
update_survey_info_state(SESSION *user,  
	const char* count, const char *update_state, const char *username, 
	const char* new_count)
{
    char filename[MAX_PATH +1];
    char base[MAX_PATH +1];
    char timestring[MAX_TIMESTRING +1];
    char www_path[MAX_PATH +1];
    SURVEY_INFO_LIST *list;
    FILE *fp;
    time_t now;
    char *str;
    now = time (NULL);
    strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
              localtime (&now));
    if(strlen(username) ==0)
      get_survey_base(user, base);
    else
      snprintf(base, MAX_PATH +1, "../%s/%s", SURVEY_DIR, username);

    snprintf(filename, MAX_PATH+1, "%s/%s",base,SURVEY_INFO_FNAME);
    list = survey_info_parser(filename);
    write_to_log( filename);

    if(list)
    {
      fp = fopen(filename, "w");
      if(!fp)
	 cs_critical_error(ERR_FOPEN_WRITE_FAILED, "");
       get_exclusive_lock(fileno(fp),1); 
      fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
      fprintf(fp, "<%s>\n", SURVEY_LIST_TAG);

      list->current = list->head;
      while(list->current)
      {
	 if(!strcmp(list->current->id, count))
	 {
           if(list->current->state)
	   {
             free(list->current->state);
	     list->current->state = malloc_str(update_state);
           }
	   if(!strcmp(update_state, SURVEY_STATE_FIN))
	   {
	      if(list->current->finish_time )
	      {
	         free(list->current->finish_time);
	         list->current->finish_time = malloc_str(timestring);
	      } 
	   }
	   else if(!strcmp(update_state, SURVEY_STATE_REL))
	   {
	      if(list->current->release_time)
	      {
		free(list->current->release_time);
		list->current->release_time = malloc_str(timestring);
	      }
	   }
	   else if(!strcmp(update_state, SURVEY_STATE_WWW))
	   {
	      if(list->current->release_time)
	      {
		free(list->current->release_time);
		list->current->release_time = malloc_str(timestring);
		snprintf(www_path, MAX_PATH +1, "../%s/%s/%s",
			 SURVEY_DIR, SURVEY_WWW_DIR, new_count);
		free(list->current->survey_domain);
		list->current->survey_domain = malloc_str(www_path);
	      }
	   }
	   else if(!strcmp(update_state, SURVEY_STATE_CLO))
	   {
	      if(list->current->close_time)
	      {
		  free(list->current->close_time);
		  list->current->release_time = malloc_str(timestring);
	      }
	   }
	 }
	 if(list->current->title)
	 {
           str = replace_sp(list->current->title);
           free(list->current->title);
           list->current->title = malloc_str(str);
           if(str)
	     free(str);
         }	   
	 write_one_survey_info(list->current, fp);
	 list->current = list->current->next;
      }
      fprintf(fp, "%s", SURVEY_INFO_END_TAG);
      release_lock(fileno(fp));
      fclose(fp);
       free_survey_info_list(list);
    }
}

