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


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

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


static void
read_parameters (char *course, char *key)
{

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_set_value("crs", course);
  cs_set_value("id", key);
}




int user_cmp(P_NODE *a, P_NODE *b)
{
  if(a->data.team != b->data.team)
  {
     if(a->data.team > b->data.team)
	    return 1;
     else
	    return -1;
  } 
  return username_cmp(&(a->data), &(b->data));   /* shared_authenticate.c */
}


P_NODE *
sort_team_list (P_NODE *head)
{
  P_NODE *q, *tail, *p, *r;
  if (head != NULL)
    {
      tail = head;
      while (tail->next)
	{
	  q = tail->next;
	  if (user_cmp(q,head) < 0 )
	    {
	      tail->next = q->next;
	      q->next = head;
	      head = q;
	    }
	  else
	    {
	      r = head;
	      p = r->next;
	      while (user_cmp(q,p) > 0) 
		{
		  r = p;
		  p = r->next;
		}
	      if (q == p)
		tail = q;
	      else
		{
		  tail->next = q->next;
		  q->next = p;
		  r->next = q;
		}
	    }
	}
    }
    return head;
}


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

  fake_conf = *conf;
  fake_conf.people = 1;   /* so we can fool print_bio_link() */
  
  head = build_option_person_list (conf);   /* shared_person_list.c */
  head = sort_team_list (head);

  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, "users.%d.username", i);
     cs_set_value(name, person_ptr->data.username);

    /* print realname as link to their 'bio' */  
    cs_set_bio_link( person_ptr->data.username, course, &fake_conf, key, 1, i, "users", MAX_TMP_NAME);  /* shared_bio.c */
      
     snprintf(name, MAX_TMP_NAME, "users.%d.realname", i);
     cs_set_value(name, person_ptr->data.realname);
       
     snprintf(name, MAX_TMP_NAME, "users.%d.id", i);
     cs_set_value(name, person_ptr->data.id);

     snprintf(name, MAX_TMP_NAME, "users.%d.group", i);
     cs_set_value(name, person_ptr->data.group == FACULTY? "faculty":"student");

     snprintf(name, MAX_TMP_NAME, "users.%d.alias", i);
     cs_set_value(name, person_ptr->data.alias);
     
     snprintf(name, MAX_TMP_NAME, "users.%d.team_internal", i);
     snprintf(temp, MAX_TMP_NAME, "%c", person_ptr->data.team);
     cs_set_value(name, temp);
  }

  free_option_person_list(head); 
  cs_set_int_value("num_people", i);
  cs_set_team();
  #undef MAX_TMP_NAME 
}



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

  cs_cgi_init();

  read_parameters (course, key);
  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 */


  if ((user.group == FACULTY) || (user.group == ADMIN))
    list_passwd_file (course, &conf, key, course);
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");

 #ifdef HIDE_MANHATTAN_USERNAMES
   cs_set_int_value("hide_manhattan_username", 1);
  #endif

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

