/* Time-stamp: <97/01/24 17:27:28 john> */

/* 
# Purpose: merger program for the streetmaster to use to move edits from ../edit-requests/new-entries.attr and ../edit-requests/new-edits.attr into the main data files
 */

#include <stdio.h>
#include "attr.h"
#include "style.h"		/* required by readattr.h */
#include "readattr.h"
#include "save.h"
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include "graphics.h"

int get_yes_no(char *prompt)
{
  printf("%s (y or n)? ", prompt);

  while (1)
    {
      char answer = getchar();
      if ((answer == 'y') || (answer == 'Y'))
	{
	  return 1;
	}
      if ((answer == 'n') || (answer == 'N'))
	{
	  return 0;
	}
      if (answer == '\n')
	continue;
      printf("%s (you must answer y or n)?\n", prompt);
    }
}

void filter_attributes(struct entry *entries)
{
  struct entry *this_entry;

  for (this_entry = entries;
       this_entry != NULL;
       this_entry = this_entry->next)
    {
      extract_attr(this_entry, "pagetype");
      extract_attr(this_entry, "pagename");
      extract_attr(this_entry, "tag");
      extract_attr(this_entry, "pagefile");
      extract_attr(this_entry, "zoom");
      extract_attr(this_entry, "output");
    }
}

void save_graphics()
{
  write_entries_to_file(all_graphics,
			0, "../graphics.attr", "w", NULL, NULL);
}

void update_graphics(char *identify_key, char *identify_value,
		     char *update_name, char *update_value)
{
  struct style dummy_style;
  struct entry dummy_entry;
  struct attribute dummy_attribute;
  struct entry *the_graphics;
  dummy_attribute.next = NULL;
  dummy_attribute.key = identify_key;
  dummy_attribute.value = identify_value;
  dummy_entry.next = NULL;
  dummy_entry.grid = NULL;
  dummy_entry.name = "";
  dummy_entry.type = "";
  dummy_entry.attributes = &dummy_attribute;

  the_graphics = get_graphics(&dummy_entry, &dummy_style);

  if (the_graphics == NULL)
    {
      printf("Could not find graphics entry to patch for %s=%s\n",
	     identify_key, identify_value);
    } else {
      set_attr(the_graphics, update_name, update_value);
      save_graphics();
    }
}

int main(argc, argv)
     int argc;
     char * argv [];
{
  struct entry *entries = NULL;
  struct entry *en;
  int print_old = 1;
  int print_new = 1;
  int debug = 0;
  int read_error = READ_OK;
  FILE *ftp_commands = NULL;

  if (argc >= 2)
    {
      if (strcmp(argv[1], "-debug") == 0)
	{
	  debug = 1;
	}
    }

  ftp_commands = fopen("update.ftp", "a+");

  if (ftp_commands != NULL)
    {
#if 0
      fprintf(ftp_commands, "verbose\nhash\n");
#endif
    }

  if (!read_attribute_file("edit-requests", "new-entries", NULL,
			   &entries, &read_error,
			   default_check_entry, 1, 0))
    {
      printf("Could not read %s\n", "edit-requests/new-entries.attr -- assuming no new entries");
    }

  for (en = entries;
       en != NULL;
       en = en->next)
    {
      struct attribute *at;
      char *pagetype = get_attr(en, "pagetype");
      char *pagename = get_attr(en, "pagename");

      printf("New entry:\n");

      for (at = en->attributes;
	   at != NULL;
	   at = at->next)
	{
	  printf("  %s=\"%s\"\n", at->key, at->value);
	}

      printf("\n");

      if ((pagetype == NULL) || (pagename == NULL))
	{
	  printf("Can't do anything with that, as it has no pagefile attribute\n");
	  continue;
	} else {
	  char pagetype_buf[1024];
	  struct entry *page_entries = NULL;
	  struct entry *old = NULL;

	  if (strlen(pagetype) > 200)
	    {
	      printf("Ridiculously long type name: %s\n", pagetype);
	      continue;
	    }

	  sprintf(pagetype_buf, "%ss", pagetype);

	  if (!read_attribute_file(pagetype_buf, pagename, NULL,
				   &page_entries, &read_error,
				   default_check_entry, 1, debug))
	    {
	      printf("Could not read %s/%s to add entries\n", pagetype_buf, pagename);
	      report_read_error(read_error, pagetype, pagename);
	      continue;
	    }



	  
	  printf("\nNewly added entry:\n");

	  for (at = en->attributes;
	       at != NULL;
	       at = at->next)
	    {
	      printf("  %s=\"%s\"\n", at->key, at->value);
	    }

	  printf("\n");

	  if (page_entries == NULL)
	    {
	      printf("Strange damage!\n");
	    } else {

	      if (get_yes_no("Save added entry"))
		{
		  char pagefile[1024];
		  sprintf(pagefile, "../%ss/%s.attr", pagetype, pagename);
		  filter_attributes(page_entries);
		  if (write_entries_to_file(page_entries,
					    0,
					    pagefile,
					    "w",
					    NULL,
					    NULL))
		    {
		      printf("Saved to %s\n\n", pagefile);
		    } else {
		      printf("Problem in saving %s\n\n", pagefile);
		    }
		}
	    }

#if 0
	  free_entries(page_entries);
#endif




	}
    }

  entries = NULL;

  if (!read_attribute_file("edit-requests", "new-edits", NULL,
			   &entries, &read_error,
			   merger_check_entry, 1, 0))
    {
      fprintf(stderr, "Could not read %s\n", "edit-requests/new-edits.attr -- assuming no edits");
    }

  for (en = entries;
       en != NULL;
       en = en->next)
    {
      struct attribute *at;
      char *pagetype = get_attr(en, "pagetype");
      char *pagename = get_attr(en, "pagename");
  
      if (print_new)
	{
	  printf("\nNew version:\n");

	  for (at = en->attributes;
	       at != NULL;
	       at = at->next)
	    {
	      printf("  %s=\"%s\"\n", at->key, at->value);
	    }

	  printf("\n");
	}

      if ((pagetype == NULL) || (pagename == NULL))
	{
	  printf("Can't do anything with that, as it has no pagefile indicated\n");
	  continue;
	} else {
	  char pagetype_buf[1024];
	  struct entry *page_entries = NULL;
	  struct entry *old = NULL;

	  if (strlen(pagetype) > 200)
	    {
	      printf("Ridiculously long type name: %s\n", pagetype);
	      continue;
	    }

	  sprintf(pagetype_buf, "%ss", pagetype);

	  if (!read_attribute_file(pagetype_buf, pagename, NULL,
				   &page_entries, &read_error,
				   merger_check_entry, 1, 0))
	    {
	      printf("Could not read %s/%s to modify entries\n", pagetype_buf, pagename);
	      report_read_error(read_error, pagetype, pagename);
	      continue;
	    }

	  old = find_matching_entry_for_merge(en, page_entries, "old-name", "name");

	  if (old == NULL)
	    {
	      free_entries(page_entries);
	      printf("Could not find entry that this replaces\n");
	      continue;
	    }

	  if (print_old)
	    {
	      printf("\nOld version:\n");

	      for (at = old->attributes;
		   at != NULL;
		   at = at->next)
		{
		  printf("  %s=\"%s\"\n", at->key, at->value);
		}

	      printf("\n");
	    }

	  merge_entries(old, en);
	  
	  printf("\nMerged version:\n");

	  for (at = old->attributes;
	       at != NULL;
	       at = at->next)
	    {
	      printf("  %s=\"%s\"\n", at->key, at->value);
	    }

	  printf("\n");

	  if (page_entries == NULL)
	    {
	      printf("Strange damage!\n");
	    } else {

	      if (get_yes_no("Save modified entry"))
		{
		  char pagefile[1024];
		  sprintf(pagefile, "../%ss/%s.attr", pagetype, pagename);
		  filter_attributes(page_entries);
		  if (write_entries_to_file(page_entries,
					    0,
					    pagefile,
					    "w",
					    NULL,
					    NULL))
		    {
		      if (ftp_commands != NULL)
			{
			  fprintf(ftp_commands, "put ../%ss/%s.attr\n",
				  pagetype, pagename);
			}
		      printf("Saved to %s\n\n", pagefile);
		    } else {
		      printf("Problem in saving %s\n\n", pagefile);
		    }
		}
	    }

#if 0
	  free_entries(page_entries);
#endif
	}
    }

  {
    time_t my_time;
  
    if (time(&my_time) != -1)
      {
    
	update_graphics("type", "merge", "date", ctime(&my_time));

	/* perhaps put name of updater here, too? */

	if (ftp_commands != NULL)
	  {
	    fprintf(ftp_commands, "put ../graphics.attr\n");
	  }
      }
  }
    
  if (ftp_commands != NULL)
    {
#if 0
      fprintf(ftp_commands, "quit\n");
#endif
      fclose(ftp_commands);
    }

  free_entries(entries);

  exit(0);
}

/* end of merger.c */
