#include <stdio.h>
#include <string.h>
#include <unistd.h>		/* for unlink, mktmp ... */

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>     /* Directory information.       */
#include "global.h"


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


static void
read_parameters( char *key)
{
   cs_get_required_parameter ("id", key, MAX_KEY);       /* shared_cs_util.c */
}


/* WNEC's academic year starts in July.
** e.g. all of the following are academic year 2006:
**
**       July 2006 
***      September 2006
***      January 2007
***      May 2007
***      June 2007
***
*** This function returns the WNEC Academic year in 'year',
*** and the actual month (1-12) in 'month', and the actual
*** day 1-31 in 'day'
**/



void  get_current_academic_year(int *year, int *month, int *day)
{
     time_t now;
     struct tm *local_time;
     now = time(NULL);
     local_time = localtime(&now);

     if(local_time->tm_mon < 4)
         *year = local_time->tm_year + 1900 - 1;
     else
         *year = local_time->tm_year + 1900;
     *month = local_time->tm_mon + 1;
     *day = local_time->tm_mday;

}



/*** looks in the ../auto_update/rosters directory for 
**** recently downloaded roster files, and sets hdf 
**** data accordingly.
****
**** Returns the number of roster files found
***/

int
set_xml_file_data()
{
   struct stat file_stat;
   char path[MAX_PATH + 1];
   char temp[MAX_PATH + 1];
   int file_count = 0;
   char name[MAX_PATH + 1], time_string[MAX_TIMESTRING + 1];
   DIR *dir_ptr;
   struct dirent *file_p;
   char *ptr, *ptr2;
   char term_div[MAX_PATH + 1];
   int i =0;

   snprintf(path, MAX_PATH + 1, AUTO_UPDATE_ROSTER_PATH);
   dir_ptr = opendir(path);
   if(!dir_ptr)
       cs_critical_error(ERR_OPENDIR_FAILED, path);
   
   while (NULL != (file_p = readdir (dir_ptr)))
   {
        if (*file_p->d_name != '.')
        {
           i = 0;
	   snprintf(temp, MAX_PATH + 1, "%s/%s", path, file_p->d_name);
	   snprintf(name, MAX_PATH + 1, "file.%d.name", file_count);
	   cs_set_value(name, temp);

           if(stat(temp, &file_stat))
                cs_critical_error(ERR_STAT_FAILED, temp);

           snprintf(name, MAX_PATH + 1, "file.%d.time", file_count);
           strftime(time_string, MAX_TIMESTRING + 1, system_data_str("DOW_DATE_TIME_FORMAT"), /* shared_system_data.h */
	                   localtime(&(file_stat.st_mtime)));
           cs_set_value(name, time_string);

           snprintf(name, MAX_PATH + 1, "file.%d.division", file_count);
           cs_set_value(name, file_p->d_name);

	   term_div[0] = '\0';   
	   strncpy(temp, file_p->d_name, MAX_PATH + 1);
	   for (ptr = temp; *ptr && *ptr !='-' ; ptr++)
	   {
			   if(*ptr == '_')
			      term_div[i] = ' ';
                           else
			      term_div[i] = *ptr;
			   term_div[i + 1] = '\0';
                           
                           i++;
	    }
	    snprintf(name, MAX_PATH + 1, "file.%d.term", file_count);
	    cs_set_value(name, term_div);

           ptr = strchr(temp, '-');
           if(ptr)
           {
                    *ptr = '\0';
                    ptr++;
                    ptr2 = strchr(ptr, '.');
                    if(ptr2)
                      *ptr2 = '\0';
	            snprintf(name, MAX_PATH + 1, "file.%d.year", file_count);
                    cs_set_value(name, ptr);
	   }
            file_count++;
   
			   
	}
   }

   return file_count;
}






void set_data( char *key, SESSION *user)
{
     int year, month, day, file_count = 0; 

     cs_set_value("id", key);

     get_current_academic_year(&year, &month, &day);
     cs_set_int_value("year", year);

     cs_set_value("instructor", user->realname);

     file_count = set_xml_file_data();

     if(file_count == 0)
        cs_set_int_value("roster_not_available", 1);
     
}

 
     
			    

int
main()
{
   char key [MAX_KEY + 1];
   SESSION user;

   cs_cgi_init();    /* shared_cs_util.c */
   read_parameters(key);
   validate_server_key(key,&user);   	/* shared_util.c */

   if(!is_wnec_faculty(user.id) || !system_data_int("ENABLE_FACULTY_COURSE_REQUEST"))        /* shared_util.c  shared_system_data.h*/
        cs_critical_error(ERR_REQUEST_DENIED, "");

   set_data(key, &user);

   cs_cgi_display ("admin_add_course_form", 1);    /* shared_cs_util.c */
   cs_cgi_destroy();        		/* shared_cs_util.c */

   return 0;
}


