#include <stdio.h>
#include <string.h>

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_cs_util.h"



static void
read_parameters (char *course, char *key, int *csv)
{
 char tmp[25 + 1];
 
  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  
  cs_get_optional_parameter("csv",tmp, 25);
  if(strlen(tmp))
     *csv = atoi(tmp);
  else
     *csv = 0;
}




static void
send_roster (const char *course, CONFIG_STRUCT *conf, int csv)
{
  
  P_NODE *ptr;
  char modified_course[MAX_PATH  + 1];
  char *cptr;
  P_NODE *p_head = build_option_person_list (conf);
  
  /* course contains a slash / separating the directories
  ** first, we convert the slash to a dot . 
  */
  course_slash_to_dot(course, modified_course);   /* shared_util.c */
  
  /* MS IE does a weird thing with an attachment that has two dots.
  ** It appears to put a [1] before the first dot.  For example, if
  ** told that the attachment is called "c102.ac10101.txt", IE will
  ** want to save the file as "c102[1].ac10101.txt".  To circumvent this,
  ** we'll replace the first dot with an underscore
  */
  cptr = strchr(modified_course, '.');
  if(cptr)
     *cptr = '_';
  
    printf ("Content-type:application/octet-stream\n");
    printf ("Content-disposition:attachment; filename=\"%s.txt\"\n\n", modified_course);


 /* Note that we don't send teachers, or any student with a real name that starts with
 ** an asterisk.  This makes it possible to mark students with asterisks if you don't want
 ** to keep track of their grades.
 **/
  for (ptr =p_head; ptr; ptr = ptr->next)
    if ( (ptr->data.group != FACULTY) && (*(ptr->data.realname) != '*') )
      printf ("\"%s\"%c\"%s\"\r\n", ptr->data.id, csv? ',': '\t' ,ptr->data.realname);



    free_option_person_list(p_head);
  
}


void print_padding(int number_padding)
{
   int i;
   for(i =0; i<number_padding; i++)
	printf(" ");
}





void send_fixlength_roster (const char *course, CONFIG_STRUCT *conf)
{
  P_NODE *ptr;
  P_NODE *p_head = build_option_person_list (conf);
  char  *cptr;
  int number_padding =0;
  char modified_course[MAX_PATH  + 1];

  course_slash_to_dot(course, modified_course);   /* shared_util.c */
  
  /* MS IE does a weird thing with an attachment that has two dots.
  ** It appears to put a [1] before the first dot.  For example, if
  ** told that the attachment is called "c102.ac10101.txt", IE will
  ** want to save the file as "c102[1].ac10101.txt".  To circumvent this,
  ** we'll replace the first dot with an underscore
  */
  cptr = strchr(modified_course, '.');
  if(cptr)
     *cptr = '_';

  printf ("Content-type:application/octet-stream\n");
  printf ("Content-disposition:attachment; filename=\"%s.txt\"\n\n", modified_course);
  for (ptr =p_head; ptr; ptr = ptr->next)
  {
    if ( (ptr->data.group != FACULTY) && (*(ptr->data.realname) != '*') )
    {
           
            number_padding = MAX_NAME - strlen(ptr->data.realname);
            printf("%s", ptr->data.realname);
	    print_padding(number_padding);
            number_padding = MAX_ID - strlen(ptr->data.id);
            printf("%s", ptr->data.id);
	    print_padding(number_padding);
            printf("\n");
    }
  }

 free_option_person_list(p_head);
}





int
main ()
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  int csv;                      /* from optional csv=? command line */
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
  
  cs_cgi_init();                                /* shared_cs_util.c */ 
  read_parameters (course, key, &csv);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user,  &conf);	        /* shared_util.c */

  if ( (user.group == FACULTY) || (user.group == ADMIN))
    {
       if(csv == 0 || csv == 1)
	  send_roster (course, &conf, csv);
       else 
	  send_fixlength_roster (course, &conf);

       cs_cgi_destroy();                       /* shared_cs_util.c */
    }
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");
 
  return 0;			/* exit successfully */

}

