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

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_time.h"

#define NOTEAM '!'		


typedef struct sched_dates
 {
  time_t start_time;
  time_t end_time;
  int 	 no_ending;
 }  SCHED_DATES;




void
read_parameters (char *course, char *key, SCHED_DATES *date, int *grp, long *offset)
{
  char tmp[20];

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  
  cs_get_required_parameter ("grp",tmp, 10);
  *grp = atoi (tmp);
  
  cs_get_required_parameter ("loc",tmp, 10);
  *offset = atol (tmp);

  /* deal with start times */  
  date->start_time = get_time ("start"); /* shared_time.c */

  /* deal with end times */
  cs_get_optional_parameter ("end",tmp, 20);
  if(!*tmp)
    {
      date->no_ending = 1;
      date->end_time = 0;
    }
  else
    {
     date->no_ending = 0;
     date->end_time = get_time ("end");
    }

}






static
void schedule_msg(SCHED_DATES *date, long offset, int *msg_id, int grp, char *sender_team)
{

 FILE *fp;
 int fd;
 char inbox_fname[MAX_PATH + 1];
 CENTRAL_INBOX_RECORD data;  
 
 if (!date->no_ending && (date->start_time >= date->end_time))
    cs_critical_error (ERR_END_BEFORE_START, "");
  
 snprintf(inbox_fname,MAX_PATH + 1,"%s%s",central_discussion_path,INBOX_FNAME);
 fp = fopen(inbox_fname,"r+");
 if(!fp)                              /* inbox.dat doesn't exist */
   cs_critical_error(ERR_FOPEN_READ_FAILED, inbox_fname);
    
 fd = fileno(fp);
 get_exclusive_lock(fd, 1);                      /* shared_lock.c */

 if(fseek(fp, offset, SEEK_SET))
    cs_critical_error(ERR_FSEEK_FAILED, inbox_fname);

 if(fread(&data,sizeof(CENTRAL_INBOX_RECORD),1,fp) != 1)
   cs_critical_error(ERR_FREAD_FAILED, inbox_fname);
   
 *msg_id = data.info.msg_id;  
   
  SET_BASIC_STATUS(data.status, TIMED_RELEASE);   /* SET_BASIC_STATUS #defined in global.h */  
  data.release_time = date->start_time;
  data.unrelease_time = date->end_time;
    
  if(fseek(fp, offset, SEEK_SET))
    cs_critical_error(ERR_FSEEK_FAILED, inbox_fname);
    
  if(fwrite(&data, sizeof(CENTRAL_INBOX_RECORD), 1, fp) != 1)
     cs_critical_error(ERR_FWRITE_FAILED, inbox_fname);

 release_lock(fd);   /* shared_lock.c */
 fclose(fp);


 if(grp == TEAM_TEACH)
   *sender_team = data.info.sender_team;
 else
   *sender_team = NOTEAM;


}  




int
main ()
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  int grp;                      /* from grp=? command line */
  long offset;                  /* from loc=? command line */
  SCHED_DATES date;             /* derived from start, end month, day ,year, etc parameters */
  int msg_id;			/* used to return user to a named anchor in NEWS_MANAGE_TOPIC_LIST */

  char team;			/* for TEAM_TEACH module, this must be set appropriately */      
  SESSION user;
  CONFIG_STRUCT conf;
  
  cs_cgi_init();
  read_parameters (course, key, &date, &grp, &offset);
  
  read_configuration_file (course, &conf);		/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */

  if (user.group != FACULTY)
     cs_critical_error (ERR_REQUEST_DENIED, "");
  
  set_discussion_names (conf.course_path, user.username, grp);	/* shared_news_util.c */


  schedule_msg(&date,offset, &msg_id, grp, &team);
  

  if(team == NOTEAM)
     printf ("Location: %s?crs=%s&id=%s&grp=%d#%d\n\n", "news_manage_topic_list", course, key, grp, msg_id);
   else
     printf ("Location: %s?crs=%s&id=%s&grp=%d&team=%c#%d\n\n", "news_manage_topic_list", course, key,grp,team,msg_id);       

 cs_cgi_destroy();
  return 0;			/* exit successfully */

}

