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

#include "global.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_translate_url.h"

char inbox_path[MAX_PATH + 1];	/* full path to inbox.dat */




static void
read_parameters (char *course, char *key, long *offset, char *url, int *from_gather)
{
  char off_set[MAX_FILENAME + 1];	/* for holding the long value */
  char tmp_url[MAX_URL*3 + 1];


  cs_get_required_parameter ("crs", course, MAX_FILENAME);
  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 ("url", tmp_url, MAX_URL *3);

  translate_url_substitutes (url, tmp_url, MAX_URL*3,1);	/* shared_translate_url.c */
  
  /* If this program is called from news_gather, an additional 'gather' parameter
  ** will be sent.  The gather parameter,if non-zero, is the named anchor to call the 
  ** news_gather program on return 
  **/
  cs_get_optional_parameter("gather", tmp_url, 10);
  *from_gather = atoi(tmp_url);
  
}

int check_url_has_protocol(char *url)
{

   char protocols[5][40] = {"ftp://", "mailto:", "https://", "telnet://", "http://"};
   int i, j, len;
   char aChar;
   int match_count =0;


   for(i = 0; i<5; i++)
   {
      match_count =0;
      len = strlen(protocols[i]);
      if(strstr(url, protocols[i]))
	    return 1;
      for(j = 0; j<len; j++)
      {
	  if(isalpha(protocols[i][j]))
            aChar = toupper(protocols[i][j]);
	  else
            aChar = protocols[i][j];
	   
          if(aChar == url[j] || url[j] == protocols[i][j])
               match_count++;
      }
      if(match_count == len)
	      return 1;
   }
   return 0; 


}




void
set_frameset (const char *course, const char *key, long offset,
	        char *url, int from_gather)
{
 char top_url[MAX_PATH + 1];
 
 
  snprintf(top_url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;loc=%ld&amp;grp=%d&amp;from=%s",
		  "web_page_back", course, key, offset, INTERNET_RESOURCES, from_gather?"news_gather":"news_read");
  cs_set_value("return_url", top_url);

 if(!check_url_has_protocol(url))
     cs_set_value("protocol", "http://");

  cs_set_value("target_url", url);
}





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

  int from_gather = 0;          /* from optional gather  = command line 
                                ** Were we called from news_gather program?
                                */
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
 

  cs_cgi_init();

  read_parameters (course, key, &offset, url, &from_gather);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */

  cs_set_course_info(&conf);
  set_frameset (course, key, offset, url, from_gather);

  cs_cgi_display("link_visit", 1);
  cs_cgi_destroy();
  return 0;
}

