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

#include "global.h"
#include "custom.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_course_search.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_super_menu.h"
#include "manhat-lib/shared_cs_util.h"


#define MAX_LINE 300



void
read_parameters (char *key, int *view_xml, int *show_form)
{
   char tmp[20];
   
   
   cs_get_required_parameter ("id", key, MAX_KEY);
   cs_get_optional_parameter("view",tmp, 19);
   *view_xml = strlen(tmp)? 1 : 0;


   cs_get_optional_parameter("form",tmp, 19);
   if(strlen(tmp))
     *show_form = atoi(tmp);
   else
     *show_form = 0;
   
}


void
set_upload_form(const char *key, const char *username)
{
   char xml_path[MAX_PATH +1];

   snprintf(xml_path, MAX_PATH + 1, XML_ROSTER_PATH, username);
   cs_set_value("xml_path", xml_path);
   if(file_exists( xml_path))
     cs_set_int_value("has_file", 1);
    
}




static 
void send_xml_file(const char *username)
{
 FILE *fp;
 char xml_path[MAX_PATH + 1];
 struct stat buf;
 char *buffer;
 
 snprintf(xml_path, MAX_PATH + 1, XML_ROSTER_PATH, username);
 cs_set_value("xml_path", xml_path);
 if(!stat(xml_path, &buf) )
 {
   buffer = (char *) malloc( (buf.st_size + 1) * sizeof(char));
   if(!buffer)
      cs_critical_error (ERR_MALLOC_FAILED, "");

    fp = fopen(xml_path,"r");
  
    if (fread (buffer, buf.st_size, 1, fp) != 1)
       cs_critical_error (ERR_FREAD_FAILED, "");
    fclose(fp);

    printf ("Content-type:text/xml\n");
    printf ("Content-disposition: filename=\"%s\"\n", "myfile");
    printf ("Content-length: %ld\n\n", (long)buf.st_size);

     if (fwrite (buffer, buf.st_size, 1, stdout) != 1)
          cs_critical_error (ERR_FWRITE_FAILED, "");
     free(buffer);
  }
  else
  {
       cs_critical_error (ERR_GENERAL_ERROR, "There is no XML roster file.");
  }

}

      


int
main()
{
  char key[MAX_KEY +1];
  int view_xml;
  int show_form;
  SESSION user;
  char url[MAX_PATH +1];
  
  cs_cgi_init();
  read_parameters( key, &view_xml, &show_form);
  validate_super_key(key, &user);

  set_admin_page(&user, key, 0, "super_new_course_form"); /* shared_super_page.c */

  if(view_xml)
  {
    cs_set_int_value("view_display", 1);
    send_xml_file(user.username);
  }
  else if(show_form == 1) 
  {
        cs_set_int_value("form_display", 1);
        set_upload_form(key, user.username);
        cs_cgi_display("super_upload_xml_roster_form", 1);
  }
  else
  {
    cs_set_int_value("frame_display", 1);

    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;form=1", "super_upload_xml_roster_form", key);
    cs_set_value("top_url", url); 

    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;view=1", "super_upload_xml_roster_form", key);
    cs_set_value("buttom_url", url);

  cs_cgi_display("super_upload_xml_roster_form", 1);
  }
  cs_cgi_destroy();
  return 0;
}
