#include <stdio.h>
#include <string.h>
#include <dirent.h>		/* Directory information. */

#include <sys/stat.h>		/* for stat()            */
#include <sys/file.h>		/* for flock()            */
#include <unistd.h>		/* for unlink, mktmp ... */


#include "global.h"
#include "custom.h"		/* for date FORMAT strings */

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_login.h"
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_potato_util.h"
#include "manhat-lib/shared_central_user.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_cs_util.h"	/* for get_string_value() */



static void
read_parameters (char *username, char *password, char *capture_username, char *course)
{


  cs_get_required_parameter ("username", username, MAX_USERNAME);
  cs_get_required_parameter ("password", password, MAX_PASSWORD);
  cs_get_required_parameter ("capture", capture_username, MAX_USERNAME);
  cs_get_required_parameter ("crs", course, MAX_FILENAME);
  
}





void
check_super_password (const char *username, const char *passwd)
{

   if(!confirm_super_password(passwd, username))   /* shared_encrypt.c */
      cs_critical_error (ERR_USERNAME_PASSWD_INCORRECT, "");
}






     
static void
standalone_login(const char *course, CONFIG_STRUCT *conf, const char *username, char *key)
{

SESSION user;

 if(!get_user_info ( username, &user, conf))    /* shared_person_list.c */
     cs_critical_error(ERR_NO_COURSE_MSG, "");

 strcpy(user.alias, ALIAS_CAPTURE_FLAG);   /* to mark this person as a capture user */
 
 create_new_key (&user, conf->course_path, key, conf->key_expire);  /* shared_login.c */

 remove_stale_symlinks (username, course, conf->access);     /* shared_news_util.c */
 kill_hotpotato_directory(username, conf);   /* shared_potato_util.c */
     
}


static void
central_login(const char *course, CONFIG_STRUCT *conf, const char *capture_username,  char *key)
{
 SESSION user;
 char base_dir[MAX_PATH + 1];
  
 get_central_user_info ( capture_username, &user, 1);       /*shared_central_user.c , set capture flag*/
 snprintf(base_dir, MAX_PATH + 1, "../%s", USERS_DIR);

 /* this creates a central user key */
 create_new_key (&user, base_dir, key, MAX_KEY_EXPIRE);      /*shared_login.c 
                                                             ** since capture flag is set for this user,
                                                             ** stale keys won't be deleted 
                                                             */


 delete_course_central_user_key(user.username,course);   /* shared_authenticate.c - delete the key for this course, if it exists */

}






    
static void
capture_login(const char *username, const char *password, const char *capture_username, const char *course)
{
 char key[MAX_KEY + 1];
 CONFIG_STRUCT conf;
 char server_string[MAX_PATH + 1];
 
 check_super_password (username, password);

 read_configuration_file(course, &conf);
 if(conf.access != ACCESS_CAPTURE)
    cs_critical_error(ERR_NOT_CAPTURE_MODE, "");
 
 if(conf.standalone)
   standalone_login(course, &conf, capture_username, key);
 else
    central_login(course, &conf, capture_username, key);

 
 get_server_string(server_string, MAX_PATH);    /* shared_server_string.c */
 printf ("Location: %s/%s/%s?crs=%s&id=%s\n\n", server_string, ALIAS, "main_menu", course, key);   /* don't bother with cookies ? */
}


int
main ()
{

  char username[MAX_USERNAME + 1]; 
  char password[MAX_PASSWORD + 1];
  char capture_username[MAX_USERNAME + 1];
  char course[MAX_FILENAME + 1];
  
  cs_cgi_init(); 
  read_parameters (username, password, capture_username, course);
  capture_login(username, password, capture_username, course);
  cs_cgi_destroy();
  return 0;
}

