/* Time-stamp: <96/12/12 18:11:52 john> */

/* 
# Purpose: Program to set up the arguments for <a href="#makeroad">makeroad</a>; this is called from <a href="#addpage.cgi">addpage.cgi</a>
 */

#include <stdio.h>
#include <ctype.h>
#include "attr.h"
#include "cgi.h"
#include "style.h"		/* needed by misc.h */
#include "misc.h"

char *safe_get_attr(struct entry *entry, char *key)
{
  int i;
  char c;
  char *result = get_attr(entry, key);
  if (result == NULL) return NULL;
  if (strlen(result) > 200) return NULL; /* someone's being funny */
  if (!vet_name(result)) return NULL;

  return result;
}

static char as_name[1024];

char *to_name(char *as_link)
{
  char pc = '_';
  char c;
  int i;

  for (i = 0;
       (((c = as_link[i]) != '\0') && (i < 1022));
       i++)
    {
      if (('a' <= c) && (c <= 'z'))
        {
	  as_name[i] = (pc == '_') ? toupper(c) : c;
	} else if (('A' <= c) && (c <= 'Z'))
	  {
	    as_name[i] = (pc != '_') ? tolower(c) : c;
	  } else {
	    as_name[i] = c = '_';
	  }
      pc = c;
    }
  as_name[i] = '\0';
  return as_name;
}

int main(argc, argv)
     int argc;
     char * argv [];
{
  struct entry from_input;
  struct attribute *attrs = (struct attribute*)NULL;
  FILE *raw_edit_file = fopen("../edit-requests/raw-addpages", "w+");
  from_input.next = NULL;
  from_input.column = 0;
  from_input.left = 0;
  from_input.width = 0;
  from_input.height = 0;
  from_input.name = "Dummy entry from input";
  from_input.type = "Dummy entry";
  from_input.attributes = attrs;
  if (raw_edit_file == NULL)
    {
      error_header("Edit logging problem");
      printf("<p>Could not open edit log file -- <strong>your edit may have been lost</strong>\n</p>");
      error_trailer();
    } else {
      if (parse_post_input(&attrs, raw_edit_file, 0))
	{
	  from_input.attributes = attrs;
	  {
	    char *name = safe_get_attr(&from_input, "name");
	    char *area = safe_get_attr(&from_input, "area");
	    char *top = safe_get_attr(&from_input, "top");
	    char *top_left = safe_get_attr(&from_input, "top_left");
	    char *top_right = safe_get_attr(&from_input, "top_right");
	    char *bot = safe_get_attr(&from_input, "bot");
	    char *bot_left = safe_get_attr(&from_input, "bot_left");
	    char *bot_right = safe_get_attr(&from_input, "bot_right");
	    char *description = safe_get_attr(&from_input, "description");
	    char top_buf[256];
	    char top_left_buf[256];
	    char top_right_buf[256];
	    char bot_buf[256];
	    char bot_left_buf[256];
	    char bot_right_buf[256];
	    char command_buffer[2048];

#if 0
	    html_header("Addpage result");
	    printf("<pre>\n");
	    printf("name = %s\n", name);
	    printf("area = %s\n", area);
	    printf("top = %s\n", top);
	    printf("top_left = %s\n", top_left);
	    printf("top_right = %s\n", top_right);
	    printf("bot = %s\n", bot);
	    printf("bot_left = %s\n", bot_left);
	    printf("bot_right = %s\n", bot_right);
	    printf("<pre>\n");
#endif

	    if ((top != NULL) && (top[0]!='\0') && (strcmp(top, "none") != 0))
	      {
		sprintf(top_buf, " -t %s", to_name(top));
	      } else {
		top_buf[0] = '\0';
	      }

	    if ((top_left != NULL) && (top_left[0]!='\0') && (strcmp(top_left, "none") != 0))
	      {
		sprintf(top_left_buf, " -tl %s", to_name(top_left));
	      } else {
		top_left_buf[0] = '\0';
	      }

	    if ((top_right != NULL) && (top_right[0]!='\0') && (strcmp(top_right, "none") != 0))
	      {
		sprintf(top_right_buf, " -tr %s", to_name(top_right));
	      } else {
		top_right_buf[0] = '\0';
	      }

	    if ((bot != NULL) && (bot[0]!='\0') && (strcmp(bot, "none") != 0))
	      {
		sprintf(bot_buf, " -b %s", to_name(bot));
	      } else {
		bot_buf[0] = '\0';
	      }

	    if ((bot_left != NULL) && (bot_left[0]!='\0') && (strcmp(bot_left, "none") != 0))
	      {
		sprintf(bot_left_buf, " -bl %s", to_name(bot_left));
	      } else {
		bot_left_buf[0] = '\0';
	      }

	    if ((bot_right != NULL) && (bot_right[0]!='\0') && (strcmp(bot_right, "none") != 0))
	      {
		sprintf(bot_right_buf, " -br %s", to_name(bot_right));
	      } else {
		bot_right_buf[0] = '\0';
	      }
	  
	  
	    printf("%s %s%s%s%s%s%s%s\n",
		   to_name(name), area,
		   top_buf, top_left_buf, top_right_buf,
		   bot_buf, bot_left_buf, bot_right_buf);
#if 0
	    printf("Command: %s\n", command_buffer);
#endif
#if 0
	    system(command_buffer);
#endif
#if 0
	    system("echo Hi there from echo\n");
#endif

#if 0
	    printf("<hr><a href=\"getplan.cgi?name=%s\">[View new page]</a>\n", name);
	    printf("<a href=\"getplan.cgi?edit=on&name=%s\">[Edit new page]</a><br>\n", name);
	    printf("</body></html>\n");
#endif
	  }

	} else {
	  printf("Could not parse post input\n");
	  exit(0);
	}

      
  
    }
  exit(0);
}

/* end of addpage.c */
