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

#include "global.h"

#include "survey.h"
#include "manhat-lib/shared_survey_info_list_parser.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_survey_util.h"
#include "manhat-lib/shared_survey_string.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_log.h"
#include "manhat-lib/shared_cs_util.h"

void
read_parameters(char *key, char *count, int *is_admin, char *username, char *www_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;

     cs_get_optional_parameter ("username", username,  MAX_USERNAME);
     cs_get_optional_parameter ("www_count", www_count,  MAX_PATH);

}





void
rewrite_info(const char *count, SESSION *user, const char *username)
{
   char file[MAX_PATH +1];
   FILE *fp;
   SURVEY_INFO_LIST *list;
   char del_dir[MAX_PATH +1];
   char base[MAX_PATH +1];
   char *str = 0;
   if(strlen(username) ==0)
   {
       get_survey_path(user, del_dir, count); 
       get_survey_base(user, base);
   }
   else
   {
       snprintf(del_dir, MAX_PATH +1, "../%s/%s/%s", SURVEY_DIR, username, count);
       snprintf(base, MAX_PATH +1, "../%s/%s", SURVEY_DIR, username);
   }
   snprintf(file, MAX_PATH +1, "%s/%s", base, SURVEY_INFO_FNAME);
   write_to_log( file);
   list = survey_info_parser(file);

   if(list->head)
   {
      fp = fopen(file, "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->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);
   kill_directory (del_dir);
}





void
delete_www_count(const char *www_count)
{
   char del_dir[MAX_PATH +1];

   snprintf(del_dir, MAX_PATH +1, "../%s/%s/%s", SURVEY_DIR, SURVEY_WWW_DIR,  www_count);
   kill_directory (del_dir);
}





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

   cs_cgi_init();
   www_count[0] ='\0';

   read_parameters(key, count, &is_admin, username, www_count);
   verify_key_path(&user, key, is_admin);
   rewrite_info( count, &user, username);
   if(strlen(www_count))
      delete_www_count(www_count);
   if(*username)
      printf("Location: %s?admin=%d&id=%s&username=%s\n\n", "survey_read_info", is_admin, key, username);
   else
   { 
      cs_set_value("success_code", SURVEY_DELETE_CODE);
      cs_cgi_display("survey_success", 1);
   }
   cs_cgi_destroy();
   return 0;
}
