# include "data.h"

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

  char filename[1024], pagename[1024];
  int N, Source, Nsource, scaleMode, pageMode, IsImage;
  
  if ((N = get_argument (argc, argv, "--help"))) goto help;
  if ((N = get_argument (argc, argv, "-h"))) goto help;

  pageMode = KAPA_PS_NEWPLOT;

  /* new page? */
  if ((N = get_argument (argc, argv, "-newpage"))) {
    remove_argument (N, &argc, argv);
    pageMode = KAPA_PS_NEWPAGE;
  }
  if ((N = get_argument (argc, argv, "-raw"))) {
    remove_argument (N, &argc, argv);
    pageMode = KAPA_PS_RAWPAGE;
  }

  /* scale image? */
  scaleMode = TRUE;
  if ((N = get_argument (argc, argv, "-noscale"))) {
    remove_argument (N, &argc, argv);
    scaleMode = 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);
  }
  /* pagename ? */
  strcpy (pagename, "default");
  if ((N = get_argument (argc, argv, "-pagename"))) {
    remove_argument (N, &argc, argv);
    strcpy (pagename, 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);
  }  
  
  if ((argc > 1) && filename[0]) goto help;

  if (argc > 1) strcpy (filename, argv[1]);

  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");
  }
  
  /* tell Ximage/Xgraph to ps the image */
  KiiPS (Source, filename, scaleMode, pageMode, pagename);
  return (TRUE);

help:
  gprint (GP_ERR, "USAGE: ps [-name file.ps] [-g | -i] [-n device] [-raw] [-noscale] [-newpage] [-pagename (name]\n");
  return (FALSE);
}

