#include <stdio.h>
#include <sys/stat.h>		/* for stat() */
#include <time.h>
#include <string.h>
#include <ctype.h>


#include "global.h"
#include "custom.h"		/* for date FORMAT strings */


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_read_infofile.h"
#include "manhat-lib/shared_lock.h"

static void
read_parameters (char *course, char *key, long *offset, char *from_prog)
{
  char off_set[MAX_FILENAME + 1];	/* for holding the long value */


  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("loc", off_set, MAX_FILENAME);
  cs_get_required_parameter ("from", from_prog, MAX_FROM);

  *offset = atol (off_set);
}





static void
process_inbox_record (long offset, INBOX_RECORD * inbox, INFO * infofile_info,
                     CONFIG_STRUCT *conf, SESSION *user)
{
  char inbox_path[MAX_PATH + 1];
  FILE *fp;

  snprintf (inbox_path, MAX_PATH + 1, "%s%s/%s/%s/%s", conf->course_path, PEOPLE_DIR,
	    user->username, MAIL_DIR, INBOX_FNAME);
  fp = fopen (inbox_path, "r");
  if (!fp)
    cs_critical_error (ERR_FOPEN_READ_FAILED, inbox_path);
    
  get_shared_lock(fileno(fp), 1);   /* shared_lock.h */  
  if (fseek (fp, offset, SEEK_SET))
    cs_critical_error (ERR_FSEEK_FAILED, inbox_path);
  if (fread (inbox, sizeof (INBOX_RECORD), 1, fp) != 1)
    cs_critical_error (ERR_FREAD_FAILED, inbox_path);
    
  release_lock(fileno(fp));  
  fclose (fp);

  /* shared_read_infofile.c */
  read_post_infofile (inbox->newfile_info.sender,
		      inbox->newfile_info.info_fname, infofile_info, conf);

}



static
void set_recipients(RECEIVE_TYPE which, const char *label)
{
  #define MAX_TMP_NAME 30

  PERSON_NODE *pptr;
  int j =0;
  char name[MAX_TMP_NAME];
  
 for (pptr = person_head; pptr; pptr = pptr->next)
 {
    if (pptr->data.how_received == which)
      {
        snprintf(name, MAX_TMP_NAME, "%s.%d.realname", label, j);
	cs_set_value(name, pptr->data.realname);
	j++;
      }
  }
  snprintf(name, MAX_TMP_NAME, "%s_count", label);
  cs_set_int_value(name, j); 
  #undef MAX_TMP_NAME 
}




static void
set_message_info (INBOX_RECORD * inbox, INFO * infofile_info, SESSION *user)
{
  char timestring[MAX_TIMESTRING + 1];
  char *escaped_subject = (char *) 0;

 /***** From: *******/
  cs_set_value("from", inbox->newfile_info.sender_realname);

 /***** Date: **********/
  strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
	    localtime (&inbox->newfile_info.time_sent));
  cs_set_value("time_sent", timestring);
 


 /* who was it sent to? */
  set_recipients(TO, "to");
  set_recipients(CC, "cc");

   

  /********* Subject *********/
   if(cgi_html_escape_strfunc(inbox->newfile_info.subject,&escaped_subject) == STATUS_OK)
      {
	cs_set_value("subject", escaped_subject);
	if(escaped_subject)
	    free(escaped_subject);
      }
   else
        cs_set_value("subject", inbox->newfile_info.subject);



   /* not really used ? */
   cs_set_value("how_sent", inbox->newfile_info.how_sent == TO ? "TO" :
                                inbox->newfile_info.how_sent == CC ? "CC" :
                                "BCC");

    
    
}





static void
set_attachment_info (char *sender, INFO * infofile_info, CONFIG_STRUCT *conf)
{
 #define MAX_TMP_NAME 30
  ATTACHMENT_NODE *aptr;
  struct stat buf;
  char mail_dir[MAX_PATH + 1];
  char file_path[MAX_PATH + 1];
  int i =0;
  char name[MAX_TMP_NAME];
  char temp[MAX_TMP_NAME];

  if (infofile_info->no_attachments == -1)
      cs_set_int_value("website_attached", 1);
  else if (attachment_head)
    {
      cs_set_int_value("attachment_count", infofile_info->no_attachments);
   
      snprintf (mail_dir, MAX_PATH + 1, "%s%s/%s/%s/", conf->course_path,PEOPLE_DIR,
		sender,MAIL_DIR);

      for (i = 0, aptr = attachment_head; aptr; aptr = aptr->next, i++)
	{
	  snprintf (file_path, MAX_PATH + 1, "%s%s", mail_dir,
		    aptr->data.unique_fname);
	  stat (file_path, &buf);
	  snprintf(name, MAX_TMP_NAME, "attachment.%d.filename", i);
	  cs_set_value(name, aptr->data.orig_fname);
	  snprintf(name, MAX_TMP_NAME, "attachment.%d.size", i);
	  snprintf(temp, MAX_TMP_NAME, "%ld", (long)buf.st_size);
	  cs_set_value(name, temp);
         }
     }    
 #undef MAX_TMP_NAME 
}




static void
set_inbox_info (char *course, char *key, long offset, char *from,
                    CONFIG_STRUCT *conf, SESSION *user)
{
  INBOX_RECORD inbox;
  INFO infofile_info;

  cs_set_course_info(conf);
  cs_set_current_time();

  process_inbox_record (offset, &inbox, &infofile_info, conf, user);
  set_message_info (&inbox, &infofile_info, user);
  set_attachment_info (inbox.newfile_info.sender, &infofile_info, conf);
  
  cs_set_int_value("is_outbox", 0);
  
}



int
main (void)
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  long offset;			/* from loc=? command line */
  char from_prog[MAX_FROM + 1];	/* from from=? command line */
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
  
  cs_cgi_init();

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

  set_inbox_info (course, key, offset, from_prog, &conf, &user);

  free_person_list ();		/* shared_read_infofile.c */
  free_attachment_list ();	/* shared_read_infofile.c */

  /** NOTE: same cs file is used for both inbox and outbox info **/
  cs_cgi_display("post_inbox_outbox_info", 1);
  cs_cgi_destroy();
  return 0;

}






