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

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

long __stack=16384;
// for global use..

int gargc;
char **gargv;

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

void DoorMain( void )
{
  struct ConfData *Conf=NULL;
  V_BIGNUM ConfNum;
  struct List *PathList=NULL;
  struct Node *node;
  BOOL Done=FALSE,Error=FALSE;
  struct InfoData ID;
  BPTR FL;
  struct FileInfoBlock FB;
  BOOL UserAround=TRUE;
  LONG nextparam=2;
  char fromname[1024],toname[1024];
  char dizfromname[1024],diztoname[1024];

  if (stricmp(gargv[2],"NOUSER")==0)
  {
    UserAround=FALSE;
    nextparam=3;
  } else DOOR_WriteText(ANSI_RESET);

  strcpy(fromname,N_ND->NodeSettings.NodePlayPen);
  strcat(fromname,gargv[nextparam+1]); // nodePlaypen/<filename>

  if (FL=Lock(fromname,ACCESS_READ))
  {
    if (Examine(FL,&FB)) Done=TRUE;
    UnLock(FL);
  }

  if (Done)
  {
    Done=FALSE;

    if (stricmp("BAD",gargv[nextparam])==0)
    {
      if (Conf=FindConf())
      {
        PathList=Conf->BadFiles;
      }
      nextparam++;
    }
    else
    {
      if (stricmp("HOLD",gargv[nextparam])==0)
      {
        if (Conf=FindConf())
        {
          PathList=Conf->HoldFiles;
        }
        nextparam++;
      }
      else
      {
        if (stricmp("PARTUPLOAD",gargv[nextparam])==0)
        {
          if (Conf=FindConf())
          {
            PathList=Conf->PartUpload;
          }
          nextparam++;
        }
        else
        {
          if (sscanf(gargv[nextparam],"%ld",&ConfNum))
          {
            if (ValidConfNum(ConfNum-1))
            {
              Conf=(struct ConfData *)GetNode(BBSGlobal->ConfList,ConfNum-1);
              PathList=Conf->Upload;
            }
            else
            {
              if (UserAround) DOOR_WriteText("Invalid Conference\r\n");
            }
            nextparam++;
          }
        }
      }
    }

    if (Conf && PathList)
    {
      for (node=PathList->lh_Head ; node->ln_Succ && !Error && !Done ; node=node->ln_Succ)
      {
        if (PathOK(node->ln_Name))
        {

          if (FL=Lock(node->ln_Name,ACCESS_READ))
          {
            if (Info(FL,&ID))
            {
              if (ID.id_DiskState == ID_VALIDATED)
              {
                if ( (ID.id_NumBlocks-ID.id_NumBlocksUsed) > (FB.fib_Size / ID.id_BytesPerBlock))
                {
                  strcpy(toname,node->ln_Name);
                  strcat(toname,gargv[nextparam]);
                  nextparam++;
                  if (!PathOK(toname)) // file exist in dest dir ?
                  {

                    // try a rename first as it's tonnes quicker!
                    if (!(Done=Rename(fromname,toname)))
                    {
                      // if that fails then copy it
                      if (Done=HBBS_CopyFile(fromname,toname,32768))
                      {
                        // and if that worked, delete the first file..
                        DeleteFile(fromname);
                      }
                    }
                    if (Done)
                    {
                      if (stricmp(gargv[nextparam],"MOVEDIZ")==0)
                      {
                        nextparam++;
                        strcpy(diztoname,node->ln_Name);
                        strcat(diztoname,gargv[nextparam]);
                        strcat(diztoname,".DIZ");

                        sprintf(dizfromname,"%sWork/%s.DIZ",N_ND->NodeLocation,gargv[nextparam]);

                        DOOR_SysopText("Copying DIZ to ");
                        DOOR_SysopText(diztoname);
                        DOOR_SysopText("\r\n");
                        HBBS_CopyFile(dizfromname,diztoname,32768);
                      }
                      if (UserAround)
                      {
                        DOOR_WriteText("File Moved To ");
                        DOOR_WriteText(toname);
                        DOOR_WriteText("\r\n");
                      }
                    }
                  }
                  else Error=TRUE;
                }
              }
            }
            UnLock(FL);
          }
        }
      }
    }
    if (!Done || Error)
    {
      // could not put the file in a dir, move it to HBBS:LostFIles and record
      // this in error log..

      sprintf(outstr,"Could Not Move %s to any Upload/BAD/HOLD Path!\nIt *MAY* now be in HBBS:LostFiles",gargv[3]);
      HBBS_LogError(BBSGlobal->ErrorLogFile,ERR_GENERAL,outstr,TYPE_WARNING);
      if (UserAround)
      {
        DOOR_WriteText("Could Not Move File, Trying To Move It To \"HBBS:LostFiles\"\r\n"
                       "Check your upload paths are valid\r\n");
      }
      strcpy(toname,"HBBS:LostFiles/");
      strcat(toname,gargv[3]);

      if (!(Done=Rename(fromname,toname)))
      {
        // if that files then copy it
        if (Done=HBBS_CopyFile(fromname,toname,32768))
        {
          // and if that worked, delete the first file..
          DeleteFile(fromname);
        }
      }


      if (Done && UserAround)
      {
        DOOR_WriteText("Moved File To HBBS:LostFiles OK\r\n");
      }
    }
  }
  if (!Done && UserAround) DOOR_WriteText("Error Locating/Moving File!\r\n");
}

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("MoveFile");

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