/*   -*- coding: utf-8 -*-  */

/*********************************************************************************
 *
 *            Module :   Explor
 *            Commande : ExplorGenerSiteParam
 *            Fichier  : ExplorGenerSiteParam.c
 *
 *********************************************************************************/

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <time.h>
#include "Explor.h"

char *ExplorInput;
char *ExplorInputTab;
char *ExplorInputVal;
char *ExplorInputColon;
char *ExplorInputFirstSlash;
char *ExplorInputLastSlash;

extern char *optarg;
extern int   optind;
int getopt();

void generParamTab(char *lang)
{
  char *k;

  printf("<?dilib file=\"%s/Input/AreaSiteParam.%s.tab\" ?>\n", ExplorAreaDir, lang );
  StrDictIteratorReset(ExplorTableParam);
  while ((k=StrDictNext(ExplorTableParam)))
    {
      printf("%s\t%s\n",k, StrDictValue(ExplorTableParam));
    }
}

char *interpretAttributeWithNS(char *attribute, char *inputKey, char *value)
{
  char *posLastSlash;
  int lenAtt;
  static Buffer *bufRule=NULL;
  static Buffer* bufKey=NULL;
  SxmlNode *streamNode;
  char *strListStream;
  SxmlNode *xmlListStream;

  if(!bufRule) bufRule=NewBuffer();
  if (!bufKey)bufKey=NewBuffer();

  posLastSlash=strrchr(inputKey,'/');
  lenAtt=strlen(attribute);
  if (strncmp(posLastSlash+1, attribute, lenAtt)!=0) return NULL;

  BufferStrcpy(bufRule, posLastSlash+lenAtt+1);
  BufferStrcat(bufRule,"/listStreams");
  strListStream=ExplorGetFromDict(BufferString(bufRule));
  if (!strListStream) return NULL;
  xmlListStream=SxmlFromString(strListStream);
  while ((streamNode=SxmlNextNode(xmlListStream))) 
    {
      char *streamCode;
      streamCode=SxmlLeafText(streamNode);
      BufferStrncpy(bufKey, inputKey,  posLastSlash-inputKey);
      BufferStrcat(bufKey, "/");
      BufferStrcat(bufKey, attribute);
      BufferStrcat(bufKey, streamCode);
      StrDictSet(ExplorTableParam, strdup(BufferString(bufKey)), strdup(value));
    }
  return value;
}

int ExplorTestProcNamesTree()
{
  static Buffer *bufPath=NULL;
  if (!bufPath) bufPath=NewBuffer();
  char *lastSlash;
  char *posAt;
  char *pathNode;
  SxmlNode *streamNode;

  if (strcmp(ExplorInput, "Area/nameRoot")==0)
    {
      SxmlSetAttribute(ExplorAreaSiteLang, "name", ExplorInputVal);
      return 1;
    }
  if (strcmp(ExplorInput, "Area/shortNameRoot")==0)
    {
      SxmlSetAttribute(ExplorAreaSiteLang, "shortName", ExplorInputVal);
      return 1;
    }
  if (!ExplorInputLastSlash) return 0;
  BufferStrcpy(bufPath, ExplorInput);
  lastSlash=strrchr(BufferString(bufPath), '/');
  lastSlash[0]='\0';
  if (ExplorInputColon)
    {
      /* for instance: Inist:BhaSolo/nameStream   BHA  */
      SxmlNode * listStreams;
      char *listStreamsStr;
      SxmlNode *streamIterNode;
      Buffer *bufList=0;

      if (!bufList)bufList=NewBuffer();
      BufferStrcpy(bufList,BufferString(bufPath));
      BufferStrcat(bufList,  "/listStreams");
      listStreamsStr=ExplorGetFromDict(BufferString(bufList));
      if (!listStreamsStr)return 0;
      listStreams=SxmlFromString(listStreamsStr);
      while ((streamIterNode=SxmlNextNode(listStreams)))
	{	
	  streamNode=ExplorSiteTreeNode(SxmlLeafText(streamIterNode), NULL, NULL);
	  if (strcmp(ExplorInputLastSlash, "/nameStream")==0)
	    {
	      SxmlSetAttribute(streamNode, "name", ExplorInputVal);
	      return 1;
	    }
	  if (strcmp(ExplorInputLastSlash, "/shortNameStream")==0)
	    {
	      SxmlSetAttribute(streamNode, "shortName", ExplorInputVal);
	      return 1;
	    }
	}
      return 0;
    }

  pathNode=BufferString(bufPath);
  streamNode=ExplorSiteTreeNodeFromPath(pathNode);
  if (strcmp(ExplorInputLastSlash, "/nameStream")==0)
    {
      SxmlSetAttribute(streamNode, "name", ExplorInputVal);
      return 1;
    }
  if (strcmp(ExplorInputLastSlash, "/shortNameStream")==0)
    {
      if (ExplorInputColon)
	{
	  return 0;  /* to be improved */
	}
      SxmlSetAttribute(streamNode, "shortName", ExplorInputVal);
      return 1;
    }
  posAt=strchr(lastSlash+1, '@');
  if (posAt)
    {
      posAt[0]='\0';
      if (strcmp(lastSlash+1, "indexShortName")==0)
	{
	  ExplorTreeInheritAttribute(streamNode,"index", posAt+1, "shortName", ExplorInputVal);
	}
      else if (strcmp(lastSlash+1, "indexName")==0)
	{
	  ExplorTreeInheritAttribute(streamNode,"index", posAt+1, "name", ExplorInputVal);
	}
      else if (strcmp(lastSlash+1, "indexPhpItem")==0)
	{
	  ExplorTreeInheritAttribute(streamNode,"index", posAt+1, "phpItem", ExplorInputVal);
	}
      else if (strcmp(lastSlash+1, "shortNameIndex")==0)
	{
	  ExplorTreeInheritAttribute(streamNode,"index", posAt+1, "shortName", ExplorInputVal);
	}
      else if (strcmp(lastSlash+1, "nameIndex")==0)
	{
	  ExplorTreeInheritAttribute(streamNode,"index", posAt+1, "name", ExplorInputVal);
	}
    }
  return 0;
}

int main (int argc, char **argv) {
   int cOption;
   SxmlNode *streamNode;
   char *pathParamFile;
   Buffer *bufInput;
   Buffer *bufRule;
   Buffer *bufType;
   char *lang;
   time_t timeData;
   
   bufInput=NewBuffer();
   bufRule=NewBuffer();
   bufType=NewBuffer();

   timeData =  time(NULL);

   while((cOption=getopt(argc,argv,"l:t:"))!=EOF)
     {switch (cOption)
	 {
	 case 't':
	   pathParamFile=optarg;
	   break;
	 case 'l':
	   lang=optarg;
	   break;
	 }
     }
   ExplorParamInit(pathParamFile);

   StrDictSet(ExplorTableParam,"Area/siteCreationTime", ctime(&timeData));

   if (ExplorAreaSite)
     {
       ExplorAreaSiteLang=SxmlClone(SxmlFirstChild(ExplorAreaSite));
       SxmlAppendChild(ExplorAreaSite, ExplorAreaSiteLang);
       SxmlSetAttribute(ExplorAreaSiteLang, "code", lang);
     }
   else 
     {
       SxmlAppendChild(ExplorAreaTree, ExplorAreaSite=SxmlElementCreate("site"));
       ExplorAreaSiteLang=SxmlClone(ExplorAreaData);
       SxmlAppendChild(ExplorAreaSite, ExplorAreaSiteLang);
       SxmlReplaceName(ExplorAreaSiteLang, strdup("lang"));
       SxmlSetAttribute(ExplorAreaSiteLang, "code", lang);
     }

   while (BufferGets(bufInput))
     {
       ExplorInput=BufferString(bufInput);
       ExplorInputTab=strchr(ExplorInput,'\t');
       ExplorInputTab[0]='\0';
       ExplorInputVal=ExplorInputTab+1;
       ExplorInputColon=strchr(BufferString(bufInput),':');
       ExplorInputFirstSlash=strchr(BufferString(bufInput),'/');
       ExplorInputLastSlash=strrchr(BufferString(bufInput),'/');
       StrDictSet(ExplorTableParam, strdup(ExplorInput), strdup(ExplorInputVal));
       if (ExplorTestProcNamesTree()) continue;
       if (ExplorInputColon&&ExplorInputFirstSlash)
	 {
	   SxmlNode *streamNode;
	   char *strListStream;
	   SxmlNode *xmlListStream;
	   if(ExplorInputFirstSlash>ExplorInputColon)
	     {
	       if (ExplorInputColon[1]=='*')
		 {
		   BufferStrncpy(bufType, BufferString(bufInput), ExplorInputColon-BufferString(bufInput));
		 }
	       else
		 {
		   BufferStrncpy(bufType, BufferString(bufInput), ExplorInputFirstSlash-BufferString(bufInput));
		 }
	       BufferStrcat(bufType, "/listStreams");
	       strListStream=ExplorGetFromDict(BufferString(bufType));
	       if (!strListStream) continue;
	       xmlListStream=SxmlFromString(strListStream);
	       while ((streamNode=SxmlNextNode(xmlListStream))) 
		 {
		   char *streamCode;
		   streamCode=SxmlLeafText(streamNode);
		   BufferStrcpy(bufRule,"Area/");
		   BufferStrcat(bufRule,streamCode);
		   BufferStrcat(bufRule,"/");
		   BufferStrcat(bufRule,ExplorInputFirstSlash+1);
		   StrDictSet(ExplorTableParam, strdup(BufferString(bufRule)), strdup(ExplorInputTab+1));
		 }
	     }
	   else  /* ExplorInputColon > ExplorInputFirstSlash */
	     {
	       if (interpretAttributeWithNS("shortNameStream", BufferString(bufInput), ExplorInputTab+1)) continue;
	       if (interpretAttributeWithNS("nameStream", BufferString(bufInput),  ExplorInputTab+1)) continue;
	     }
	 }
     }
   {  /* MainLastStep */
     char *mainLastStep;
     char *shortNameMainLastStep;
     char *keyForShortNameMainLastStep;
     mainLastStep=ExplorGetFromDict("Area/mainLastStep");
     keyForShortNameMainLastStep=malloc(strlen("Area/shortNameStep")+1+strlen(mainLastStep));
     strcpy(keyForShortNameMainLastStep, "Area/shortNameStep");
     strcat(keyForShortNameMainLastStep, mainLastStep);
     shortNameMainLastStep=ExplorGetFromDict(keyForShortNameMainLastStep);
     StrDictSet(ExplorTableParam, "Area/shortNameMainLastStep", shortNameMainLastStep);
   }

   { /* implicit values for streams */
     Buffer *bufName;
     bufName=NewBuffer();

     /*  SxmlReset(ExplorListStream); 
     while(streamNode=SxmlNextNode(ExplorListStream))
     */
     SxmlReset(ExplorListStream); 
     while ((streamNode=SxmlNextNode(ExplorAreaSiteLang)))
       {
	 char *streamCode;
	 char *streamName;
	 char *streamShortName;
	 BufferStrcpy(bufName,"Area/shortNameStream");
	 /* streamCode=SxmlLeafText(streamNode); */
	 streamCode=SxmlGetAttribute(streamNode, "code");
	 BufferStrcat(bufName, streamCode);
	 if (!(streamShortName=ExplorGetFromDict(BufferString(bufName))))
	   {
	     streamShortName=streamCode;
	     StrDictSet(ExplorTableParam, strdup(BufferString(bufName)), strdup(streamShortName)); 
	   }
	 BufferStrcpy(bufName,"Area/nameStream");
	 BufferStrcat(bufName, streamCode);
	 streamName=ExplorGetFromDict(BufferString(bufName));
	 if (!(streamName))
	   {
	     streamName=streamShortName;
	     StrDictSet(ExplorTableParam, strdup(BufferString(bufName)), streamName); 
	   }
       }
   }
   ExplorSetDictK2("Area", "areaTree", strdup(SxmlToString(ExplorAreaTree)));
   generParamTab(lang);
}

