/* Time-stamp: <97/01/23 14:12:06 john> */

/*
# Purpose: The program for accepting an edit to a streetplan or other entry.
 */

#include <stdio.h>
#include "attr.h"
#include "cgi.h"
#include "putedit.h"
/* #include "misc.h" */

/* #define debugging 1 */

int main(argc, argv)
     int argc;
     char * argv [];
{
  int debug = 0;
  int argi;
  char *attr_file_name = NULL;
  struct attribute *attrs = (struct attribute*)NULL;
  char file_name_buf[1024];
  FILE *raw_edit_file;
  int mode=EDIT_MODIFY;

  char *maintainer = "john@harlequin.co.uk";

#ifdef debugging
  debug = 1;
#endif
  
  printf("Content-type: text/html\n\n");
  printf("<html><head><title>%s</title></head>\n", "Edit confirmation");
  printf("<body bgcolor=\"#7f8fff\">\n<h1>%s</h1>\n", "Edit confirmation");

  if (debug) printf("<p>Debugging output included; %d args</p>\n", argc);

  if (argc >= 2)
    {
      if (strcmp(argv[1], "-modify") == 0)
	{
	  mode = EDIT_MODIFY;
	} else if (strcmp(argv[1], "-addentry") == 0)
	  {
	    mode = EDIT_ADDENTRY;
	  } else if (strcmp(argv[1], "-delete") == 0)
	    {
	      mode = EDIT_DELETE;
	    } else if (strcmp(argv[1], "-addpage") == 0)
	      {
		mode = EDIT_ADDPAGE;
	      } else {
		printf("Unknown edit mode %s\n", argv[1]);	  
		mode = EDIT_UNKNOWN;
	      }
      if (debug) printf("<p>Option \"%s\", recognized as code %d</p>\n", argv[1], mode);
    }

#if 1
  printf("<p>Since this facility is under development, some information in the raw <a href=\"../doc/authoring/format.html\">data-file format</a> appears in this confirmation page. If this confirmation appears to show a problem, please contact the <a href=\"mailto:streetmaster@cb1.com\">streetmaster</a>.</p>\n");
#endif

  printf("<p>Your modified version of this entry will be displayed only under the [include unvetted edits] option (which by default is <strong>on</strong>) until the <a href=\"mailto:streetmaster@cb1.com\">streetmaster</a> has merged it into the main data files.</p>\n");

  raw_edit_file = fopen("../edit-requests/raw-edits", "w+");
  if (raw_edit_file == NULL)
    {
      printf("<p>Could not open edit log file -- <strong>your edit may have been lost</strong>\n</p>");
    } else {
      if (!parse_post_input(&attrs, raw_edit_file, debug))
	{
	  printf("Could not parse post input\n");
	  exit(0);
	}

      switch (mode)
	{
	case EDIT_MODIFY:
	  if (debug)
	    {
	      printf("Editing...");
	    }

	  if (!modify(attrs))
	    {
	      printf("Modify failed!\n");
	    }

	  break;
	case EDIT_ADDENTRY:
	  if (debug)
	    {
	      printf("Adding entry...");
	    }

	  if (!addentry(attrs))
	    {
	      printf("Addentry failed!\n");
	    }
	  break;
	case EDIT_DELETE:
	  if (debug)
	    {
	      printf("Deleting entry...");
	    }
	  if (!delentry(attrs))
	    {
	      printf("Delentry failed!\n");
	    }
	  break;
	case EDIT_ADDPAGE:
	  if (debug)
	    {
	      printf("Adding page...");
	    }
	  if (!addpage(attrs))
	    {
	      printf("Addpage failed!\n");
	    }
	  break;
	}
	  
      if (debug)
	{
	  printf("</pre><p>End of parsing input</p>\n");
	}
      fclose(raw_edit_file);
#if 0
      free_attributes(attrs); 
#endif
    }
  printf("\n<hr>\n<a href=\"http://www.cb1.com/cb1/\"><img align=\"right\" alt=\"CB1 home\" src=\"../../www/small-CB1-home.gif\"></a><p><a href=\"../credits.html\">Credits</a></p>\n");
  printf("</body></html>\n");

  exit(0);

}

/* end of editor.c */
