/*** Include Files ***/

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/notify.h>
#include <dos/dostags.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#include <HBBS/release.h>
char *versionstr="$VER: AutoComment "RELEASE_STR;

/*** Some Variables ***/

#define MAX_FILES 200
char filenames[2][MAX_FILES][33];
int  amountfiles[2]={0,0};
BOOL quiet=FALSE;

char dirname[101]="HBBS:Nodes/Node0/PlayPen";

extern struct Library *DOSBase;

/*** start program ***/

void CommentTheFile(char *filename)
{
  char *name;
  int namelen;

  namelen=strlen(filename)+strlen(dirname)+2;
  name=malloc(namelen);
  strcpy(name,dirname);
  AddPart(name,filename,namelen);
  if (!quiet) printf("namelen %d, name %s\n",namelen,name);
  SetComment(name,"This file came from Utopia!");
}

void GetFileList(int buffer)
{
  BPTR FL;
  struct FileInfoBlock *FB;
  long err;
  char tmpstr[401];
  BPTR fp;

  amountfiles[buffer]=0;
  if (FB=AllocMem(sizeof(struct FileInfoBlock),0L))
  {
    if ((FL=Lock(dirname,(long)ACCESS_READ))==NULL)
    {
      if (!quiet) printf("Error Locking Dir!");
    }
    else
    {
      err= !Examine(FL,FB);
      if (!err)
      {
        do
        {
          err=!ExNext(FL,FB);
          if (!err)
          {
            if (FB->fib_DirEntryType<0 && FB->fib_Size>0 && amountfiles[buffer]<MAX_FILES)
            {
              strcpy(tmpstr,dirname);
              AddPart(tmpstr,FB->fib_FileName,400);
              if ((fp=Open(tmpstr,MODE_OLDFILE))!=NULL)
              {
                Close(fp);
                strcpy(filenames[buffer][amountfiles[buffer]],FB->fib_FileName);
                amountfiles[buffer]++;
              }
            }
          }
        } while (!err);
      }
      UnLock(FL);
    }
    FreeMem(FB,sizeof(struct FileInfoBlock));
  }
  else if (!quiet) printf("Memory allocation error!!");

}

void CheckNewFiles(void)
{
  int loop1,loop2;
  BOOL found=FALSE;

  for (loop1=0;loop1<amountfiles[0];loop1++)
  {
    loop2=0;
    found=FALSE;
    do
    {
      /*if (!quiet) printf("Comparing %d to %d\n",loop1,loop2);*/
      if (stricmp(filenames[0][loop1],filenames[1][loop2])==0)
      {
        found=TRUE;
      }
      loop2++;
    } while (loop2<amountfiles[1] && !found);
    if (!found)
    {
      CommentTheFile(filenames[0][loop1]);
    }
  }
}

void CopyFileList(int from, int to)
{
  int loop;

  for (loop=0;loop<amountfiles[from];loop++)
  {
    strcpy(filenames[to][loop],filenames[from][loop]);
  }
  amountfiles[to]=amountfiles[from];
}

void main(int argc, char **argv)
{

  BOOL done=FALSE;
  struct NotifyRequest *notifyrequest;
  UBYTE *filename;
  LONG signum;
  ULONG signals;

  int loop;

  for (loop=1;loop<argc;loop++)
  {
    printf("%d %s",loop,argv[loop]);
    if (stricmp("QUIET",argv[loop])) quiet=TRUE;
  }

  if (!quiet) printf("\23332m\2331m--> AutoComment! <--\2330m V1.0 (C) 1994 Hydra/dAT!\n");

  if (DOSBase->lib_Version >= 37)
  {
    /* Allocate a NotifyRequest structure */
    if (notifyrequest = AllocMem(sizeof(struct NotifyRequest), MEMF_CLEAR))
    {
      /* And allocate a signalsbit */
      if ((signum = AllocSignal(-1L)) != -1)
      {
        /* Initialize notification request */
        filename = dirname;
        notifyrequest->nr_Name = filename;
        notifyrequest->nr_Flags = NRF_SEND_SIGNAL;
        /* Signal this task */
        notifyrequest->nr_stuff.nr_Signal.nr_Task = (struct Task *) FindTask(NULL);
        /* with this signal bit */
        notifyrequest->nr_stuff.nr_Signal.nr_SignalNum = signum;

        if ((StartNotify(notifyrequest)) == DOSTRUE)
        {
          if (!quiet) printf("Press \23334mCTRL-C\23331m in this window to exit AutoComment\n\n");

          GetFileList(1);  // Get the list of all files in the directory.

          while(!done)
          {
            if (!quiet) printf("\23332;43mWaiting..\23331;40m\n");

            signals = Wait(  (1L << signum) | SIGBREAKF_CTRL_C  );
            if (signals & (1L << signum))
            {
              GetFileList(0);
              CheckNewFiles();
              CopyFileList(0,1);
            }
            if (signals & SIGBREAKF_CTRL_C)
            {
              done=TRUE;
            }
          }
        }
        else if (!quiet) printf("AutoComment Can't start notification\n");
        FreeSignal(signum);
      }
      else if (!quiet) printf("No signals available\n");
      FreeMem(notifyrequest, sizeof(struct NotifyRequest));
    }
    else if (!quiet) printf("No Mem available\n");
  }
  else if (!quiet) printf("AutoID Requires at least V37 dos.library\n");
  if (!quiet) printf("Thanks for using AutoComent From Perspex, Written By HYDRA!");
}
