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

#define LEFT_BORDER 70

struct spotcolor {
	int id;
	char *name;
	char *colorspace;
	float c1;
	float c2;
	float c3;
	float c4;
};

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

size_t writeproc(PSDoc *p, void *data, size_t size) {
	return fwrite(data, 1, size, stdout);
}

void footer(PSDoc *p, const char *text) {
	int psfont;
	psfont = PS_findfont(p, "Helvetica", "", 0);
	PS_setfont(p, psfont, 8.0);
	PS_setcolor(p, "stroke", "gray", 0.0, 0.0, 0.0, 0.0);
	PS_show_xy(p, "MMK", LEFT_BORDER, 60);
	PS_show_xy(p, "MultiMedia Kommunikations-", LEFT_BORDER, 50);
	PS_show_xy(p, "systeme GmbH", LEFT_BORDER, 40);
	PS_show_xy(p, "Universitätsstr. 11", LEFT_BORDER, 30);
	PS_show_xy(p, "58097 Hagen", LEFT_BORDER, 20);
}

int main() {
	PSDoc *ps;
	int i;
	struct spotcolor spotcolors[1] = {
		{0, "PANTONE 5565 C", "cmyk", 0.31, 0.0, 0.24, 0.27}};
	int psfont;
	int pstemplate;

	PS_boot();
#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

	if (0 > PS_open_file(ps, "mmk.ps")) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

	PS_set_parameter(ps, "warning", "true");

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Letter template");
	PS_set_info(ps, "Keywords", "MMK Letter");

	for(i=0; i<1; i++) {
		PS_setcolor(ps, "fill", spotcolors[i].colorspace, spotcolors[i].c1, spotcolors[i].c2, spotcolors[i].c3, spotcolors[i].c4);
		spotcolors[i].id = PS_makespotcolor(ps, spotcolors[i].name, 0);
	}

	pstemplate = PS_begin_template(ps, 580.0, 68.0);
	PS_setcolor(ps, "fill", "spot", (float) spotcolors[0].id, 0.5, 0.0, 0.0);
	PS_rect(ps, 0, 0, 36, 68);
	PS_fill(ps);
	PS_setcolor(ps, "fill", "spot", (float) spotcolors[0].id, 1.0, 0.0, 0.0);
	PS_rect(ps, 36, 0, 221, 68);
	PS_fill(ps);
	PS_circle(ps, 221+36, 8.5, 8.5);
	PS_fill(ps);
	PS_rect(ps, 221+36, 8.5, 8.5, 68-8.5);
	PS_fill(ps);
	PS_save(ps);
	PS_setcolor(ps, "fill", "gray", 0.0, 0.0, 0.0, 0.0);
	PS_scale(ps, 2.8, 1.0);
	PS_circle(ps, (221+36+8.5)/2.8, 35, 18.0);
	PS_fill(ps);
	PS_setlinewidth(ps, 0.5);
	PS_setcolor(ps, "fillstroke", "gray", 1.0, 0.0, 0.0, 0.0);
	PS_circle(ps, (221+36+8.5)/2.8, 36.0, 12.0);
	PS_stroke(ps);
	PS_circle(ps, (221+36+8.5)/2.8, 36.5, 7.5);
	PS_stroke(ps);
	PS_circle(ps, (221+36+8.5)/2.8, 37.0, 3.5);
	PS_fill(ps);
	PS_circle(ps, (221+36+8.5)/2.8-7.5, 37, 1.6);
	PS_fill(ps);
	PS_circle(ps, (221+36+8.5)/2.8+7.5, 37, 1.6);
	PS_fill(ps);
	PS_circle(ps, (221+36+8.5)/2.8-12.0, 37, 1.0);
	PS_fill(ps);
	PS_circle(ps, (221+36+8.5)/2.8+12.0, 37, 1.0);
	PS_fill(ps);
	PS_restore(ps);
	PS_setcolor(ps, "stroke", "gray", 0.0, 0.0, 0.0, 0.0);
	psfont = PS_findfont(ps, "Helvetica", "", 0);
	PS_setfont(ps, psfont, 10.0);
	PS_show_xy(ps, "MMK", 330, 45);
	PS_show_xy(ps, "MultiMedia Kommunikationssysteme GmbH", 330, 33);
	PS_setcolor(ps, "stroke", "spot", (float) spotcolors[0].id, 1.0, 0.0, 0.0);
	PS_show_xy(ps, "E-Learnig · Web-Anwendungen · Content Mangagement", 330, 18);
	PS_show_xy(ps, "Beratung · Schulung · Entwicklung", 330, 6);
	PS_end_template(ps);

	PS_begin_page(ps, 596, 842);
	PS_place_image(ps, pstemplate, 0.0, 750.0, 1.0);
	PS_setcolor(ps, "stroke", "gray", 0.0, 0.0, 0.0, 0.0);
	PS_setfont(ps, psfont, 8.0);
	PS_show_xy(ps, "MMK GmbH · Universitätsstr. 11 · 58097 Hagen", LEFT_BORDER, 690);
	PS_show_xy(ps, "Ihr Zeichen", LEFT_BORDER, 569);
	PS_show_xy(ps, "Ihr Schreiben vom", LEFT_BORDER+120, 569);
	PS_show_xy(ps, "Unser Zeichen", LEFT_BORDER+270, 569);
	PS_show_xy(ps, "Datum", LEFT_BORDER+404, 569);

	footer(ps, "");

	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  

	PS_shutdown();
	exit(0);
}
