
/*
 * 1/2/2001 -- fix bug in main() by putting the strcpy calls for
 *             "keyword" and "val" into a loop over MAX_PAIRS
 *             MWR
 */


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "bait.h"
#include "pf.h"
#include "header.h"
#include "fits.h"


	/* put a header-line value into a true FITS or pseudo-FITS file. 
	   The syntax is:

	          putsym file=name [stanza=n] symbol=value [symbol=value ...]

	   where 'file' is the pseudo-FITS file to modify.

	   If 'stanza' is not specified, the first stanza is used (this 
	   applies only to pseudo-FITS files, of course).

	   The program inserts the symbol if it does not already exist, or
	   replaces the old value with the new one. if the specified 
	   file cannot be found, or if there is some problem inserting
	   the value, a non-zero return code is returned and a message
	   printed to stderr. */

#define MAX_PAIRS      50
#define CARD_LEN       80

static int put_pseudo( /* char *filename, int stanza, num_pairs */ );
static int put_true( /* char *filename, int num_pairs */ );
static int get_pair( /* char *str, *keyword, *value */ );

static char keyword[MAX_PAIRS][PF_LEN + 1];
static char val[MAX_PAIRS][PF_LEN + 1];
char *progname = "putsym";

main(argc, argv)
int argc;
char *argv[];
{
	int i, j, num_pairs, type, stanza, ret;
	char filename[100];
	char *gotit, *find();
	pf_handle pf_in;

	if (argc < 3) {
		fprintf(stderr, "usage: putsym file=name [stanza=n] keyword=value ...\n");
		exit(1);
	}

	num_pairs = 0;
	stanza = 1;
	strcpy(filename, "");
	for (i = 0; i < MAX_PAIRS; i++) {
		strcpy(keyword[i], "");
		strcpy(val[i], "");
	}

	for (i = 1; i < argc; i++) {
		if ((gotit = find("file", argv[i])) != NULL) {
			strcpy(filename, gotit);
			continue;
		}
		if ((gotit = find("stanza", argv[i])) != NULL) {
			if (sscanf(gotit, "%d", &stanza) != 1) {
				fprintf(stderr, "bad value for stanza in arg ..%s..\n",
								argv[i]);
				exit(1);
			}
			continue;
		}
		if (get_pair(argv[i], keyword[num_pairs], val[num_pairs]) < 0) {
			fprintf(stderr, "putsym: invalid keyword=value pair: ..%s..\n",
				argv[i]);
			exit(1);
		}
		else {
			if (++num_pairs >= MAX_PAIRS) {
				fprintf(stderr, "putsym: only putting first %d specified values\n",
						MAX_PAIRS);
				break;
			}
		}
	}
	if (strcmp(filename, "") == 0) {
		fprintf(stderr, "usage: putsym file=name [stanza=n] keyword=value ...\n");
		exit(1);
	}

	for (i = 0; i < num_pairs; i++) {
		if (strcmp(keyword[i], "") == 0) {
			fprintf(stderr, "usage: putsym file=name [stanza=n] keyword=value ...\n");
			exit(1);
		}
		for (j = 0; j < strlen(keyword[i]); j++)
			keyword[i][j] = toupper(keyword[i][j]);
	}

	if ((type = fits_file_type(filename)) == NOT_FITS) {
		fprintf(stderr, "can't open file %s, or type not FITS-like\n", filename);
		exit(1);
	}
	if (type == PSEUDO_FITS)
		ret = put_pseudo(filename, stanza, num_pairs);
	else 
		ret = put_true(filename, num_pairs);

	exit(ret);
}


	/* perform the insertion for a true FITS file. return 0 if all goes
	   well, or 1 if there's a problem. */

static int
put_true(filename, num_pairs)
char *filename;
int num_pairs;
{
	int i;
	unsigned nrow, ncol;
	char str[CARD_LEN + 1];
	FITS_HANDLE fh;

	if ((fh = fits_open(filename, "r+", &nrow, &ncol)) < 0) {
		fprintf(stderr, "can't open file %s, or it isn't FITS\n", filename);
		return(1);
	}

	for (i = 0; i < num_pairs; i++) {
		if (strcmp(keyword[i], "COMMENT") == 0) {
			strncpy(str, "COMMENT   ", 10);
			if (strlen(val[i]) < CARD_LEN - 10) {
				strcpy(str + 10, val[i]);
			}
			else {
				strncpy(str + 10, val[i], CARD_LEN - 10);
				str[CARD_LEN] = '\0';
			}
			if (fits_put_headerline(fh, str) != FITS_PASS) {
				fits_close(fh);
				fprintf(stderr, "error inserting comment ..%s.. to file %s\n",
						str, filename);
				return(1);
			}
			else
				continue;
		}

		if (fits_put_symbol(fh, keyword[i], val[i]) != FITS_PASS) {
			fits_close(fh);
			fprintf(stderr, "error inserting %s=%s to file %s\n", 
					keyword[i], val[i], filename);
			return(1);
		}
		else
			continue;
	}

	fits_close(fh);
	return(0);
}


	/* perform the insertion for a pseudo-FITS file */

static int
put_pseudo(filename, stanza, num_pairs)
char *filename;
int stanza, num_pairs;
{
	int i;
	char str[PF_LEN + 1];
	pf_handle pf_in;

	if ((pf_in = pf_open(filename, stanza, PF_EXIST)) < 0) {
		fprintf(stderr, "can't open file %s stanza %d\n", filename, stanza);
		exit(1);
	}

	for (i = 0; i < num_pairs; i++) {
		if (strcmp(keyword[i], "COMMENT") == 0) {
			strncpy(str, "COMMENT   ", 10);
			if (strlen(val[i]) < PF_LEN - 10) {
				strcpy(str + 10, val[i]);
			}
			else {
				strncpy(str + 10, val[i], PF_LEN - 10);
				str[PF_LEN] = '\0';
			}
			if (pf_putcomment(pf_in, str) == NULL) {
				pf_close(pf_in);
				fprintf(stderr, "error inserting comment ..%s.. to file %s\n",
						str, filename);
				return(1);
			}
			else
				continue;
		}

		if (pf_putval(pf_in, keyword[i], val[i]) == NULL) {
			pf_close(pf_in);
			fprintf(stderr, "error inserting %s=%s to file %s\n", 
						keyword[i], val[i], filename);
			return(1);
		}
		else
			continue;
	}

	pf_close(pf_in);
	return(0);
}
	

	/* given a string of the form (we hope)
	               keyword=value
	   copy into 'keyword' the part of the string up to the equal-sign,
	   and into value the part that comes afterwards. return 0 if it
	   looks OK, or 1 if there's some problem. */

static int
get_pair(str, keyword, val)
char *str, *keyword, *val;
{
	char *p, *q;

	if ((p = strchr(str, DELIM_CHAR)) == NULL)
		return(0);
	for (q = str; q != p; q++)
		*keyword++ = *q;
	*keyword = '\0';
	for (q = p + 1; *q != '\0'; q++) 
		*val++ = *q;
	*val = '\0';

	return(0);
}
