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

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
              


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_roster_xml_parser.h"
#include "manhat-lib/shared_survey_string.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_course_info.h"
#include "manhat-lib/shared_super_menu.h"

static
void write_roster_file(const char *buf, char *roster_path)
{


  int fd;
  FILE *fp;
  const char *ptr;
  
  snprintf (roster_path, MAX_PATH + 1, "%sCGI_DATA_roster_XXXXXX", TMP_PATH);
  fd = mkstemp (roster_path);
  if(fd == -1)
     cs_critical_error (ERR_MKSTEMP_FAILED, "");
  close(fd);
  change_mkstemp_permission(roster_path);   /*shared_file_util.c */
  fp = fopen (roster_path, "w");
  if (!fp)
    cs_critical_error (ERR_FOPEN_WRITE_FAILED, roster_path);

  for(ptr=buf; *ptr; ptr++)
   {
      fputc(*ptr, fp);
   }     

  fclose(fp);

}



void
buffer_standard_xml_file ( const char *roster_path, char **buffer )
{
 FILE *fp;
 struct stat statbuf;
 
 *buffer = (char *) 0;
 
 if (stat (roster_path, &statbuf)) 
 {
    cs_critical_error(ERR_FOPEN_READ_FAILED , "");
 }  

 *buffer = (char *) malloc((statbuf.st_size + 1)  * sizeof(char));
 if(!*buffer)
  {
   cs_critical_error (ERR_MALLOC_FAILED, "");
  }  
  
 fp = fopen(roster_path, "r");
 if(!fp)
  {
   cs_critical_error(ERR_FOPEN_READ_FAILED , "");
  } 
 
 if(fread(*buffer,statbuf.st_size,1,fp) != 1)
  {
    cs_critical_error(ERR_FREAD_FAILED, "");
  }

 fclose(fp);
 *(*buffer + statbuf.st_size) = '\0';   /* null terminate the string */
}


XML_COURSE_INFO_LIST *
build_xml_data_list( char *roster_path, const char *username)
{
   XML_COURSE_INFO_LIST *head;    /* shared_roster_xml_parser.h */
   char *buf;
   char *xml_data =0;
   char *roster =0;

   xml_data = hdf_get_value(global_cgi->hdf, "Query.xml_data", 0);  /* was xml_data passed via CGI input? */
   roster = hdf_get_value(global_cgi->hdf, "Query.roster_file", 0);  /* was xml_data passed via CGI input? */
   

   if(xml_data)
     {         
       /** allocate enough space to hold all of the XML data */
       buf = malloc_str(xml_data);          /* shared_survey_string.c */
       write_roster_file(buf, roster_path);
     }
   else if(roster && strlen(roster))
   {
       snprintf(roster_path, MAX_PATH + 1, "%s/%s", AUTO_UPDATE_ROSTER_PATH, roster);
       buffer_standard_xml_file(roster_path, &buf);
   }
   else     /* no xml_data provided via a form - try reading the standard text file */
     {
       snprintf(roster_path, MAX_PATH + 1, XML_ROSTER_PATH, username);
       buffer_standard_xml_file(roster_path, &buf);
     } 
       
   
      
   head = xml_course_data_parser(buf, roster_path);   /* shared_roster_xml_parser.c */
   
   free(buf);
   return head;
}





   

int main()
{
   char key[MAX_KEY + 1];
   char roster_path[MAX_PATH + 1];  /* path to roster file containing XML data */
   XML_COURSE_INFO_LIST *list;    /* shared_roster_xml_parser.h */
   SESSION user;
   SUB_DIR_ID_NODE *unique_dir_list;
   SUBDIR_COURSE_LIST *sub_list;
  
   cs_cgi_init(); 
   cs_get_required_parameter ("id", key, MAX_KEY );    /* shared_cs_util.c */
   validate_super_key(key, &user);   /* shared_super_util.c */ 
   
   set_admin_page(&user, key, 0, "super_new_course_form");  /* shared_super_menu.c */
   list = build_xml_data_list(roster_path, user.username);  /* shared_roster_xml_paraser.c */
   
   unique_dir_list = build_unique_subdir_list(list);    /* shared_course_info.c */
   sub_list = build_sub_list_info(unique_dir_list);     /* shared_course_info.c */
   free_sub_list_info(unique_dir_list);                 /* shared_course_info.c */
   
   list_courses(list, key, roster_path, sub_list);      /* shared_course_info.c */
   free_subdir_course_list(sub_list);                   /* shared_course_info.c */
   free_xml_data_list(list);                            /* shared_roster_xml_parser.c */
   cs_cgi_display("super_xml_create_courses", 1);
   cs_cgi_destroy();
   return 0;
}
    



	       
