#include "memo.h"


list_memos(nmemos, memos)
int nmemos;
char *memos[];
{
	long start_t, stop_t, repeat_t, next_t, time(), now_t = time(0);
	char status = ' ', text[MAXLINE];
	char *ctime(), tstring[MAXLINE];
	int i;

	clr_specs();
	if (nmemos == 0)
		parse_specs("a");
	else {
		while (nmemos-- > 0)
			parse_specs(*memos++);
	}
	
	if ((fp = fopen(mfile, "r")) == NULL)
		fprintf(stderr, "memo: can't open memo file %s\n", mfile);
	else {
		for (i=1; fscanf(fp, "%ld%ld%ld", &start_t, &stop_t, &repeat_t) != EOF && fgets(text, MAXLINE, fp) != NULL; i++) {
			if (!check_specs(i))
				continue;
			if (now_t > stop_t && repeat_t == 0)
				status = '-';
			if (current(start_t, stop_t, repeat_t))
				status = '+';
			printf("%c%3d) %s", status, i, text);
			printf("\tValid from %.24s", ctime(&start_t));
			if (stop_t != 0) {
				printf(" until %.24s", ctime(&stop_t));
				if (repeat_t == 0)
					printf(".\n");
				else {
					rctime(repeat_t, tstring);
					printf(",\n\trepeated every %s.", tstring);
					if (current(start_t, stop_t, repeat_t))
						printf("  Valid now.\n");
					else {
						next_t = (now_t < start_t) ? start_t : (now_t - (now_t-start_t)%repeat_t + repeat_t);
						printf("  Next valid %.24s.\n", ctime(&next_t));
					}
				}
			}
			else
				printf(".\n");
		}
		fclose(fp);
		printf("(It is now %.24s.)\n", ctime(&now_t));
	}
}
