
	/* a program to calculate the current UT or LST or Julian Date 
       from the system clock, or at some time 
	   specified by the user.  Usage is 

	         lst  [ut=hh:mm:ss]  [date=dd/mm/yyyy]  [help]
	   or
	         jd  [ut=hh:mm:ss]  [date=dd/mm/yyyy]  [help]
	   or
	         ut  [lst=hh:mm:ss]  [date=dd/mm/yyyy]  [help] 

	*/

#include <stdio.h>
#include <time.h>
#include "bait.h"

char progname[100];

main(argc, argv)
int argc;
char *argv[];
{
	int i, day, month, year, ut_flag, date_flag, lst_flag;
	double jd, ut, lst, cur_jd, cur_ut, cur_lst;
	char str[20];
	long t, time();
	struct tm *tm;

	strcpy(progname, argv[0]);

	ut_flag = 0;
	date_flag = 0;

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "help") == 0) {
			if (strcmp(progname + strlen(progname) - 3, "lst") == 0)
				printf("lst [ut=hh:mm:ss] [date=dd/mm/yyyy] [help] - print local sidereal time\n");
			else if (strcmp(progname + strlen(progname) - 2, "jd") == 0)
				printf("jd [ut=hh:mm:ss] [date=dd/mm/yyyy] [help] - print Julian Date \n");
			else
				printf("ut [lst=hh:mm:ss] [date=dd/mm/yyyy] [help] - print Universal Time\n");
			exit(0);
		}
		if (strncmp(argv[i], "ut=", 3) == 0) {
			if (read_timestr(argv[i] + 3, &ut) != 0) {
				fprintf(stderr, "invalid time argument '%s'\n", argv[i]);
				exit(1);
			}
			ut_flag = 1;
			continue;
		}
		if (strncmp(argv[i], "lst=", 3) == 0) {
			if (read_timestr(argv[i] + 4, &lst) != 0) {
				fprintf(stderr, "invalid time argument '%s'\n", argv[i]);
				exit(1);
			}
			lst_flag = 1;
			continue;
		}
		if (strncmp(argv[i], "date=", 5) == 0) {
			if ((is_date(argv[i] + 5) != 0) ||
			    (sscanf(argv[i] + 5, "%d/%d/%d", &day, &month, &year) != 3)) {
				fprintf(stderr, "invalid date argument '%s'\n", argv[i]);
				exit(1);
			}
			date_flag = 1;
			continue;
		}
		/* if we get here, must be an invalid argument */
		fprintf(stderr, "invalid argument '%s'\n", argv[i]);
		exit(1);
	}


	/* first, figure out today's date */

	t = time((long *) 0);
	tm = gmtime(&t);
	if (date_flag == 0) {
		day = tm->tm_mday;
		month = tm->tm_mon + 1;        /* gmtime puts month in range 0-11 */
		year = tm->tm_year + 1900;
	}

	/* get the current time and JD */        
	cur_ut = (double)tm->tm_hour + tm->tm_min/60.0 + tm->tm_sec/3600.0;
	get_jd(day, month, year, cur_ut, &cur_jd);

	/* now, do different things depending on how the program was invoked */
	/* if invoked as "ut" */
	if (strcmp(progname + strlen(progname) - 2, "ut") == 0) {
		if (lst_flag == 0) {
			write_timestr(cur_ut, str);
			printf("%8s\n", str);
			exit(0);
		}
		else {
			get_jd(day, month, year, 0.0, &jd);
			lst2ut(jd, lst, &ut);
			write_timestr(ut, str);
		}
		printf("%8s\n", str);
		exit(0);
	}
	
	/* called as "jd" */
	if (strcmp(progname + strlen(progname) - 2, "jd") == 0) {
		if (ut_flag == 0) {
			jd = cur_jd;
		}
		else {
			get_jd(day, month, year, ut, &jd);
		}	
		printf("%.4lf\n", jd);
		exit(0);
	}
			
	/* called as "lst" */
	if (ut_flag == 0) {
		get_lst(cur_jd, &lst);
	}
	else {
		get_jd(day, month, year, ut, &jd);
		get_lst(jd, &lst);
	}
	write_timestr(lst, str);
	printf("%8s\n", str);
	exit(0);
}
	
