#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_super_util.h"
#include "manhat-lib/shared_survey_info_list_parser.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_survey_reuse_util.h"
#include "manhat-lib/shared_cs_util.h"




void
read_parameters( char *key, char *count, int *is_admin, 
		int *domain, char *course, char *linkTitle, char *username, int *to)
{
   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("crs", course, MAX_PATH);
    
    cs_get_optional_parameter ("domain",tmp,  9);
    if(*tmp)
         *domain = atoi(tmp);
    else
         *domain = -1;
    
    
    cs_get_optional_parameter ("username", username,  MAX_USERNAME);
    
    cs_get_optional_parameter ("to", tmp,  9);
    *to = (!strcmp(tmp, "yes"))? 1 : 0;

    cs_get_optional_parameter ("linkTitle", linkTitle,  MAX_SURVEY_TITLE);
}


 



    
void
do_action(const char  *count, SESSION *user, int is_admin, 
		int domain,  char *linkTitle, 
		const char *username, int to)
{
   char survey_path[MAX_PATH +1];
   SURVEY_INFO *info;
   char new_count[MAX_PATH +1];
   char org_survey_path[MAX_PATH +1];

   org_survey_path[0] = '\0';
  
   if(!*username) 
      get_survey_base(user, survey_path); /* shared_survey_path_util.c */
   else if(!to)
      snprintf(survey_path, MAX_PATH + 1, "../%s/%s", SURVEY_DIR, username);
   else if(to && strlen(username))
      snprintf(survey_path, MAX_PATH + 1, "../%s/_%s_", SURVEY_DIR, ADMIN_DIR);

   /* create new survey id */ 
   mkdir(survey_path, 0770);
   get_survey_id(new_count, survey_path); /* shared_survey_util.c */
   create_new_survey_dir(survey_path, new_count);

   if((!is_admin && (domain == 1)) || (is_admin && (domain == 1)  && *username))
	 snprintf(org_survey_path, MAX_PATH +1, "../%s/_%s_/%s", SURVEY_DIR, ADMIN_DIR, count);
   
   info = get_survey_info(count, new_count , user, org_survey_path, linkTitle, username, to, 0);
   if(*org_survey_path)
      copy_survey_file(org_survey_path,  new_count, user, username, to);
   else
      cs_critical_error(ERR_COPY_ERROR, "");
  record_survey_info(info, user, username, to);
  free_survey_info(info);

}

int 
main()
{
   int is_admin;
   char course[MAX_PATH + 1];
   char linkTitle[MAX_SURVEY_TITLE +1];
   char key[MAX_KEY +1];
   SESSION user;
   char count[MAX_PATH +1];
   int domain;
   char username[MAX_USERNAME +1];
   int to;

   cs_cgi_init();
   read_parameters(key, count, &is_admin, &domain, course, linkTitle, username,&to);
   verify_key_path( &user,key, is_admin);
   do_action(count, &user, is_admin, domain, linkTitle, username, to);
   if(to)
   {
     decode_space(linkTitle);
     cs_set_value("linktitle", linkTitle);
     cs_set_value("success_code", SURVEY_COPY_CODE);
     cs_cgi_display("survey_success", 1);
   }
   else if(is_admin )
   {
       if(!*username)
          printf("Location: %s?id=%s&admin=%d\n\n", "survey_read_info",  key, is_admin);
       else
          printf("Location: %s?id=%s&admin=%d&username=%s\n\n", "survey_read_info",  key, is_admin, username);
   }
   else  
       printf("Location: %s?id=%s&admin=%d&crs=%s\n\n", "survey_read_info",  key, is_admin, course);

   cs_cgi_destroy();       
   return 0;
}
