// 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 <unistd.h>
#include <string.h>

#define PREFIX "sat"

struct program {
	char *name;
	int (*mainfunc)(int, char **);
	char *help;
};

int dispatch(int argc, char **argv);
int help(int argc, char **argv);
int tool(int argc, char **argv);

struct program progs[] = {
	{"tool", tool, NULL},
	{"help", help, "display this help message."},
	{"cat", cat, "send input list to output."},
	{"writetle", writetle, "write out input list as a tle file."},
	{"filter", filter, "filter the input list according to an expression."},
	{"marker", marker, "write an xearth-compliant marker file."},
	{"markers", marker, NULL},
	{"datetest", datetest, NULL},
	{"live", live, "live ncurses display of the input."},
	{"predict", predict, "predict satellite passes."},
	{"passes", passes, "print the passlist in a human-readable form."},
	{"step", step, "prints out all encounters matching an expression."},
	{"summary", summary, "display a one line summary of the passes."},
	{NULL, NULL, NULL}
};

// This redispatches the command. (This handles commands of the format
// satool <command>)
int tool(int argc, char **argv) {
	return dispatch(argc - 1, &argv[1]);
}

// The help command.
int help(int argc, char **argv) {
	struct program *prog;
	
	printf("Command supported by sattool are:\n");
	prog = progs;

	while (prog->name) {
		if (prog->help)
			printf("  %-8s - %s\n", prog->name, prog->help);
		prog++;
	}
}

// This dispatches the command, calling the appropriate function from
// the table above.
int dispatch(int argc, char **argv) {
	struct program *prog;

	if (!argc) {
		error("Command name not supplied.\n");
		return -1;
	}
	
	if (!argv[0]) {
		error("Command name is NULL.\n");
		return -1;
	}
	
	prog = progs;
	while(prog->name) {
		if (!strcmp(prog->name, argv[0]))
			return prog->mainfunc(argc, argv);
		prog++;
	}

	error("Command '%s' not found.\n", argv[0]);
}
	
// This looks to see the name of the command, and then dispatches
// it appropriately.
int main(int argc, char **argv) {
	char *zeroname;
	char *zerotmp;
	int prefixlen;
	
	zeroname = argv[0];
	while (zerotmp = index(zeroname, '/'))
		zeroname = zerotmp + 1;

	prefixlen = strlen(PREFIX);
	
	if (!strncmp(zeroname, PREFIX, prefixlen)) {
		argv[0] = &zeroname[prefixlen];
	} else {
		argv++;
		argc--;
	}

	return dispatch(argc, argv);
}
