/*   -*- coding: utf-8 -*-  */
/******************************************************************************
*
*               Module   : Explor
*               Commande : ExplorParamFromRowCol
*               Fichier  : ExplorParamFromRowCol.c
*
******************************************************************************/
#include "SxmlNode.h"
#include "Buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "StrDict.h"
#include "MediaWiki.h"

MediaWikiParser *wikiParser;

      SxmlNode *c1;
      SxmlNode *c2;
      SxmlNode *c3;

int getopt();
extern char *optarg;

void usage()
{
  perror ("usage: ExplorParamFromRowCol [-w]\n");
  exit(1);
}

char *testSource(char *line)  /* looks if first line contains <source and returns lang */
{
  static Buffer* bLang=NULL;
  char *source;
  SxmlNode *firstLine;

  char *posLang;
  char *dq1;
  char *dq2;
  firstLine=SxmlFirstChild(c3);
  line=SxmlLeafText(firstLine);
  if (!line) return NULL;
  source=strstr(line, "&lt;source");
  if(!source) return NULL;
  if (!bLang) bLang=NewBuffer();
  posLang=strstr(source, "lang");
  if (!posLang) return NULL;
  dq1=strchr(posLang,'\"');
  if (!dq1) return NULL;
  dq2=strchr(dq1+1,'\"');
  if (!dq2) return NULL;
  BufferStrncpy(bLang, dq1+1, dq2-dq1-1);
  return (BufferString(bLang));
}

void proceedPhp()
{
  SxmlNode *firstLine;
  SxmlNode *currentLine;
  char *line;
  char *lang;

  firstLine=SxmlFirstChild(c3);
  line=SxmlLeafText(firstLine);
  lang=testSource(line);
  if ((!lang)||(strcmp(lang,"php")!=0))
    {
      printf("%s\n", line);
      return;
    }
  printf("<php>");
  currentLine=SxmlNextSibling(firstLine);
  while(currentLine)
    {
      int i;
      unsigned char c;
      line=SxmlLeafText(currentLine);
      if(!line)
	{
	  printf("<l></l>");
	  currentLine=SxmlNextSibling(currentLine);
	  continue;
	}
      if (strstr(line, "&lt;/source")) printf("</php>\n");
      else
	{
	  printf("<l>");
	  i=0;
	  while ((c=line[i++]))
	    {
	      switch(c)
		{
		case '\'':
		  printf("&quot;");
		  break;
		case '\"':
		  printf("&dquot;");
		  break;
		default:
		  putchar(c);
		  break;
		}
	    }
	  printf("</l>");
	}
      currentLine=SxmlNextSibling(currentLine);
    }
}


int main(int argc, char **argv)
{
  SxmlNode *inputRow;
  Buffer    *bufFile;
  Buffer    *bufParam;
  StrDict   *dictType;
  int       optCode;
  int       warningMode; 

  bufFile=BufferFromString(getenv("DILIB"));
  bufParam=NewBuffer();
  BufferStrcat (bufFile, "/data/Explor/ListParamTypes.tab");
  dictType=StrDictFromFile(BufferString(bufFile));
  wikiParser=NewMediaWikiParser();
  warningMode=0;

  while((optCode=getopt(argc,argv,"w"))!=EOF)
    {
      switch (optCode)
	{
	case 'w':
	  warningMode=1;
	  break;
	default:
	  usage();
	}
    }

  while  ((inputRow=SxmlInputNextDocumentElement()))
    {
      char *type;
      char *param;
      char *posSlash;
      char *posArobase;
      char *paramRoot;

      c1=SxmlGetFirstChildByTagName(inputRow,"c1");
      c2=SxmlGetFirstChildByTagName(inputRow,"c2");
      c3=SxmlGetFirstChildByTagName(inputRow,"c3");
      param=SxmlLeafText(SxmlFirstChild(c1));
      if ((posSlash=strchr(param,'/')))
	{
	  BufferStrncpy(bufParam, param, posSlash-param);
	  BufferStrcat(bufParam, "/*");
	  paramRoot=BufferString(bufParam);
	}
      else if ((posArobase=strchr(param,'@')))
	{
	  BufferStrncpy(bufParam, param, posArobase-param);
	  BufferStrcat(bufParam, "@");
	  paramRoot=BufferString(bufParam);
	}
      else paramRoot=param;

      printf("%s/%s\t", SxmlLeafText(SxmlFirstChild(c2)), param);
      if ((type=StrDictSearch(dictType, paramRoot)))
	{
	  if (strcmp(type, "term")==0) 
	    {
	      printf("%s\n", SxmlLeafText(SxmlFirstChild(c3)));
	      continue;
	    }
	  if (strcmp(type, "shell")==0)
	    {
	      char *command;
	      int i;
	      unsigned char c;
	      command=SxmlLeafText(SxmlFirstChild(c3));
	      i=0;
	      while ((c=command[i++]))
		{
		  if (c=='&')
		    {
		      if (strncmp(command+i, "lt;nowiki&gt;", 13)==0) i+=13;
		      else if (strncmp(command+i, "lt;/nowiki&gt;", 14)==0) i+=14;
		      else if (strncmp(command+i, "lt;", 3)==0) {i+=3; putchar ('<');}
		      else if (strncmp(command+i, "gt;", 3)==0) {i+=3; putchar ('>');}
		      else if (strncmp(command+i, "amp;", 4)==0) {i+=4; putchar ('&');}
                      else putchar(c);
		    }
		  else putchar(c);
		}
	      putchar('\n');
	      continue;
	    }
	  if (strcmp(type, "php")==0)
	    {
	      proceedPhp();
	      continue;
	    }
	  if (strcmp(type, "list")==0) 
	    {
	      SxmlPrint(MediaWikiSxmlFromListText(wikiParser, c3));
	      putchar('\n');
	      continue;
	    }
	  if (strcmp(type, "wiki")==0) 
	    {
	      SxmlPrint(MediaWikiSxmlFromWicriCell(wikiParser, c3));
	      putchar('\n');
	      continue;
	    }
	  printf("%s<unk>%s</unk>\n", type, SxmlLeafText(SxmlFirstChild(c3)));
	  continue;
	}
      else 
	{
	  if (warningMode==1)   fprintf(stderr,"**** unknowed type for : %s\n", paramRoot);
	  else printf("%s\n", SxmlLeafText(SxmlFirstChild(c3)));
	}
    }
   exit (EXIT_SUCCESS);
}
