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

#include "Explor.h"

int ExplorCurationRecordOperations(char *stream, char *step)
{
  char *curationSortUniqIdno;
  char *curationRejectIdnoRBID;
  char *curationKeepIdnoRBID;
  char *curationRejectIdnoISSN;
  char *curationRecordCutXpath;
  char *curationRejectKeywords;
  char *curationFileAddKeywords;
  char *curationFilter;

  curationSortUniqIdno=ExplorGetFromDictK4("Area", stream, step,  "sortUniqIdno");
  curationRejectIdnoRBID=ExplorGetFromDictK4("Area", stream, step,  "rejectIdnoRBID");
  curationKeepIdnoRBID=ExplorGetFromDictK4("Area", stream, step,  "keepIdnoRBID");
  curationRejectIdnoISSN=ExplorGetFromDictK4("Area", stream, step,  "rejectIdnoISSN");
  curationRecordCutXpath=ExplorGetFromDictK4("Area", stream, step,  "curationRecordCutXpath");
  curationRejectKeywords=ExplorGetFromDictK4("Area", stream, step,  "rejectKeywords");
  curationFileAddKeywords=ExplorGetFromDictK4("Area", stream, step,  "fileAddKeywords");
  curationFilter=ExplorGetFromDictK4("Area", stream, step,  "curationFilter");

  if(curationRecordCutXpath)
    {
      printf ("   | SxmlCut %s  \\\n", curationRecordCutXpath);
    }

  if(curationFilter)
    {
      printf ("   | %s  \\\n", curationFilter);
    }
  
  if(curationSortUniqIdno)
    {
      printf ("   | SgmlSelect -g record/TEI/teiHeader/fileDesc/publicationStmt/idno@type=%s# -p @g1 -p @1  \\\n", curationSortUniqIdno);
      printf ("   | sort | BufferSelectFirstByKey      \\\n");
    }

  if (curationRejectIdnoRBID)
    {
      SxmlNode *listReject;
      SxmlNode *nodeReject;
      listReject = SxmlFromString(curationRejectIdnoRBID);
      printf ("   | TeiRejectOnIdno -T RBID -i ");
      while ((nodeReject=SxmlNextNode(listReject)))
	{
	  printf ("%s ", SxmlLeafText(nodeReject));
	}
      printf (" \\\n");
    }

  if (curationRejectIdnoISSN)
    {
      SxmlNode *listReject;
      SxmlNode *nodeReject;
      listReject = SxmlFromString(curationRejectIdnoISSN);
      printf ("   | TeiRejectOnIdno -w ISSN -i "); 
      while ((nodeReject=SxmlNextNode(listReject)))
	{
	  printf ("%s ", SxmlLeafText(nodeReject));
	}
      if (curationKeepIdnoRBID)
	{
	  SxmlNode *listKeep;
	  SxmlNode *nodeKeep;
	  listKeep = SxmlFromString(curationKeepIdnoRBID);
	  printf (" -k "); 
	  while ((nodeKeep=SxmlNextNode(listKeep)))
	    {
	      printf ("%s ", SxmlLeafText(nodeKeep));
	    }
	}
      printf (" \\\n");
    }

  if (curationRejectKeywords)
    {
      SxmlNode *listReject;
      SxmlNode *nodeReject;
      listReject = SxmlFromString(curationRejectKeywords);
      printf ("   | TeiRejectOnKeywords -i ");
      while ((nodeReject=SxmlNextNode(listReject)))
	{
	  printf ("\"%s\" ", SxmlLeafText(nodeReject));
	}
      printf (" \\\n");
    }

  if (curationFileAddKeywords)
    {
            SxmlNode *listFiles;
      SxmlNode *nodeFile;
      listFiles = SxmlFromString(curationFileAddKeywords);
      printf ("   | TeiAddKeywordsTermsFromTableRbid ");
      while ((nodeFile=SxmlNextNode(listFiles)))
	{
	  printf (" -T %s ", SxmlLeafText(nodeFile));
	}
      printf (" \\\n");
    }

  /*printf ("   | TeiPutRefToIdno -t wicri:Area/%s/%s  \\\n", stream, step );  must be moved in calling proc */
  return 1;
}

/* int ExplorGenerShellCreateStep(char *NS, char*stream, char *step) */

void ExplorExplorationCommonOperations(char *stream, char *step, int curationPers)
{  
  if (curationPers!=0)
    {
      char *curationOption;
      curationOption=ExplorGetFromDictK4("Area", stream, step, "persCurationOption");
      printf ("   | TeiCurationPers -p $DILIB/data/Wicri/pers.dict -o $DILIB/data/Wicri/org.dict");
      if (curationOption) printf(" -%s ",curationOption);
      printf ("  \\\n");
    }
  printf ("   | SxmlSelect -g record/TEI/teiHeader/fileDesc/publicationStmt/date/1 -p @g1 -p @2 \\\n");
  printf ("   | sort -rn \\\n");
  printf ("   | SgmlFast -c 1  \\\n");
  /* printf ("   | TeiPutRefToIdno -t wicri:Area/%s/%s  \\\n", stream, step ); */
}

void ExplorGenerMakeStep(char*stream, char *step) 
{
  SxmlNode *stepListIndex;
  SxmlNode *stepListAssoc;
  SxmlNode *stepListCross;
  SxmlNode *stepListCluster;
  SxmlNode *stepListFiles;
  SxmlNode *stepTreeNode;
  SxmlNode *indexNode;

  stepTreeNode=ExplorAreaTreeNode(stream, step, NULL);
  printf("<?dilib appendFile=\"%s/bin/area.mk\" ?>\n", ExplorAreaDir );
  printf("# This part is generated by ExplorDataShellTools / ExplorGenerMakeStep\n" );
  printf("$(EXPLOR_AREA)/Make/%s/%s.step: ", stream, step);
  printf("\\\n                  $(EXPLOR_AREA)/Make/%s/%s/biblio ", stream, step);
  SxmlReset(stepTreeNode);
  while ((indexNode=SxmlNextNode(stepTreeNode))&&SxmlNodeHasName(indexNode,"index"))
    {
      char *indexCode;
      indexCode=SxmlGetAttribute(indexNode, "code");
      printf("\\\n                $(EXPLOR_AREA)/Make/%s/%s/%s.index ", stream, step, indexCode);
    }
  if((stepListAssoc=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listAssoc"))))
    {
      SxmlReset (stepListAssoc);
      while ((indexNode=SxmlNextNode(stepListAssoc)))
	{
	  printf("\\\n                $(EXPLOR_AREA)/Make/%s/%s/%s.assoc ", stream, step, SxmlLeafText(indexNode));
	}
    }
  if((stepListCross=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listCross"))))
    {
      SxmlReset (stepListCross);
      while ((indexNode=SxmlNextNode(stepListCross)))
	{
	  /* printf("\\\n                 %s/Make/%s/%s/%s.cross ", ExplorAreaDir, stream, step, SxmlLeafText(indexNode)); */
	  printf("\\\n             $(EXPLOR_AREA)/Make/%s/%s/%s.cross ", stream, step, SxmlLeafText(indexNode)); 
	}
    }
  if((stepListCluster=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listCluster"))))
    {
      SxmlNode *clusterNode;
      SxmlReset (stepListCluster);
      while ((clusterNode=SxmlNextNode(stepListCluster)))
	{
	  /* printf("\\\n                  %s/Make/%s/%s/%s.cluster ", ExplorAreaDir, stream, step, SxmlLeafText(clusterNode));*/
	    printf("\\\n                  $(EXPLOR_AREA)/Make/%s/%s/%s.cluster ", stream, step, SxmlLeafText(clusterNode));
	}
    }
  printf("\n\t touch $(EXPLOR_AREA)/Make/%s/%s.step \n\n", stream, step);
}

void ExplorGenerShellCreateStep(char*stream, char *step) 
{
  SxmlNode *stepListIndex;
  SxmlNode *stepListAssoc;
  SxmlNode *stepListCross;
  SxmlNode *stepListCluster;
  SxmlNode *indexNode;
  printf("<?dilib file=\"%s/bin/%sCreate%s.sh\" ?>\n", ExplorAreaDir, stream, step );
  printf ("#!/bin/sh\n");
  printf ("# This file is generated by Dilib / Explor / ExplorDataShellTools \n");
  printf ("\n");
  printf ("echo \"---- Executing sh %s/bin/%sCreate%s.sh\" \n", ExplorAreaDir, stream, step );
  printf("sh %s/bin/%s%sBiblio.sh \n", ExplorAreaDir, stream, step );
  if((stepListIndex=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listIndexes"))))
    {
      SxmlReset (stepListIndex);
      while ((indexNode=SxmlNextNode(stepListIndex)))
	{
	  printf("sh %s/bin/%s%sIndex%s.sh\n", ExplorAreaDir, stream, step, SxmlLeafText(indexNode));
	}
    }
  if((stepListAssoc=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listAssoc"))))
    {
      SxmlReset (stepListAssoc);
      while ((indexNode=SxmlNextNode(stepListAssoc)))
	{
	  printf("sh %s/bin/%s%sAssoc%s.sh\n", ExplorAreaDir, stream, step, SxmlLeafText(indexNode));
	}
    }
  if((stepListCross=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listCross"))))
    {
      SxmlReset (stepListCross);
      while ((indexNode=SxmlNextNode(stepListCross)))
	{
	  printf("sh %s/bin/%s%sCross%s.sh\n", ExplorAreaDir, stream, step, SxmlLeafText(indexNode));
	}
    }
  if((stepListCluster=SxmlFromString(ExplorGetFromDictK4("Area",stream, step, "listCluster"))))
    {
      SxmlNode *clusterNode;
      SxmlReset (stepListCluster);
      while ((clusterNode=SxmlNextNode(stepListCluster)))
	{
	  printf("sh %s/bin/%s%sCluster%s.sh\n", ExplorAreaDir, stream, step, SxmlLeafText(clusterNode));
	}
    }
  ExplorGenerMakeStep(stream, step) ;
}

void ExplorGenerShellIndexBegin(char *stream, char *step, char *index, char *typeIndex)
{
  printf("<?dilib file=\"%s/bin/%s%sIndex%s.sh\" ?>\n", ExplorAreaDir, stream, step, index );
  printf ("#!/bin/sh\n");
  printf ("#  This shell is generated by ExplorDataShellTools / ExplorGenerShellIndex%s\n", typeIndex);
  printf ("\n");
  printf ("echo \"---- Executing sh %s/bin/%s%sIndex%s.sh\" \n", ExplorAreaDir, stream, step, index ); 
  printf ("export LC_ALL=C \n");
  printf ("\n");
  ExplorGenerDeleteHfd(stream, step, index);
  /* printf ("HfdCat %s/Data/%s/%s/biblio.hfd  \\\n", ExplorAreaDir, stream, step); */
  printf ("HfdCat $EXPLOR_AREA/Data/%s/%s/biblio.hfd  \\\n", stream, step); 
}

void ExplorGenerShellIndexHidSort(char*stream, char *step, char *index, int type)
{
  switch(type)
    {
    case 'i':
      printf ("IndexBuildHid -h  $EXPLOR_AREA/Data/%s/%s/%s -e kw  \n\n", stream, step, index);
      break;
    case 'g':
      printf ("IndexBuildHid -h  $EXPLOR_AREA/Data/%s/%s/%s -e k  \n\n", stream, step, index);
      break;
    }

  printf ("HfdCat $EXPLOR_AREA/Data/%s/%s/%s.hfd   \\\n", stream, step, index);
  switch(type)
    {
    case 'i':
      printf ("     | SgmlFast -c l  \\\n");
      printf ("     | SxmlSelect -g idx/f/1 -p @g1 -p @2  \\\n");
      break;
    case 'g':
      printf ("      |  SxmlSelect -g g/t/1 -g g/k/1 -p @g1 -p \"<idx><kw>@g2</kw><f>@g1</f></idx>\"     \\\n");
      break;
    }
  printf ("     | sort -rn  \\\n");
  printf ("     | SgmlFast -c 1  \\\n");
  printf ("     | head -500 > $EXPLOR_AREA/Data/%s/%s/%s.sort  \n\n", stream, step, index);

  printf (" echo \"==== \" %s, step %s, index %s done\n", stream, step, index);
  printf (" HfdCat $EXPLOR_AREA/Data/%s/%s/%s.hfd | wc \n", stream, step, index);

}

void ExplorGenerShellIndexMakeRules(char *stream, char *step, char *index, char *indexRule)
{
  printf("<?dilib appendFile=\"%s/bin/area.mk\" ?>\n", ExplorAreaDir );
  printf("# This part is generated by ExplorDataShellsTools /ExplorGenerShellIndexMakeRules \n");
  printf("$(EXPLOR_AREA)/Make/%s/%s/%s.index:  \\\n ", stream, step, index);
  if (indexRule) printf("                %s \\\n", indexRule);
  printf("                $(EXPLOR_AREA)/Make/%s/%s/biblio \n", stream, step);
  printf("\tsh $(EXPLOR_AREA)/bin/%s%sIndex%s.sh \n", stream, step, index );
  printf("\ttouch $(EXPLOR_AREA)/Make/%s/%s/%s.index \n\n", stream, step, index);
}

void ExplorGenerShellIndexEnd(char *stream, char *step, char *index, char *path, char *indexRule)
{
  printf ("     | sort -u  \\\n");
  printf ("     | IndexFastBuildRec  \\\n");
  printf ("     | HfdBuild -h $EXPLOR_AREA/Data/%s/%s/%s \n\n", stream, step, index);


  if (path) printf ("IndexBuildHid -h  $EXPLOR_AREA/Data/%s/%s/%s -p %s -e kw  \n\n", stream, step, index, path);
  else printf ("IndexBuildHid -h  $EXPLOR_AREA/Data/%s/%s/%s -e kw  \n\n", stream, step, index);
  printf ("HfdCat $EXPLOR_AREA/Data/%s/%s/%s.hfd   \\\n", stream, step, index);
  printf ("     | SgmlFast -c l  \\\n");
  printf ("     |  SxmlSelect -g idx/f/1 -p @g1 -p @2  \\\n");
  printf ("     | sort -rn  \\\n");
  printf ("     | SgmlFast -c 1  \\\n");
  printf ("     | head -500 > $EXPLOR_AREA/Data/%s/%s/%s.sort  \n\n", stream, step, index);
  printf (" echo \"==== \" %s, step %s, index %s done\n", stream, step, index);
  printf (" HfdCat $EXPLOR_AREA/Data/%s/%s/%s.hfd | wc \n", stream, step, index);
  printf("<?dilib appendFile=\"%s/bin/area.mk\" ?>\n", ExplorAreaDir );
  printf("# This part is generated by ExplorDataShellsTools /ExplorGenerShellIndexPath \n");
  printf("$(EXPLOR_AREA)/Make/%s/%s/%s.index:  \\\n ", ExplorStreamCode, step, index);
  if (indexRule) printf("                %s \\\n", indexRule);
  printf("                $(EXPLOR_AREA)/Make/%s/%s/biblio \n", ExplorStreamCode, step);
  printf("\tsh $(EXPLOR_AREA)/bin/%s%sIndex%s.sh \n", ExplorStreamCode, step, index );
  printf("\ttouch $(EXPLOR_AREA)/Make/%s/%s/%s.index \n\n", ExplorStreamCode, step, index);
}

void ExplorGenerShellIndexShell(char *stream, char *step, char *index, SxmlNode *shell)
{
  SxmlNode *lineNode;
  ExplorGenerShellIndexBegin(stream, step, index, "shell");
  lineNode=SxmlNextSibling(SxmlFirstChild(shell));
  while (lineNode)
    {
      char *lineStr;
      lineStr=SxmlLeafText(lineNode);
      printf (" | %s  \\\n", lineStr);
      lineNode=SxmlNextSibling(lineNode);      
    }
  ExplorGenerShellIndexEnd(stream, step, index, NULL, NULL);
}

void ExplorGenerShellIndexMake(char *stream, char *step, char *index, SxmlNode *shell)
{
  SxmlNode *lineNode;
  char *makeRule;
  SxmlNode *makeNode;
  char *lineStr;
  ExplorGenerShellIndexBegin(stream, step, index, "shell");
  lineNode=SxmlNextSibling(SxmlFirstChild(shell));
  lineStr=SxmlLeafText(lineNode);
  printf (" | %s  \\\n", lineStr);
  if ((makeNode=SxmlNextSibling(lineNode)))
    {
      char *makeStr;
      FILE *fMake;
      static Buffer *bufMake=NULL;

      if (!bufMake)bufMake=NewBuffer();
      makeStr=SxmlLeafText(makeNode);
      ExplorGenerShellIndexEnd(stream, step, index, NULL, lineStr);
      fMake=fopen(makeStr, "r");
      printf("<?dilib appendFile=\"%s/bin/area.mk\" ?>\n", ExplorAreaDir );
      while ((BufferFgets(bufMake, fMake)))
	{
	  printf("%s\n", BufferString(bufMake));
	}
      fclose(fMake);
    }
  else ExplorGenerShellIndexEnd(stream, step, index, NULL, NULL);
}

void ExplorGenerShellIndexSxPath(char *stream, char *step, char *index, char *path)
{
  ExplorGenerShellIndexBegin(stream, step, index, "Path");

  printf ("     | SxmlSelect -s %s -p @s1 -p @1  \\\n", path);

  ExplorGenerShellIndexEnd(stream, step, index, path, NULL);
}

void ExplorGenerShellIndexPath(char *stream, char *step, char *index, char *path)
{
  ExplorGenerShellIndexBegin(stream, step, index, "Path");

  printf ("     | SgmlSelect -s %s -p @S -p @1  \\\n", path);

  ExplorGenerShellIndexEnd(stream, step, index, path, NULL);
}

int ExplorGenerDeleteHfd(char *stream, char *step, char *root)
{
  printf ("if test -e  $EXPLOR_AREA/Data/%s/%s/%s.hcs\n", stream , step, root);
  printf ("then\n");
  printf ("echo \"----- Executing -rf  $EXPLOR_AREA/Data/%s/%s/%s.*\"\n", stream, step, root);
  printf ("rm -rf  $EXPLOR_AREA/Data/%s/%s/%s.*\n", stream, step, root);
  printf ("fi\n");
  return 1;
}

void ExplorGenerBuildBiblio(char *stream, char *step, char *corpus)
{
  if (strcmp(step, "Corpus")==0)
    {
      printf ("   | TeiPutRefToIdno -t wicri:Area/%s/%s -S %s -s %s -c %s\\\n", stream, step, stream, step, corpus );
    }
  else
    {
      printf ("   | TeiPutRefToIdno -t wicri:Area/%s/%s -S %s -s %s \\\n", stream, step, stream, step );
     }
  printf ("   | HfdBuild -h $EXPLOR_AREA/Data/%s/%s/biblio\n\n", stream, step);

  printf (" echo \"==== \" %s, step %s, biblio done\n", stream, step);
  printf (" HfdCat $EXPLOR_AREA/Data/%s/%s/biblio.hfd | wc \n", stream, step);
}

int ExplorGenerBiblioFilter(char *stream, char *step)
{
  char *extractionFilter;
  extractionFilter=ExplorGetFromDictK4("Area", stream, step, "extractionFilter");
  if (extractionFilter) printf ("   | %s     \\\n", extractionFilter);
  /* printf ("   | SgmlFast -c 1  \\\n"); */
  return 1;
}
