#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>

#include "global.h"


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_file_util.h"

void
read_parameters( char *key, char *file)
{
   cs_get_required_parameter("id", key, MAX_KEY);
   cs_get_required_parameter("roster_file", file, MAX_PATH);
   
   
}

void  delete_file(const char *file)
{
   char xml_path[MAX_PATH +1];

   snprintf(xml_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_REQUEST_PATH, file);
   if(file_exists(xml_path))
      cs_critical_error(ERR_GENERAL_ERROR, "Request file needs this file to create courses. You can't delete this file");
   snprintf(xml_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_ROSTER_PATH, file);
   unlink(xml_path);
}

void send_xml_file(const char *file, char *test)
{
   FILE *fp;
   char xml_path[MAX_PATH +1];
   struct stat buf;
   char *buffer;

   if(strcmp(test, "roster") ==0)
      snprintf(xml_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_ROSTER_PATH, file);
   else
      snprintf(xml_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_REQUEST_PATH, file);

   if(stat(xml_path, &buf))
     cs_critical_error(ERR_STAT_FAILED, "send xml file");
   buffer = (char *) malloc (sizeof(char) * (buf.st_size +1));
   if(!buffer)
     cs_critical_error(ERR_MALLOC_FAILED, "send xml file");
   fp = fopen(xml_path, "r");
   if(!fp)
      cs_critical_error(ERR_FOPEN_READ_FAILED, xml_path);

   if(fread (buffer, buf.st_size, 1, fp) != 1)
     cs_critical_error(ERR_FREAD_FAILED, "send xml file");
   fclose(fp);
   *(buffer + buf.st_size) = '\0';

   printf("Content-type:text/xml\n\n");
  
   if(fwrite(buffer, buf.st_size, 1, stdout) != 1)
    cs_critical_error(ERR_FWRITE_FAILED, "");
   free(buffer);

}


int
main()
{
    char key[MAX_KEY +1];
    char file[MAX_PATH +1];
    char *delete =0;
    char *test =0;
    char *create =0;
    char *add_faculty =0;
    char *add_student =0;
    char *update =0;
    char *request =0;

    SESSION user;
    
   
    cs_cgi_init(); 
    
    read_parameters(key, file);
    validate_super_key(key, &user);    
    delete = hdf_get_value(global_cgi->hdf, "Query.delete", "");
    test = hdf_get_value(global_cgi->hdf, "Query.test", "");
    add_faculty = hdf_get_value(global_cgi->hdf, "Query.add_faculty", "");
    add_student = hdf_get_value(global_cgi->hdf, "Query.add_student", "");
    update = hdf_get_value(global_cgi->hdf, "Query.update", "");
    request = hdf_get_value(global_cgi->hdf, "Query.request", "");
    create = hdf_get_value(global_cgi->hdf, "Query.create", "");
    if(strlen(delete))
    {
	 delete_file(file);
	 printf("Location: %s?id=%s\n\n", "super_auto_update_form", key);
    }
    else if(strlen(test))
    {
	 send_xml_file(file, test);
    }
    else if(strlen(add_faculty))
	 printf("Location: %s?id=%s&roster_file=%s\n\n", "super_auto_add_faculty", key, file);
    else if(strlen(add_student))
	 printf("Location: %s?id=%s&std=yes&roster_file=%s\n\n", "super_auto_add_faculty", key, file);
    else if(strlen(update))
	 printf("Location: %s?id=%s&roster_path=%s\n\n", "super_update_courses", key, file);
    else if(strlen(request))
	 printf("Location: %s?id=%s&roster_path=%s\n\n", "super_add_new_courses", key, file);

    else
	 printf("Location: %s?id=%s&roster_file=%s\n\n", "super_xml_create_courses", key, file);

    
    
    cs_cgi_destroy();
    return 0;
}
