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


#include "global.h"
#include "custom.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, NEWS_FILTER_CMD *cmd, char *from)
{
  char msg[80];


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

  cs_get_required_parameter ("grp", msg, 79);	/* shared_cgi_util.c */
  *grp = atoi (msg);

  cs_get_required_parameter ("cmd", msg, 79);	/* shared_cgi_util.c */

  *cmd = OLDEST_FIRST;   /* so smart compilers won't complain about uninitialized variables */
  switch(atoi(msg))
  {
   case 0:  *cmd = OLDEST_FIRST; break;
   case 1:  *cmd = NEWEST_FIRST; break;
   case 2:  *cmd = SHOW_USER_HIDDEN; break;
   case 3:  *cmd = HIDE_USER_HIDDEN; break;
   case 4:  *cmd = SHOW_TEACHER_HIDDEN; break;
   case 5:  *cmd = HIDE_TEACHER_HIDDEN; break;
   default:  cs_critical_error(ERR_UNKNOWN_CASE_IN_SWITCH, "");
             break;
  }           

}



static
void set_news_filter(NEWS_FILTER *filter, NEWS_FILTER_CMD cmd)
{

    switch (cmd)
    {
     case OLDEST_FIRST:         filter->oldest_first = 1;
                                break;
     case NEWEST_FIRST:         filter->oldest_first = 0;
                                break;
     case SHOW_USER_HIDDEN:     filter->show_user_hidden = 1;
                                break;
     case HIDE_USER_HIDDEN :    filter->show_user_hidden = 0;
                                break;
     case SHOW_TEACHER_HIDDEN : filter->show_teacher_hidden = 1;
                                break;
     case HIDE_TEACHER_HIDDEN : filter->show_teacher_hidden = 0;
                                break;
     
     default: cs_critical_error(ERR_UNKNOWN_CASE_IN_SWITCH, "set_news_filter()");
              break;
    }

}




static void
write_new_filter (NEWS_FILTER_CMD cmd, const char *fromstring)
{
  NEWS_FILTER filter;
  FILE *fp;
  char filter_path[MAX_PATH + 1];


  snprintf (filter_path, MAX_PATH + 1, "%s%s", discussion_path,
	    strstr(fromstring,"post_outbox")?OUTBOX_FILTER_FNAME:NEWS_FILTER_FNAME);

  fp = fopen (filter_path, "r+");
  if(fp)
   {
    if(fread (&filter, sizeof(NEWS_FILTER),1,fp) != 1)
       cs_critical_error(ERR_FREAD_FAILED,filter_path);

    set_news_filter(&filter, cmd);

    if(fseek(fp,0,SEEK_SET))
       cs_critical_error(ERR_FSEEK_FAILED, filter_path);
    
    if(fwrite (&filter, sizeof(NEWS_FILTER),1,fp) != 1)
       cs_critical_error(ERR_FWRITE_FAILED,filter_path);
   fclose(fp);                                                                                                                                                                       
   }
  else   /* file couldn't be opened for r+ - write a new one */
   {
      fp = fopen (filter_path, "w");
      if(!fp)
        cs_critical_error(ERR_FOPEN_WRITE_FAILED, filter_path);
      filter. oldest_first = 1;
      filter.show_user_hidden = 1;
      filter.show_teacher_hidden = 1;
      
      set_news_filter(&filter,cmd);
      
      if(fwrite(&filter, sizeof(NEWS_FILTER), 1, fp) != 1)
         cs_critical_error(ERR_FWRITE_FAILED, filter_path);
               
      fclose(fp);
   }
}




static void
redirect_user (char *from)
{
  char new_from[MAX_PATH + 1];

  translate_url_substitutes (new_from, from, MAX_PATH + 1, 0);	/* shared_translate_url.c */
  printf ("Location: %s\n\n", new_from);
}


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 */
  NEWS_FILTER_CMD cmd;		/* from cmd=? in submitted form */
  char from[MAX_FROM + 1];	/* from from=? in submitted form */
  SESSION user;			/* loaded by validate_user() */
  CONFIG_STRUCT conf;		/* the configuration read from config file */

  cs_cgi_init();
  read_parameters (course, key, &grp, &cmd, from);

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

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

  write_new_filter (cmd,from);

  redirect_user (from);
  cs_cgi_destroy();
  return 0;
}



