/* Gguide2txt
 *
 * Auto: sc >ERR:<file>.err <path><file>
 *
 * Reads in an Amigaguide file. Strips out all links, controls and linefeeds
 * Paginates the resulting text.
 *
 * Arguments:
 *   First argument is to be the input (guide) path/filename
 *   2nd argument to be the output path/filename.
 *
 * Optional 3rd argument
 *     <textlength> = number of lines of text wanted per page (defaults to 63)
 *                       (Use n0 if no formfeeds wanted)
 *
 * Based on Gstrip by Gnome
 *
 * Author: Gnome  31 Jan 95
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

#include <stdio.h>
#include <stdlib.h>
#include <exec/types.h>
#include <string.h>
#include <pragmas/dos_pragmas.h>

#define AT       '@'
#define QUOTE    '\"'
#define BRA      '{'
#define KET      '}'

#define VER    "01.01"

const char version[] = "\0$VER: Gguide2txt "VER" ("__DATE__") "__TIME__" by Gnome";

extern DOSBase;

BOOL DeleteFile(STRPTR);

main(argc, argv)
int argc;
char *argv[];

{
  FILE *Inhandle, *Outhandle;   /* file pointers  */
  int i, j, linecount, textlength;
  char work[4];
  char infile[80];
  char outfile[80];
  char *result;
  char buffer[600], buff2[600];
  BOOL dunnit, endline, formfeeds, endlink;

  linecount=0; textlength=63;
  dunnit=endline=FALSE;
  formfeeds=TRUE;

  if (*argv[1] == '?' || argc<3)
  {
    printf("\nGguide2txt Vn%s   by Gnome\n\n",VER);
    printf("Enter the path/inputfile path/outputfile names:\n");
    printf(" Optional 3rd argument <lines-per-page>\n");
    printf("     (63 by default. 0 if formfeeds not wanted.\n\n");
    printf("e.g. %s df0:myguide ram:mytext 66 \n\n",argv[0]);
    return(0);
  }

  strcpy(infile,argv[1]);
  strcpy(outfile,argv[2]);

  if (argc>3)
  {
    strcpy(work,argv[3]);
    i=atoi(work);
    if (i==0) formfeeds=FALSE;
      else textlength=i;
  }

  Inhandle = fopen(infile,"r");

  if (Inhandle==0)
  {
    printf("Could not open %s for reading.\n",argv[1]);
    exit (20);
  }

  Outhandle = fopen(outfile,"w");
  if (Outhandle==0)
  {
    printf("Could not open %s for writing.\n",outfile);
    fclose(Inhandle);
    exit (20);
  }

  printf("\nProcessing %s onto %s\n",infile, outfile);

/*------------- main loop ------------------------------------------*/

  for (;;)          /* for ever (til break) */
  {
    result = fgets(buffer,599,Inhandle);
    if (result ==0) { dunnit=TRUE; break; }

    if (strnicmp(buffer,"@DATABASE",9) ==0) continue;  /* discard these lines */
    if (strnicmp(buffer,"@INDEX",6) ==0) continue;
    if (strnicmp(buffer,"@REMARK",7) ==0) continue;
    if (strnicmp(buffer,"@MASTER",7) ==0) continue;
    if (strnicmp(buffer,"@NODE",5) ==0) continue;
    if (strnicmp(buffer,"@DNODE",6) ==0) continue;
    if (strnicmp(buffer,"@ENDNODE",8) ==0) continue;
    if (strnicmp(buffer,"@TITLE",6) ==0) continue;
    if (strnicmp(buffer,"@PREV",5) ==0) continue;
    if (strnicmp(buffer,"@NEXT",5) ==0) continue;
    if (strnicmp(buffer,"@WIDTH",6) ==0) continue;
    if (strnicmp(buffer,"@TOC",4) ==0) continue;
    if (strnicmp(buffer,"@HEIGHT",7) ==0) continue;
    if (strnicmp(buffer,"@WORDWRAP",9) ==0) continue;
    if (strnicmp(buffer,"@FONT",5) ==0) continue;
    if (strnicmp(buffer,"@HELP",5) ==0) continue;
    if (strnicmp(buffer,"@KEYWORDS",9) ==0) continue;

    i=0;
    if (strnicmp(buffer,"@author",7) ==0) i=1;
    if (strnicmp(buffer,"@(c)",4) ==0) i=1;
    if (strnicmp(buffer,"@$VER",5) ==0) i=2;
    if (strnicmp(buffer,"@VER",4) ==0) i=1;
    if(i>0)
    {
      fprintf(Outhandle,"%s",&buffer[i]);
      linecount++;
      continue;
    }

/* deal with format commmands & parse into buff2 */

    buff2[0]='\0';
    for (i=0;i<strlen(buffer);i++)
    {
      if (buffer[i]=='\0') break;             /* end of line */
      if ((buffer[i]==AT)&(buffer[i+1]==BRA)&(buffer[i+2]!=QUOTE))
      {                            /* non-link command found */
        i=i+2;
        if((buffer[i]=='b')|(buffer[i]=='i')|(buffer[i]=='u')|(buffer[i]=='B')|(buffer[i]=='I')|(buffer[i]=='U')) i++;
        if((buffer[i]=='b')|(buffer[i]=='i')|(buffer[i]=='u')|(buffer[i]=='B')|(buffer[i]=='I')|(buffer[i]=='U')) i++;
        if(buffer[i]==KET) continue;

        if(((buffer[i]=='f')&(buffer[i+1]=='g'))|((buffer[i]=='F')&(buffer[i+1]=='G'))) /* foreground color */
        {                                       /* skip to end      */
          for (j=i+2;j<strlen(buffer);j++)
          {
            if (buffer[j]==KET) { i=j; break; }
          }
        }
      }
      else strncat(buff2,&buffer[i],1);
    }

/* all retained chars are now in buff2. Ready to copy back to buffer */
/* now deal with links */

    buffer[0]='\0';

    for (i=0;i<strlen(buff2);i++)
    {
      if (buff2[i]=='\0') break;             /* end of line */
      if ((buff2[i]==AT)&(buff2[i+1]==BRA)&(buff2[i+2]==QUOTE))
      {                               /* link command found */
        endlink=FALSE;
        for (j=i+3;j<strlen(buff2);j++)
        {
          if (buff2[j]==QUOTE)
          {
            for (j=j+1;j<strlen(buff2);j++)
            { 
              if (buff2[j]==KET) 
              { i=j; endlink=TRUE; break; } 
            }
          }
          if (endlink)  /* break; */
          {
            i=j;
            break;
          }
          strncat(buffer,&buff2[j],1);
          i=j;
        }
      }
      else strncat(buffer,&buff2[i],1);
    }

    fprintf(Outhandle,"%s",buffer);
    linecount++;
    if ((formfeeds)&(linecount >= textlength))
    {
      fputc('\x0c',Outhandle);  /* formfeed */
      linecount = 0;  /* Reset at new page */
    }
  }

  if(formfeeds) fputc('\x0c',Outhandle);
  fclose(Outhandle);
  fclose(Inhandle);
 
  printf("Done\n");
  return(0);
}

/*--------------------------------------------------*/
