#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_news_util.h"
#include "manhat-lib/shared_translate_url.h"


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

  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("from", from, MAX_PATH);
  
  cs_get_required_parameter ("msg_id", msg, 79);	/* shared_cs_util.h */
  *msg_id = atoi (msg);

  cs_get_optional_parameter("from_from", from_from, MAX_PATH);
}






/* note no file locking...
** only this user should be reading/writing
** from the user inbox record file
*/
static void
toggle_flag (int msg_id)
{
  USER_INBOX_RECORD data;
  FILE *fp;
  long offset = 0;
  int done = 0;
  
  fp = open_user_inbox("r+");     /* shared_news_util.c */
  if(fp)   /* if the local inbox file exists */
   {
    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)    /* match not found, write a new record */
         {
           data.msg_id = msg_id;
           data.time_read = 0;
           data.user_unlocked = 0;
           data.user_hidden = 0;
           data.flagged = 1;
           data.future1 = 0;
           data.future2 = 0;
         
           if(fwrite(&data, sizeof(USER_INBOX_RECORD), 1, fp) != 1)
            cs_critical_error(ERR_FWRITE_FAILED, USER_INBOX_FNAME);
           done = 1;
         }
       else   
        if(data.msg_id == msg_id)
         {
           data.flagged = !data.flagged;
          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);
    }
   else  /* local inbox file doesn't exist - must write a new record */
     {
     fp = open_user_inbox("a");
     if(fp)
      {
           data.msg_id = msg_id;
           data.time_read = 0;
           data.user_unlocked = 0;
           data.user_hidden = 0;
           data.flagged = 1;
           data.future1 = 0;
           data.future2 = 0;
         
           if(fwrite(&data, sizeof(USER_INBOX_RECORD), 1, fp) != 1)
            cs_critical_error(ERR_FWRITE_FAILED, USER_INBOX_FNAME);
           
           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 */

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

  char from[MAX_PATH + 1];
  char from_from[MAX_PATH + 1];
  char translated_from[MAX_PATH + 1];
 
  cs_cgi_init(); 
  read_parameters (course, key, &grp, &msg_id, from, from_from);

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

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

  translate_url_substitutes(translated_from, from,  MAX_PATH, 0);   /* shared_translate_url.c */


  /* start the Location: redirection string */
  printf ("Location: %s%s%s", 
                        translated_from, 
                        *from_from?"&from=":"", 
                        from_from);

  /* Location: ends with a named anchor to the msg_id,
  ** unless this program was called from news_gather or assign_gather.
  ** In that case, the named anchar part is already in the translated_from string
  */

  if(!strstr(translated_from, "_gather"))
       printf("#%d\n\n", msg_id);
  else
      printf("\n\n");
     
  
                        
  cs_cgi_destroy();
  return 0;
}



