#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.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"

#define NOTEAM '!'		


static void
read_parameters (char *course, char *key, int *grp, int *protect, long *offset)
{
  char msg[50];    /* scratch string */

  cs_get_required_parameter ("crs", course, MAX_PATH);	/* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);

  cs_get_required_parameter ("protect", msg, 5);
  *protect = atoi (msg);

  cs_get_required_parameter ("loc", msg, 40);
  *offset = atol (msg);

  cs_get_required_parameter ("grp", msg, 5);
  *grp = atoi (msg);
  
}



   

void
protect_unprotect_msg(SESSION *user, int grp, long offset, int protect, 
                                CONFIG_STRUCT *conf, char *sender_team, int *msg_id)
{
 int fd;
 FILE *fp;
 char inbox_fname[MAX_PATH + 1];  /* full path to central inbox */
 CENTRAL_INBOX_RECORD data;

  set_discussion_names (conf->course_path, user->username, grp);	/* shared_news_util.c */

  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);

  if(protect)
     SET_PROTECTED(data.status); /* #defined in global.h */
  else
     CLEAR_PROTECTED(data.status);  /* #defined in global.h */

  
  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;
   
 *msg_id = data.info.msg_id;  
   
}


   

int
main (void)
{
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  int grp;                      /* from grp=? command line */
  int protect;                     /* from hide=? command line - hide or unhide the msg? */
  long offset;                   /* from loc=? command line */
  char team;                    
  int msg_id;                    /* used to return to a named anchor */
  
  
  cs_cgi_init();
  read_parameters (course, key, &grp, &protect, &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, "");


  protect_unprotect_msg(&user, grp, offset,protect, &conf, &team, &msg_id);
  
  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;
}



