# include "data.h"

int ps (int argc, char **argv) {

  char filename[1024], buffer[64];
  int N, Source, Nsource, RawImage, NoScale, IsImage;
  
  if (N = get_argument (argc, argv, "--help")) {
    fprintf (stderr, "USAGE: ps [-name file.ps] [-g | -i] [-n device] [-raw] [-scale]\n");
    return (FALSE);
  }

  /* raw image? */
  RawImage = FALSE;
  if (N = get_argument (argc, argv, "-raw")) {
    remove_argument (N, &argc, argv);
    RawImage = TRUE;
  }
  /* scale image? */
  NoScale = FALSE;
  if (N = get_argument (argc, argv, "-noscale")) {
    remove_argument (N, &argc, argv);
    NoScale = TRUE;
  }
  /* what file? */
  filename[0] = 0;
  if (N = get_argument (argc, argv, "-name")) {
    remove_argument (N, &argc, argv);
    strcpy (filename, argv[N]);
    remove_argument (N, &argc, argv);
  }
  /* which tool */
  Nsource = -1;
  if (N = get_argument (argc, argv, "-n")) {
    remove_argument (N, &argc, argv);
    Nsource = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  /* which device */
  if (N = get_argument (argc, argv, "-g")) {
    remove_argument (N, &argc, argv);
    SetImageDevice (FALSE);
  }  
  if (N = get_argument (argc, argv, "-i")) {
    remove_argument (N, &argc, argv);
    SetImageDevice (TRUE);
  }  
  IsImage = GetCurrentDevice ();
  if (IsImage) {
    if (!GetImage (&Source, &Nsource)) return (FALSE);
    if (!filename[0]) strcpy (filename, "Ximage.ps");
  } else {
    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
    if (!filename[0]) strcpy (filename, "Xgraph.ps");
  }
  
  if (RawImage) write (Source, "PSRW", 4); /* tell Ximage to ps the image */
  if (NoScale)  write (Source, "PSNS", 4); /* tell Ximage to ps the image */
  if (!RawImage && !NoScale) write (Source, "PSIT", 4); /* tell Ximage to ps the image */

  sprintf (buffer, "LEN: %11d", strlen(filename));
  write (Source, buffer, 16);
  write (Source, filename, strlen(filename));
  read (Source, buffer, 4);

  return (TRUE);
}

