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

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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_login.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_cs_util.h"


void
read_parameters(char *key, char *crs)
{

   cs_get_required_parameter ("id", key, MAX_KEY);
   cs_get_required_parameter ("crs", crs, MAX_COURSE_NO);
}




void  
build_user_session(const char *username, const char *admin_key, const char *crs)
{
 SESSION user;
 CONFIG_STRUCT conf;
 char base_dir[MAX_PATH + 1];
 char course_key[MAX_KEY + 1];
 char server_string[MAX_PATH + 1]; 
 
 strncpy(user.username, username, MAX_USERNAME +1);
 strncpy(user.realname, admin_key, MAX_NAME +1);   /* the realname field holds the server key */
 user.group = ADMIN;
 *user.alias = '\0';
 *user.id = '\0';
 user.team='Z';

 read_configuration_file(crs,&conf);
 if(conf.standalone)
     create_new_key(&user,conf.course_path, course_key, MAX_KEY_EXPIRE);  /* shared_login.c */
 else
  {
   snprintf(base_dir, MAX_PATH + 1, "../%s", USERS_DIR);
   create_new_key(&user,base_dir, course_key, MAX_KEY_EXPIRE);  /* shared_login.c */
  } 
  
 get_server_string(server_string, MAX_PATH + 1);    /* shared_server_string.h */
 printf("Location: %s/%s/%s?crs=%s&id=%s\n\n", 
			server_string, ALIAS, "admin_main_menu", crs, course_key);
}
	

	   
int
main()
{
   char key[MAX_KEY + 1];
   char crs[MAX_COURSE_NO +1];
   SESSION user;

   cs_cgi_init();
   read_parameters( key , crs);
   validate_super_key(key, &user);


   build_user_session(user.username, key, crs);
   cs_cgi_destroy(); 
   return 0;

}

