#include <exec/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dos.h>

#include <dlg/dlg.h>
#include <dlg/misc.h>

#include <link/io.h>
#include <link/lang.h>

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

#include <pragmas/dlg.h>

#include <private/Version.h>
#define  ObjRev "1"
const UBYTE version[]="\0$VER: Today In History (TDIH) " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

void  main(void);
void  CleanUp(char *);
void _CXBRK(void);

BPTR            sout    = NULL;
char            Ext[4]  = "";
FILE           *tfp     = NULL;
struct Library *DLGBase = NULL;
char          **SA;

char  filename[80] = "DLGConfig:Text/today.";
char *marray[]     = {"JAN",
                      "FEB",
                      "MAR",
                      "APR",
                      "MAY",
                      "JUN",
                      "JUL",
                      "AUG",
                      "SEP",
                      "OCT",
                      "NOV",
                      "DEC" 
                     };


void main(void)

{struct ATime tm;
 long         currtime;
 char         buffer[256];
 char         month [3];
 char         day   [3];
 char         tmonth[3];
 char         tday  [3];
 char         year  [5];
 char         spec  [2];
 UBYTE        bdayflag  = FALSE;
 UBYTE        eventflag = FALSE;

 sout = Output();
 if (!(DLGBase = OpenLibrary(DLGNAME,DLGVERSION)))
       CleanUp("Unable to open DLG.library");

 GetDevName(Ext);
 if (!(SA = getlang(Ext)))
       CleanUp("Can't read Language file");

 currtime = AmigaTime();
 UnpackTime(currtime, &tm);
 strcat(filename, marray[tm.mon]);

 if (!(tfp=fopen(filename,"r")))
       CleanUp(SA[3025]);

 do{if (!fgets(buffer,256,tfp))  break;

    if (*buffer=='*')                                      continue;
    if ((*buffer!='B') && (*buffer!='R')&&(*buffer!='S'))  continue;

    strncpy(month,buffer+1,2);
    month[2]='\0';
    strncpy(day,buffer+3,2);
    day[2]='\0';

    if (*buffer=='R')
       {strncpy(tmonth,buffer+5,2);
        tmonth[2]='\0';
        strncpy(tday,buffer+7,2);
        tday[2]='\0';
       }
     else
       {strncpy(year,buffer+5,4);
        year[4]='\0';
       }

    strncpy(spec,buffer+9,1);
    spec[1]='\0';

    if (*buffer == 'R')
       {if ((atoi(tmonth)) || (atoi(tday)))
           {if (atoi(tday) >= atoi(day))
               {if (( (atoi(month)) && (atoi(month)>(tm.mon+1)) )  ||
                   ( (atoi(tmonth)) && (atoi(tmonth)<(tm.mon+1)) )
        || ( (atoi(day)) && (atoi(day)>tm.mday) )
        || ( (atoi(tday)) && (atoi(tday)<tm.mday) )
        || ( (atoi(spec)) && (atoi(spec)!=(tm.wday+1)) ) )
       continue;
   }
   else {
     if(( (atoi(month)) && (atoi(month)>(tm.mon+1)) )
        || ( (atoi(tmonth)) && (atoi(tmonth)<(tm.mon+1)) )
        || ( ( (atoi(day)) && (atoi(day)>tm.mday) )
       && ( (atoi(tday)) && (atoi(tday)<tm.mday) ) )
        || ( (atoi(spec)) && (atoi(spec)!=(tm.wday+1)) ) )
       continue;
   }
      }
      else {
   if( ( (atoi(month)) && (atoi(month)!=(tm.mon+1)) )
      || ( (atoi(day)) && (atoi(day)!=tm.mday) )
      || ( (atoi(spec)) && (atoi(spec)!=(tm.wday+1)) ))
     continue;
      }

      if (!eventflag)
         {eventflag = TRUE;
          AFPrintf(NULL, sout, SA[3026]);
          AFPrintf(NULL, sout, SA[3027]);
         }
      AFPrintf(NULL, sout, SA[3028], buffer+10);
    }
    else
      if( ( (!atoi(month)) || (atoi(month)==(tm.mon+1)) )
    && ( (!atoi(day)) || (atoi(day)==tm.mday) )
    && ( (!atoi(spec)) || (atoi(spec)==(tm.wday+1)) )) {
   if((*buffer=='B')&&!bdayflag) {
     bdayflag=TRUE;
     AFPrintf(NULL, sout, SA[3029]);
     AFPrintf(NULL, sout, SA[3030]);
   }
   else if((*buffer=='S')&&!eventflag) {
     eventflag=TRUE;
     AFPrintf(NULL, sout, SA[3026]);
     AFPrintf(NULL, sout, SA[3027]);
   }

   if(*spec!='C') AFPrintf(NULL, sout, SA[3031],year);
   else AFPrintf(NULL, sout, SA[3032]);
   AFPrintf(NULL, sout, SA[3033],buffer+10);
      }
  }
  while(TRUE);

 AFPrintf(NULL, sout, "\n");
 CleanUp(NULL);
}


void _CXBRK(void)

{CleanUp(SA[3034]);
}


void CleanUp(char *s)

{if (tfp)      fclose(tfp);
 if (DLGBase)  CloseLibrary(DLGBase);

 if (s)
    {Write(sout, "\n Error: ", 9);
     Write(sout, s, strlen(s));
     Write(sout, "\n\n", 2);
    }

 exit(s?5:0);
}
