// sattool - visual satellite tracking and prediction tool.
// Copyright 2000 Tom Rothamel <tom-idbg@onegeek.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  

#include "sattool.h"
#include "filter.h"
#include <stdlib.h>

// This handles the stepping of a single pass.
void StepPass(FILE *of, Pass *p, int stepsize, Step *step) {
	double t;
	double ds;
	int banner = 1;
	Encounter *e;
	
	ds = SECDAY * stepsize;
	e = new Encounter();
	
	t = p->max;
	while (t > p->min0) t -= ds;

	while ((t += ds) < p->min1) {
		e->calc(p->sat, p->site, t);
		e->calcmore();
		
		if (!step->eval(e)) continue;	

		if (banner) {
			banner = 0;
			fprintf(of, "%05d %s %-30s (%.3f days old.)\n",
				p->sat->norad, p->sat->desig,
				p->sat->name, t - p->sat->epoch);
		}

		print_encounter(of, p, "\t", t);
	}
	
	if (!banner) fprintf(of, "\n");
	delete e;
}
		
// This is the step command.
int step(int argc, char **argv) {
	Options *o;
	List *l;
	Thing *t;
	Pass *p;
	Encounter *e;
	Step *step;
	FILE *of;
	int optoff;
	int stepsize;

	
	o = ParseGlobalOptions(argc, argv, &optoff);

	if (optoff >= argc - 1) {
		error("step takes an argument and an expression.\n");
		return -1;
	}

	stepsize = atoi(argv[optoff]);
	if (stepsize <= 0) {
		error("The stepsize must be a positive integer number of seconds.\n");
		return -1;
	}

	step = filter_setup(argc, optoff+1, argv);
	if (!step) return -1;
	
	l = ReadInput(o);
	of = OutputFile(o);
	
	while (t = l->next()) {
		p = getPass(t);

		if (!p) continue;
		
		StepPass(of, p, stepsize, step);
	}

	fclose(of);
}
		
		
