# include "data.h"

int wd (int argc, char **argv) {
  
  int N, Extend;
  int newUnsign, newBitpix, newScale, newZero;
  int outUnsign, outBitpix;
  double outScale, outZero;
  Header temp_header;
  Matrix temp_matrix;
  Buffer *buf;

  /* XXX I must have dropped the old 'newplane' option */
  Extend  = FALSE;
  if ((N = get_argument (argc, argv, "-extend"))) {
    remove_argument (N, &argc, argv);
    Extend  = TRUE;
  }

  outZero = 0;
  newZero = FALSE;
  if ((N = get_argument (argc, argv, "-bzero"))) {
    remove_argument (N, &argc, argv);
    outZero  = atof(argv[N]);
    newZero  = TRUE;
    remove_argument (N, &argc, argv);
  }

  outScale = 1;
  newScale = FALSE;
  if ((N = get_argument (argc, argv, "-bscale"))) {
    remove_argument (N, &argc, argv);
    outScale = atof(argv[N]);
    newScale = TRUE;
    remove_argument (N, &argc, argv);
  }

  outBitpix = 16;
  newBitpix = FALSE;
  if ((N = get_argument (argc, argv, "-bitpix"))) {
    remove_argument (N, &argc, argv);
    outBitpix = atof(argv[N]);
    newBitpix = TRUE;
    remove_argument (N, &argc, argv);
  }

  outUnsign = FALSE;
  newUnsign = FALSE;
  if ((N = get_argument (argc, argv, "-unsign"))) {
    remove_argument (N, &argc, argv);
    outUnsign = -1;
    if (!strcasecmp (argv[N], "t") || !strcasecmp (argv[N], "true")) outUnsign = TRUE;
    if (!strcasecmp (argv[N], "f") || !strcasecmp (argv[N], "false")) outUnsign = FALSE;
    if (outUnsign == -1) {
      gprint (GP_ERR, "-unsign options: t, f, true, false\n");
      return (FALSE);
    }
    newUnsign = TRUE;
    remove_argument (N, &argc, argv);
  }

  if (argc != 3) {
    gprint (GP_ERR, "USAGE: wd <buffer> <filename> [-bitpix N] [-bscale X] [-bzero X] [-extend] [-newplane]\n");
    return (FALSE);
  }

  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);

  if (!newBitpix) outBitpix = buf[0].bitpix;
  if (!newScale) outScale = buf[0].bscale;
  if (!newZero) outZero = buf[0].bzero;
  if (!newUnsign) outUnsign = buf[0].unsign;

  /* Convert the buffer from (float) to correct format */
  /* save the (float) version, write out a temporary buffer */
  temp_matrix = buf[0].matrix;
  ALLOCATE (temp_matrix.buffer, char, MAX(1, temp_matrix.size));
  memcpy (temp_matrix.buffer, buf[0].matrix.buffer, temp_matrix.size);
  temp_header = buf[0].header;
  ALLOCATE (temp_header.buffer, char, MAX(1, temp_header.size));
  memcpy (temp_header.buffer, buf[0].header.buffer, temp_header.size);

  if (temp_header.Naxes) gfits_convert_format (&temp_header, &temp_matrix, outBitpix, outScale, outZero, outUnsign);

  if (Extend) {
    Header Xhead;
    FILE *f;
    int status, Nextend;

    /* assume failure means non-existent file */
    if (!gfits_read_header (argv[2], &Xhead)) {

      Xhead.simple = TRUE;
      Xhead.bitpix = 16;
      Xhead.Naxes = 0;
      
      Xhead.extend = TRUE;
      Xhead.bscale = 1.0;
      Xhead.bzero  = 0.0;
      Xhead.unsign = FALSE;
      
      gfits_create_header (&Xhead);
      gfits_modify (&Xhead, "NEXTEND", "%d", 1, 0);
      f = fopen (argv[2], "w");
      fclose (f);
    }

    gfits_modify (&Xhead, "EXTEND", "%t", 1, TRUE);

    Nextend = 0;
    gfits_scan (&Xhead, "NEXTEND", "%d", 1, &Nextend);
    Nextend ++;
    gfits_modify (&Xhead, "NEXTEND", "%d", 1, Nextend);

    /* write the main header to the start of the file */
    f = fopen (argv[2], "r+");
    if (f == NULL) {
      gprint (GP_ERR, "failed to write file\n");
      status = FALSE;
      goto done1;
    }
    
    /* position to begining of file to write header */
    fseek (f, 0, SEEK_SET);
    status = fwrite (Xhead.buffer, 1, Xhead.size, f);
    if (status != Xhead.size) {
      gprint (GP_ERR, "ERROR: failed writing data to image header\n");
      status = FALSE;
      goto done1;
    }
    
    /* fix up header */
    {
      static char simple[] = "XTENSION= 'IMAGE  '            / Image extension";
      int Ns, No;
      Ns = strlen (simple);
      No = 80 - Ns;
      strncpy (temp_header.buffer, simple, Ns);
      memset (&temp_header.buffer[Ns], ' ', No);
    }

    /* position to end of file to write new extend */
    fseek (f, 0, SEEK_END);
    status = fwrite (temp_header.buffer, 1, temp_header.size, f);
    fclose (f);
    if (status != temp_header.size) {
      gprint (GP_ERR, "failed to write file\n");
      status = FALSE;
      goto done1;
    }
    /* write the matrix buffer (automatically goes to end of file */
    if (!gfits_write_matrix (argv[2], &temp_matrix)) {
      gprint (GP_ERR, "failed to write file\n");
      status = FALSE;
      goto done1;
    }
    status = TRUE;
  done1:
    gfits_free_header (&Xhead);
    gfits_free_header (&temp_header);
    gfits_free_matrix (&temp_matrix);
    return (status);
  }
  
  /* the actual write-to-disk goes here */
  if (!gfits_write_header (argv[2], &temp_header)) {
    gprint (GP_ERR, "failed to write header\n");
    gfits_free_header (&temp_header);
    gfits_free_matrix (&temp_matrix);
    return (FALSE);
  }
  
  if (!gfits_write_matrix (argv[2], &temp_matrix)) {
    gprint (GP_ERR, "failed to write matrix\n");
    gfits_free_header (&temp_header);
    gfits_free_matrix (&temp_matrix);
    return (FALSE);
  }

  gfits_free_header (&temp_header);
  gfits_free_matrix (&temp_matrix);
  
  return (TRUE);
}
