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


#include "global.h"
#include "custom.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_central_user.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_bio.h"
#include "manhat-lib/shared_cs_util.h"

#include "manhat-lib/shared_teamstring.h"

static void
read_parameters (char *course, char *key, int *open, char *username)
{

  char temp[10];

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_optional_parameter ("open", temp, 9);
  cs_get_optional_parameter ("username", username, MAX_USERNAME);
  if(strlen(temp)>0)
     *open =1;
  else
     *open =0;

}




static void
list_passwd_file (const char *course, CONFIG_STRUCT *conf, int open, const char *username, const char *key)
{
  #define MAX_TMP_NAME 30
  P_NODE *person_ptr;
  P_NODE *head;
  CONFIG_STRUCT fake_conf;
  char name[MAX_TMP_NAME];
  int i;
  char temp[MAX_TMP_NAME];

  fake_conf = *conf;
  fake_conf.people = 1;   /* so print_bio_link() prints the bio link whether or not the people module is actually turned on */

  head = build_option_person_list (conf);    /* shared_person_list.c */

 
  /* if the open parameter was sent, it means this program was called so that it opens in a
  ** separate window.  If this is the case, we need to let the cs template know that it needs
  ** to include a "close window" button
  */
  
  if(open)
      cs_set_int_value("include_close_window_button", 1);
      
  cs_set_course_info(conf);
  cs_set_current_time();

  for (person_ptr = head, i = 0; person_ptr; person_ptr = person_ptr->next, i++)
  {
    snprintf(name, MAX_TMP_NAME, "user.%d.username", i);
    cs_set_value(name, person_ptr->data.username);
   
    snprintf(name, MAX_TMP_NAME, "user.%d.realname", i);
    cs_set_value(name, person_ptr->data.realname);


    snprintf(name, MAX_TMP_NAME, "user.%d.is_faculty", i);
    cs_set_int_value(name, (person_ptr->data.group == FACULTY)? 1 : 0);

   if(!strcmp(person_ptr->data.username, username))
   {
      snprintf(name, MAX_TMP_NAME, "user.%d.matches_search", i);
      cs_set_int_value(name, 1);
   }
   
   
   cs_set_bio_link(person_ptr->data.username, course, &fake_conf, key, 1, i, "user", MAX_TMP_NAME);  /* shared_bio.c */
    
    
    
    snprintf(name, MAX_TMP_NAME, "user.%d.group", i);
    cs_set_int_value(name, person_ptr->data.group);

    snprintf(name, MAX_TMP_NAME, "user.%d.alias", i);
    cs_set_value(name, person_ptr->data.alias); 

    snprintf(name, MAX_TMP_NAME, "user.%d.team", i);
    get_team_name(person_ptr->data.team, temp);   /* shared_teamstring.c */
    cs_set_value(name, temp); 

    snprintf(name, MAX_TMP_NAME, "user.%d.id", i);
    cs_set_value(name, person_ptr->data.id); 
  }

  free_option_person_list(head);     /* shared_person_list.c */

   cs_set_int_value("total_users", i); 

  #undef MAX_TMP_NAME 
}



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

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


  if ( (user.group == FACULTY) || (user.group == ADMIN))
    list_passwd_file (course, &conf, open, username, key);
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");
  #ifdef HIDE_MANHATTAN_USERNAMES
    cs_set_int_value("hide_manhattan_username", 1);

  #endif

  cs_cgi_display("admin_list_passwd", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */
}

