#include <stdio.h>   /* program for conferting Ventura fmt to ascii */
FILE *fopen(),*infile,*outfile;
#define FALSE 0
#define TRUE 1
main(argc,argv)
int argc;
char *argv[];
{
char *line,*cp,*code,*end,*outline,*type,inname[20],outname[20];
int course,y,i,len,subjtext,ignore_cr,x;

if (argc != 2) {
  printf("wrong # of args\n");
  exit(1);
}

line = (char *) malloc(100);
outline = (char *) malloc(100);
type = (char *) malloc(20);
code = (char *) malloc(10);

          sprintf(inname,"%s",argv[1]);
	  sprintf(outname,"%s.txt",inname);
printf("%s\n",outname);
printf("%s\n",inname);
	  infile = fopen(inname,"r");
if (!infile) {
  printf("cant open file");
  exit(1);}
          outfile = fopen(outname,"w");
subjtext = FALSE;
ignore_cr = FALSE;
while (fgets(line,100,infile) != NULL)
  {
    if (strlen(line) <= 2)
      continue;
    for (cp = line;*cp != '\0'; cp++) {
    if (*cp == '<') {
      *cp = '\0';
      cp++;
      end = (char *) index(cp,'>');
      len = end -  cp + 1;
      *end = '\0';
      strncpy(code,cp,len);
      cp = end++;
      if ((*code == 'R') && (*(cp+1) != '\n'))
	sprintf(outline,"%s\n",outline);
      if ((*code == 'I') && (subjtext == TRUE))
	ignore_cr = TRUE;
      else if (strncmp(code,"197",3)==0)
	sprintf(outline,"%s--",outline);
      else if (strncmp(code,"169",3)==0)
	sprintf(outline,"%s\"",outline);
    } /* end of if code */
if ((*cp == '\n') && (ignore_cr == TRUE))
  continue;

if (*cp == '@') /* skip it */
  {
    subjtext = FALSE;
    ignore_cr = FALSE;
  strncpy(type,cp,9);
    *(type+9) = '\0';
    if (strncmp(type,"@SUBJTEXT",7) == 0){
      fprintf(outfile,"%s\n","--------------------------------------------------------");
      subjtext = TRUE;}
    if (strncmp(type,"@SUBJINFO",7) == 0)
      fprintf(outfile,"%s\n","--------------------------------------------------------");
    if (strncmp(type,"@SECT_HEAD",7) == 0)
      fprintf(outfile,"\n%s\n","==========================================================================");
    if ((strncmp(type,"@SUBTITLE",8) == 0)||
      (strncmp(type,"@SUBNOSPTITLE",8) == 0))
      fprintf(outfile,"\n\n");
    cp = (char *) index(cp,'=');
    if (cp)
      cp = cp + 2;
    else
      break;
/*     cp = (char *) (index(cp,'=') + 2); */
    if (strncmp(type,"@END_NOTE",7) == 0) {
      fprintf(outfile,"\n");
       cp = cp + 2; }

}
sprintf(outline,"%s%c",outline,*cp);
  } 
fprintf(outfile,"%s",outline);
*outline = '\0';
} /* end of while */
fclose(infile);
fclose(outfile);

}	

