#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <libps/pslib.h>
#ifdef MEMORY_DEBUGGING
#include <libps/pslib-mp.h>
#endif

void errorhandler(PSDoc *p, int error, const char *str, void *data) {
	fprintf(stderr, "PSLib: %s\n", str);
}

int main() {
	PSDoc *ps;
	int psfont;

#ifdef MEMORY_DEBUGGING
	PS_mp_init();
#endif

#ifdef MEMORY_DEBUGGING
	ps = PS_new2(errorhandler, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	ps = PS_new();
#endif

	/* adding grid to the background */
	if (0 > PS_open_file(ps, "pagehook-grid.ps")) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Creating document with page hooks");
	PS_set_info(ps, "Keywords", "Page Hook");

	psfont = PS_findfont(ps, "Courier", "", 0);
	PS_include_file(ps, "grid.ps");

	PS_begin_page(ps, 596, 842);
	PS_rect(ps, 68, 384, 440, 342);
	PS_stroke(ps);
	PS_setfont(ps, psfont, 14);
	PS_set_parameter(ps, "linebreak", "true");
	PS_show_boxed(ps, "/PslibPageBeginHook{\n  /pagenumber exch def\n  /ury exch def /urx exch def\n  /lly exch def /llx exch def\n  0.7 setgray\n  0 72 urx { dup 0 moveto ury lineto stroke } for\n  0 72 ury {\n    dup 0 exch moveto urx exch lineto stroke\n  } for\n  0.0 setgray\n  /Helvetica findfont 8 scalefont setfont\n  0 72 urx {\n    dup 2 add 2 moveto\n    72 div 10 (aaaaaaaaaa) cvrs show ('') show\n  } for\n  72 72 ury {\n    dup 2 add 2 exch moveto\n    72 div 10 (aaaaaaaaaa) cvrs show ('') show\n  } for\n} B", 72, 384, 432, 338, "left", NULL);
	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING
	ps = PS_new2(errorhandler, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	ps = PS_new();
#endif

	/* Put a big 'Draft' into the background */
	if (0 > PS_open_file(ps, "pagehook-draft.ps")) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Creating document with page hooks");
	PS_set_info(ps, "Keywords", "Page Hook");

	psfont = PS_findfont(ps, "Courier", "", 0);
	PS_include_file(ps, "draft.ps");

	PS_begin_page(ps, 596, 842);
	PS_rect(ps, 68, 384, 440, 292);
	PS_stroke(ps);
	PS_setfont(ps, psfont, 16);
	PS_set_parameter(ps, "linebreak", "true");
	PS_show_boxed(ps, "/PslibPageBeginHook{\n  /pagenumber exch def\n  /ury exch def /urx exch def\n  /lly exch def /llx exch def\n  0.8 setgray\n  /Helvetica findfont 144 scalefont setfont\n  45 rotate 350 60 moveto\n  (Draft) show\n} B", 72, 384, 432, 288, "left", NULL);
	PS_end_page(ps);

	PS_begin_page(ps, 596, 842);
	PS_setfont(ps, psfont, 16);
	PS_show_boxed(ps, "Page hooks are good to set a common background on each page.", 72, 144, 144, 288, "left", NULL);
	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING
	ps = PS_new2(errorhandler, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	ps = PS_new();
#endif

	/* Show execution time of page */
	if (0 > PS_open_file(ps, "pagehook-exec.ps")) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Creating document with page hooks");
	PS_set_info(ps, "Keywords", "Page Hook");

	psfont = PS_findfont(ps, "Courier", "", 0);
	PS_include_file(ps, "exectime.ps");

	PS_begin_page(ps, 596, 842);
	PS_rect(ps, 68, 384, 440, 292);
	PS_stroke(ps);
	PS_setfont(ps, psfont, 16);
	PS_set_parameter(ps, "linebreak", "true");
	PS_show_boxed(ps, "/PslibPageBeginHook{\n  pop pop pop pop pop realtime exch\n} B\n/PslibPageEndHook{\n  pop exch\n  /Helvetica findfont 8 scalefont setfont\n  10 10 moveto (This page needed ) show\n  realtime exch sub 10 (aaaaaaaa) cvrs show\n  ( ms for execution) show\n} B", 72, 384, 432, 288, "left", NULL);
	PS_end_page(ps);

	PS_begin_page(ps, 596, 842);
	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  

	exit(0);
}
