#include "shared_super_menu.h"

static void
set_header_part ( int first, const char *username)
{
   char fname[MAX_FILENAME + 1];
   char last_time[MAX_TIMESTRING + 1];
   char last_host[MAX_PATH + 1];


    cs_set_current_time();

     if(first)  /* show last time user logged in */
     {
        /* sub_dir_name() is in shared_authenticate.c */
          snprintf(fname, MAX_PATH +1, "../%s/%s/%c/%s/%s",
                        USERS_DIR, ADMIN_DIR,  sub_dir_name(username), username, LAST_LOGIN_FNAME);

      
          get_last_time_login(fname,last_time, last_host);    /* shared_login.c */

          cs_set_value("cur_remote_host", REMOTE_HOST);

          if(!*last_time)
              cs_set_int_value("welcome_msg", 1);
          else
	  {
              cs_set_value("last_remote_host", last_host);
              cs_set_value("last_time", last_time);
          }
         
      }
         
}





static void 
set_one_url(const char *url, const char *key, const char *opername, const char *prefix, const char *server_name)
{
 #define MAX_TMP_NAME 50
  char mk_url[MAX_PATH +1];
  char name[MAX_TMP_NAME];
  if(strcmp(opername, "surveys") )
     snprintf(mk_url, MAX_PATH +1, "%s/%s/%s%sid=%s", server_name, SBIN_ALIAS, url, strchr(url,'?')? "&amp;":"?",key);
  else
     snprintf(mk_url, MAX_PATH +1, "%s%sid=%s", url, strchr(url,'?')? "&amp;":"?",key);
  snprintf(name, MAX_TMP_NAME, "%s.%s", prefix, opername);
  cs_set_value(name, mk_url);
#undef MAX_TMP_NAME  
}

 
 
 



static void
do_course_operations(ADMIN_CONFIG_STRUCT *conf, const char *key, const char *server_name)
{
  char url[MAX_PATH +1];
  
  
  if(has_course_progs(conf))   /* shared_super_util.c */
  {
    if(conf->create_new_course)
    {
      #ifdef WNEC_CREATE_COURSE_LINK
      set_one_url( "super_wnec_sis_form", key, "wnec_sis", "create", server_name);
      cs_set_int_value("has_create", 1);
      #endif
     
      set_one_url( "super_new_course_form", key, "roster_file", "create", server_name);
      set_one_url( "super_upload_xml_roster_form", key, "xml_file", "create", server_name);
      set_one_url( "super_import_course_form", key, "import_course", "create", server_name);
      cs_set_int_value("has_create", 1);

     if(ENABLE_FACULTY_COURSE_REQUEST)   /* custom.h */
     {
               set_one_url( "super_auto_update_form", key, "auto_update", "create", server_name);
               cs_set_int_value("has_create", 1);
     } 


    }

    if(conf->delete_courses)
      set_one_url("super_course_search_form?prog=super_delete_course_form", key, "delete_course", "course", server_name);

    if(conf->config_courses)
      set_one_url( "super_course_search_form?prog=super_config_course_list", key, "configure_course", "course", server_name);

    if(conf->change_course_access)
      set_one_url( "super_course_search_form?prog=super_change_course_access_form", key, "change_course_access", "course", server_name);
    if(conf->archive_courses)
      set_one_url( "super_course_search_form?prog=super_archive_course_form", key, "archive_courses", "course", server_name);



    if(conf->admin_surveys)
    {
      snprintf(url, MAX_PATH +1, "%s/%s/%s", server_name, ALIAS, "survey_read_info?admin=1"); 
      set_one_url( url, key, "surveys", "survey", server_name);
    }
      
  }

}



static void
do_administrator_operations(ADMIN_CONFIG_STRUCT *conf, const char *key, const char *server_name)
{

 if(conf->is_super_user)  /* only the super-user should get access to these */
  {

    if(conf->add_admin)
      set_one_url( "super_add_admin_form", key, "add_admin", "super", server_name);

    if(conf->del_admin)
      set_one_url("super_del_admin_form", key, "delete_admin", "super", server_name);

    if(conf->grant_admin)
      set_one_url( "super_grant_admin_form", key, "modify_admin", "super", server_name);
  }

}



static void
do_people_operations(ADMIN_CONFIG_STRUCT *conf, const char *key, const char *server_name)
{

 if(has_people_progs(conf))    /* shared_server_util.c */
  {
  
    if(conf->search_profiles)
      set_one_url( "super_info_form", key, "profile", "people", server_name);
    if(conf->whos_on)
      set_one_url("super_manwho_cgi", key, "who_is_on_manhattan", "people", server_name);

   if(conf->statistics)
      set_one_url( "super_stat_list", key, "login_logs", "people", server_name);

  }

}







static 
void do_server_operations(ADMIN_CONFIG_STRUCT *conf, const char *key, 
                                                  const char *server_name, const char *prog)
{
 int is_lock;

 is_lock = server_locked();    /* shared_server_locked.c */
 if(!is_lock)
      cs_set_int_value("server_open_for_logins", 1);
 
  if(has_server_progs(conf))   /* shared_super_util.c */
  {

    if(conf->disk_space)
      set_one_url("super_server_disk", key, "disk_space", "usageReport", server_name);
    if(conf->course_disk_usage)
      set_one_url( "super_list_usage_reports", key, "course_disk_usage", "usageReport", server_name);

    if(conf->disk_space || conf->course_disk_usage)
         cs_set_int_value("has_usageReport", 1);

    if(conf->chat_server)
      set_one_url("super_chat", key, "manage_chat", "server", server_name);

    if(conf->lock_server)
     {
       if(!strcmp(prog, "super_lock"))
          set_one_url("super_unlock", key, "unlock_server", "server", server_name);

       else if(!strcmp(prog, "super_unlock" ))
       {
          set_one_url("super_lock", key, "lock_server", "server", server_name);
	  cs_set_int_value("server_open_for_logins", 1);
       }

       else if(!is_lock)
	{
	  
	  set_one_url("super_lock", key, "lock_server", "server", server_name);
	}
	else
          set_one_url("super_unlock", key, "unlock_server", "server", server_name);
     }
     else
     {
	 if(!is_lock)
	  cs_set_int_value("server_open_for_logins", 1);
     }
     

    if(conf->clean_up)
      set_one_url("super_clean", key, "clean_up", "cleanup", server_name);
    if(conf->purge_postoffice_message)
      set_one_url( "super_course_search_form?prog=super_purge_postoffice_message_form", key, "purge_postoffice_message", "cleanup", server_name);
    if(conf->clean_up || conf->purge_postoffice_message)
	    cs_set_int_value("has_cleanup", 1);

  }
}  
  



static void
set_passwd_exit_buttons(const char *key, const char *server_name)
{
  char url[MAX_PATH +1];

   snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s",server_name, SBIN_ALIAS, "super_chg_passwd_form", key);
   cs_set_value("chg_passwd_url", url);
   snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s",server_name, SBIN_ALIAS, "super_exit", key);
   cs_set_value("exit_url", url);
   snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s", server_name, SBIN_ALIAS, "super_admin_page", key);
   cs_set_value("home_url", url);
   snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s", server_name, ALIAS, "calendar", key);
   cs_set_value("calendar_url", url);
}                               
 





static int
check_permission(ADMIN_CONFIG_STRUCT *conf, const char *prog)
{
    if(!strcmp(prog, "super_new_course_form") && conf->create_new_course)
	    return 1;
    else if(!strcmp(prog,  "super_delete_course_form") && conf->delete_courses)
	    return 1;
    else if(!strcmp(prog, "super_config_course_list") && conf->config_courses)
	    return 1;
    else if(!strcmp(prog, "super_change_course_access_form") && conf->change_course_access)
	    return 1;
    else if(!strcmp(prog, "super_purge_postoffice_message_form") && conf->purge_postoffice_message)
	    return 1;
    else if(!strcmp(prog, "super_list_usage_reports") && conf->course_disk_usage)
	    return 1;
    else if(!strcmp(prog, "super_archive_course_form") && conf->archive_courses)
	    return 1;
    else if(!strcmp(prog, "super_add_admin_form") && conf->add_admin)
	    return 1;
    else if(!strcmp(prog, "super_del_admin_form") && conf->del_admin)
            return 1;
    else if(!strcmp(prog, "super_grant_admin_form") && conf->grant_admin)
            return 1;
    else if(!strcmp(prog, "super_info_form") && conf->search_profiles)
            return 1;
    else if(!strcmp(prog, "super_server_disk") && conf->disk_space)
            return 1;
    else if(!strcmp(prog, "super_manwho_cgi") && conf->whos_on)
            return 1;
    else if(!strcmp(prog, "super_stat_list") && conf->statistics)
            return 1;
    else if(!strcmp(prog, "super_chat") && conf->chat_server)
            return 1;
    else if((!strcmp(prog, "super_unlock") || !strcmp(prog, "super_lock")) && conf->lock_server)
            return 1;
    else if(!strcmp(prog, "super_clean") && conf->clean_up)
            return 1;
    else if(!strcmp(prog, "super_admin_page") )
            return 1;
    else if(!strcmp(prog, "super_wnec_sis_form") )
            return 1;

    return 0;
    
}





void
set_admin_page(SESSION *user, char *key, int first, const char *prog)
{
  ADMIN_CONFIG_STRUCT conf;
  char server_name[MAX_PATH +1];

  if(!read_super_configuration_file(user->username, &conf))
      cs_critical_error(ERR_ERROR_READING_CONFIG, user->username);


  if(!check_permission(&conf, prog))
      cs_critical_error(ERR_REQUEST_DENIED, prog);
      
  get_server_string(server_name, MAX_PATH + 1);
  cs_set_value("super_realname", user->realname);
  
  set_header_part (first, user->username);
  
  cs_set_value("page_title", conf.page_title);
  cs_set_value("installation_title", INSTALLATION_TITLE);   /* custom.h */
  cs_set_value("installation_version", VERSION);            /* global.h */

  do_course_operations(&conf, key, server_name);
  do_people_operations(&conf, key, server_name);
  do_server_operations(&conf,key, server_name, prog);
  do_administrator_operations(&conf,key, server_name);
  
  set_passwd_exit_buttons(key, server_name);
  cs_load_lang("super_menu");
}






