# include "data.h"

int save (int argc, char **argv) {
  
  int N, n, ISCEL;
  char buffer[512], buffer2[512];
  int Ximage, Nimage;
  
  Nimage = -1;
  if (N = get_argument (argc, argv, "-n")) {
    remove_argument (N, &argc, argv);
    Nimage = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetImage (&Ximage, &Nimage)) return (FALSE);

  ISCEL = FALSE;
  if (N = get_argument (argc, argv, "-c")) {
    remove_argument (N, &argc, argv);
    ISCEL = TRUE;
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: save (overlay) <filename> [-c]\n");
    fprintf (stderr, "  -c: write contour in celestial coords\n");
    return (FALSE);
  }

  if (!SelectOverlay (argv[1], &n)) 
    return (FALSE);

  sprintf (buffer, "FILE: %d %s", n, argv[2]);
  sprintf (buffer2, "NBYTES: %8d", strlen(buffer));
  
  if (ISCEL) 
    write (Ximage, "CSVE", 4); /* force Ximage to look for the incoming image */
  else 
    write (Ximage, "SAVE", 4); /* force Ximage to look for the incoming image */
  write (Ximage, buffer2, 16);
  write (Ximage, buffer, strlen(buffer));
  
  return (TRUE);

}
