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

#include <dirent.h>		/* Directory information  */
#include <sys/stat.h>		/*for mkdir(), stat()     */
#include <unistd.h>             /* for stat()             */


#include "../global.h"
#include "shared_add_util.h"
#include "shared_util.h"

#include "shared_authenticate.h"

#include "shared_lock.h"
#include "shared_strtrm.h"
#include "shared_cs_util.h"


static int
has_digits (const char *string)
{
  const char *ptr;

  for (ptr =  string; *ptr; ptr++)
    if (isdigit (*ptr))
      return 1;
  return 0;
}

static int
has_banned_chars(const char *string)
{
  const char *ptr;
  
  for (ptr =string; *ptr; ptr++)
    if (strchr(ILLEGAL_ID_CHARS,*ptr))  /* global.h */
      return 1;
  return 0;
}

  

void
check_id (const char *id, const char *passwd_fname)
{
#define MAX_MSG 100
  FILE *fp;
  char line[200];
  char *ptr;
  char msg[MAX_MSG];

  if (!has_digits (id))
    cs_critical_error (ERR_ID_MUST_HAVE_DIGITS, "");

  if(has_banned_chars(id))
   {
    snprintf(msg,MAX_MSG, "%s", ILLEGAL_ID_CHARS);
    cs_critical_error(ERR_ID_HAS_BANNED_CHARS_X,msg); 
   } 
       

  fp = fopen (passwd_fname, "r");
  if (!fp)
    cs_critical_error (ERR_PASSWD_OPEN_FAILED, "");
  get_shared_lock(fileno(fp), 1);  /* shared_lock.c */
  while (fgets (line, 199, fp))
    {
      if (*line == '?')
	continue;
      ptr = strrchr (line, ':');
      if (!ptr)
	cs_critical_error (ERR_PASSWD_PARSE_ERROR, "");
      ptr++;
      *(line + strlen (line) - 1) = '\0';	/* remove trailing newline */
      if (!strcmp (ptr, id))
	{
	  fclose (fp);
	  cs_critical_error (ERR_ID_ALREADY_USED, id);
	}
    }
  release_lock(fileno(fp));  /* shared_lock.c */
  fclose (fp);
#undef MAX_MSG
}





int
add_to_passwd (const char *username, const char *realname,
					char team, const char *id,
					const char *passwd_fname, const char *faculty)
{
  FILE *fp;

  fp = fopen (passwd_fname, "a");
  if (!fp)
     return 0;

  if(get_exclusive_lock(fileno(fp), 0) )
   {
	   fclose(fp);
	   return 0;
   }
   fprintf (fp, "%s:x:%s:%s:anonymous:%c:%s\n",
    	                    username, realname,
              (strcmp(faculty, "student") ==0) ? "student":"faculty",  team, id);
  release_lock(fileno(fp));
  fclose (fp);
  return 1;
}



int
create_module_directory(const char *course_path, const char *username, const char *module_dir)
{

 char dest_dir[MAX_PATH + 1];
 
 snprintf(dest_dir, MAX_PATH + 1, "%s%s/%s/%s",
      course_path, PEOPLE_DIR, username, module_dir);
 if(mkdir(dest_dir,0770) == -1)
     return 0;
 return 1;    
}      




int
create_directories (const char *course_path, const char *username)
{
  char dest_dir[MAX_PATH + 1];
  char basedir[MAX_PATH + 1];

  snprintf (basedir, MAX_PATH + 1, "%s%s", course_path, PEOPLE_DIR);

  snprintf (dest_dir, MAX_PATH + 1, "%s/%s", basedir, username);
 if( mkdir (dest_dir, 0770) == -1)
	 return 0;

  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, ANONYMOUS_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, ASSIGNMENTS_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username,DISCUSSION_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;

  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, TEAM_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, INET_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, LOUNGE_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, MAIL_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;

  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, HANDOUTS_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, TEAM_TEACH_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;
  
  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, GRADES_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;


  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, SELFTEST_DIR);
  if(mkdir (dest_dir, 0770) == -1)
     return 0; 

  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, LECTURES_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;

  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, CHAT_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;


  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, SURVEYS_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;


  snprintf (dest_dir, MAX_PATH + 1, "%s/%s/%s", basedir, username, PODCASTS_DIR);
  if(mkdir (dest_dir, 0770) == -1)
	  return 0;


  return 1;
}







