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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_cs_course_search.h"

    
void
read_parameters (char *key, char *crs, char *group, int *oper,COURSE_SEARCH_PARAMS* params)
{
   char temp[MAX_COURSE_SUBDIR +1];
   cs_get_required_parameter ("id", key, MAX_KEY);
   cs_get_required_parameter ("crs", crs, MAX_PATH );
   cs_get_required_parameter ("group", group, MAX_ID );
   cs_get_optional_parameter ("mysearch", temp, MAX_COURSE_SUBDIR);
   if(strlen(temp)>0)
     *oper =1;
   else
     *oper =0;
   if(*oper)
   {
     cs_get_required_parameter ("type", temp, MAX_COURSE_SUBDIR );
     params->type = atoi(temp);
     cs_get_required_parameter ("where", temp, MAX_COURSE_SUBDIR);
     params->where = atoi(temp);
     cs_get_optional_parameter ("search", params->search, MAX_COURSE_SUBDIR );
     params->subdir_head = (SUBDIR_NODE *) malloc(sizeof(SUBDIR_NODE));
     if(!params->subdir_head)
      cs_critical_error(ERR_MALLOC_FAILED, "");
     cs_get_required_parameter ("subdir", params->subdir_head->subdir, MAX_COURSE_SUBDIR);
     params->subdir_head->next =0;
   } 
}






void
set_search_form( char *key,  char *crs,  char *group, SESSION *user)
{
   cs_set_value("id", key);
   cs_set_value("crs", crs);
   cs_set_value("group", group);
   cs_set_int_value("type", 0);
   cs_set_int_value("where", 0);
   
   cs_set_int_value("max_search_len", MAX_FILENAME);
   cs_set_course_group_option_list(COURSE_PATH, user->username);   /* shared_cs_course_search.c */

}









		
void
show_search_form( char *key,  char *crs,  char *group, int oper, 
                                       COURSE_SEARCH_PARAMS *params, CONFIG_STRUCT *conf, SESSION *user)
{
   COURSE_SEARCH_NODE *course_head;   /* shared_course_search.h */
   char url[MAX_PATH +1];
   char server_name[MAX_PATH +1];
   
   get_server_string(server_name, MAX_PATH);
   cs_set_value("sever_name", server_name);
   cs_set_value("bin_alias", ALIAS);
   
   cs_set_course_info(conf);
   cs_set_current_time();

   cs_set_value("add_type", !strcmp(group,"student")? "student":"faculty");

   snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s&amp;crs=%s&amp;group=%s", server_name, ALIAS, "admin_search_form", key, crs, group);
   cs_set_value("back_url", url);

   set_search_form(key, crs, group, user); 

   if(oper)
    {
      course_head = build_course_search_list(params); /* shared_course_search.c */
      cs_set_searched_courses (key, course_head, params, ""); /* shared_cs_course_search.h */
      cs_set_int_value("course_list", 1);
    }

}




int
main()
{
  char key[MAX_KEY +1];
  char crs[MAX_PATH + 1];   /* from command line - which program to pass form selections to? */
  SESSION user;
  char group[MAX_ID +1];
  CONFIG_STRUCT conf;
  COURSE_SEARCH_PARAMS params;
  int oper;

  cs_cgi_init();
  read_parameters (key, crs, group, &oper, &params);
  read_configuration_file (crs, &conf);
  validate_key(key, &user, &conf);              /* shared_util.c */

  if(!has_write_permission(&user, &conf))       /* shared_access.c */
         access_denied_error();                 /* shared_access.c */

  if ( (user.group == FACULTY) || (user.group == ADMIN))
		show_search_form(key, crs, group, oper, &params, &conf,&user);
		
  cs_cgi_display("admin_course_search_form", 1);
  cs_cgi_destroy();
  return 0;
}


