/***************************************************************
*
*			ToLib.c
*
*		Disk Library Documentation System
*		Convert README.list files to LIB system
*
*	By Wilson Snyder		(c) January 1988
*
****************************************************************
*
*	NOTICE:
*
*	This program was written as a service to the amiga
*	users.  This program may be used by all parties, including
*	sale (see below) as long as the author's name and address
*	remains intact in the program and documentation.
*	Distribution of this program by parties selling it
*	as part of a commercial for-profit package of over $10
*	must contact the address below before such a package is sold.
*
*	I make no warranties over this program, performance,
*	etc.  And any costs incurred by this program, or its
*	actions are placed on the user.
*
*	PLEASE FORWARD INPROVEMENTS MADE TO THIS PROGRAM TO:
*
*	Wilson Snyder, 15 Davis Parkway, South Burlington, VT 05403
*
***************************************************************/

#include "exec/types.h"
#include "stdio.h"

#define VERSION	"AMIGA-TOLIB.  Version 1.0.0.  By Wilson Snyder"

#define BRK	70	/* Where to break up long, continous lines */
#define BRKTIT	40	/* Where to break title */
#define	SPACEEQ	3	/* Spaces seperating filename and description */

void	main(argc, argv)
	int argc;
	char *argv [];
	{
	FILE *InFile,*OutFile;		/* Files for reading and writing */
	unsigned char cntr=0;		/* Insert line break every BRK chars */
	int spaces;			/* Number spaces encountered */
	int mode; /* 0=line begin, 1=space drop, 2=coping, 3=title extract */
	BOOL linelast;			/* Copied text on the last line */
	char ch;			/* Current read character */

	/* Print usage information */
	if (argv[1][0]=='?') {
		printf ("%s\nUseage: %s Input-File Output-File [header]\n",
			VERSION, argv[0]);
		exit (0);
		}

	/* Open input file */
	if (!(InFile = fopen (argv[1],"r"))) {
		printf ("Bad input file.\n");
		exit (0);
		}

	/* Open output file */
	if (!(OutFile = fopen (argv[2],"w"))) {
		printf ("Bad output file.\n");
		fclose (InFile);
		exit (0);
		}


	mode = 0;
	cntr = 0;
	linelast = FALSE;

	/* Begin the copy */
	while ((ch=getc(InFile))!=EOF) {
	    /* Newline, reset mode to 0, */
	    if (ch=='\n') {
		linelast = (mode==1 || mode==2);
		if (linelast) {
			/* output continuation if text printed on last line */
			putc(' ',OutFile);
			putc('&',OutFile);
			}
		putc (ch,OutFile);
		mode = 0;
		continue;
		}
		
	    switch (mode) {
		case 0:	/* Beginning of Line */
			cntr = 0;
			/* If begins with space or tab is a text line */
			if (ch==' ' || ch=='\t') {
				mode = 1;	/* Skip leading spaces */
				}
			else	{
				/* Otherwise is a new title */
				if (linelast)	putc ('\n',OutFile);
				if (argc>3)	{
					/* Print optional disk title */
					fputs (argv[3],OutFile);
					putc ('\n',OutFile);
					}
				spaces = 0;
				mode = 3;	/* Copy title */
				putc(ch,OutFile);
				}
			break;

		case 1:	/* Skipping leading spaces or tabs before text */
			if (ch==' ' || ch=='\t')	break;	/* drop it */
			putc (ch,OutFile);
			cntr = 1;
			mode = 2;	/* now copy remainder */
			break;

		case 2:	/* Coping to EOL */
			cntr++;
			if (cntr>BRK) {
				putc('&',OutFile);
				putc('\n',OutFile);
				cntr = 0;
				}
			putc (ch,OutFile);
			break;

		case 3:	/* Copying title until tab or several spaces */
			cntr++;
			/* count spaces, after SPACEEQ start text copy */
			if (ch==' ')
				spaces++;
			/* End of title, found a tab or line over length */
			if ((spaces>=SPACEEQ) || ch=='\t' || cntr>=BRKTIT) {
				putc ('\n', OutFile);
				mode = 1;	/* skip rest */
				break;
				}
			if (ch==' ') break;

			/* Write title info after stored spaces*/
			while (spaces--)	putc (' ', OutFile);
			spaces = 0;
			if (ch!=' ') putc (ch, OutFile);
			if (cntr>=BRKTIT)	mode = 1;	/* Chr limit */
			break;

		default:
			/* Bad state */
			printf ("Internal mode error\n");
			fclose (InFile);
			fclose (OutFile);
			exit(20);
		}
	    }

	/* All done. */
	fclose (InFile);
	fclose (OutFile);
	}
