#include <stdio.h>
#include <string.h>
#include <sys/stat.h>		/* for stat() */
#include <unistd.h>		/* for unlink() */

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_bio.h"
#include "manhat-lib/shared_spelling_util.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_system_data.h"

/** Course is an optional parameter.  If course is sent,
*** the the user is working within a Manhattan course.
*** Otherwise, they are working from the My Manhattan page.
***/

static void
read_parameters (char *course, char *key, char *username, int *is_admin)
{
  char tmp[10 + 1];


  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("user", username, MAX_USERNAME);
  cs_get_optional_parameter ("crs", course, MAX_PATH);
  cs_get_optional_parameter ("admin", tmp, 10);
  if(strlen(tmp))
    *is_admin =1;
  else
    *is_admin =0;
}
 
 
static 
void set_photo_url(const char *bio_dir, const char *target_username, const char *course, const char *key, GROUP_TYPE group)
{
 char photo_path[MAX_PATH + 1];
 char url[MAX_PATH + 1];
 
 snprintf(photo_path, MAX_PATH + 1, "%s%s", bio_dir, BIO_PIC_FNAME);
 
 if(group == ADMIN)
     snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s&amp;user=%s&amp;admin=1",
        "bio_send_photo",
        key,
        course,
        target_username);
 else
     snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s&amp;user=%s",
        "bio_send_photo",
        key,
        course,
        target_username);
 cs_set_value("photo_url", url);

}                   
                   

  
        

static 
void set_label_content_pairs(FILE *fp)
{
  #define MAX_TMP_NAME 30
 char email[MAX_URL + 1];
 char website[MAX_URL + 1];
 char custom_label[BIO_MAX_CUSTOM_LABEL + 10];
 char custom_content[BIO_MAX_CUSTOM_CONTENT + 10];
 int i, j=0;
 char name[MAX_TMP_NAME];

 
 *email = '\0';
 *website = '\0';

  
 if(fp)
  {
   if(fgets(email,MAX_URL,fp))   /* try to get email address from first line */
    {
      strtrm(email);  		      /* shared_strtrm.c */
      if(fgets(website,MAX_URL,fp))   /* try to read website from second line */
           strtrm(website);           /* shared_strtrm.c */
    }
  }       
 /* set the email address **/  
 cs_set_value("email", email);  
 /* set the website **/  
 cs_set_value("website", website);  

 for(i = 1; i <= BIO_MAX_CUSTOM_ELEMENTS; i++)
  {
   *custom_label = 0;
   *custom_content = '\0';
   if(fp)
    {
     if(fgets(custom_label,BIO_MAX_CUSTOM_LABEL + 10,fp)) 
      {
       strtrm(custom_label);  		  /* shared_strtrm.c */
       snprintf(name, MAX_TMP_NAME, "custom.%d.label", j);
       cs_set_value(name, custom_label);
       if(fgets(custom_content,BIO_MAX_CUSTOM_CONTENT + 10,fp)) 
       {
            strtrm(custom_content);           /* shared_strtrm.c */
            snprintf(name, MAX_TMP_NAME, "custom.%d.content", j);
            cs_set_value(name, custom_content);
       }
       j++;
      }
    }       
  } 
 cs_set_int_value("bio_max_custom_label", BIO_MAX_CUSTOM_LABEL);
 cs_set_int_value("bio_max_custom_content", BIO_MAX_CUSTOM_CONTENT);
 cs_set_int_value("bio_max_custom_elements", BIO_MAX_CUSTOM_ELEMENTS);


#undef MAX_TMP_NAME 
 
} 





static
FILE *open_bio(const char *bio_dir)
{

 char bio_path[MAX_PATH + 1];
 FILE *fp;
 
 snprintf(bio_path, MAX_PATH + 1, "%s/%s", bio_dir, BIO_FNAME);
 fp = fopen(bio_path,"r");
 return fp;
}



static
void set_bio_text(FILE *fp)
{
#define MAX_TMP_NAME 30
 char buffer[200];
 char name[MAX_TMP_NAME];
 char servername[MAX_PATH +1];
 CONFIG_STRUCT fake_conf;
 int i =0;
 
 fake_conf.misc = 0;                                  /* so we don't get a warning on next */
 fake_conf.misc = SET_SPELL_CHECK(fake_conf.misc);    /* so we can fool insert_spell_check_button() */
 if(system_data_int("SPELL_CHECK_ENABLED") && ALLOW_SPELL_CHECK(fake_conf.misc))
 {
    cs_set_int_value("spell_check", 1); 
    cs_set_value("alias", system_data_str("ALIAS"));
    get_server_string(servername, MAX_PATH);
    cs_set_value("servername", servername);

 }

  if(fp)
   {
    while (fgets (buffer, 200, fp))
    {
        snprintf(name, MAX_TMP_NAME, "bio.%d.line", i);
        cs_set_value(name, buffer); 
	i++;
    }
 } 

#undef MAX_TMP_NAME
}




 

static void
set_bio_form_data (const char *course,   /* this could be an empty string */
              const char *key, 
              SESSION *user,         /* person running this program */
              CONFIG_STRUCT *conf,   /* contains junk if course is an empty string */
              const char *target_username)   /* username whose profile we're displaying */
{

 char target_realname[MAX_NAME + 1];
 char bio_dir[MAX_PATH + 1];
 FILE *fp;
 

 
  /** The full (real) name of the person is not stored in the profile. **/
  get_bio_realname(course, conf, target_username, target_realname);   /* shared_bio.c */
  cs_set_value("realname", target_realname);

  /* bio info is either stored in ../users/x/username/bio or, for standalone courses in
  ** course_dir/people/username/bio
  */     

  get_bio_dir(course, conf, target_username, bio_dir);       /* shared_bio.c */

  set_photo_url(bio_dir, target_username, course, key, user->group);
  fp = open_bio(bio_dir);
  set_label_content_pairs(fp);
  set_bio_text(fp);
  if(fp)
      fclose(fp);

  cs_set_int_value("editor_width", system_data_int("EDITOR_WIDTH_STANDARD"));
  cs_set_int_value("hard_wrap", system_data_int("USE_TEXTAREA_HARD_WRAP"));   /* shared_system_data.h */
      
}        
    
     
         
  




int
main ()
{
  char course[MAX_PATH + 1];	            /* from crs=? command line */
  char key[MAX_KEY + 1];	            /* from id=? command line */
  char target_username[MAX_USERNAME + 1];   /* from user=? command line */
  int is_admin;                             /* from admin=? command line */
  SESSION user;
  CONFIG_STRUCT conf;		            /* the configuration read from config file */
 
  cs_cgi_init();

  read_parameters (course, key, target_username,&is_admin);
  
  /** This program can be run by:
  ***   A teacher or student from their "My Manhattan Page":
  ***       - The 'course' string should be empty
  ***       - call validate_server_key()
  ***       - we know it's not a standalone course
  ***       - later, we'll check to make sure their username matches the target_username
  ***           since users should only be allowed to edit their own profile.
  ***
  ***   A teacher or student from within a particular Manhattan classroom's People module
  ***       - The course is contained within the 'course' string
  ***       - We read the course's config file to determine whether or not the course is a standalone course
  ***       - call validate_key()
  ***       - both this user and the target_username must be members of this course
  ***           (so people can't randomly view profiles of people in other courses)
  ***
  ***   An admin running the program from the administrative interface, or from within the Config menu of
  ***   a course:
  ***         - An admin=1 parameter is passed via the CGI, and sets is_admin to true
  ***         - there may or may not be a non-empty course string.  It can be empty if the admin is accessing
  ***             the program from View/Modify users.  It will be non-empty if the admin is accessing the
  ***             program from within a course's Config menu
  ***         - we validate_super_key()
  **/


  /* If a course was provided on the command line, read its configuration file.
  ** Set whether or not we're dealing with a standalone course
  **/

  if(*course)
     read_configuration_file(course, &conf);
     

 /* validate the appropriate key */
  if(is_admin)
   {
    if(!*course)
       validate_super_key(key, &user);     /* shared_super_util.c */
    else
       validate_key (key, &user, &conf);	/* shared_util.c */
   }     
  else if(!*course)
    validate_server_key(key, &user);       /* shared_util.c */
  else
    validate_key (key, &user, &conf);	/* shared_util.c */

  if(!bio_edit_allowed(&user,target_username, course, &conf))  /* shared_bio.c */
      cs_critical_error (ERR_REQUEST_DENIED, "");
  

  set_bio_form_data (course, key, &user, &conf,  target_username);
  cs_cgi_display("bio_edit_form", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

