#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <libps/pslib.h>
#ifdef MEMORY_DEBUGGING
#include <libps/pslib-mp.h>
#endif

#define HMODE "right"

void usage() {
	fprintf(stderr, "Usage: txt2ps infile outfile\n\n");
}

int main(int argc, char *argv[]) {
	PSDoc *psdoc;
	int fraktur, antiqua;
	float boxwidth, boxheight, baseline, colsep, leftmargin, x;
	float fontsize;
	int rest, boxed;
	char *text, *str;
	FILE *fp;
	struct stat fst;

	boxwidth = 200;
	boxheight = 630;
	baseline = 100;
	colsep = 20;
	leftmargin = 100;
	boxed = 0;
	fontsize = 10.0;

	if(argc < 3) {
		usage();
		exit(1);
	}

	if(NULL == (fp = fopen(argv[1], "r"))) {
		fprintf(stderr, "Cannot open input file\n");
		exit(1);
	}

	PS_boot();
#ifdef MEMORY_DEBUGGING
	PS_mp_init();
#endif

#ifdef MEMORY_DEBUGGING
	psdoc = PS_new2(NULL, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	psdoc = PS_new();
#endif
	if(0 > PS_open_file(psdoc, argv[2])) {
		fprintf(stderr, "Cannot open output file\n");
		exit(1);
	}
	PS_set_info(psdoc, "Creator", __FILE__);
	PS_set_info(psdoc, "Author", "Uwe Steinmann");
	PS_set_info(psdoc, "Title", "Long Text Examples");
	PS_set_info(psdoc, "Keywords", "boxed text, Einstein");

	fraktur = PS_findfont(psdoc, "AlteSchwabacher", "AlteSchwabacher.enc", 1);
	antiqua = PS_findfont(psdoc, "Helvetica", "", 0);
	PS_begin_page(psdoc, 596, 842);

	PS_setfont(psdoc, fraktur, fontsize);
	PS_set_value(psdoc, "leading", 14);

	stat(argv[1], &fst);
	text = malloc(fst.st_size+1);
	str = text;
	while(!feof(fp)) {
		fread(str++, 1, 1, fp);
	}
	str--;
	*str = '\0';
	fclose(fp);

	PS_set_parameter(psdoc, "hyphenation", "true");
	PS_set_parameter(psdoc, "hyphendict", "hyph_de.dic");
	PS_set_value(psdoc, "hyphenminchars", 2);
	PS_set_value(psdoc, "parindent", fontsize*1.2);
	PS_set_value(psdoc, "parskip", fontsize*1.2);
	PS_set_value(psdoc, "parindentskip", 0);
//	PS_set_parameter(psdoc, "parbreak", "false");
//	PS_set_parameter(psdoc, "linebreak", "true");
//	PS_set_value(psdoc, "numindentlines", 1);
	x = leftmargin;
	if(boxed) {
		PS_rect(psdoc, x, baseline, boxwidth, boxheight);
		PS_stroke(psdoc);
	}
	str = text;
	rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, HMODE, "");
	PS_set_value(psdoc, "parindentskip", 1);
	if(rest) {
		str += strlen(str)-rest;
		x = leftmargin+boxwidth+colsep;
		if(boxed) {
			PS_rect(psdoc, x, baseline, boxwidth, boxheight);
			PS_stroke(psdoc);
		}
		rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, HMODE, "");
	}
	PS_setfont(psdoc, antiqua, 8.0);
	PS_show_boxed(psdoc, argv[1], leftmargin, baseline-30, colsep+2*boxwidth, 20, "left", "");
	PS_setfont(psdoc, fraktur, fontsize);
	PS_end_page(psdoc);

	while(rest) {
		str += strlen(str)-rest;
		PS_begin_page(psdoc, 596, 842);
		PS_setfont(psdoc, fraktur, fontsize);
		PS_set_value(psdoc, "leading", 14);
		x = leftmargin;
		if(boxed) {
			PS_rect(psdoc, x, baseline, boxwidth, boxheight);
			PS_stroke(psdoc);
		}
		rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, "justify", "");
		if(rest) {
			x = leftmargin+boxwidth+colsep;
			str += strlen(str)-rest;
			if(boxed) {
				PS_rect(psdoc, x, baseline, boxwidth, boxheight);
				PS_stroke(psdoc);
			}
			rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, "justify", "");
		}
		if(!rest) {
			PS_set_value(psdoc, "textx", x+boxwidth-PS_stringwidth(psdoc, "Albert Einstein", fraktur, fontsize));
			PS_continue_text(psdoc, "Albert Einstein");
		}
		PS_end_page(psdoc);
	}
	PS_deletefont(psdoc, fraktur);
	PS_deletefont(psdoc, antiqua);
	PS_close(psdoc);
	PS_delete(psdoc);
#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif
	PS_shutdown();
	exit(0);
}
