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

/**********************************************************************
*
*  module   : Explor
*  fichier  : ExplorGenerAreaPages.c
*
***********************************************************************/

#include "ExplorGenerPages.h"

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

/* SxmlNode *ExplorGenerPagesListZooms; */

char *getBandeauCode(SxmlNode *stream, char*type, char *monoMulti)
{
  SxmlNode *paramNode;
  SxmlNode *bandeauNode;
  paramNode=SxmlGetFirstChildByTagName(stream, "param");
  if ((bandeauNode=SxmlGetFirstChildTagAtt(paramNode, "Bandeau", "type", monoMulti))) return SxmlLeafText(bandeauNode);
  else return NULL;
}

char *printImportFile (SxmlNode *importNode)
{
  char * attCode;
  char * nsCode;
  attCode=SxmlGetAttribute(importNode, "code");
  nsCode=SxmlGetAttribute(importNode, "NS");
  printf("|-\n|");
  if(nsCode) printf("%s:", nsCode);
  if (attCode) printf("%s", attCode);
  else printf("importFile");
  printf("\n|Area/Main/Corpus\n|%s\n", SxmlLeafText(importNode));
  return SxmlLeafText(importNode);
}

char *getPrintImportFile(SxmlNode *stream, char*type)
{
  SxmlNode *paramNode;
  SxmlNode *importNode;
  char *importFile;
  paramNode=SxmlGetFirstChildByTagName(stream, "param");
  if ((importNode=SxmlGetFirstChildByTagName(paramNode, "importFile")))
    {
      importFile=printImportFile (importNode);
      if ((importNode=SxmlGetNextSiblingByTagName(importNode, "importFile")))
	 importFile=printImportFile (importNode);
      return importFile;
    }
  else return NULL;
  
}

char *getDataCode(SxmlNode *stream, char*type)
{
  SxmlNode *paramNode;
  SxmlNode *dataNode;
  char *NS;
  char *dataString;
  static Buffer *bufNS=NULL;
  if (!bufNS)bufNS=NewBuffer();
  paramNode=SxmlGetFirstChildByTagName(stream, "param");
  dataNode=SxmlGetFirstChildTagAtt(paramNode, "Data", "type", type);
  if (!dataNode)return NULL;
  dataString=SxmlLeafText(dataNode);
  if ((NS=SxmlGetAttribute(dataNode,"NS")))
    {
      BufferStrcpy(bufNS,NS);
      BufferStrcat(bufNS,":");
      BufferStrcat(bufNS,dataString);
      return BufferString(bufNS);
    }
  return dataString;
}

void generCorpusCommand(SxmlNode *nodeStream)
{
  SxmlNode *corpusNode;
  SxmlNode *paramNode;
  paramNode=SxmlGetFirstChildByTagName(nodeStream, "param");
  if ((corpusNode=SxmlGetFirstChildByTagName(paramNode, "Corpus")))
    {
      printf("<source lang=\"sh\">\n");
      /* printf ("  %s > %s/Import/\n", SxmlLeafText(corpusNode), ExplorGenerPagesAreaCode); */
      printf (SxmlLeafText(corpusNode), ExplorGenerPagesAreaCode); 
      printf("\n</source>\n");
    }
}

void printCompilInterface()
{
  printf("<source lang=\"sh\">\n");
  printf("sh $EXPLOR_AREA/bin/AreaCreateSite.fr.sh\n");
  printf("</source>\n");
}


SxmlNode *getTypeFromTable(SxmlNode *onNode, char *type)
{
  char *paramStr;
  SxmlNode *dictParamNode;
  if ((paramStr=StrDictSearch(ExplorGenerPagesParamFile, type)))
    {
      SxmlNode *paramNode;
      SxmlNode *c3Node;
      dictParamNode=SxmlFromString(paramStr);
      c3Node=SxmlGetFirstChildByTagName(dictParamNode, "c3");
      SxmlAppendChild(onNode, paramNode=SxmlClone(c3Node));
      SxmlReplaceName(paramNode, strdup("param"));
    }
  else
    {
      ExceptSetError("ExplorGenerAreaPages","UN","Type stream", type, "unknown",2);
      return NULL;
    }
  return dictParamNode;
}


int main (int argc, char **argv)
{
  int cOption;
  /*  int nZooms; */
  SxmlNode *nodeStream;

  ExplorGenerPagesInit();

  /*  ExplorGenerPagesListZooms=SxmlElementCreate("zooms"); */
  ExplorGenerPagesNumberOfStreams=0;
  /* nZooms=0; */

  while((cOption=getopt(argc,argv,"a:g:p:q:mt:w:W:s:T:z:"))!=EOF)
    {switch (cOption)
	{
	case 'a':
	  ExplorGenerPagesAreaCode=optarg;
	  break;
	case 'm':
	  ExplorGenerPagesMapOption=1;
	  break;
	case 'p':
	  ExplorGenerAreaPath=optarg;
	  break;
	case 'g':
	  ExplorGenerPagesGenericCode=optarg;
	  break;
	case 's':
	  ExplorGenerPagesParamStream(optarg);
	  break;
	case 't':
	  ExplorGenerAreaTitle=optarg;
	  break;
	case 'w':
	  ExplorGenerPagesWikiLink=optarg;
	  break;
	case 'T':
	  DilibSetTraceLevel(atoi(optarg));
	  break;
	case 'W':
	  ExplorGenerPagesWikiCode=optarg;
	  break;
	case 'z':
	  ExplorGenerPagesParamZoom(optarg);
	  break;
	}
    }

  if  (DilibGetTraceLevel()>0) SxmlDump(ExplorGenerPagesListStreams);

  SxmlReset(ExplorGenerPagesListStreams);
  while ((nodeStream=SxmlNextNode(ExplorGenerPagesListStreams)))
    {
      SxmlNode *paramNode1;
      SxmlNode *streamNode1;
      SxmlNode *nodeStream2;
      if((paramNode1=SxmlGetFirstChildByTagName(nodeStream,"param"))
	 && (streamNode1=SxmlGetFirstChildByTagName(paramNode1,"stream")))
	{
	  while(streamNode1)
	    {
	      char *type;
	      ExplorGenerPagesNumberOfStreams++;
	      type= SxmlLeafText(streamNode1);
	      SxmlInsertBefore(nodeStream, nodeStream2=SxmlElementCreate("stream"));
	      SxmlSetAttribute(nodeStream2,"type", type);
	      getTypeFromTable(nodeStream2, type);
	      streamNode1=SxmlGetNextSiblingByTagName(streamNode1,"stream");
	    }
	}
    }

  if  (DilibGetTraceLevel()>0) SxmlDump(ExplorGenerPagesListStreams);

  ExplorGenerPagesWikiMain();

  if  (ExplorGenerPagesNumberOfStreams ==1)
    {
      SxmlNode *streamNode;
      SxmlNode *wikiNode;
      SxmlNode *unixSizeNode;
      SxmlNode *paramNode;
      printf ("La boîte ci dessous donne accès au serveur d'exploration proprement dit et aux serveurs intermédiaires de curation.\n");
      streamNode=SxmlGetFirstChildByTagName(ExplorGenerPagesListStreams, "stream");
      paramNode=SxmlGetFirstChildByTagName(streamNode, "param");
      wikiNode=SxmlGetFirstChildByTagName(paramNode, "Wiki");
      unixSizeNode=SxmlGetFirstChildByTagName(paramNode, "UnixSize");
      if ((wikiNode)||(unixSizeNode))
	{
	  char *pageName;
	  char *envSizeName;
	  char *size;
	  char *hasSize;
	  if (wikiNode)pageName=SxmlLeafText(wikiNode);
	  else pageName=NULL;
	  if (wikiNode)hasSize=SxmlGetAttribute(wikiNode,"taille");
	  else hasSize=NULL;
	  printf("le corpus contient ");
	  if (unixSizeNode)envSizeName=SxmlLeafText(unixSizeNode);
	  else   envSizeName=NULL;

	  if(hasSize)printf("[[%s::", hasSize);

	  if (envSizeName) 
	    {
	      size=getenv(envSizeName);
	      printf("%s", size);
	    }
	  if(hasSize)printf("]] ");
	  if (pageName)
	    {
	      char *linkName;
	      linkName=SxmlGetAttribute(wikiNode, "page");
	      printf (" [[A pour base utilisée::");
	      if (linkName)printf ("%s|", linkName);
	      printf ("%s]].\n", pageName);
	    }
	}
    }
  ExplorGenerPagesBeginNavi();

  if (ExplorGenerPagesNumberOfStreams >1)
    {
      SxmlNode *streamNode;
      char *bandeauType;
      int i;
      i=1;
      SxmlReset(ExplorGenerPagesListStreams);
      while ((streamNode=SxmlNextNode(ExplorGenerPagesListStreams)))
	{
	  printf("|-\n!{{Explor icon number|n=%d}}", i++);
	  printf("\n|colspan=2|\n");
	  bandeauType=getBandeauCode(streamNode, SxmlGetAttribute(streamNode, "type"), "multi");
	  printf("\n{{Explor bandeau flux|type=%s|code=Main|path=%s}}\n", bandeauType, ExplorGenerPagesAreaCode);
	}
      printf("|-\n!Flux principal\n");
      printf("|\n{{Explor bandeau flux|type=MainMerge|code=Main|path=%s/%s}}\n|\n", ExplorGenerPagesGenericCode, ExplorGenerPagesAreaCode);
      SxmlReset(ExplorGenerPagesListZooms);
      while ((nodeStream=SxmlNextNode(ExplorGenerPagesListZooms)))
	{
	  char *codeZoom;
	  codeZoom= SxmlGetAttribute(nodeStream,"name");
	  printf("|-\n!Zoom\n!%s\n",codeZoom);
	   if (ExplorGenerPagesGenericCode) printf("|\n{{Explor bandeau flux|type=zoom|code=%s|path=%s/%s}}\n", codeZoom, ExplorGenerPagesGenericCode, ExplorGenerPagesAreaCode);
	   else  printf("|\n{{Explor bandeau flux|type=zoom|code=%s|path=%s}}\n", codeZoom, ExplorGenerPagesAreaCode);
	}
    }
  else
    {
      SxmlNode *streamNode;
      char *bandeauType;
      streamNode=SxmlGetFirstChildByTagName(ExplorGenerPagesListStreams, "stream");
      bandeauType=getBandeauCode(streamNode, SxmlGetAttribute(streamNode, "type"), "mono");
      if (ExplorGenerNumberOfZooms>0)
	printf("|colspan=3|");
      else  printf("|");
      printf("\n{{Explor bandeau flux|type=%s|code=Main|path=%s}}\n", bandeauType, ExplorGenerPagesAreaCode);
    }
  ExplorGenerNaviZooms();
  printf("|-\n|}\n");

  ExplorGenerPagesWikiMainEnd();

  ExplorGenerPagesWikiTechBegin();

  printf(";Génération des pages wikis:\n<source lang=\"sh\">\n");
  printf(" ExplorGenerAreaPages \\\n");
  if (ExplorGenerPagesGenericCode) printf ("       -g %s   \\\n", ExplorGenerPagesGenericCode);
  /*  SxmlReset(ExplorGenerPagesListStreams);  */
  printf ("       -a %s   \\\n", ExplorGenerPagesAreaCode);
  if (ExplorGenerPagesMapOption) printf ("       -m   \\\n");
  /*
  printf ("       -w %s  \\\n", ExplorGenerPagesWikiLink);
  printf ("       -W %s   \\\n", ExplorGenerPagesWikiCode);
  */
  ExplorGenerGener(0);

  ExplorGenerPagesInitDirTree(0);

  printf(";Récuparation des corpus:\n");

  SxmlReset(ExplorGenerPagesListStreams);
  while ((nodeStream=SxmlNextNode(ExplorGenerPagesListStreams)))
    {
      generCorpusCommand(nodeStream);
    }
  ExplorGenerPagesPrintGetParGener();

  ExplorGenerPagesWikiTechCompilArea();

  ExplorGenerPagesGenerFtp(1);

  ExplorGenerPagesTargetInit();

  printf(";Transfert par FileZilla:\n");
  printf("Transférer les fichiers Site.tar.gz, Import.tar.gz, Data.tar.gz de %s (émetteur) vers %s (cible).\n", ExplorGenerPagesAreaCode, ExplorGenerPagesAreaCode);
  ExplorGenerPagesTargetInstall();

  ExplorGenerPagesWikiDataBegin();

  if (ExplorGenerPagesNumberOfStreams==1)
    {
      char *dataType;
      SxmlNode *streamNode;

      streamNode=SxmlGetFirstChildByTagName(ExplorGenerPagesListStreams, "stream");
      printf("|-\n|listStreams\n|Area\n|Main");
      ExplorGenerPagesGenerListZooms();
      printf("\n");
      printf("|-\n|typeStream\n|Area/Main\n");
      if ((dataType=getDataCode(streamNode, "mono"))) 
	{
	  printf("|%s\n", dataType);
	}
      /*  getPrintImportFile(streamNode, NULL); */
    }
  else
    {
      printf("|-\n|listStreams\n|Area\n|");
      SxmlNode *streamNode;
      while ((streamNode=SxmlNextNode(ExplorGenerPagesListStreams)))
	{
	  char *streamName;
	  streamName=SxmlGetAttribute(streamNode,"name");
	  printf("%s ;",streamName); 
	}
      printf("Main\n");
      ExplorGenerPagesGenerListZooms();
      printf("|-\n|typeStream\n|Area/Main\n|Explor:MainMerge\n");

      printf("|-\n|mergingList\n|Area/Main\n|");

      SxmlReset(ExplorGenerPagesListStreams);
      while ((streamNode=SxmlNextNode(ExplorGenerPagesListStreams)))
	{
	  char *streamName;
	  streamName=SxmlGetAttribute(streamNode,"name");
	  if (SxmlNextSibling(streamNode))printf("%s ; ",streamName); 
	  else printf("%s\n",streamName); 
	}
      /*
      SxmlReset(ExplorGenerPagesListStreams);
      while ((streamNode=SxmlNextNode(ExplorGenerPagesListStreams)))
	{
	  char *streamName;
	  streamName=SxmlGetAttribute(streamNode,"name");
	  printf("|-\n|typeStream\n|Area/%s\n|\n", streamName);
	  getPrintImportFile(streamNode, NULL);
	}
      */
      
    }
  ExplorGenerPagesGenerListTypesZoom();
  printf("|-\n|}\n");
  ExplorGenerCorpus();

  ExplorGenerPagesWikiDataEnd();

  ExplorGenerPagesPrintGetParNavi();
  printCompilInterface();

  ExplorGenerPagesWikiNaviBegin();

  printf("|-\n|areaRootIntroduction\n|Area\n|");
  printf("<h2>Plateforme %s</h2>\n", ExplorGenerAreaTitle);
  printf("<p>Cette page introduit une application générée par des moyens automatiques. Dans un contexte monobase, cette page n\'est pas significative</p>");
  printf("\n");
  printf("|-\n|streamIntroduction\n|Area/Main\n|");
  printf("<h2>Flux principal %s</h2>\n", ExplorGenerAreaTitle);
  printf( "<p>Cette page introduit une chaine d'étapes générée par des moyens automatiques pour faciliter l\'analyse des contenus</p>");
  printf("\n");
  printf("|-\n|stepIntroduction\n|Area/Main/Exploration\n|");
  printf("<h2>%s</h2>\n", ExplorGenerAreaTitle);
  printf("<p>Cette page introduit un portail d'exploration généré par des moyens automatiques pour faciliter l\'analyse des contenus </p>");
  printf("\n");

  ExplorGenerPagesWikiNaviEnd();

  ExplorGenerPagesPrintGetParNavi();

  printf("Ou (dans les cas plus complexes) :\n");
  printf("<source lang=\"sh\">\n");

  printf("sh $EXPLOR_AREA/bin/AreaResetSite.sh\n");
  printf("sh $EXPLOR_AREA/bin/AreaCreateSite.fr.sh\n");
  printf("</source>\n");

  ExplorGenerPagesMap();

  ExplorGenerPagesWikiTemplateSize();

  ExplorGenerPagesWikiTemplateInclude();

  exit(EXIT_SUCCESS);
}
