#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>		/* for tolower() */


#include "global.h"
#include "grade.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_msg_line.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_translate_url.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_bio.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_testpilot_util.h"
#include "manhat-lib/shared_system_data.h"
#include "manhat-lib/shared_server_string.h"



SESSION user;
CONFIG_STRUCT conf;




static void
read_parameters (char *course, char *key, long *offset, int *grp)
{

  char off_set[MAX_FILENAME + 1];	/* for holding the long value, length should
					   be long enough, don't you think? */

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);

  cs_get_required_parameter ("loc", off_set, MAX_FILENAME);
  *offset = atol (off_set);
  cs_get_required_parameter ("grp", off_set, MAX_FILENAME);
  *grp = atoi (off_set);

}



MSG_TREE_NODE *find_last_node(MSG_TREE_NODE *root)
{
 MSG_TREE_NODE *ptr;

 ptr = root;
 
 for(;;)
 {
  for(;ptr->next;ptr=ptr->next);
  if(!ptr->reply)
      return ptr;
  ptr=ptr->reply;
 }
}   



void get_tp4_name(char *msg_path, char *tp4_name)
{
    FILE *fp;

    fp = fopen(msg_path, "r");
    if(!fp)
	 cs_critical_error(ERR_FOPEN_READ_FAILED, msg_path);
    fgets(tp4_name, MAX_PATH, fp);
    strtrm(tp4_name);
    fclose(fp);
}

int get_grade_book_itemid(char *tp4_name, CONFIG_STRUCT *conf)
{
     char path[MAX_PATH +1];
     char line[MAX_PATH +1];
     char *ptr, *ptr2;

     FILE *fp;

     snprintf(path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, TESTPILOT_EXPORT_FNAME);
     if(!file_exists(path))
        return -1;
     fp = fopen(path, "r");
     if(!fp)
        cs_critical_error(ERR_FOPEN_READ_FAILED, path);
     while( fgets(line, MAX_PATH, fp))
     {
          ptr = strstr(line, tp4_name);
          if(ptr)
	  {
              ptr2 = strchr(line, ':');
	      if(ptr2)
	      {
		 *ptr2 = '\0';
		 ptr2++;
		 fclose(fp);
		 return atoi(ptr2);
	      }
	      else 
	      {
		 fclose(fp);
                return -1; 
	      }
	  }	  
     }
     fclose(fp);
     return -1; 
}



static void
set_news_msg_values (char *course, char *key,
		  long offset, CONFIG_STRUCT *conf, SESSION *user,  int grp)
{
 MSG_TREE_NODE *msg_ptr;
 MSG_TREE_NODE *topic_ptr;
 int msg_count;
 time_t current_time;
 int teacher_hidden;
 int read_only;
 char msg_path[MAX_PATH + 1];
 char tp4_name[MAX_PATH + 1]; 
 char testpilot_pw[MAX_PATH + 1]; 
 int gitemId = -1; 
 char server[MAX_PATH + 1]; 
 char server_string[MAX_PATH + 1]; 
 char tpass[MAX_PATH +1];

 build_fac_assign_read_tree(user,  offset, "", &msg_ptr, &topic_ptr, &msg_count, &current_time); /* shared_news_util.c */

 if(!msg_ptr || !tree_head)
   cs_critical_error(ERR_CANT_FIND_THIS_OFFSET, "set_news_msg_values()");
 
 teacher_hidden = is_teacher_hidden (topic_ptr->data.status, topic_ptr->data.release_time,   /* shared_news_util.c */
                                        topic_ptr->data.unrelease_time, current_time);
                                      
 read_only = BASIC_STATUS(topic_ptr->data.status) == READ_ONLY ? 1 : 0;  /* see global.h */                                       

 snprintf (msg_path, MAX_PATH + 1, "%s%s/%s/%s/%s",
	    conf->course_path, PEOPLE_DIR, msg_ptr->data.info.sender, discussion_fname, msg_ptr->data.info.msg_fname);

 get_tp4_name(msg_path, tp4_name);

 get_test_pilot_password(user,testpilot_pw);   /* shared_testpilit_util.c */
 free_msg_tree(tree_head); /*shared_news_util.c */

 gitemId = get_grade_book_itemid(tp4_name, conf);

 get_server_string(server_string, MAX_PATH +1);   /* shared_server_string.h */
 snprintf(server, MAX_PATH +1, "%s/%s", server_string, system_data_str("ALIAS"));

 get_test_pilot_password(user, tpass);

 printf("Location: %s?action=viewScore&id=%s&crs=%s&uid=%s&upass=%s&tp4=%s&gitemId=%d&manhat_server=%s&tid=%s&tpass=%s\n\n", 
		 system_data_str("TESTPILOT_SCRIPT_PATH"), key, course, user->id, 
		 testpilot_pw, tp4_name, gitemId,  server, user->id, tpass);

}



int
main (void)
{
  char course[MAX_PATH + 1];			/* from crs=? command line */
  char key[MAX_KEY + 1];			/* from id=? command line */
  long offset;					/* from loc=? command line */
  int grp;
  int can_write = 0;

  cs_cgi_init();                 		/* shared_cs_util.c */
  read_parameters (course, key, &offset, &grp);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);		/* shared_util.c */

  
  
  if (user.group != FACULTY)
       cs_critical_error (ERR_REQUEST_DENIED, "");    /* shared_cs_util.c */


  can_write = has_write_permission(&user, &conf);	/* shared_access.c */
  if(!can_write)
      cs_critical_error(ERR_COURSE_IS_READ_ONLY, "testpilot_view_score");
  
  cs_set_course_info(&conf);     /* shared_cs_util.c */
  cs_set_value("crs", course);    		/* shared_cs_util.h */
  cs_set_int_value("grp", grp);         /* shared_cs_util.c */
  cs_set_value("key", key);	  		/* shared_cs_util.h */
  cs_set_value("username",user.username);  	/* shared_cs_util.h */
     
  set_discussion_names (conf.course_path, user.username, grp);	/* shared_news_util.c */

  set_news_msg_values (course, key, offset,  &conf, &user,  grp);



  cs_cgi_destroy();                    /* shared_cs_util.c */
  return 0;
}




