#include <stdio.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_util.h"
#include "manhat-lib/shared_news_util.h"



static void
read_parameters (char *course, char *key,
		 int *grp, int *msg_id, int *hide)
{

  char msg[80];


  cs_get_required_parameter ("crs", course, MAX_PATH);	        /* shared_cs_util.h */
  cs_get_required_parameter ("id", key, MAX_KEY);	        /* shared_cs_util.h */

  cs_get_required_parameter ("grp", msg, 79);	/* shared_cs_util.h */
  *grp = atoi (msg);

  cs_get_required_parameter ("msg_id", msg, 79);	/* shared_cs_util.h */
  *msg_id = atoi (msg);

  cs_get_required_parameter ("hide", msg, 79);	/* shared_cs_util.h */
  *hide = atoi (msg);


}






/* note no file locking...
** only this user should be reading/writing
** from the user inbox record file
*/
static void
modify_user_inbox (int msg_id, int hide)
{
  USER_INBOX_RECORD data;
  FILE *fp;
  long offset = 0;
  int done = 0;
  
  fp = open_user_inbox("r+");     /* shared_news_util.c */
  if(fp)   /* the file should always exist, but we'll ignore it if it doesn't */
   {
    while(!done)
     {
       offset = ftell(fp);
       if(offset == -1)
         cs_critical_error(ERR_FTELL_FAILED, USER_INBOX_FNAME);
       if(fread(&data, sizeof(USER_INBOX_RECORD), 1, fp) != 1)
         cs_critical_error(ERR_FREAD_FAILED, USER_INBOX_FNAME);
       else   
        if(data.msg_id == msg_id)
         {
          data.user_hidden = hide;
          if(fseek(fp, offset, SEEK_SET))
            cs_critical_error(ERR_FSEEK_FAILED, USER_INBOX_FNAME);
          if(fwrite(&data, sizeof(USER_INBOX_RECORD), 1, fp) != 1)
            cs_critical_error(ERR_FWRITE_FAILED, USER_INBOX_FNAME);
          done = 1;
         }
      }
      fclose(fp);
    }
}          


int
main (void)
{
  char course[MAX_PATH + 1];	/* from crs=? in submitted form */
  char key[MAX_KEY + 1];	/* from id=? in submitted form */
  int grp;			/* from grp=? in submitted form */
  int msg_id;                   /* from msg_id=? in submitted form */
  int hide;                     /* from hide=? in submitted form */

  SESSION user;			/* loaded by validate_user() */
  CONFIG_STRUCT conf;		/* the configuration read from config file */

  char team_scratch[10];
  char team_string[20];          /* holds "&team=A" for e.g. */
 
  cs_cgi_init(); 
  read_parameters (course, key, &grp, &msg_id, &hide);

  read_configuration_file (course, &conf);
  validate_key (key, &user, &conf);


   if ((user.group == FACULTY) && (grp == TEAM_TEACH))
    {
      cs_get_required_parameter ("team", team_scratch, 5);	/* shared_cs_util.h */
      snprintf(team_string, 20, "&team=%c", *team_scratch);
    }
   else
     *team_string='\0'; 



  set_discussion_names (conf.course_path, user.username, grp);	/*shared_news_util.c */
  modify_user_inbox(msg_id, hide);

  printf ("Location: %s?crs=%s&id=%s&grp=%d%s#%d\n\n", 
                        "news_topic", course, key, grp, team_string,msg_id);
    
  cs_cgi_destroy();
  return 0;
}



