// skeleton code for writing a new door

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

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


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>
#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas.h>

struct Library *HBBSCommonBase=NULL;
struct Library *HBBSNodeBase=NULL;

struct BBSGlobalData *BBSGlobal=NULL;
struct NodeData *N_ND=NULL;
int N_NodeNum=-1;
char outstr[1024]; // temp string for displaying text..

static VOID cleanup(ULONG num)
{
  if (HBBSNodeBase)
  {
    HBBS_CleanUpDoor();
    CloseLibrary (HBBSNodeBase);
  }

  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

  if (num) printf("Door Error = %d\n",num);

  exit(0);
}

static VOID init(char *name)
{
  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    cleanup(1);
  }

  if (!(HBBS_InitCommon()))
  {
    cleanup(2);
  }

  if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  {
    cleanup(3);
  }

  if (!(HBBS_InitDoor(N_NodeNum,name)))
  {
    cleanup(4);
  }
  SetProgramName(name);
}

V_BOOL HBBS_FindConfFile(V_BIGNUM ConfNum,char *filename,char *fullname)
{
  // searches the download paths of the conf for the filename specified
  // and returns TRUE if the file is found.  The full path and filename
  // are then stored in fullname if fullname is not a null pointer

  V_BOOL retval=FALSE;
  struct ConfData *Conf;
  char tmpstr[1024];
  struct Node *node;


  // valid conf num ?
  if (ConfNum>=1 && ConfNum<=BBSGlobal->Conferences)
  {
    Conf=(struct ConfData *)GetNode(BBSGlobal->ConfList,ConfNum-1);

    // any download paths to search ??
    if (Conf->Download)
    {
      for (node=Conf->Download->lh_Head;!retval && node->ln_Succ;node=node->ln_Succ)
      {
        strcpy(tmpstr,node->ln_Name);
        strcat(tmpstr,filename);
        if (PathOK(tmpstr))
        {
          retval=TRUE;
          if (fullname)
          {
            strcpy(fullname,tmpstr);
          }
        }
      }
    }
  }
  return(retval);

}

V_BIGNUM GetFileSize(char *filename)
{
  BPTR FL;
  struct FileInfoBlock FB;
  V_BIGNUM retval=0;

  if (filename)
  {
    if (FL=Lock(filename,ACCESS_READ))
    {
      if (Examine(FL,&FB))
      {
        retval=FB.fib_Size;
      }
      UnLock(FL);
    }
  }
  return(retval);
}

BOOL AlreadyTagged(UBYTE *fullname)
{
  struct Node *node;
  BOOL Found=FALSE;

  for (node=N_ND->TaggedFileList->lh_Head;!Found && node->ln_Succ;node=node->ln_Succ)
  {
    Found = (stricmp(fullname,node->ln_Name)==0);
  }
  return(Found);
}

void TagPrompt( void )
{
  DOOR_WriteText(ANSI_FG_YELLOW "Tagging "ANSI_FG_BLUE"> "ANSI_FG_WHITE);
}

void DoorMain(int argc,char *argv[])
{
  short loop;
  struct TaggedFile *filetagnode;
  char filename[1024],fullname[1024];
  V_BIGNUM ConfNum;
  BOOL FileOK;

  V_BIGNUM tagged=0;

//  DOOR_WriteText("\r\n[0;36m-=[ [37;44m [0;3;37;44mHydra AddTag V1.0[0;37;44m [36;40m ]=-[35m ============ [36m-=[ [37;44m [0;3;37;44m(C) 1995 Deluxe Software Ltd[0;37;44m [36;40m ]=-\r\n");


  for(loop=2;loop<argc;loop++)
  {
    if (loop==2) TagPrompt();

    if (sscanf(argv[loop],"C=%ld,N=%s",&ConfNum,filename)==2)
    {
      RemoveSpaces(filename);
      DOOR_WriteText(filename);

      FileOK=FALSE;


      // check existence of file.
      if (ConfNum>0)
      {
        if (HBBS_FindConfFile(ConfNum,filename,fullname))
        {
          FileOK=TRUE;
        }
      }
      else
      {
        if (PathOK(filename))
        {
          FileOK=TRUE;
          strcpy(fullname,filename);
        }
      }

      if (FileOK)
      {
        if (AlreadyTagged(fullname))
        {
          DOOR_WriteText(ANSI_FG_BLUE" - "ANSI_FG_CYAN"File Already Tagged!\r\n"ANSI_FG_WHITE);
          if (loop<argc-1) TagPrompt();
        }
        else
        {
          if (filetagnode=AllocVec(sizeof(struct TaggedFile),MEMF_PUBLIC|MEMF_CLEAR))
          {
            filetagnode->node.ln_Name=DupStr(fullname);

            if (ConfNum>0) filetagnode->WarezFile=TRUE; else filetagnode->WarezFile=FALSE;
            filetagnode->ConferenceNum=ConfNum;


            filetagnode->FileSize=GetFileSize(fullname);

            AddTail(N_ND->TaggedFileList,(struct Node*) filetagnode);
            N_ND->TaggedFiles++;
            tagged++;

            if (loop<argc-1) DOOR_WriteText(ANSI_FG_BLUE","ANSI_FG_WHITE);

          }
        }
      }
      else
      {
        DOOR_WriteText(ANSI_FG_BLUE" - "ANSI_FG_CYAN"File Not Found!\r\n"ANSI_FG_WHITE);
        if (loop<argc-1) TagPrompt();
      }
    }
  }
  DOOR_WriteText("\r\n");
  sprintf(filename,"%ld",tagged);
  DOOR_Return(filename);
}

int main(int argc,char *argv[])
{
  if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  {
    printf("Invalid/No Paramaters for door!\n");
    exit (20);
  }
  init("TAG_FILE");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
    {
      DoorMain(argc,argv);
    }
  }
  cleanup(0);
}
