/* Recall.c
** sc link optimize nostandardio smallcode smalldata Recall.c lib:reqtools.lib
*/
#include <stdio.h>
#include <stdlib.h>
#include <libraries/reqtools.h>
#include <intuition/intuition.h>
#include	<exec/alerts.h>

#include <exec/types.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/reqtools.h>

#include "Includes/Globals.h"
#include "Includes/Date_Time_Utils.h"
#include "Includes/MyAlert.h"
#include "Includes/FileUtils.h"

struct Library			*IntuitionBase;
struct ReqToolsBase *ReqToolsBase;

#define Once_A_Day	eventnode.frequent==1
#define	Always			eventnode.frequent==0
#define REQUESTER		0
#define	ALERT				1
#define	RUN_PROGRAM	2
#define TMP_FILE		"RAM:Reca.tmp"

/* PROTOTYPE */
void cancel(void);

main(int argc,char *argv[]) {
	int		lines=0;
	BOOL	first=FALSE,run=FALSE,save=FALSE;
	struct Date datenow,eventdate;
	struct Time timenow,eventtime;
	struct Config			config;
	struct EventNode	eventnode;
	struct TextNode		textnode;
	FILE *inprefs,*outprefs;
	char  filename[255]=prefsdir;
	char	buf[8],*text;

	if(argc>1)
		strcpy(filename,argv[1]);
	else
		strcat(filename,prefsname);

	if((text=malloc(2500))==NULL)
		exit(0);

	if(IntuitionBase=OpenLibrary("intuition.library",37)) {
		if ( (ReqToolsBase=(struct ReqToolsBase *)OpenLibrary
											(REQTOOLSNAME, REQTOOLSVERSION)) != NULL ) {
			if((inprefs=fopen(filename,"r")) && (outprefs=fopen(TMP_FILE,"w"))) {
				if(fread(&config,1,sizeof(config),inprefs)) {
					fwrite(&config,1,sizeof(config),outprefs);
					if(stricmp(CONFIGHEADER,config.header)!=0) {
						rtEZRequest("The loaded configuration is not\n"
												"a %s configuration!",
												"I guess not...",NULL,
												(struct TagItem *)normaltag,
												recall);
						exit(0);
					}
					}// else cancel();

				getclk(buf);
				datenow.DD=buf[3];
				datenow.MM=buf[2];
				datenow.YY=buf[1]+80;
				timenow.HH=buf[4];
				timenow.MM=buf[5];

				while(!feof(inprefs)) {
					if(fread(&eventnode,1,sizeof(eventnode),inprefs)) {
						eventdate=to_date(eventnode.date,DDMMYY);
						eventtime=to_time(eventnode.time);

						if(	checkdate(eventdate,datenow,eventnode.prefixdate,
								eventnode.days,eventnode.repeat) &
								cmptime(eventtime,timenow,eventnode.prefixtime))
							run=TRUE;
						else
							run=FALSE;

						if(run)
							if((datenow.DD!=eventnode.DD | datenow.MM!=eventnode.MM |
									datenow.YY!=eventnode.YY)) {
								first=TRUE;
								eventnode.DD=datenow.DD;
								eventnode.MM=datenow.MM;
								eventnode.YY=datenow.YY;
								}
							else
								first=FALSE;
						fwrite(&eventnode,1,sizeof(eventnode),outprefs);

						lines=0;
						strcpy(text,"");
						while(eventnode.lines--) {
							if(fread(&textnode,1,sizeof(textnode),inprefs)==0)
								;//cancel();
							fwrite(&textnode,1,sizeof(textnode),outprefs);
							if(run) {
								if(lines++)
									strcat(text,"\n");
								strcat(text,CalcField(textnode.text,eventnode.calctype,datenow));
							}
						}
						strcat(text,'\0');
						if((Once_A_Day & first) | Always) {
							if(Once_A_Day & first)
								save=TRUE;
							if(lines && run) {
								if(eventnode.type==REQUESTER)
									if(config.CentreRequesters)
										rtEZRequest ("%s","OK",NULL,(struct TagItem *)centretag,text);
									else
										rtEZRequest ("%s","OK",NULL,(struct TagItem *)normaltag,text);
								else if(eventnode.type==ALERT)
									MyAlert(RECOVERY_ALERT,text);
								else
									if(System(text,NULL))
										rtEZRequest("Could not run file:\n'%s'",
																"Ok", NULL,(struct TagItem *)normaltag,text);
							}
						}
					}// else cancel();
				}
				fclose(inprefs);
				fclose(outprefs);
				if(save) {
					if(!MoveFile(TMP_FILE,filename))
						rtEZRequest("Unable to update %s.\n"
												"All changed fields will be lost!",
												"OK", NULL,(struct TagItem *)normaltag,filename);
					}
				else
					DeleteFile(TMP_FILE);
			} else
				rtEZRequest("Could not open '%s'\nor temporary file!",
										"OK", NULL,(struct TagItem *)normaltag,filename);
			CloseLibrary((struct Library *)ReqToolsBase);
		}
		CloseLibrary(IntuitionBase);
	}
	if(text) free(text);
}

void cancel(void) {
	DisplayBeep(NULL);
	rtEZRequest("Error discovered in loaded project!",
							"OK",NULL,
							(struct TagItem *)normaltag);
	CloseLibrary((struct Library *)ReqToolsBase);
	CloseLibrary(IntuitionBase);
	exit(0);
}
