#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <libps/pslib.h>

void * my_malloc(PSDoc *p, size_t size, const char *caller) {
	void *a;
	a = (void *) malloc(size);
	return(a);
}   

void * my_realloc(PSDoc *p, void *mem, size_t size, const char *caller) {
	return((void *) realloc(mem, size));
} 
  
void my_free(PSDoc *p, void *mem) {
	free(mem);
}

int main() {
	float radius = 200;
	float margin = 20;
	int pagecount = 600;
	int delay = 0;
	int alpha;
	time_t calendar_time;
	struct tm *local_tm;
	PSDoc *ps;

	PS_boot();
	ps = PS_new();

	if (0 > PS_open_file(ps, "psclock.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", "Analog Clock");
	PS_set_info(ps, "Keywords", "Analog Clock, Time");

	while(pagecount-- > 0) {
		PS_begin_page(ps, 2 * (radius + margin), 2 * (radius + margin));

		PS_set_parameter(ps, "transition", "wipe");
		PS_set_value(ps, "duration", 0.5);
	
		PS_translate(ps, radius + margin, radius + margin);

		/* minute strokes */
		PS_save(ps);
		PS_setcolor(ps, "stroke", "rgb", 0.0, 0.0, 1.0, 0.0);
		PS_setlinewidth(ps, 2.0);
		for (alpha = 0; alpha < 360; alpha += 6) {
			PS_rotate(ps, 6.0);
			PS_moveto(ps, radius, 0.0);
			PS_lineto(ps, radius-margin/3, 0.0);
			PS_stroke(ps);
		}
		PS_restore(ps);

		/* 5 minute strokes */
		PS_save(ps);
		PS_setlinewidth(ps, 3.0);
		for (alpha = 0; alpha < 360; alpha += 30) { 
			PS_rotate(ps, 30.0);
			PS_moveto(ps, radius, 0.0);
			PS_lineto(ps, radius-margin, 0.0);
			PS_stroke(ps);
		}
		PS_restore(ps);

		calendar_time = time(NULL);
		local_tm = localtime(&calendar_time);

		/* draw hour hand */
		PS_save(ps);
		PS_rotate(ps,-((local_tm->tm_min/60.0)+local_tm->tm_hour-3.0)*30.0);
		PS_moveto(ps, -radius/10, -radius/20);
		PS_lineto(ps, radius/2, 0.0);
		PS_lineto(ps, -radius/10, radius/20);
		PS_closepath(ps);
		PS_fill(ps);
		PS_restore(ps);

		/* draw minute hand */
		PS_save(ps);
		PS_rotate(ps,-((local_tm->tm_sec/60.0)+local_tm->tm_min-15.0)*6.0);
		PS_moveto(ps, -radius/10, -radius/20);
		PS_lineto(ps, radius * 0.8, 0.0);
		PS_lineto(ps, -radius/10, radius/20);
		PS_closepath(ps);
		PS_fill(ps);
		PS_restore(ps);

		/* draw second hand */
		PS_save(ps);
    PS_setcolor(ps, "stroke", "rgb", 1.0, 0.0, 0.0, 0.0);
		PS_setlinewidth(ps, 2);
		PS_rotate(ps, -((local_tm->tm_sec - 15.0) * 6.0));
		PS_moveto(ps, -radius/5, 0.0);
		PS_lineto(ps, radius, 0.0);
		PS_stroke(ps);
		PS_restore(ps);

		/* draw little circle at center */
		PS_circle(ps, 0, 0, radius/30);
		PS_fill(ps);

		PS_end_page(ps);

		/* to see some difference */
#ifndef WIN32
		if(delay)
			sleep(delay);
#endif
	}

	/*buf = PS_get_buffer(ps);
	len = strlen(buf);

	printf("Content-type: application/ps");
	printf("Content-Length: len");
	printf("Content-Disposition: inline; filename=foo.pdf");
	printf("\n\n");
	printf(buf);
	*/

	PS_close(ps);
	PS_delete(ps);
	PS_shutdown();
	exit(0);
}
