#include <stdio.h>
#include "struc/starpasr.h"
#include "struc/datablk.h"
#include "struc/pasrmain.h"
#include "struc/iterator.h"

void truncateSpace(char*);

main(argc, argv)
int argc;
char** argv;
{

  FILE * outFile;
  int printAck=1;
  id data, iter, iter1, iter2, iter3, iter4, current;
  char *file, *dataname, *itemname, outFileName[50], *currentLine;
  char *prevLine1=0, *prevLine2=0, *prevLine3=0;
  starParser=[WDCStarParser alloc];

  if (argc==2) file=*(argv+1);
  else {
    printf("usage: %s filename\n", argv[0]);
    exit(1);
  }
  [starParser parseStarFile:file];
  strcpy(outFileName, file);
  strcat(outFileName, ".html");
  outFile=fopen(outFileName, "w");
  data=[[starParser getStarFileNamed:file]  getDataItem : "data_1"]; 
  outFileName[strlen(outFileName)-9]='\0';
  fprintf(outFile, "<title>%s</title>\n", outFileName);
  fprintf(outFile, "<h2>");
  iter=[[WDCListIterator alloc] initFrom : 
	[[data getDataItem : "_ACA_abstract_title"] getValue]];
  while ([iter goNext]) {
    currentLine=(char*)[[iter getCurrentObject] cStringValue];
    truncateSpace(currentLine);
    if (currentLine[0]!=';') fprintf(outFile, "%s\n", currentLine);
  }
  [iter free];
  fprintf(outFile, "</h2><br>\n");
  iter=[data getAllDataItems : "_ACA_author_name"];
  iter1=[data getAllDataItems : "_ACA_author_institution"];
  iter2=[data getAllDataItems : "_ACA_author_state"];
  iter3=[data getAllDataItems : "_ACA_author_country"];
  if (iter){
    iter=[[WDCListIterator alloc] initFrom: iter];
    iter1=[[WDCListIterator alloc] initFrom: iter1];
    iter2=[[WDCListIterator alloc] initFrom: iter2];
    iter3=[[WDCListIterator alloc] initFrom: iter3];
    while ([iter goNext]) {
      [iter1 goNext];
      [iter2 goNext];
      [iter3 goNext];

       // _ACA_author_name

      currentLine=(char*)[[[[iter getCurrentObject]
			    getContent] getValue] cStringValue];
      if (currentLine[0] == '\'') {
	currentLine[strlen(currentLine)-1]='\0';
	fprintf(outFile, "%s<br>", &currentLine[1]);
      }
      else fprintf(outFile, "%s<br>", currentLine);

      // _ACA_author_institution

      currentLine=(char*)[[[[iter1 getCurrentObject]
			    getContent] getValue] cStringValue];
      if (currentLine[0]!='.' && currentLine[1]!='.') {
	if (currentLine[0] == '\'') {
	  currentLine[strlen(currentLine)-1]='\0';
	  fprintf(outFile, "%s, ", &currentLine[1]);
	  prevLine1=&currentLine[1];
	}
	else {
	  fprintf(outFile, "%s, ", currentLine);
	  prevLine1=currentLine;
	}
      }
      else if (prevLine1 && prevLine1[0]!='.') 
	fprintf(outFile, "%s, ", prevLine1);
      
      // _ACA_author_state
      
      currentLine=(char*)[[[[iter2 getCurrentObject]
			    getContent] getValue] cStringValue];
      if (currentLine[0]!='.' && currentLine[1]!='.') {
	if (currentLine[0] == '\'') {
	  currentLine[strlen(currentLine)-1]='\0';
	  fprintf(outFile, "%s, ", &currentLine[1]);
	  prevLine2=&currentLine[1];
	}
	else {
	  fprintf(outFile, "%s, ", currentLine);
	  prevLine2=currentLine;
	}
      }
      else if (prevLine2 && prevLine2[0]!='.') 
	fprintf(outFile, "%s, ", prevLine2);
      
      // _ACA_author_institution 
      
      currentLine=(char*)[[[[iter3 getCurrentObject]
			    getContent] getValue] cStringValue];
      if (currentLine[0]!='.' && currentLine[1]!='.') {
	if (currentLine[0] == '\'') {
	  currentLine[strlen(currentLine)-1]='\0';
	  fprintf(outFile, "%s<br><br>\n", &currentLine[1]);
	  prevLine3=&currentLine[1];
	}
	else {
	  fprintf(outFile, "%s<br><br>\n", currentLine);
	  prevLine3=currentLine;
	}
      }
      else if (prevLine3 && prevLine3[0]!='.') 
	fprintf(outFile, "%s<br><br>\n", prevLine3);  
    }
    [iter free];
    [iter1 free];
    [iter2 free];
    [iter3 free];
  }
  fprintf(outFile, "<hr>\n");
  iter=[[WDCListIterator alloc] initFrom :
	[[data getDataItem : "_ACA_abstract_text"] getValue]];
  while ([iter goNext]) {
    currentLine=(char*)[[iter getCurrentObject] cStringValue];
    truncateSpace(currentLine);
    if (currentLine[0]!=';'){
      if (strlen(currentLine)) fprintf(outFile, "%s\n", currentLine);
      else fprintf(outFile, "<p>\n");
    }
  }
  iter=[[data getDataItem : "_ACA_acknowledgements"] getValue];
  if (iter && (![iter isMemberOfClassNamed:"WDCString"] ||  
	       (!strcmp([iter cStringValue], "?") && 
		!strcmp([iter cStringValue], ".")))){
    iter=[[WDCListIterator alloc] initFrom :iter];
    while ([iter goNext]) {
      currentLine=(char*)[[iter getCurrentObject] cStringValue];
      truncateSpace(currentLine);
      if (strlen(currentLine) && currentLine[0]!=';' && 
	  currentLine[0]!='.' && currentLine[0]!='?' ){
	if (printAck) {
	  fprintf(outFile, "<p><B>%s</B><p>\n", "Acknowledgements:");
	  printAck=0;
	}
	if (strlen(currentLine)) fprintf(outFile, "%s\n", currentLine);
	// else fprintf(outFile, "\n");
      }
    }
    [iter free];
  }
  fprintf(outFile, "</body></html>\n");
  fclose(outFile);
  return 0;
}


void truncateSpace(char* str){
  char* tail;
  for (tail=&str[strlen(str)-1]; (tail[0]==' ' && tail!=str); tail--);
  if (tail[0] != ' ') tail++;
  tail[0]='\0';
}




