
#include "shared_grade_book_release.h"

void
write_student_newfile (char *username, CONFIG_STRUCT *conf)
{

  FILE *fp;
  char fname[MAX_PATH + 1];

  snprintf (fname, MAX_PATH + 1, "%speople/%s/grades/%s", conf->course_path,
	    username, GRADES_NEW_FNAME);
  fp = fopen (fname, "w");
  if (!fp)
    cs_critical_error (ERR_FOPEN_WRITE_FAILED, fname);
  fclose (fp);
}



static void
kill_redfiles (char *dirname)
{
  DIR *dir_p;
  struct dirent *dir_entry_p;
  char *ptr;
  char red_fname[MAX_PATH + 1];



  dir_p = opendir (dirname);
  if (!dir_p)
    cs_critical_error (ERR_OPENDIR_FAILED, "");


  while (NULL != (dir_entry_p = readdir (dir_p)))
    {
      ptr = strstr (dir_entry_p->d_name, ".red");
      if (ptr && *(ptr + 4) == '\0')	/* i.e. if file ends in .red */
	{
	  snprintf (red_fname, MAX_PATH + 1, "%s/%s", dirname,
		    dir_entry_p->d_name);
	  unlink (red_fname);
	}

    }
  closedir (dir_p);

}



/* deletes the 'inf' file and all 'red' files for all teachers */
void
delete_redfiles (P_NODE *head, CONFIG_STRUCT *conf)
{

  char dirname[MAX_PATH + 1];
  char fname[MAX_PATH + 1];
  P_NODE *ptr;
  
  for(ptr= head; ptr; ptr=ptr->next)
    if(ptr->data.group == FACULTY)
     {

     snprintf (dirname, MAX_PATH + 1, "%speople/%s/grades/", conf->course_path, ptr->data.username);
     snprintf (fname, MAX_PATH + 1, "%s%s", dirname, GRADES_INF_FNAME);

     unlink (fname);   

     kill_redfiles (dirname);
     }

}

