/*   -*- coding: utf-8 -*-  */
#include <stdio.h>
#include "SxmlNode.h"
#include "StrDict.h"
#include <getopt.h>
#include <stdlib.h>

StrDict *orgDict;

void usage()
{
  fprintf(stderr,"usage: TeiTableOrgFromWiki -t typeOrg \n");
  exit(EXIT_FAILURE);
}

int getopt();
extern char *optarg;

SxmlNode *inputDoc;
SxmlNode *org;
char     *orgType;
char     *orgLevel;

int main (int argc, char **argv)
{
  int cOption;
  
  orgType=NULL;
  orgLevel=NULL;
  orgDict=NewStrDict();

 while((cOption=getopt(argc,argv,"n:t:"))!=EOF)
    {
      switch (cOption)
	{
	case 't':
	  orgType=optarg;
	  break;
	case 'n':
	  orgLevel=optarg;
	  break;
	default:
	  usage();
	  break;
	}
    }

  while ((inputDoc=SxmlInputNextDocumentElement()))
    {
      SxmlNode *keyElem;
      SxmlNode *listKey;
      SxmlNode *c1;
      SxmlNode *c3;
      SxmlNode *orgName;
      SxmlNode *orgName2;
      SxmlNode *country;
      SxmlNode *region;
      SxmlNode *placeName;
      SxmlNode *settlement;
      SxmlNode *firstNode;
      SxmlNode *typeNode;
      char *orgPageName;

      org=SxmlElementCreate("org");
      placeName=NULL;
      listKey=SxmlGetFirstChildByTagName(inputDoc,"c2");
      c1=SxmlGetFirstChildByTagName(inputDoc,"c1");
      c3=SxmlGetFirstChildByTagName(inputDoc,"c3");
      country=SxmlGetFirstChildByTagName(c3,"country");
      firstNode=SxmlFirstChild(c3);
      region=SxmlGetFirstChildByTagName(c3,"region");
      settlement=SxmlGetFirstChildByTagName(c3,"settlement");
      typeNode=SxmlGetFirstChildByTagName(c3,"type");
      orgPageName=SxmlLeafText(c1);


      if ((SxmlNodeHasName(firstNode,"#idem")))
	{
	  org=SxmlClone((SxmlNode *)StrDictSearch(orgDict, SxmlLeafText(firstNode)));
	  SxmlLeafSetText(SxmlFirstChild(org), orgPageName);
	}
      else
	{
	  if ((SxmlNodeHasName(firstNode,"orgName"))
	      && (!(SxmlFirstChild(firstNode))))
	    {
	      SxmlRemoveChild(firstNode);
	      orgName=firstNode;
	      SxmlAppendChild(org, orgName);
	      SxmlAppendChild(orgName, SxmlTextCreate( orgPageName));
	    }
	  else
	    {
	      SxmlAppendChild(org, orgName=SxmlLeafCreate("orgName",orgPageName));
	    }
	  if (typeNode)
	    {
	      SxmlSetAttribute(orgName, "type", SxmlLeafText(typeNode));
	    }
	  else if (orgType) SxmlSetAttribute(orgName, "type", orgType);
	  if (orgLevel) SxmlSetAttribute(orgName, "n", orgLevel);
	  orgName2=SxmlGetFirstChildByTagName(c3,"orgName"); /* caution must be after managing c1*/
	  if (country) SxmlAppendChild(org, SxmlClone(country));
	  while (orgName2)
	    {
	      SxmlAppendChild(org, SxmlClone(orgName2));
	      orgName2=SxmlGetNextSiblingByTagName(orgName2, "orgName");
	    }
	  while (settlement)
	    {
	      if (!placeName)SxmlAppendChild(org, placeName=SxmlElementCreate("placeName"));
	      SxmlAppendChild(placeName, SxmlClone(settlement));
	      settlement=SxmlGetNextSiblingByTagName(settlement, "settlement");
	    }
	  while(region)
	    {
	      if (!placeName)SxmlAppendChild(org, placeName=SxmlElementCreate("placeName"));
	      SxmlAppendChild(placeName, SxmlClone(region));
	      region=SxmlGetNextSiblingByTagName(region, "region");
	    }
	}

      SxmlReset(listKey);
      while ((keyElem=SxmlNextNode(listKey)))
	{
	  printf ("%s\t", SxmlLeafText(keyElem));
	  SxmlPrint(org);
	  putchar ('\n');
	}
      StrDictSet(orgDict,strdup(orgPageName), (char *)org);
      /* SxmlFree(org);*/
    }
  exit(EXIT_SUCCESS);
}
