#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "shared_survey_reuse_util.h"
#include "shared_survey_util.h"
#include "shared_survey_string.h"
#include "shared_util.h"
#include "shared_survey_info_list_parser.h"
#include "shared_survey_path_util.h"

 
void
record_survey_info( SURVEY_INFO *info, SESSION *user, const char *username, int to)
{
    char file_path[MAX_PATH +1];
    char base[MAX_PATH +1];
    
    if(strlen(username) <= 0)
      get_survey_base(user, base);
    else if(!to)
      snprintf(base, MAX_PATH+1, "../%s/%s",SURVEY_DIR, username);
    else if(to && username >0)
      snprintf(base, MAX_PATH+1, "../%s/_%s_",SURVEY_DIR, ADMIN_DIR);

    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( const char *count, const char *new_count, 
		SESSION *user, char *org_survey_path, char *linkTitle, const char *username,
		int to, int flag)
{
	char timestring[MAX_TIMESTRING +1];
	time_t now;
	char survey_path[MAX_PATH +1];
	char base[MAX_PATH +1];
	char *str = 0;
	SURVEY_INFO *info, *ptr=0;
	SURVEY_INFO_LIST *list;

        if(strlen(username) ==0  || flag) 
          get_survey_base(user, base); 
	else 
	    snprintf(base, MAX_PATH +1, "../%s/%s", SURVEY_DIR, username);
	snprintf(survey_path, MAX_PATH +1,"%s/%s", base,  SURVEY_INFO_FNAME); 
	list = survey_info_parser(survey_path);
	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);
	if(list)
	{
	   list->current = list->head;
	   while(list->current)
	   {
	      if(!strcmp(list->current->id, count))
	      {
		  ptr = list->current;
		  break;
	      }
	      list->current = list->current->next;
	   }
	}
	info->id =malloc_str(new_count);
	if(ptr)
	{
	  if(ptr->title)
	  {
	    str = replace_sp(ptr->title);
	    info->title = malloc_str( str);
	    if(str)
	      free(str);
	  }
	}
	if(!ptr)
	{
	    
            decode_space(linkTitle);
	    info->title = malloc_str(linkTitle);
	}
	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");
	if(strlen(username) ==0)
	{
	   info->author_id = malloc_str(user->username);
	   info->author_name = malloc_str(user->realname);
           get_survey_path(user, survey_path, new_count);	
	}
	else if(!to && strlen(username))
	{
	   info->author_id = malloc_str(username);
	   info->author_name = malloc_str(user->realname);
           snprintf(survey_path, MAX_PATH +1, "../%s/%s/%s", SURVEY_DIR, username, new_count);
	}
	else
	{
	   info->author_id = malloc_str("admin");
	   info->author_name = malloc_str("Admin");
          snprintf(survey_path, MAX_PATH +1, "../%s/_%s_/%s", SURVEY_DIR, ADMIN_DIR, new_count);
        }
	info->survey_domain = malloc_str(survey_path);
	if(ptr && strlen(org_survey_path) ==0)
	{
	   snprintf(org_survey_path, MAX_PATH +1,"%s/%s", base, count);
	}
	free_survey_info_list(list);   
	return info; 
}


     
    

void
create_new_survey_dir( char *dir, const char *count)
{
   char dir_name[MAX_PATH +1];
   snprintf(dir_name, MAX_PATH +1, "%s/%s", dir, count);
   mkdir(dir_name, 0770);
}

void
copy_survey_file(const char *org_survey_path, const char *new_count, SESSION *user, 
		const char *username, int to)
{

   char new_base[MAX_PATH +1];
   char file_or[MAX_PATH +1];
   char file_new[MAX_PATH +1];
   char com[MAX_PATH *2 + 1]; 

   snprintf(file_or, MAX_PATH +1, "%s/%s", org_survey_path,  SURVEY_XML_FNAME);
   if(strlen(username) <=0) 
      get_survey_path(user, new_base, new_count);
   else if(!to)
      snprintf(new_base, MAX_PATH+1, "../%s/%s/%s",SURVEY_DIR, username, new_count);
   else if(to && strlen(username) >0)
      snprintf(new_base, MAX_PATH+1, "../%s/_%s_/%s",SURVEY_DIR, ADMIN_DIR, new_count);

 
   snprintf(file_new, MAX_PATH +1, "%s/%s", new_base,  SURVEY_XML_FNAME);
   snprintf(com, MAX_PATH *2 +1, "cp %s %s",
		   file_or, file_new);
   system(com);

}

    
