# include "fits.h"

char *fits_keyword_end (char *line);
void pad_ending (char *line, char value, int Nbyte);

int fits_modify (Header *header, char *field, char *mode, int N,...) {
  
  /* this function expects one more argument, the value to be written */
  /* this function is extremely similar to fits_print, except it allows for changing an existing field. */

  char comment[82], string[82], data[82];
  char *p, *q;
  va_list argp;
  
  va_start (argp, N);
  bzero (data, 82);
  bzero (string, 82);
  bzero (comment, 82);

  if (mode[0] != '%') {
    fprintf (stderr, "fits_print: weird mode:  %s\n", mode);
    return (FALSE);
  }

  /* find location of desired entry */
  p = fits_header_field (header, field, N);
  if (p == NULL)  {

    /* new entry, find the END of the header */
    p = fits_header_field (header, "END", 1);
    if (p == NULL) return (FALSE); 
    
    /* is there enough space for 1 more line? */
    if (header[0].size - (p - (header[0].buffer)) < 2*FT_LINE_LENGTH) {
      header[0].size += FT_RECORD_SIZE;
      REALLOCATE (header[0].buffer, char, header[0].size);
      p = fits_header_field (header, "END", 1);
      if (p == NULL) return (FALSE); 
      memset (p + FT_LINE_LENGTH, ' ', FT_RECORD_SIZE);
    }
    
    /* push END line back 1 */
    memmove ((p + FT_LINE_LENGTH), p, FT_LINE_LENGTH);
    memset (p, ' ', FT_LINE_LENGTH);
  } else {
    q = fits_keyword_end (p);
    q += 3;
    q = MIN (p + 80, q);
    strncpy (comment, q, p + 80 - q);
  }
  pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */

  /* write the numeric modes */
  if (!strcmp (mode, "%d"))  snprintf (string, 81, "%-8s= %20d / %-s ",     field, va_arg (argp, int),      comment);
  if (!strcmp (mode, "%u"))  snprintf (string, 81, "%-8s= %20d / %-s ",     field, va_arg (argp, unsigned), comment);
  if (!strcmp (mode, "%ld")) snprintf (string, 81, "%-8s= %20ld / %-s ",    field, va_arg (argp, long),     comment);
  if (!strcmp (mode, "%hd")) snprintf (string, 81, "%-8s= %20d / %-s ",     field, va_arg (argp, int),      comment);
  if (!strcmp (mode, "%f"))  snprintf (string, 81, "%-8s= %20.10f / %-s ",  field, va_arg (argp, double),   comment); 
  if (!strcmp (mode, "%lf")) snprintf (string, 81, "%-8s= %20.10f / %-s ",  field, va_arg (argp, double),   comment); 
  if (!strcmp (mode, "%e"))  snprintf (string, 81, "%-8s= %20.10E / %-s ",  field, va_arg (argp, double),   comment); 
  if (!strcmp (mode, "%le")) snprintf (string, 81, "%-8s= %20.10E / %-s ",  field, va_arg (argp, double),   comment); 
  if (!strcmp (mode, "%g"))  snprintf (string, 81, "%-8s= %20.10G / %-s ",  field, va_arg (argp, double),   comment); 
  if (!strcmp (mode, "%lg")) snprintf (string, 81, "%-8s= %20.10G / %-s ",  field, va_arg (argp, double),   comment); 

  /* write the boolean mode */
  if (!strcmp (mode, "%t")) {
    if (va_arg (argp, int)) 
      snprintf (string, 81, "%-8s= %18s T / %-s ", field, " ", comment);
    else
      snprintf (string, 81, "%-8s= %18s F / %-s ", field, " ", comment);
  }

  /* string value.  Quotes must be at least 18 chars apart */
  if (!strcmp (mode, "%s")) {
    strncpy (data, va_arg (argp, char *), 68);
    snprintf (string, 81, "%-8s= '%-18s' / %-s ", field, data, comment);
  }

  /* comment type of value.  */
  if (!strcmp (mode, "%S")) {
    strncpy (data, va_arg (argp, char *), 71);
    snprintf (string, 81, "%-8s %-71s", field, data);
  }  

  /* just a comment associated with a value.  this is assumes a fixed format position for the comment */
  if (!strcmp (mode, "%C")) { 
    q = fits_keyword_end (p);
    strncpy (data, p + 9, MAX (MIN ((q - p - 9), 71), 0));
    strncpy (comment, va_arg (argp, char *), 80);
    pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
    snprintf (string, 81, "%-8s=%21s / %-s", field, data, comment);
  }

  strncpy (p, string, 80);
  va_end (argp);
  return (TRUE);

}

/* given a FITS card line, return pointer to the end of the data area */
char *fits_keyword_end (char *line) {

  int done;
  char *c1, *c2;

  /* find the end of the existing data region */
  c2 = line + 8;
  if (*c2 != '=') return (c2);  /* no data in COMMENT field */
  
  c1 = c2 + 2;
  if (c1[0] == 0x27) { /* entry a string, skip over 'fred' */
    for (done = FALSE, c2 = c1 + 1; !done && (c2 < line + 80); c2++) {
      if ((c2[0] == 0x27) && (c2[1] == 0x27)) {
	c2 += 2;
	continue;
      }
      if (c2[0] == 0x27) {
	c1 = c2 + 1;
	done = TRUE;
      }
    }
    if (!done) { /* error in line: mismatched ' chars, return fixed position */
      c1 = line + 30;
    }
  } else {
    while ((*c1 == ' ') && (c1 < line + 80)) c1++;
    while ((*c1 != ' ') && (c1 < line + 80)) c1++;
  }
  return (c1);
}

/* according to the FITS guidelines, a string is supposed to start with a single quote
   on the 11th character.  I'll be generous and allow the string to start at the 10th.
   I read the string into tmp then strip off the leading single quote and spaces.
   a string of "  '  fred is here  '  " should become "fred is here".
   a string of "  fred is here  " should also become "fred is here".
   the remaining problem:
   "'O''HARA'" should be kept as O''HARA.  need to test for double single quotes
   d -- /(0x2f: comment)  (*(p + i + 10) != 0x2f) &&  && (*(p + i + 10) != 0x27)
 */

/* fill 'line' with Nbyte space from first NULL to last byte with value */
void pad_ending (char *line, char value, int Nbyte) {
  
  char *p;
  int N;

  line[Nbyte-1] = 0;
  
  p = line + strlen (line);
  N = MAX (Nbyte - strlen (line) - 1, 0);
  memset (p, value, N);
}


/* in this routine, the value field will expand as needed, forcing out the comment
   the total line is limited by snprintf to only 80 chars + EOL */
