#include <stdio.h>
#include <math.h>
#include "bait.h"
#include "pf.h"

	/* working from a template, create a request file for all the objects in
	   a .lst file */

#undef NEWVERSION					/* define is format is same as all.lst */

#define BUFLEN      100
#define NO_MAG      -100
#define TEMPLATE    "template.rqs"		/* template request file */

main(argc, argv)
int argc;
char *argv[];
{
	int num, rah, ram, decd, decm, nras;
	double ras, decs, mag;
	char buf[BUFLEN], name[BUFLEN];
	FILE *fp;

	if (argc != 2) {
		fprintf(stderr, "usage: mkrqs file.lst\n");
		exit(-1);
	}
	if ((fp = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "can't open file %s\n", argv[1]); 
		exit(-1); 
	} 
	num = 0; 
#ifdef NEWVERSION 
	while (fgets(buf, BUFLEN, fp) != NULL) {
		if (sscanf(buf, "%d %d %lf %d %d %lf", &rah, &ram, &ras,
				&decd, &decm, &decs) != 6) {
			fprintf(stderr, "bad RA/Dec format, line ..%s..\n", buf);
			break;
		}
		strncpy(name, buf + 24, 16);
		if (strlen(name) != 16) {
			fprintf(stderr, "bad name format, line ..%s..\n", buf);
			break;
		}
		if (sscanf(buf + 39, "%lf", &mag) != 1)
			mag = NO_MAG;
		num++;
#else
	while (fgets(buf, BUFLEN, fp) != NULL) {
		if (sscanf(buf, "%02d.%02d%02d %d.%02d", &rah, &ram, &nras,
				&decd, &decm) != 5) {
			fprintf(stderr, "bad RA/Dec format, line ..%s..\n", buf);
			break;
		}
		ras = nras;
		decs = 0;
		strncpy(name, buf + 27, 20);
		if (strlen(name) != 20) {
			fprintf(stderr, "bad name format, line ..%s..\n", buf);
			break;
		}
		mag = NO_MAG;
		num++;
#endif

		create_req(rah, ram, ras, decd, decm, decs, name, mag);
	}

	fclose(fp);
}

	/* given all the information, create a simple request file */

create_req(rah, ram, ras, decd, decm, decs, name, mag)
int rah, ram, decd, decm;
double ras, decs, mag;
char *name;
{
	char rqs[BUFLEN], rqsfile[BUFLEN], val[PF_LEN + 1], buf[BUFLEN];
	pf_handle pf_out;

	if (mkid(rqs) == -1) {
		fprintf(stderr, "can't make ID \n");
		exit(-1);
	}
	strcpy(rqsfile, rqs);
	strcat(rqsfile, RQS_EXTENSION);
	copy_file(TEMPLATE, rqsfile);	

	if ((pf_out = pf_open(rqsfile, 1, PF_EXIST)) < 0) {
		fprintf(stderr, "can't pf_open file %s\n", rqsfile);
		exit(-1);
	}

	/* now fill in the few lines that are missing with the passed values */
	sprintf(buf, " %02d:%02d:%04.1lf", rah, ram, ras);
	pf_strtoval(buf, val);
	pf_putval(pf_out, "RA", val);

	sprintf(buf, "%+02d:%02d:%04.1lf", decd, decm, decs);
	pf_strtoval(buf, val);
	pf_putval(pf_out, "DEC", val);

	pf_strtoval(name, val);
	pf_putval(pf_out, "OBJECT", val);

	if (mag != NO_MAG) {
		pf_doubletoval(mag, val);
		pf_putval(pf_out, "MAGNITUD", val);
	}

	pf_close(pf_out);
}	
	 


	/* copy the first file to the second, creating the second */

copy_file(from, to)
char *from, *to;
{
	char buf[BUFLEN];
	FILE *fpfrom, *fpto;

	if ((fpfrom = fopen(from, "r")) == NULL) {
		fprintf(stderr, "can't open template file %s\n", from);
		exit(-1);
	}
	if ((fpto = fopen(to, "w")) == NULL) {
		fprintf(stderr, "can't open new file %s\n", from);
		exit(-1);
	}
	while (fgets(buf, BUFLEN, fpfrom) != NULL)
		fputs(buf, fpto);
	fclose(fpfrom);
	fclose(fpto);
}

	/* create a unique file name of the form
	              Decxxxxxx.rqs
	   where the first three letters are the current month,
	   and the x's are replaced by some characters.  
	   return 0 if OK, or -1 if something goes wrong. */

static int
mkname(s)
char *s;
{
	if (mkid(s) < 0)
		return(-1);
	strcat(s, RQS_EXTENSION);
	return(0);
}

	/* create a unique name of the form
	              Decxxxxxx
	   where the first three letters are the current month,
	   and the x's are replaced by some characters.  
	   return 0 if OK, or -1 if something goes wrong. */

static int
mkid(s)
char *s;
{
	int i;
	char buf[80], *mktemp();
	long ltime, time();

	ltime = time((long *) 0);
	strcpy(buf, ctime(&ltime));
	s[0] = buf[4];
	s[1] = buf[5];
	s[2] = buf[6];
	for (i = 3; i < 9; i++)
		s[i] = 'X';
	s[10] = '\0';
	if (mktemp(s) == NULL) 
		return(-1);
	return(0);
}

