/* 
 *	zapauto.c	Zap Auto-Doc files --> InfoMaker Format
 *
 *	This is used to take an C-Amiga format "auto-doc"
 *	and convert it into into InfoMaker input format.
 *
 *	Input file should be specified on command line as <file>
 *	where the actual name on disk is <file>.doc. Output file
 *	name is DEST:<file>.htxt.
 *
 *	Output files are hardcoded to go to the disk/directory
 *	with the logical assignment "DEST:", which should
 *	be made before you run this program.
 *
 *	Jim Becker	-- Terrapin Software
 *			   April 18th
 *
 *	This software is placed in the public domain, feel free to
 *	hack as appropriate, but please leave my name in..
 */

#include <exec/types.h>
#include <lattice/ctype.h>
#include <libraries/dos.h>
#include <lattice/stdio.h>

/* variables */
int	i,j,k;
char	line[120];
char	*p1;
short	eof = FALSE;
FILE	*in, *out;

/*
 *	get the next input line
 */

getline()
{
	if( eof ) return FALSE;

	if( fgets( line, sizeof(line), in ) == NULL ) {
		eof = TRUE;
		return FALSE;
	}

	p1 = line;
	while( *p1 == ' ' || *p1 == '\t' ) p1++;
	return TRUE;
}

main( argc, argv )
int	argc;
char	*argv[];
{
	if( argc <= 1 ) {
		printf( "Usage: zapauto <file>, where file is <file>.doc\n");
		exit(1);
	}

	strcpy( line, *++argv );
	strcat( line, ".doc"  );

	printf("Trimming file %s... ", line );

	in = fopen( line, "r" );
	if( in <= 0 ) {
		printf("\nCan't open input file %s\n", line);
		exit(1);
	}

	strcpy( line, "DEST:");
	strcat( line, *argv );
	strcat( line, ".htxt" );
	
	out = fopen( line, "w" );

	if( out <= 0 ) {
		printf( "\nCan't open output file %s\n", line );
		exit(1);
	} 

	/* output header into to file */
	fprintf( out, "$HEADER %s \"%s documentation\"\n", *argv, *argv );
	fprintf( out, "$ATTRIBUTE $LEFT=10 $JUSTIFY=N\n");

	/* flush out the table of contents information */
	while( getline() && *p1 != 0x0C );

	if( eof ) {
		printf("file is incorrect format.\n");
		fclose(in);
		fclose(out);
		exit(1);
	}

	getline();

	while( getline() ) {
		if( *p1 == 0x0C ) {
			getline();
		}
		else
		if( strcmp( p1, "NAME\n" ) == 0 ) {
			getline();
			fprintf(out, "$SECTION 1 \"%s", p1);
		}
		else
			fprintf(out, "%s", line );
	}
	printf("done.\n");	
	fclose(in);
	fclose(out);

	exit(0);
}
