#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

/*
#include "memory.h"
#include "FieldCore.h"
#include "FieldType.h"
#include "verbrgy.h"
#include "action.h"
#include "altime.h"
#include "registry.h"
#include "darray.h"
*/
#include "data.h"
#include "dataP.h"
#include "bool.h"
/*
#include "rule.h"
#include "ruleset.h"
#include "RSreg.h"
*/
#define MAX_INPUT	128
#define	MAX_TIME_STRING	128	/* obviously not */
#define HELP_START	puts("start: enables logging and sets logging time for specified categories");\
			puts("  Usage: start {ra, rl, msg, all} [<start-time>]");\
			puts("  You will be queried for end time or period length.\n");
#define HELP_LOG	puts("log: enables or disables logging independent of start/stop times");\
			puts("  Usage: log {ra, rl, msg, all}-{on, off}");\
/**/			puts("  Example: log ra-off\n");
#define HELP_SAVE	puts("save: saves changes.\n");
#define	HELP_STATUS	puts("status: print start and stop times for collection categories.\n");
#define HELP_EXIT	puts("exit: saves changes then exits.\n");
#define HELP_ABORT	puts("abort: exits without saving changes.\n");
#define HELP_HELP	puts("help, ?: prints this message or help on specified command");\
			puts("  Usage: help [<command>]\n");


Bool
first(string, word)
char	*string,
	*word;
{
	char	buf[MAX_INPUT];

	sscanf(string, "%s", buf);
	return strcmp(buf, word) ? Bool_FALSE : Bool_TRUE;
}

char *
skip_white(str)
char 	*str;
{
	if (str == NULL)
		return (char *)NULL;
	while(*str && isspace(*str))
		++str;
	return str;
}

print_help(arg)
char	*arg;
{
	if(arg == (char *)NULL)
	{	HELP_START
		HELP_LOG
		HELP_SAVE
		HELP_STATUS
		HELP_EXIT
		HELP_ABORT
		HELP_HELP
		return;
	}
	arg = skip_white(arg);
	if(!strcmp(arg, "start"))
	{	HELP_START
	}
	else if(!strcmp(arg, "log"))
	{	HELP_LOG
	}
	else if(!strcmp(arg, "save"))
	{	HELP_SAVE
	}
	else if(!strcmp(arg, "status"))
	{	HELP_STATUS
	}
	else if(!strcmp(arg, "exit"))
	{	HELP_EXIT
	}
	else if(!strcmp(arg, "abort"))
	{	HELP_ABORT
	}
	else if(!strcmp(arg, "help"))
	{	HELP_HELP
	}
}

set_start(str)
char	*str;
{
	AlDCinfo	plug;
	static char	buf[MAX_INPUT];
	char		s_time[MAX_TIME_STRING];
	time_t	t;
/*	EntCode	code;*/
	short	code;

	if(str == (char *)1)
	{	HELP_START
		return;
	}
	sscanf(str, " %s", buf);
	str = (char *)index(str, ' ');
	if(!strcmp(buf, "ra"))
		code = R_ACT;
	else if(!strcmp(buf, "rl"))
		code = R_LIFE;
	else if(!strcmp(buf, "msg"))
		code = MSG;
	else if (!strcmp(buf, "all"))
		code = R_ACT + R_LIFE + MSG;
	else
	{	printf("Unknown type of data collection: %s\n", buf);
		HELP_START
		return;
	}
	if(str == NULL || *(str = skip_white(str)) == '\0')
	{	printf("Start time (default: now): ");
		if(gets(buf) == NULL)	/* ^D */
			return;
		if(*buf != '\0')
			strcpy(s_time, buf);
		else
			strcpy(s_time, "now");
	} 
	else
	{	sscanf(str, "%s", s_time);
		str = (char *)index(str, ' ');
	}
	t = getdate(s_time, 0);	
	if(t == -1)
	{	printf("Cannot parse date: %s\n", s_time);
		return;
	}
	if(str != NULL && *(skip_white(str)) != '\0')
		printf("(I am stupid and ignoring \"%s\")\n", skip_white(str));
	printf("until date/time ('i' to set time interval, RET for quit-option): ");
/***/
	s_time[0] = '\0';
	while(gets(s_time) != NULL)
	{	time_t	tmp;
		short c;

		if(s_time[0] == '\0')
			return;
		if(!strcmp(s_time, "i"))
			break;
		tmp = getdate(s_time, 0);
		if(tmp == (time_t)-1)
		{	printf("Cannot parse date: %s\nuntil: ", s_time);
			s_time[0] = '\0';
			continue;
		}
		/* What ugly code!!!!! */
		if(code < (short)PLUGS)
		{	plug = &AlDC_plugs[code];
			plug->start = t;
			plug->stop = tmp;
			plug->data = (int)(tmp - t);
			return;
		}
		for(c = 0; c < (int)PLUGS; c++)
		{	plug = &AlDC_plugs[(EntCode)c];
			plug->start = t;
			plug->stop = tmp;
			plug->data = (int)(tmp - t);
		}
		return;
	} 
	if(s_time[0] == '\0')
	{	putchar('\n');
		exit(0);
	}
	printf("Interval unit (Hour, Day, Week, Month, Six-months, or RET): ");
	if(gets(buf) == NULL)
	{	putchar('\n');
		exit(0);
	}
	if(buf[0] == '\0')
		return;
	if(code == R_LIFE + R_ACT + MSG)
	{	Al_general_col_int(t, buf, R_LIFE);
		AlData_general_enable(R_LIFE);
		AlData_periodic_col_int(t, buf, MSG);
		AlData_general_enable(MSG);
		AlData_periodic_col_int(t, buf, R_ACT);
		AlData_general_enable(R_ACT);
	}
	else if(code == R_LIFE)
	{	Al_general_col_int(t, buf, code);
		AlData_general_enable(code);
	}
	else if(code == R_ACT || code == MSG)
	{	AlData_periodic_col_int(t, buf, code);	/* does special stuff */
		AlData_general_enable(code);
	}
}
print_status()
{
	AlDCinfo	plug;
	EntCode		i;
	char		t_str[26];

	printf("\t\tEnabled\t\tStart\t\t\t\tStop\n");
	for(i = 0; i < PLUGS; i++)
	{	switch(i) {
		case R_ACT:
			printf("Rule-activity:");
			break;
		case R_LIFE:
			printf("Rule-life:");
			break;
		case MSG:
			printf("Message-log:");
			break;
		}
		plug = &AlDC_plugs[i];
		my_ctime(plug->start);
		printf("\t%s\t%s\t%s\n", plug->enabled ? "Yes" : "No",
		       t_str, ctime(&plug->stop));
	}
}

main(argc, argv)
int	argc;
char	*argv[];
{
	static char	input[MAX_INPUT];
	char		*i_ptr;

	while(*argv++)
		if(!strcmp(*argv, "-help") || !strcmp(*argv, "-h"))
		{	print_help((char *)NULL);
			exit(0);
		}
	i_ptr = input;
	puts("\t\t**Lens Data-collection Control**");
	puts("NOTE: changes saved only on explicit \"save\" command or on");
	puts("      exit with \"quit,\" \"exit,\" or \"bye.\"\n");
	AlProfile_init();
	AlProfile_setup();
	AlData_init();
	while(1)
	{	char	*ptr;

		printf("lensdc-> ");
		i_ptr = skip_white(gets(input));
		if(i_ptr == NULL || !strcmp(i_ptr, "abort"))
		{	puts("\nNo changes saved (since last save).");
			exit(0);
		}
		if(*i_ptr == '\0')
			continue;
		if(!strcmp(i_ptr, "quit") ||
		   !strcmp(i_ptr, "exit") ||
		   !strcmp(i_ptr, "bye"))
			break;
		if(*i_ptr == '?' || first(i_ptr, "help"))
		{	print_help((char *)index(i_ptr, ' '));
			continue;
		}
		if(first(i_ptr, "log"))
		{	if((ptr = (char *)index(i_ptr, ' ') + 1) == (char *)1)
			{	HELP_LOG
				continue;
			}
			AlData_set_option(ptr);
			continue;
		}

		if(first(i_ptr, "start"))
		{	set_start((char *)index(i_ptr, ' ')+1);
			continue;
		}
		if(first(i_ptr, "save"))
		{	AlData_save_state();
			continue;
		}
		if(first(i_ptr, "status"))
		{	print_status();
			continue;
		}
/*		if(first(i_ptr, "period"))
		{	set_period((char *)index(i_ptr, ' ')+1);
			continue;
		}
*/		printf("%s: Unknown lensdc command\n", i_ptr);
	}
	AlData_save_state();
}
