/* **** HBBS Door Code****************************************************** */

/*
  DoorName
  ========

    what the door does

  Version
  =======

    x.xx, release xx, dd/mm/yyyy

  Options
  =======

    N_ND->ActiveDoor->SystemOptions
    -------------------------------



    Command Line Arguments
    ----------------------

    2  <option>

    3  ...

    4  .

  ToDo
  ====

    what you still have to do



*/

/* **** Includes *********************************************************** */

#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>

#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>
#include <HBBS/release.h>
char *versionstr="$VER: AskDLFiles "RELEASE_STR;

/* **** Variables ********************************************************** */


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

struct BBSGlobalData *BBSGlobal = NULL;
struct NodeData *N_ND           = NULL;
int    N_NodeNum                = -1;
char   outstr[1024];

long __stack=16*1024; // increse this in 4k incrments if you suffer from
                      // random/suprious crashings after or during the running
                      // of your door.

int    gargc;         // these are just copies of main()'s argc and argv..
char   **gargv;

/* **** Functions ********************************************************** */


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

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);
}

/* **** DoorMain *********************************************************** */

/*
void DisplayTaggedFiles( void )
{
  struct TaggedFile *Tag;
  int loop=0;

  for (Tag = (struct TaggedFile *)N_ND->TaggedFileList->lh_Head ; Tag->node.ln_Succ ; Tag =(struct TaggedFile*)Tag->node.ln_Succ)
  {
    loop++;
    if (Tag->WarezFile)
    {
      sprintf(outstr,ANSI_FG_CYAN "%d" ANSI_FG_WHITE ") \"" ANSI_FG_GREEN "%-12s" ANSI_FG_WHITE "\", %d, " ANSI_FG_PURPLE "%s\r\n",loop,FilePart(Tag->node.ln_Name),Tag->FileSize,HBBS_ListName(BBSGlobal->ConfList,Tag->ConferenceNum-1)); //-1 cos HBBS_ListName starts at 0.. conf numbers start at 1
    }
    else
    {
      sprintf(outstr,ANSI_FG_CYAN "%d" ANSI_FG_WHITE ") " ANSI_FG_YELLOW "%s\r\n",loop,Tag->node.ln_Name);
    }

    DOOR_WriteText(outstr);
  }
  if (loop==0)  DOOR_WriteText(ANSI_FG_CYAN "No Tagged Files!\r\n" ANSI_RESET);

  DOOR_WriteText(ANSI_FG_WHITE);
}
*/

void DoorMain( void )
{
  LONG Bytes=0;
  LONG Files=0;
  V_BIGNUM Mins=0;
  BOOL Done=FALSE,Cancel=FALSE,IsFile;
  struct TaggedFile *node;
  BOOL BytesDisabled=FALSE;
  BOOL FilesDisabled=FALSE;

  V_BIGNUM tempnum,loop;

  // bytes uploaded * ratio, - bytes downloaded = bytes allowed!

  if (N_ND->User.CallData.BytesRatio)
  {
    Bytes=(N_ND->User.CallData.UploadBytes * N_ND->User.CallData.BytesRatio)-N_ND->User.CallData.DownloadBytes;
  }
  else
  {
    BytesDisabled=TRUE;
  }

  // work out the files ratio in the same way..

  if (N_ND->User.CallData.FilesRatio)
  {
    Files=(N_ND->User.CallData.UploadFiles * N_ND->User.CallData.FilesRatio)-N_ND->User.CallData.DownloadFiles;
  }
  else
  {
    FilesDisabled=TRUE;
  }

  // then remove all the bytes and files of the other files that are already tagged

  for (node=(struct TaggedFile *)N_ND->TaggedFileList->lh_Head;node->node.ln_Succ;node=(struct TaggedFile *)node->node.ln_Succ)
  {
    if (!BytesDisabled) Bytes-=node->FileSize;
    if (!FilesDisabled) Files--;
  }

  if (N_ND->TaggedFiles)
  {
    DOOR_WriteText("You have got these files tagged already..\r\n");
//    DisplayTaggedFiles();
    DOOR_UserDoor("A","L");
  }

  while ( ((Bytes>=0) || (BytesDisabled)) && ((Files>=0) || (FilesDisabled)) && (!Done))
  {
    sprintf(outstr,ANSI_RESET "Files Tagged : %ld ,",N_ND->TaggedFiles);
    DOOR_WriteText(outstr);

    if (FilesDisabled)
    {
      DOOR_WriteText("Files Disabled ,");
    }
    else
    {
      sprintf(outstr," Files Left %ld ,",Files);
      DOOR_WriteText(outstr);
    }
    if (BytesDisabled)
    {
      DOOR_WriteText("Bytes Disabled ,");
    }
    else
    {
      sprintf(outstr," Bytes Left : %ld ,",Bytes);
      DOOR_WriteText(outstr);
    }
    sprintf(outstr," Mins Left : %ld\r\n\r\n",Mins);
    DOOR_WriteText(outstr);
    DOOR_MenuPrompt("[D]one, [L]ist, [Q]uit, or name of file : ",'D');
    DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',0,0,NULL);
    if (N_ND->OnlineStatus==OS_ONLINE)
    {
      RemoveSpaces(N_ND->CurrentLine);
      if (N_ND->CurrentLine[0])
      {
        IsFile=FALSE;
        if (N_ND->CurrentLine[1]==0) // an option!
        {
          switch (toupper(N_ND->CurrentLine[0]))
          {
            case 'D':
              Done=TRUE;
              break;
            case 'Q':
              DOOR_WriteText("Quit!\r\n");
              Done=TRUE;
              Cancel=TRUE;
              break;
            case 'L':
              DOOR_UserDoor("A","L");
              break;
            default:
              IsFile=TRUE;
              break;
          }
        }
        else
        {
          IsFile=TRUE;
        }
        if (IsFile)
        {
          DOOR_UserDoor("A",N_ND->CurrentLine);
          if (N_ND->OnlineStatus==OS_ONLINE)
          {
            // how many files did we tag ?
            if (sscanf(N_ND->DoorReturn,"%ld",&tempnum))
            {
              if (tempnum>0)
              {
                // lots, so check the bytes and files..
                for (loop=N_ND->TaggedFiles-tempnum;loop<N_ND->TaggedFiles;loop++)
                {
                  node=(struct TaggedFile *)GetNode(N_ND->TaggedFileList,loop);
                  if (!FilesDisabled) Files--;
                  if (!BytesDisabled) Bytes-=node->FileSize;
                }
              }
            }
          }
        }
      }
      else
      {
        Done=TRUE;
      }
    }
    if (N_ND->OnlineStatus==OS_OFFLINE)
    {
      Done=TRUE;
      Cancel=TRUE;  // lost carrier!
    }
  }

  // user gone over the limit ???

//  if ((!Done) && (!Cancel) && (N_ND->OnlineStatus==OS_ONLINE))
  if ( ((Bytes<0) && (!BytesDisabled)) || ((Files<0) && (!FilesDisabled)) && (N_ND->OnlineStatus==OS_ONLINE))
  {
    DOOR_WriteText("Not Enough Byte or File Credits, please untag some files\r\n"
                   "or upload more files!\r\n");
    Cancel=TRUE;
  }
  if (Cancel) DOOR_Return("CANCEL"); else DOOR_Return("OK");
}

int main(int argc,char **argv)
{
  gargc=argc;
  gargv=argv;

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

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

/* **** End Of File ******************************************************** */
