/* Author: Ram Samudrala (me@ram.org)
 * Version: O1.0
 * Detail: <http://www.ram.org/computing/mutate/mutate.html>
 * January 27, 1995.
 *
 * See the URL above for more information.
 */

#include "cgi_common.h"
#include "cgi_defines.h"
#include "cgi_error_handlers.h"
#include "cgi_display.h"

int content_type_displayed = FALSE;

/******************************************************************/

#define MUTATING_LIST_FILENAME "/home/ram/tmp/mutating_file"

void display_form();
void get_location(char name[], char location[]);

/******************************************************************/

int main() 
{
  entry entries[MAX_ENTRIES];
  int i, number_of_entries;
  int location_index = 0, action_index, title_index, url_index;
  char line[1000], filename[100];
  FILE *input_fp, *output_fp;

  if(strcmp(getenv("REQUEST_METHOD"), "POST") != 0) 
    {
      display_form();
      return TRUE;
    }
  
  check_content_type("main");
  number_of_entries = read_entries(entries);
  
  for(i = 0; i <= number_of_entries; i++)
    {
      if (strcmp(entries[i].name, "wheretogo") == 0)
	location_index = i;
      if (strcmp(entries[i].name, "action") == 0)
	action_index = i;
      if (strcmp(entries[i].name, "title") == 0)
	title_index = i;
      if (strcmp(entries[i].name, "url") == 0)
	url_index = i;
    }

  if ((entries[title_index].val[0] != '\0') && (entries[url_index].val[0] != '\0'))
    {
      open_file(&output_fp, MUTATING_LIST_FILENAME, "a", "mutate");
      fprintf(output_fp, "%s | %s\n", entries[title_index].val, entries[url_index].val);
      close_file(&output_fp, MUTATING_LIST_FILENAME, "mutate");
      display_form();
      return TRUE;
    }
  
  if (strcmp(entries[action_index].val, "go") == 0)
    {
      if (entries[location_index].val[0] == '\0')
	{
	  display_form();
	  return TRUE;
	}      
      get_location(entries[location_index].val, line);
      if (line[0] == '\0')
	strcpy(line, DEFAULT_URL);
      printf("Location: %s%c%c", line, 10, 10);
      return TRUE;
    }

  if (strcmp(entries[action_index].val, "delete") == 0)
    {
      if ((entries[location_index].val[0] == '\0') || (entries[location_index].val[0] == '\n'))
	{	  
	  display_form();
	  return TRUE;
	}
      open_file(&input_fp, MUTATING_LIST_FILENAME, "r", "mutate");
      sprintf(filename, "%s.%d", TMP_FILENAME_STRING, (int) getpid());
      open_file(&output_fp, filename, "w", "mutate");

      while (fgets(line, 200, input_fp))
	if (!strstr(line, entries[location_index].val))
	  fputs(line, output_fp);

      close_file(&input_fp, MUTATING_LIST_FILENAME, "mutate");
      close_file(&output_fp, filename, "mutate");

/*      sprintf(line, "yes | mv  %s %s", filename, MUTATING_LIST_FILENAME);
      system(line); */
      rename(filename, MUTATING_LIST_FILENAME);
      
      display_form();
      return TRUE;
    }  

  display_form();
  return TRUE;
}

/******************************************************************/

void get_location(char name[], char location[])
{
  char line[200];
  FILE *input_fp;
  int i, j;

  open_file(&input_fp, MUTATING_LIST_FILENAME, "r", "get_location");
  while (fgets(line, 200, input_fp))
    if (strstr(line, name))
      {
	for(i = 0; line[i] != '|'; i++);
	i += 2;
	for(j = 0; (line[i] != '\0') && (line[i] != '\n'); i++, j++)
	  location[j] = line[i];
	location[j] = '\0';
	  break;
      }
  close_file(&input_fp, MUTATING_LIST_FILENAME, "get_location");
  return;
}
  
/******************************************************************/
   
void display_form()
{
  char line[200];
  FILE *input_fp;
  int i;

  open_file(&input_fp, MUTATING_LIST_FILENAME, "r", "display_form");
  display_content_type("The mutating list of cool www sites");
  
  printf("<h1 align=center>The mutating list of cool www sites</h1>\n");
  printf("<p>This mutating list exists so that the users of the www can decide for themselves which sites\n");
  printf("are cool to visit and which aren't!  I got sick of trying to maintain lists of sites\n");
  printf("and thus I made it so you now have the freedom to add a site you think\n");
  printf("rocks, delete a site you think sucks, or just visit a choice made by your peers.</p>\n");
  printf("<hr>\n");
 
  printf("<h5 align=center> <form method=\"POST\" action=\"/cgi-bin/mutate\">");
  printf("Enter a title and a URL if you want to add something: <br>\n");  
  printf("name: <input type=\"text\" name=\"title\" size=40> <br>\n");
  printf("URL: <input type=\"text\" name=\"url\" size=40> <br> <br>\n");  
  printf("OR<br><br> choose what you want to do:<br>\n");
  printf("<select name=\"wheretogo\">\n");
  while (fgets(line, 200, input_fp))
    if ((line[0] != '\0') || (line[0] != '\n'))
      {
	printf("<option> ");
	for(i = 0; (line[i] != '|'); i++)
	  printf("%c", line[i]);
	printf("\n");
      }      
  fclose(input_fp);
  printf("</select><br>");
  printf("<input type=\"radio\" name=\"action\" value=\"go\" checked> take me there\n");
  printf("<input type=\"radio\" name=\"action\" value=\"delete\"> get rid of it!<br> <br>\n");
  printf("<input type=\"submit\" value=\"just do it!\">\n");
  printf("</form> </h5>\n");

  printf("\n<hr><p>The motivation for doing this is to see how Darwinism works on the www. \n");
  printf("The idea being that URLs with a lot of information content are \"more fit\" than URLs \n");
  printf("which contain nothing but links to other places (and there are too many of these---yet \n");
  printf("another reason for me to do this) and thus they will be selected for.  Given \n");
  printf("changing environments and competition, URLs will have to continuously evolve in order \n");
  printf("to be selected constantly.</p>\n");

  printf("<hr>\n<a href=\"/ram.html \">Ram Samudrala</a> <b>||</b> ");
  printf("<var><a href=\"mailto:me@ram.org\">me@ram.org</a></var>\n<hr>");
  return;
}

/******************************************************************/
