#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "ClearSilver.h"
#include "calendar.h"
#include "sqlite3.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_calendar_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_calendar_event.h"


void
read_parameters ( char *course, char *key, int *id)
{
   char temp[20];

   cs_get_optional_parameter("crs", course, MAX_PATH);
   cs_get_required_parameter("id", key, MAX_KEY);
   cs_get_required_parameter("event_id", temp, 19);

   *id = atoi(temp); 

}

void
set_event_detail()
{

    if(event_list && event_list->head)
	set_event(event_list->head, "");


}

int
set_value(CONFIG_STRUCT *conf, int event_id,  SESSION *user)
{
   if(!get_event(conf, event_id))
      return 0;

   cs_set_course_info(conf);

    if(event_list && event_list->head)
	set_event(event_list->head, "");

   cs_set_int_value( "show_para", user->group);
   if(user->group != STUDENT)
	 cs_set_int_value( "submit_button", 1);
   else 
	 cs_set_int_value( "submit_button", 0);

   if(event_list)
   {
      free_event_list(event_list->head);
      free(event_list);
   }
   return 1;
}


int
main ()
{
  char course[MAX_PATH +1];	/* from crs=? command line */
  char key[MAX_KEY +1];	/* from id=? command line */
  int event_id;

  SESSION user;
  CONFIG_STRUCT conf;
  *course = '\0';

  cs_cgi_init();
  read_parameters ( course, key, &event_id);

  if(!strlen(course))
  {
     validate_super_key(key, &user);  /* shared_super_util.c */
     mk_conf(&user, &conf);           /* shared_calendar.c */ 
  }
  else 
  {
    read_configuration_file (course, &conf);	/* shared_util.c */
    validate_key (key, &user, &conf);	/* shared_util.c */
  }

  if(!set_value(&conf,event_id, &user))
      cs_critical_error(ERR_CALENDAR_EVENT_DETAIL, "");
  else
  {
    cs_load_lang("calendar_event_edit");
    cs_cgi_display ("calendar_event_detail", 0);
  }
 
  cs_cgi_destroy();

  return 0;			/* exit successfully */

}
    


