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



static void
read_parameters (char *course, char *key, int *grp, int *unlock, 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);       /* shared_cgi_util.c */

  cs_get_required_parameter ("unlock", msg, 5);		/* shared_cgi_util.c */
  *unlock = atoi (msg);

  cs_get_required_parameter ("loc", msg, 40);		/* shared_cgi_util.c */
  *offset = atol (msg);


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







static void
get_filelist(CONFIG_STRUCT *conf, CENTRAL_INBOX_RECORD *data, char **filelist)
{
 int max_filelist;
 char infopath[MAX_PATH + 1];
 FILE *fp;
 int fd;
 NEWS_INFO infofile_info;
 ATTACHMENT attach; 
 int i;
  
 max_filelist = (abs(data->info.attachments) * (MAX_FILENAME + 30)) + 20;
 *filelist = (char *) malloc(max_filelist * sizeof(char) );
 if(!*filelist)
     cs_critical_error(ERR_MALLOC_FAILED,"get_filelist()");
 
 strcpy(*filelist, " ");
 
 if(!data->info.attachments)   /* this should never happen */
   **filelist = '\0';
 else  
  if(data->info.attachments == -1)
    cs_set_int_value("website_was_attached", 1);    /* shared_cs_util.c */
 else
  {
   snprintf(infopath,MAX_PATH + 1,"%s%s/%s/%s/%s",conf->course_path,PEOPLE_DIR,
                                                          data->info.sender,discussion_fname,data->info_fname);
  fp = fopen(infopath,"r");
  if(!fp)                             
     cs_critical_error (ERR_FOPEN_READ_FAILED, infopath);
  
  fd = fileno(fp);
  get_shared_lock(fd, 1);                      /* shared_lock.c */    

  if(fread(&infofile_info, sizeof(NEWS_INFO),1,fp) != 1) 
   cs_critical_error (ERR_FREAD_FAILED, "");      /* shared_cs_util.c */

  for (i = 1; i <= infofile_info.attachments; i++)
    {

      if(fread(&attach,sizeof(ATTACHMENT),1,fp) != 1)
	 cs_critical_error (ERR_FREAD_FAILED, infopath);   /* shared_cs_util.c */

      strcat(*filelist,attach.orig_fname);
      strcat(*filelist,"&nbsp;&nbsp;&nbsp;");
    }				/* for .. */

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

  }
 
}



static
void set_unlock_form_data(CONFIG_STRUCT *conf, SESSION *user,  char *course, char *key, int grp, long offset)   
{
 
 CENTRAL_INBOX_RECORD data;
 char *filelist = 0;
 char temp[40];
 
  read_msg_record(offset, &data);   /* shared_news_util.c */
  
  /* BASIC_STATUS is #defined in global.h */
  if(BASIC_STATUS(data.status) == READ_ONLY)
      cs_critical_error(ERR_MSG_IS_READ_ONLY, "");
      
  get_filelist(conf, &data, &filelist);

  cs_set_value("filelist", filelist);        /* shared_cs_util.c */
  if(filelist)
      free(filelist);  /* memory was allocated in get_filelist() */
 


 cs_set_value("crs", course);
 cs_set_value("id", key);
 cs_set_int_value("grp", grp);

 snprintf(temp, 40, "%ld", offset);
 cs_set_value("loc", temp);

 if((user->group == FACULTY) && (grp == TEAM_TEACH))
 {
   snprintf(temp, 40, "%c", data.info.sender_team);
   cs_set_value("team", temp);
 }

}





/* to unlock a msg, simply mark it as read
** then return user to the NEWS_READ program
*/

static 
void unlock_message(CONFIG_STRUCT *conf, SESSION *user, char *course,  char *key, int grp, long offset)
{

 CENTRAL_INBOX_RECORD data;
 time_t now;

  read_msg_record(offset, &data);   /* shared_news_util.c */
 
  now = time(NULL);

  /* is_teacher_hidden() is in shared_news_util.c 
  ** BASIC_STATUS() is a macro #defined in global.h 
  */  
  if(is_teacher_hidden(data.status, data.release_time, data.unrelease_time, now) ||
      BASIC_STATUS(data.status) == READ_ONLY)
    cs_critical_error(ERR_REQUEST_DENIED, "");     /* shared_cs_util.c */
 
  mark_msg_as_read(&data, conf, user);   /* shared_news_util.c */
  
  if((user->group == FACULTY) && (grp == TEAM_TEACH))
   printf("Location:%s?crs=%s&id=%s&loc=%ld&grp=%d&team=%c\n\n", 
  			"news_read", course, key, offset, grp, data.info.sender_team);
  else 
     printf("Location:%s?crs=%s&id=%s&loc=%ld&grp=%d\n\n", 
  			"news_read", course, key, offset, grp);

}





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 unlock;                    /* from unlock=? command line*/
  long offset;                   /* from loc=? command line */
  
 
  cs_cgi_init();                  /* shared_cs_util.c */
  unsetenv ("CONTENT_TYPE");      /* for Internet Explorer ??? */

  read_parameters (course, key, &grp, &unlock, &offset);

  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);		/* shared_util.c */

   if(!has_write_permission(&user, &conf))   /* shared_access.c */
      access_denied_error();	             /* shared_access.c */

  set_discussion_names (conf.course_path, user.username, grp); /* shared_news_util.c */
 
  if(unlock)
     unlock_message(&conf, &user,course,key,grp,offset);
  else
  {
     cs_set_course_info(&conf);                     /* shared_cs_util.c */
     cs_set_current_time();                         /* shared_cs_util.c */
     cs_set_int_value("grp", grp);		   /* shared_cs_util.c */
     
     set_unlock_form_data(&conf, &user, course,key,grp,offset);
     cs_cgi_display("news_unlock", 1);		   /* shared_cs_util.c */
     
  }
  cs_cgi_destroy();                                  /* shared_cs_util.c */
  return 0;
}



