/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	routines for generating the
	postscript files
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h> 

#include "adobe.h"

#define NBUF 80

static FILE *fout;
static char filename[NBUF];

#define XSCALE 1.0
#define YSCALE -1.0

static long showpage;

static void scaleit( int * , int *);
static void check_showpage(void);

void
adobe_erase()
{
	long ltime;

	time(&ltime);

	showpage=0;
#ifdef LINUX
	sprintf(filename,"/tmp/xplot%d.tmp",getpid());
#else
	sprintf(filename,"/tmp/xplot%ld.tmp",getpid());
#endif
	fout=fopen(filename,"w");
	if(fout==NULL){
		fprintf(stderr,"adobe: can't open '%s'\n",filename);
		exit(1);
	}

	fprintf(fout,"%s\n","%!PS-Adobe-");
	fprintf(fout,"%s\n","%%Title: plot");
	fprintf(fout,"%s\n","%%Creator: xplot");
	fprintf(fout,"%s %s","%%CreationDate:",ctime(&ltime));
	fprintf(fout,"%s\n","%%Pages: 1");
	fprintf(fout,"%s\n","%%EndComments");
	/*	Define macros
		myfont - is the font for lettering
	*/
	fprintf(fout,"/Helvetica findfont 10 scalefont setfont\n");
	fprintf(fout,"500 750 translate\n");
	fprintf(fout,"-90 rotate\n");
}

void
adobe_close(char *outputfile)
{
	int status;

	showpage=ftell(fout);
	fprintf(fout,"showpage\n");

	fflush(fout);

	if (strcmp(outputfile, "") != 0) {
		printf("saving postscript to file %s\n", outputfile);
		if (fork() == 0) {
			status = execlp("cp", "cp", filename, outputfile, NULL);
			exit(0);
		}
		return;
	}

	if(fork() == 0){
		status=execlp("lp","lp","-c",filename,NULL);
		exit(0);
	}
}

void
adobe_cleanup()
{
	if (fout != NULL) {
		fclose(fout);
	}
	unlink(filename);
}

void 
adobe_drawtext(int x, int y, char *s)
{

	check_showpage();
	scaleit(&x,&y);

	fprintf(fout,"%d %d moveto\n",x,y);
	fprintf(fout,"(%s) show\n",s);
}

void adobe_drawpoly(int n, XPOINT *p,int mode) 
{
	int i,x,y;
	char *mstring;

	check_showpage();
	fprintf(fout,"newpath\n");
	mstring=(mode & ADOBE_PREVIOUS) ? "r":"";

	for(i=0; i<n; i++){
		x=p[i].x;
		y=p[i].y;
		scaleit(&x,&y);
		if(i== 0)
			fprintf(fout,"%d %d moveto\n",x,y);
		else
			fprintf(fout,"%d %d %slineto\n",x,y,mstring);
	}
	if(mode & ADOBE_FILL){
/*		fprintf(fout,"0.5 setgray\n"); */
		fprintf(fout,"fill\n");
	}else
		fprintf(fout,"stroke\n");
}
	

/*	just a hack to change the dot 
*/

void adobe_setdash(int value)
{
	check_showpage();
	switch(value){
		default:
		case 0:
		case 1:
			fprintf(fout,"[] 0 setdash\n");
			break;
		case 2:		
			fprintf(fout,"[3] 0 setdash\n");
			break;	
		case 3:		
			fprintf(fout,"[2 3] 11 setdash\n");
			break;	
	}
}

static void scaleit(int *px,int *py)
{
	*px = (int) (XSCALE * (*px)); 
	*py = (int) (YSCALE * (*py)); 
}

static void check_showpage()
{
	if(showpage)
		fseek(fout,showpage,SEEK_SET);
	showpage=0;
}
