/*
 *		gssub.exe:	Utility program for dviout/dviprt
 *
 *		Make "gs_exec.bat" to make *.pbm/*.gif from *.ps 
 *
 *									written by SHIMA	   Feb 21, 1993
 *                                  modified by Asayama    Apr  2, 1993
 */
#include	<stdio.h>
#include	<string.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<io.h>

#define	BUF_LEN 	256
#define	MAX_LINE	128
#define	F_SAME		1
#define	F_DIFF		2

#define	TEMP_PS		"tmp$$.ps"
#define	OTHER_PS	"tmp$.ps"
unsigned char *BAT_NAME = "gs_exec.bat";

unsigned char name[BUF_LEN];
unsigned char line[BUF_LEN];
unsigned char tmp[BUF_LEN + 16];
unsigned char *pt[MAX_LINE];

extern unsigned _stklen  = 256;

void usage(void)
{
	printf("gssub :Used by dviout/dviprt with -GS=5 option\n"
		   "\t\t\t\tVer 0.4 written by SHIMA, Mar 7 1993"
	);
	exit( 1 );
}

int strlcmp(char *s, char *t)
{
	int len;

	len = strlen(t);

	if (strncmp(s, t, len) == 0 && s[len] <= ' ')
		return( 0 );
	return( 1 );
}

int main(int argc, char **argv)
{
	int i, j, ch, file, same, f_run, line_number;
	unsigned char *ps, *pd, *ppage;
	FILE *fp, *fq;

	if (argc < 3) usage();
							/* argv[file] is the main PS file */
	file = argc - 1;

	strcpy(name, "REM ");
	ppage = getenv("page");

	same = f_run = j = line_number = 0;
	if (strcmp(argv[file], TEMP_PS) == 0){
		fprintf(stderr, "\nTemporary PS file: %s\n", TEMP_PS);

		if( (fp = fopen(BAT_NAME, "a")) == NULL ){
			fprintf( stderr, "Cannot open %s\n", BAT_NAME);
			exit(1);
		}
		if (ppage != NULL) {
			fprintf(fp,"REM Page: %s\n",ppage);
		}
		fprintf(fp, "%s%s\n", name, TEMP_PS);
										/* writing "REM PS_file_name" */

		for (i = 1; i < argc; i++){
			if (*argv[i] == '-') continue;
			if ((fq = fopen(argv[i], "r")) == NULL){
				fprintf(stderr, "Cannot open %s\n", argv[i]);
				exit(1);
			}
			while ((ch = fgetc(fq)) != EOF){
				if (!j){
					fprintf(fp, "echo ");
					j++;
				}
				if (isspace(ch)){
					if (ch == '\n' || j > 60){
						j = 0;
						fprintf(fp, " %s %s\n",
							(line_number++ == 0)?">":">>",argv[i]);
						continue;
					}
				}
				fputc(ch, fp);
				j++;
			}
			if (j)
				fprintf(fp, " %s %s\n",
							(line_number++ == 0)?">":">>",argv[i]);
			fclose(fq);
		}
		goto cmd;
	}
	if (strcmp(argv[file], OTHER_PS) != 0){
v_err:	fprintf(stderr, "Use the correct versions of dviout/prt & gssub\n");
		exit(1);
	}
							/* Now making a new batch for the PS file */
	for (i = 1; i < argc; i++){
		if (*argv[i] == '-') continue;
		if ((fq = fopen(argv[i], "r")) == NULL){
			fprintf(stderr, "Cannot open %s\n", argv[i]);
			continue;
		}
		j = 0;
		while (fgets(line, BUF_LEN, fq)){
			line[strlen(line)-1] = 0;
			sprintf(tmp, "echo %s %s %s\n",
				line, (j++ == 0)?">":">>", argv[i]);
			if (line_number >= MAX_LINE-1){
				fprintf(stderr, "Too many lines\n" );
				exit(1);
			}
			if ((pt[line_number++] = strdup(tmp)) == NULL){
				fprintf(stderr, "Not enough memory\n" );
				exit(1);
			}
			if (*line == '(' && strcmp(line + strlen(line) - 5, ") run") == 0){
				for(ps = line + 1, pd = name + 4; *ps != ')' && *ps != 0; )
					*pd++ = *ps++;
				*pd = 0;
				f_run++;
			}
		}
		fclose(fq);
	}

	if(f_run != 1) goto v_err;

	fprintf(stderr, "\nWriting %s for %s to make PBM/GIF\n",
		BAT_NAME, name + 4);

	strcpy(tmp, "gs");
	for (i = 1; i < argc; i++){
		strcat(tmp, " ");
		strcat(tmp, argv[i]);
	}
	strcat(tmp, "\n");
	if ((pt[line_number] = strdup(tmp)) == NULL){
		fprintf(stderr, "Not enough memory\n" );
			exit(1);
	}
											/* compare with the old one */
	if( (fp = fopen(BAT_NAME, "r")) != NULL ){
		while (fgets(line, BUF_LEN, fp)){
			if (strlcmp(line, name) == 0){		/* the same PS file? */
				for (i = 0 ; i <= line_number; i++ ){
					fgets(line, BUF_LEN, fp);
					if (strcmp(line, pt[i]) != 0 || line[0] == '\n') break;
				}
				if (i > line_number){
					fprintf(stderr, "Quit because the same part exists in %s\n"
						, BAT_NAME);
					exit(1);
				}
				same++;
			}
		}
		fclose(fp);

	}

	if (same > 0 ){
		fprintf(stderr,
			"Warning: The same PS file is used in a different way\n");
	}

	if ((fp = fopen(BAT_NAME, "a")) == NULL){
		fprintf( stderr, "Cannot open %s\n", BAT_NAME);
		exit(1);
	}

	if (ppage != NULL) {
		fprintf(fp,"REM Page: %s\n",ppage);
	}
	fputs(name, fp);
	if (same > 0) fprintf(fp, " %d", same);
	fputs(" \n", fp);
	for (i = 0; i < line_number; i++)
		fputs(pt[i], fp);

cmd:
	fprintf(fp, "gs");
	for (i = 1; i < argc; i++)
		fprintf(fp, " %s", argv[i]);
	fprintf(fp, "\n\n");
	fclose(fp);
	
	return 0;
}
