#include <stdio.h>
#include <stdlib.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_cookie.h"
#include "manhat-lib/shared_login.h"

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

  cs_get_required_parameter ("id", key, MAX_KEY);    /* shared_cs_util.c */
  cs_get_optional_parameter("crs",course,MAX_PATH);  /* shared_cs_util.c */
  cs_get_optional_parameter("host",host,MAX_PATH);  /* shared_cs_util.c */  

}



static void
redirect_browser(const char *url)
{
 printf("<html>\n"
        "<head><meta http-equiv=\"REFRESH\" content=\"0; URL=%s\" ></head>"
        "</html>",
               url);
}               



int main()
{

 char key[MAX_KEY + 1];
 SESSION user;
 char base_dir[MAX_PATH + 1];
 char course[MAX_PATH + 1];
 char host[MAX_PATH + 1];
 char url[MAX_PATH + 1];
 CONFIG_STRUCT conf;
 int cookies_work;
 
  cs_cgi_init();                          /* shared_cs_util.c */
  read_parameters (key, course, host);

  if(!COOKIES_ENABLED)
   {
      if(!strlen(course))   /* central login - run MYMANHATTAN */
              printf("Location: %s?id=%s&host=%s\n\n",  "mymanhattan", key, host);
      else   /* standalone login -run MAIN_MENU */
              printf("Location: %s?crs=%s&id=%s\n\n",  "main_menu", course, key);
   }
  else  /* cookies are enabled */
   {
    cookies_work = parse_cookie(key);   /* shared_cookie.c */
    if(!cookies_work)    
     {
      if(REQUIRE_COOKIES)           /* custom.h - cookies don't work -stop!*/
         cs_critical_error(ERR_COOKIES_MUST_BE_ENABLED, "");
       else                         /* allow user to continue, even though he's disabled cookies on his browser */
       {
         if(!strlen(course))   /* central login - run MYMANHATTAN */
              printf("Location: %s?id=%s&host=%s\n\n",  "mymanhattan", key, host);
         else   /* standalone login -run MAIN_MENU */
              printf("Location: %s?crs=%s&id=%s\n\n",  "main_menu", course, key);
       }        
                    
     }
    else    /* if cookies are enabled and they work */
      {
         if(!strlen(course))    /* then this is a central login */
          {
            snprintf(base_dir,MAX_PATH + 1, "../%s", USERS_DIR);
            validate_server_key(key, &user); 			/* shared_util.c */
            create_new_key(&user,base_dir,key,MAX_KEY_EXPIRE);   /* shared_login.c */
            set_cookie(key, ALIAS,"");                           /* shared_cookie.c */
            snprintf(url, MAX_PATH +1, "%s?id=%s&host=%s","mymanhattan",USE_COOKIE,host);
          }
         else  		/* this is a standalone course */ 
           {
  	    snprintf(base_dir,MAX_PATH + 1, "%s%s", COURSE_PATH, course);
            read_configuration_file(course, &conf);      		/* shared_util.c */
            validate_key(key, &user, &conf);             		/* shared_util.c */
            create_new_key(&user,base_dir, key, conf.key_expire);  	/* shared_login.c */
            set_cookie(key, ALIAS,"");   				/* shared_cookie.c */
            snprintf(url, MAX_PATH +1, "%s?crs=%s&id=%s",
                        "main_menu",course,USE_COOKIE);

           } 
           
         redirect_browser(url);          
           
      }  /* if cookies are enabled and they work */
    }   /* cookies are enabled */
    cs_cgi_destroy();							/* shared_cs_util.c */

  return 0;
  
}


