# include "uniphot.h"
# include <signal.h>

static int Xgraph[5] = {0,0,0,0,0};
static int active;

void XDead () {
  signal (SIGPIPE, XDead);
  fprintf (stderr, "kapa is dead, must restart\n");
  Xgraph[active] = -1;
}

int open_graph (int N) {

  int i, InitSocket, status, addreslen;
  struct sockaddr_un Address;
  char temp[100], *display_name;
  char socket_name[100];
  FILE *f;
  
  active = N;
  sprintf (socket_name, "/tmp/kapa%d", N);
  sprintf (temp, "rm -f %s", socket_name);
  system (temp);
    
  strcpy (Address.sun_path, socket_name); 
  Address.sun_family = AF_UNIX; 
  InitSocket = socket (AF_UNIX, SOCK_STREAM, 0); 
  status = bind (InitSocket, &Address, sizeof (Address));
  status = listen (InitSocket, 1);
  
  sprintf (temp, "kapa %s &", socket_name);
# ifndef DEBUG
  system (temp);
# else  
  fprintf (stderr, "start kapa, press return: %s\n", temp);
  fscanf (stdin, "%d", &i);
# endif
  
  addreslen =  sizeof (Address);
  Xgraph[N] = accept (InitSocket, &Address, &addreslen);
  if (Xgraph[N] < 0) {
    fprintf (stderr, "error starting kapa\n");
    return (FALSE);
  }
  else {
    return (TRUE);
  }
  
}

void DonePlotting (Graphdata *graphmode, int N) {
  char buffer[65], buffer2[65];

  if (Xgraph[N] == 0) return;

  write (Xgraph[N], "DBOX", 4);
  sprintf (buffer, "%f %f %f %f", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
  write (Xgraph[N], buffer2, 16);
  write (Xgraph[N], buffer, strlen (buffer));

  sprintf (buffer, "%s %s %s", "2222", "2222", "2222");
  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
  write (Xgraph[N], buffer2, 16);
  write (Xgraph[N], buffer, strlen (buffer));
}

void JpegPlot (Graphdata *graphmode, int N, char *filename) {
  char buffer[65], buffer2[65];

  if (Xgraph[N] == 0) return;

  write (Xgraph[N], "PNGF", 4);
  sprintf (buffer, "LEN: %11d", strlen (filename));
  write (Xgraph[N], buffer, 16);
  write (Xgraph[N], filename, strlen (filename));
  read (Xgraph[N], buffer, 4);
}

void PSPlot (Graphdata *graphmode, int N, char *filename) {
  char buffer[65], buffer2[65];

  if (Xgraph[N] == 0) return;

  write (Xgraph[N], "PSIT", 4);
  sprintf (buffer, "LEN: %11d", strlen (filename));
  write (Xgraph[N], buffer, 16);
  write (Xgraph[N], filename, strlen (filename));
  read (Xgraph[N], buffer, 4);
}

void PrepPlotting (int Npts, Graphdata *graphmode, int N) {

  int i, status;
  char buffer[128], buffer2[128];

  active = N;
  if (Npts < 1) return;

  if (Xgraph[N] < 1) if (!open_graph(N)) return;

  /* test Xgraph[N], flush junk from pipe */
  signal (SIGPIPE, XDead);
  fcntl (Xgraph[N], F_SETFL,  O_NONBLOCK); 
  for (i = 0; (read (Xgraph[N], buffer, 64) > 0) && (i < 20); i++);
  fcntl (Xgraph[N], F_SETFL, !O_NONBLOCK); 
  
  write (Xgraph[N], "ERAS", 4);
  
  /* tell kapa to look for the incoming image */
  status = write (Xgraph[N], "PLOT", 4); 

  /* send Xgraph[N] the plot details */
  sprintf (buffer, "%8d %8d %d %d %d %d %f %f", 
	   Npts, graphmode[0].style, 
	   graphmode[0].ptype, graphmode[0].ltype, 
	   graphmode[0].etype, graphmode[0].color, 
	   graphmode[0].lweight, graphmode[0].size);
  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
  write (Xgraph[N], buffer2, 16);
  write (Xgraph[N], buffer, strlen (buffer));
  
  sprintf (buffer, "%f %f %f %f", 
	   graphmode[0].xmin, graphmode[0].xmax, 
	   graphmode[0].ymin, graphmode[0].ymax);
  sprintf (buffer2, "NBYTES: %6d", strlen (buffer));
  write (Xgraph[N], buffer2, 16);
  write (Xgraph[N], buffer, strlen (buffer));

}

void PlotLabel (char *string, int N) {

  char buffer[32];

  if (Xgraph[N] == 0) return;

  write (Xgraph[N], "LABL", 4);
  sprintf (buffer, " %6d %6d", strlen (string), 2);
  write (Xgraph[N], buffer, 16);
  write (Xgraph[N], string, strlen (string));
}

void PlotVector (int Npts, double *vect, int mode, int N) {

  float *values;
  int i, Nbytes;

  active = N;
  if (Npts < 1) return;

  ALLOCATE (values, float, Npts);
  for (i = 0; i < Npts; i++) {
    values[i] = vect[i];
  }

  Nbytes = Npts * sizeof (float);
  write (Xgraph[N], values, Nbytes);
  free (values);

}

/* include these lines to plot a pair of vectors: 

   typedef struct {
   double xmin, xmax, ymin, ymax;
   int style, ptype, ltype, etype, color;
   double lweight, size;
   } Graphdata;
   Graphdata graphdata;
   
   graphdata.xmin = -200;
   graphdata.xmax = 4200;
   graphdata.ymin = -500;
   graphdata.ymax = 500;
   graphdata.style = 2;
   graphdata.ptype = 2;
   graphdata.ltype = 0;
   graphdata.etype = 0;
   graphdata.color = 0;
   graphdata.lweight = 0;
   graphdata.size = 0.5;
   
   PrepPlotting (N, &graphdata, n);
   PlotVector (N, Y, 0, n);
   PlotVector (N, dM, 1, n);
   DonePlotting (&graphdata, n);
   
 */
