
	/* given a FITS (or pseudo-FITS) file and another file, 
	   prepend the FITS header to the other file.

	      addheader fitsfile datafile  [ outfile=new ]

	   if the 'outfile' flag is given, place the combined pseudo-FITS header
	   plus data into the specified new file.  Otherwise, overwrite the
	   original data file so that it contains the pseudo-FITS header 
	   in addition to its original contents. */

#include <stdio.h>
#include <ctype.h>
#include "pf.h"
#include "header.h"

int copy_pseudo_fits(/* char *fitsfile, *otherfile */ );
int copy_true_fits(/* char *fitsfile, *otherfile */ );

char err_msg[BIG_BUF];
char usage[] = "usage: addheader fitsfile datafile  [ outfile= ]";

main(argc, argv)
int argc;
char *argv[];
{
	int type;
	char outfile[BIG_BUF], *find(), *gotit;
	
	strcpy(outfile, "");
	if ((argc != 3) && (argc != 4)) {
		fprintf(stderr, "%s\n", usage);
		exit(1);
	}
	if (argc == 4) {
		if ((gotit = find("outfile", argv[3])) == NULL) {
			fprintf(stderr, "%s\n", usage);
			exit(1);
		}
		strcpy(outfile, gotit);
	}

	if ((type = fits_file_type(argv[1])) == NOT_FITS) {
		fprintf(stderr, "%s\n", usage);
		exit(1);
	}
	else if (type == TRUE_FITS) {
		copy_true_fits(argv[1], argv[2], outfile);
	}
	else {	
		copy_pseudo_fits(argv[1], argv[2], outfile);
	}

}

	/* copy all the header information from a pseudo-FITS file into
	   a pseudo-FITS form (with newlines at the end of each 80-char
	   line) at the beginning of the given file */

int
copy_pseudo_fits(fitsfile, otherfile, outfile)
char *fitsfile, *otherfile, *outfile;
{
	FILE *fits_fp, *other_fp, *temp_fp;
	char line[BIG_BUF + 1];
	
	if ((fits_fp = fopen(fitsfile, "r")) == NULL) {
		sprintf(err_msg, "can't open pseudo-FITS file %s\n", fitsfile);
		die(err_msg);
	}
	if ((other_fp = fopen(otherfile, "r")) == NULL) {
		sprintf(err_msg, "can't open other file %s\n", otherfile);
		die(err_msg);
	}
	if (strcmp(outfile, "") != 0) {
		if ((temp_fp = fopen(outfile, "w")) == NULL) {
			sprintf(err_msg, "can't open new file %s\n", outfile);
			die(err_msg);
		}
	}
	else if ((temp_fp = fopen(TEMPFILE, "w")) == NULL) {
		sprintf(err_msg, "can't open temporary file %s\n", TEMPFILE);
		die(err_msg);
	}

	line[BIG_BUF] = '\0';
	while (fgets(line, BIG_BUF, fits_fp) != NULL) {
		fprintf(temp_fp, "%s", line);
		if (strncmp(line, "END", 3) == 0)
			break;
	}
	while (fgets(line, BIG_BUF, other_fp) != NULL) {
		fprintf(temp_fp, "%s", line);
	}
	fclose(fits_fp);
	fclose(other_fp);
	fclose(temp_fp);

	if (strcmp(outfile, "") != 0) 
		return(0);

	/* now it's time for the ugly copying operation */
	if ((other_fp = fopen(otherfile, "w")) == NULL) {
		sprintf(err_msg, "can't open other file %s\n", otherfile);
		die(err_msg);
	}
	if ((temp_fp = fopen(TEMPFILE, "r")) == NULL) {
		sprintf(err_msg, "can't open temporary file %s\n", TEMPFILE);
		die(err_msg);
	}
	rewind(temp_fp);
	while (fgets(line, BIG_BUF, temp_fp) != NULL)		
		fprintf(other_fp, "%s", line);
	fclose(other_fp);
	fclose(temp_fp);
	if (unlink(TEMPFILE) < 0) {
		fprintf(stderr, "can't unlink temp file %s\n", TEMPFILE);
	}
}

	/* copy all the header information from a true FITS file into
	   a pseudo-FITS form (with newlines at the end of each 80-char
	   line) at the beginning of the given file */

int
copy_true_fits(fitsfile, otherfile, outfile)
char *fitsfile, *otherfile, *outfile;
{
	FILE *fits_fp, *other_fp, *temp_fp;
	char line[BIG_BUF + 1];
	
	if ((fits_fp = fopen(fitsfile, "r")) == NULL) {
		sprintf(err_msg, "can't open FITS file %s\n", fitsfile);
		die(err_msg);
	}
	if ((other_fp = fopen(otherfile, "r")) == NULL) {
		sprintf(err_msg, "can't open other file %s\n", otherfile);
		die(err_msg);
	}
	if (strcmp(outfile, "") != 0) {
		if ((temp_fp = fopen(outfile, "w")) == NULL) {
			sprintf(err_msg, "can't open new file %s\n", outfile);
			die(err_msg);
		}
	}
	else if ((temp_fp = fopen(TEMPFILE, "w")) == NULL) {
		sprintf(err_msg, "can't open temporary file %s\n", TEMPFILE);
		die(err_msg);
	}

	line[FITS_LINELEN] = '\0';
	while (fgets(line, FITS_LINELEN + 1, fits_fp) != NULL) {
		fprintf(temp_fp, "%s\n", line);
		if (strncmp(line, "END", 3) == 0)
			break;
	}
	while (fgets(line, BIG_BUF, other_fp) != NULL) {
		fprintf(temp_fp, "%s", line);
	}
	fclose(fits_fp);
	fclose(other_fp);
	fclose(temp_fp);

	if (strcmp(outfile, "") != 0) 
		return(0);

	/* now it's time for the ugly copying operation */
	if ((other_fp = fopen(otherfile, "w")) == NULL) {
		sprintf(err_msg, "can't open other file %s\n", otherfile);
		die(err_msg);
	}
	if ((temp_fp = fopen(TEMPFILE, "r")) == NULL) {
		sprintf(err_msg, "can't open temporary file %s\n", TEMPFILE);
		die(err_msg);
	}
	rewind(temp_fp);
	line[BIG_BUF] = '\0';
	while (fgets(line, BIG_BUF, temp_fp) != NULL) {
		fprintf(other_fp, "%s", line);
	}
	fclose(other_fp);
	fclose(temp_fp);
	if (unlink(TEMPFILE) < 0) {
		fprintf(stderr, "can't unlink temp file %s\n", TEMPFILE);
	}
}
		
