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

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_news_util.h"



static void
read_parameters (char *course,
		 char *key,
		 int *grp, long *offset, char *from, char *team,
		  int *inbox, char *student, int *noframe)
{

  char astring[25];


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

  cs_get_required_parameter ("from", from, MAX_FROM);		/* shared_cs_util.c */
  cs_get_required_parameter ("grp", astring, 5);		/* shared_cs_util.c */
  *grp = atoi (astring);

  if (*grp == MAIL)
    {
       cs_get_optional_parameter ("inbox", astring, 10);		/* shared_cs_util.c */
       if(strlen(astring)) 
         *inbox = 1;
       else
         *inbox = 0;
    }


  cs_get_required_parameter ("loc", astring, 15);		/* shared_cs_util.c */
  *offset = atol (astring);

  /* team parameter is optional */
  cs_get_optional_parameter ("team", astring, 5); 		/* shared_cs_util.c */
  if(strlen(astring))
     *team = *astring;
  else
     *team =0;
  cs_get_optional_parameter ("student", student, MAX_USERNAME);   /* shared_cs_util.c */
  cs_get_optional_parameter ("noframe", astring, 5); 		/* shared_cs_util.c */
  if(strlen(astring))
      *noframe = 1;
}




static void
set_return_button_params(char *name, char *value)
{
  #define MAX_TMP_NAME 30
  
  char lname[MAX_TMP_NAME];
  
  snprintf(lname, MAX_TMP_NAME, "return_button_param.%s", name);
  cs_set_value(lname, value);
  
  #undef MAX_TMP_NAME 
}



int
main (void)
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  int grp;			/* from grp=? command line */
  long offset;			/* from loc=? command line */
  char from[MAX_FROM + 1];	/* from from=? command line 
				** this time 'from' gives us info we need to put into the button
				 */


  char team;			/* from optional team=? parameter.. needed only for when the teacher
  			        ** is working with the team/teacher module
				*/

  char student[MAX_USERNAME + 1];   /* optional student parameter is used by ASSIGN_READ */		 


  int inbox = 0;
  
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
  char temp[20];
  char url[MAX_PATH +1];
  int noframe =0; 
  cs_cgi_init();                /* shared_cs_util.c */
  
  read_parameters (course, key, &grp, &offset, from, &team,  &inbox,student, &noframe);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);		/* shared_util.c */
  cs_set_course_info(&conf);     	/* shared_cs_util.c */
  cs_set_current_time();         	/* shared_cs_util.c */
  if(noframe)
       cs_set_int_value("close_button", 1);
 else
 {	 
    cs_set_value("from", from);			/* shared_cs_util.c */
    set_return_button_params("crs", course);              /* shared_cs_util.c */
    set_return_button_params("id", key);                  /* shared_cs_util.c */

    snprintf(temp, 20, "%ld", offset);            
    set_return_button_params("loc", temp);                /* shared_cs_util.c */

    if (grp == INTERNET_RESOURCES)
      {
       if(conf.access != ACCESS_CAPTURE)
        {
           snprintf(temp, 20, "%d", grp);
           set_return_button_params("grp", temp);
        }
       else /* capturing - use hyperlink instead of a form */
        {
          snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;loc=%ld&amp;grp=%d",
               "news_read", course, key, offset, grp);
	  cs_set_value("url", url);                      /* shared_cs_util.c */
        }
     }   /* end if(grp== INTERNET_RESOURCES) */
    
    
  else if (grp == MAIL)
    {
      if (!strcmp (from, "post_outbox_gather"))
	{
          if(conf.access != ACCESS_CAPTURE)   /* not capturing, use form */
           {
	     /* next used for POST_OUTBOX_GATHER */
             set_return_button_params("del_link", "1");
	   }
          else   /* capturing , use hyperlink */
           {
             snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;loc=%ld",
                               from,  course,key,offset);
	     cs_set_value("url", url);
           }                    
        }  /* end POST_OUTBOX_GATHER */  
      else if (!strcmp (from, "post_inbox_gather"))
	{

          if(conf.access != ACCESS_CAPTURE)   /* not capturing, use form */
           {

     	     /* next used for POST_INBOX_GATHER */
             set_return_button_params("del_link", "1");
           }
          else    /* capturing , use link */
           {
             snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;loc=%ld",
                               from,  course,key,offset);
	     cs_set_value("url", url);
           }
	}   /* end POST_INBOX_GATHER */
      else   /* other mail programs */
	{
          if(conf.access != ACCESS_CAPTURE)  /* not capturing, use form */
            {
             set_return_button_params("del_link", "1");
	    }
          else /* capturing, use hyperlink */
            {
              snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;loc=%ld",
                               inbox? "post_read_inbox": "post_read_outbox", 
                                 course,key,offset);
	      cs_set_value("url", url);    /* shared_cs_util.c */
	    }
        }  /* end 'other mail programs' */
    }  /* end if(grp == MAIL) */
        
  else    /* not INTERNET RESOURCES or MAIL */
    {
      if(conf.access != ACCESS_CAPTURE)    /* not capturing, use form */
      {
           snprintf(temp, 20, "%d", grp);
           set_return_button_params("grp", temp);
           set_return_button_params("del_link", "1");
           if (team)
	     {
              snprintf(temp, 20, "%c", team);
              set_return_button_params("team", temp);
	     }
           if(*student)
              set_return_button_params("student", student);
      }
     else   /* capturing, use link */
      {
	if(team && !strlen(student))
          snprintf(url, MAX_PATH+1, "<a href=%s?crs=%s&amp;id=%s&amp;loc=%ld&amp;grp=%d&amp;team=%c", 
			from, course, key, offset, grp, team);
	else 
	   if(team && strlen(student))
              snprintf(url, MAX_PATH+1, "<a href=%s?crs=%s&amp;id=%s&amp;loc=%ld&amp;grp=%d&amp;team=%c&amp;student=%s", 
			from, course, key, offset, grp, team, student);
	   else 
	      if(!team && strlen(student))
                   snprintf(url, MAX_PATH+1, "<a href=%s?crs=%s&amp;id=%s&amp;loc=%ld&amp;grp=%d&amp;student=%s", 
			       from, course, key, offset, grp,  student);
        cs_set_value("url", url);
      }
   }    /* end all other groups */

   cs_set_int_value("grp", grp);  	/* shared_cs_util.c */
 }
 cs_cgi_display("web_page_back", 1);  /* shared_cs_util.c */
 cs_cgi_destroy();			/* shared_cs_util.c */
     					
  return 0;
}

