# include "gastro.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) {

# ifdef ANSI
#   define F_SETFL      4   
#   define O_NONBLOCK 0200000  
#   define       AF_UNIX         1          
#   define       SOCK_STREAM     1          
#define ENOENT          2       /* No such file or directory    */
# endif /* ANSI */

  int InitSocket, status, addreslen;
  struct sockaddr_un Address;
  char temp[100];
  char socket_name[100];
  
  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, (struct sockaddr *) &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, (struct sockaddr *) &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];

  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 PrepPlotting (int Npts, Graphdata *graphmode, int N) {

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

  active = N;
  if (Npts < 1) 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); 
  
  if (Xgraph[N] < 1) if (!open_graph(N)) return;
  
  /* 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 PlotVector (int Npts, float *vect, int mode, int N) {

  int Nbytes;

  if (Npts < 1) return;

  active = N;
  Nbytes = Npts * sizeof (float);
  write (Xgraph[N], vect, Nbytes);

}

void PlotReset (int N) {

  char buffer[128];
  int i;

  /* 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); 
  
  if (Xgraph[N] < 1) if (!open_graph(N)) return;
  
  write (Xgraph[N], "ERAS", 4);

}
/* 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);
   
 */
