/*
 *	Convert Atari Ghostscript .hlp files to Postscript
 *	Tim Gallivan, 1994.
 *
 *	The /width and /height printed by this program depend
 *	critically on the contents of the file procs.ps. I
 *	don't suggest changing that file or this one unless
 *	you know enough PostScript to fix things up.
 */

/* 	Usage: "hlptops file1.hlp [file2.hlp ...]"		*/

#define MAXLEN	 	(81)
#define MARGINS		(20)
#define CHARHEIGHT	(20)
#define HEADERHEIGHT	(4 * CHARHEIGHT + 15)

#define ERROR1 	"hlptops usage: hlptops file1.hlp [file2.hlp ...]"
#define ERROR2	"hlptops: %s does not appear to be a Ghostscript help file.\n"

#define HEADER1 "%!PS-Adobe-\n\
\n\
currentdict /procsdef known not {(procs.ps) runlibfile} if\n"

#define HEADER2 "/height 000 def \n\
setsize\n\n"

#define TRAILER	"\nfooter showpage restoresize\n"

#include <stdio.h>
#include <string.h>

main(int argc, char **argv)
{
    FILE *hlpfile, *psfile;

    char infile[MAXLEN], outfile[MAXLEN], line[MAXLEN], temp[MAXLEN], *ptr;

    int i, j, k, length, longest, linecount, width, height;

    fpos_t widthpos, heightpos;

    if (argc < 1) {		/* check for no input */
	puts(ERROR1);
	exit(-1);
    }

    /* Process each file in the argument list. */

    for (i=1; i < argc; i++, linecount=0) {

	/* See if it looks like a Ghostscript hlp file. */

	if ((ptr = strstr(argv[i], "hlp")) == NULL) {
	    printf(ERROR2, argv[i]);
	    break;
	}
	
	/* Copy the input file name and open it. */

	strcpy(infile, argv[i]);
	if ((hlpfile = fopen(infile, "r")) == NULL) {
	    printf("hlptops: Unable to open %s!\n", infile);
	    continue;
	}

	/* Construct the output file name and open it. */

	*ptr = '\0';
	strcpy(outfile , argv[i]);
	strcat(outfile, "ps");
	if ((psfile = fopen(outfile, "w")) == NULL) {
	    printf("hlptops: Unable to open %s!\n", outfile);
	    fclose(psfile);
	    continue;
	}

	/* Write the PostScript header in the output file. */

	fputs(HEADER1, psfile);
	fgetpos(psfile, &widthpos);	/* get position of /width */
	fputs("/width 000 def \n", psfile);
	fgetpos(psfile, &heightpos);	/* get position of /height */
	fputs(HEADER2, psfile);

	/* skip any blank lines at the beginning of the hlp file */

	do {
	    if ((ptr = fgets(line, MAXLEN, hlpfile)) == NULL) {
		printf("hlptops: Unexpected end of file in %s\n", infile);
		goto endloop;
	    }

	    line[strlen(line)-1] = '\0';	/* get rid of \n */
	}
	while (strlen(line) == 0);

	/* Use the first non-empty line as the header. */
	/* skip leading spaces */

	++linecount;

	/* Escape any parentheses or backslashes that occur. */

	if (strchr(line, '(') || strchr(line, ')') || strchr(line, '\\')) {
	    for(j=0, k=0; line[j] != NULL; j++, k++) {
		if (line[j] == '(' || line[j] == ')' || line[j] == '\\') {
		    temp[k] = '\\';
		    ++k;
		}
		temp[k] = line[j];
	    }
	    temp[k] = '\0';
	    strcpy(line, temp);
	}

	longest = strlen(line);

	fprintf(psfile, "(%s) header\n\n", line+strspn(line, " "));
	
	/* Convert the remainder of the file. */

	while ((ptr = fgets((char *)line, MAXLEN, hlpfile)) != NULL) {
	    length = strlen(line) - 1;
	    line[length] = '\0';	/* get rid of \n */
	    ++linecount;
	    /* Escape any parentheses or backslashes that occur. */

	    if (strchr(line, '(') || strchr(line, ')') || strchr(line, '\\')) {
		for(j=0, k=0; line[j] != NULL; j++, k++) {
		    if (line[j] == '(' || line[j] == ')' || line[j] == '\\') {
			temp[k] = '\\';
			++k;
		    }
		    temp[k] = line[j];
		}
		temp[k] = '\0';
		strcpy(line, temp);
	    }

	    if (strlen(line) == 0) {
		if (linecount != 2) {		/* no return after title */
		    fputs("return\n\n", psfile);
		}
	    }
	    else {
		fprintf(psfile, "(%s) jnl\n", line);
	    }

	    if (length > longest) longest = length;

	}

	endloop:

	/* Write the trailer in the output file. */

	fputs(TRAILER, psfile); 

	/* Assume the average character width is .41 * height */

	width = (int)(longest * CHARHEIGHT * .41 + 2 * MARGINS);

	fsetpos(psfile, &widthpos);	/* move back to set /width */
	fprintf(psfile, "/width %d def ", width); 

	height = linecount * CHARHEIGHT + HEADERHEIGHT;

	fsetpos(psfile, &heightpos);	/* move back to set /height */
	fprintf(psfile, "/height %d def ", height); 

	/* Close both files and move to next input file. */

	fclose(hlpfile);
	fclose(psfile);

    }

}
