#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 "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_calendar_event.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_strtrm.h"

int 
delete_event(CONFIG_STRUCT *conf, int event_id) 
{
    int ret;
    char select[MAX_PATH +1]; 
    char db_name[MAX_PATH +1]; 
    sqlite3 *db;
   
    
    snprintf(db_name, MAX_PATH +1, "%s%s/%s", conf->course_path, CALENDAR_DIR, EVENT_DB);

    snprintf(select, MAX_PATH +1, 
	     "delete from event where id=%d", event_id);

    ret = sqlite3_open(db_name, &db);
    if(ret)
      return 0; 

    ret = sqlite3_exec(db,select,0,0,0);
    sqlite3_close(db);
    return 1;
}



int
set_event_value( int event_id, CONFIG_STRUCT *conf, SESSION *user)
{

   if(!get_event(conf, event_id) )
	return 0;

    if(event_list && event_list->head)
	    set_event(event_list->head, "");
    
   cs_set_int_value( "show_para", user->group);
   cs_set_int_value( "titlelen", MAX_CALENDAR_TITLE);

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


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


   cs_get_optional_parameter( "crs", course, MAX_PATH);
   cs_get_required_parameter("id", key, MAX_KEY);
   cs_get_required_parameter("event_id", temp, 39);
   *id = atoi(temp);
}

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

  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(user.group == STUDENT)
     cs_critical_error(ERR_REQUEST_DENIED, "");

  operE = hdf_get_value(global_cgi->hdf, "Query.operE", 0); 
  operD = hdf_get_value(global_cgi->hdf, "Query.operD", 0); 

  cs_set_course_info( &conf);
  if(operE && strlen(operE) )
   {
       if(!set_event_value( event_id, &conf, &user))
	   cs_critical_error(ERR_CALENDAR_SET_EVENT, "set_event_value");
       else
       {
         cs_load_lang("calendar_event_edit");
         cs_cgi_display("calendar_event_modify", 0);
       }
   } 
  else if(operD && strlen(operD))
   { 
       if(!delete_event(&conf, event_id))
	   cs_critical_error(ERR_CALENDAR_DELETE_EVENT, "delete_event_value");
       else
       {
	 cs_set_int_value( "delete_success", 1);
         cs_load_lang("calendar_event_edit");
         cs_cgi_display("calendar_event_detail", 0);
       }
   }

 
   cs_cgi_destroy();

  return 0;			/* exit successfully */

}
    


