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

#include "global.h"

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

#define NOTEAM '!'



static void
read_parameters (char *course, char *key, char *team, int *grp)
{
  char tmp[10];


  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/*shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("grp", tmp, 5);
  *grp = atoi (tmp);

  cs_get_optional_parameter ("team", tmp, 5);
  if(strlen(tmp))
     *team = *tmp;
  else
    *team = NOTEAM;

}




RECEIVER_NODE *
build_receiver_list (int grp, CONFIG_STRUCT *conf, SESSION *user)
{
    RECEIVER_NODE *to_head = 0;
    RECEIVER_NODE *ptr, *current_ptr = 0;
    P_NODE *pptr;
    P_NODE *p_head;
    p_head = build_option_person_list (conf);
    for(pptr = p_head; pptr; pptr = pptr->next)
    {
      if( pptr->data.team == user->team || ((grp == TEAM_TEACH) && pptr->data.group == FACULTY))
      {
          ptr = (RECEIVER_NODE *) malloc (sizeof(RECEIVER_NODE));
          strncpy (ptr->username, pptr->data.username, MAX_USERNAME + 1);
	  snprintf (ptr->realname, MAX_PATH + 1, "%s%s",
		    (pptr->data.group == FACULTY) ? "** " : "", pptr->data.realname);
	  ptr->next = 0;
         if(!to_head)
            to_head = ptr;
         else
            current_ptr->next = ptr;
         current_ptr = ptr;
      }
    }
   free_option_person_list (p_head);
   return to_head;
}

static void
free_receiver_list (RECEIVER_NODE *to_head)
{
  RECEIVER_NODE *ptr;

  while (to_head)
    {
      ptr = to_head;
      to_head = to_head->next;
      free (ptr);
    }

}



void
set_receiver_list (CONFIG_STRUCT *conf, RECEIVER_NODE *to_head,
                     SESSION *user, const char *key, const char *course, int grp)
{
  #define MAX_TMP_NAME 30

  RECEIVER_NODE *ptr;
  int i =0;
  char name[MAX_TMP_NAME];
  char temp[MAX_TMP_NAME];


  get_team_name(user->team, temp);    /* shared_teamstring.c */
  cs_set_value("team_name", temp);

  for (i = 0, ptr = to_head; ptr; ptr = ptr->next, i++)
    {
	 snprintf(name, MAX_TMP_NAME, "member.%d.realname", i);
	 cs_set_value(name, ptr->realname);
         cs_set_bio_link( ptr->username, course, conf, key, grp,i, "member", MAX_TMP_NAME);   /* shared_bio.c */
    } 

  #undef MAX_TMP_NAME 
}





void
set_member_list (int grp, CONFIG_STRUCT *conf, SESSION *user, const char *key, const char *course)
{
  RECEIVER_NODE * to_head = build_receiver_list (grp, conf, user);
  
  cs_set_course_info(conf);
  cs_set_current_time();

  set_receiver_list (conf, to_head, user, key, course, grp);
  free_receiver_list (to_head);
}




int
main (void)
{
  char course[MAX_FILENAME];
  char key[MAX_KEY + 1];
  char team;
  int grp;
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */

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

  if (team != NOTEAM)
    user.team = team;

  set_member_list (grp, &conf, &user, key, course);
  
  cs_cgi_display("news_members", 1);
  cs_cgi_destroy();
  return 0;
}




