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

#include <sys/types.h>  /* for opendir(), etc */
#include <dirent.h>

#include <ctype.h>
       
#include "../global.h"
#include "../custom.h"

#include "shared_util.h"
#include "shared_add_student.h"
#include "shared_authenticate.h"  
#include "shared_cs_util.h"  

int
realname_exists (const char *realname,  const char *passwd_fname)
{
  FILE *fp;
  char line[200];
  char *ptr, *ptr2;
  int i;


    fp = fopen (passwd_fname, "r");
    if (!fp)
       cs_critical_error (ERR_PASSWD_OPEN_FAILED, "");

    while (fgets (line, 199, fp))
    {
       if (*line == '?')
        continue;
       for (i = 1, ptr = line; i <= 2; i++)
        {
             ptr = strchr (ptr, ':');
             if (!ptr)
              cs_critical_error (ERR_PASSWD_PARSE_ERROR, "");
             ptr++;
        }

        ptr2 = strchr (ptr, ':');
       if (!ptr2)
          cs_critical_error (ERR_PASSWD_PARSE_ERROR, "");
       *ptr2 = '\0';
      if (!strcmp (ptr, realname))
      {
           fclose (fp);
           return 1;
      }
    }
    fclose (fp);
     return 0;
}


int
get_ctr_realname( char *first,  char *last, char *realname)
{
    *first = toupper(*first);
    *last = toupper(*last);
    if((strlen (first) + strlen(last) +1 ) > MAX_NAME)
       return 0;
     snprintf(realname, MAX_NAME + 1, "%s %s", first, last);
       return 1;
}




void
check_realname (char *first, char *last, char *realname, const char *passwd_fname)
{
#define MAX_MSG 120

  char *ptr;
  char msg[MAX_MSG];

  *first = toupper (*first);
  *last = toupper (*last);


  if ((strlen (first) + strlen (last) + 1) > MAX_NAME)
  {
       snprintf (msg, MAX_MSG, "%d", MAX_NAME);
       cs_critical_error (ERR_REALNAME_TOO_LONG, msg);
   }

    snprintf (realname, MAX_NAME + 1, "%s %s", first, last);

    for (ptr = realname; *ptr; ptr++)
       if (!isalpha (*ptr) && !isspace (*ptr) && !isdigit (*ptr) &&
	          *ptr != '-' && *ptr != '\'' && *ptr != '.')
       {
        snprintf (msg, MAX_MSG, "%c %s",  *ptr, realname);
        cs_critical_error (ERR_INVALID_CHARACTER_IN_NAME,""); 
      }


      if (realname_exists (realname, passwd_fname))
      {
          snprintf (msg, MAX_MSG, "%s",realname);
        cs_critical_error (ERR_ALREADY_ENROLLED,msg); 
      }
#undef MAX_MSG
}



int
username_exists (const char *username, const CONFIG_STRUCT *conf)
{
  FILE *fp;
  char line[200];
  char *ptr;
  char dirname[MAX_PATH + 1];
  DIR *dirptr;
  char passwd_fname[MAX_PATH +1];
  
  snprintf (passwd_fname, MAX_PATH + 1, "%s%s", conf->course_path,PASSWD_FNAME);
  fp = fopen (passwd_fname, "r");
  if (!fp)
     cs_critical_error (ERR_PASSWD_OPEN_FAILED, "");

  while (fgets (line, 199, fp))
  {
       if (*line == '?')
          continue;
       ptr = strchr (line, ':');
      if (!ptr)
       cs_critical_error (ERR_PASSWD_PARSE_ERROR, "");
       *ptr = '\0';
      if (!strcmp (line, username))
      {
         fclose (fp);
         return 1;
      }
 }
  fclose (fp);
  snprintf (dirname, MAX_PATH + 1, "%s%s/%s", conf->course_path, PEOPLE_DIR, username);
  dirptr = opendir (dirname);
  if (dirptr)
  {
       closedir (dirptr);
       return 1;
   }

  return 0;
}






void
get_standalone_username (char *first, char *last, char *username, char *id, CONFIG_STRUCT *conf)
{
 int try;
 char passwd_fname[MAX_PATH +1];

 snprintf(passwd_fname, MAX_PATH +1, "%s%s", conf->course_path,PASSWD_FNAME);
 derive_username (first, last, id, username, MAX_USERNAME + 1);   /* shared_authenticate.c */

  for(try = 1;
    (try <= MAX_USERNAME_TRIES) && (username_exists (username, conf ));
              try++)
  {
       derive_another_username(first, last, id, username, MAX_USERNAME + 1, try);
       if(!strlen(username))
             cs_critical_error (ERR_NO_UNUSED_USERNAMES_FOUND,
                              "");

   }

     if(try > MAX_USERNAME_TRIES)
             cs_critical_error (ERR_NO_UNUSED_USERNAMES_FOUND,
                              "");

}


