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

/*
  FileHistory
  ===========

    Creates a file called <Filename>.History containing info about the bbs, user, time
    that the file was uploaded to.

    Then it add this back the .LHA archive

    So, when the file has been traded around a bit you can see exactly where the
    file has been, who got it first etc..  So other util writers could create
    a program to provide statistics on which bbs gets the warez first, who's the
    fastest trader etc...

  Version
  =======

    1.0

  Options
  =======

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

    none

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

    see ExtractDIZ

  ToDo
  ====

    Support other files as well as LHA's



*/

/* **** 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 <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/Access.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/Hbbscommon_protos.h>
#ifdef __SASC
#include <HBBS/hbbscommon_pragmas_sas.h>
#else
#include <HBBS/hbbscommon_pragmas_stc.h>
#endif

#include <HBBS/Hbbsnode_protos.h>
#ifdef __SASC
#include <HBBS/Hbbsnode_pragmas_sas.h>
#else
#include <HBBS/Hbbsnode_pragmas_stc.h>
#endif
#include <HBBS/release.h>
char *versionstr="$VER: FileHistory "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;

BOOL UserAround=TRUE;

/* **** 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 DoorMain( void )
{
  char fname[BIG_STR];
  char timestr[20];
  char datestr[20];
  char tmpfilename[20];
    
  char histname[BIG_STR];

  sprintf(tmpfilename,"T:HBBS/FH%ld.TMP",N_ND->NodeNum);
  
  if (UserAround)
  {
    DOOR_WriteText("[37m   [34m_ [37m  [34m_[37m   [36m_   _[37m   _   [36m                                  [37m  _   [36m_   _[37m   [34m_   _\r\n"
                   "\\_/ \\_/[37m [36m\\_/ \\_/ [37m\\_/ \\[34m-[36m=[35m[[36m [32mFile History[36m [32mV1.0 by Hydra^cP[35m ][36m=[34m-[37m/ \\_/ [36m\\_/ \\_/[34m \\_/ \\_/[0;37m\r\n");
  }

  if (gargc>=7)
  {
    if ((stricmp(gargv[6],"LHA")==0) || (stricmp(gargv[6],"LZX")==0))
    {

      // what's the filename?

      sprintf(histname,"%s.history",strlen(gargv[2])> 10 ? "File" : gargv[2]);

      strcpy(fname,"T:");
      strcat(fname,histname);

      // delete it from t: if it's already there..
      DeleteFile(fname);
      
      outstr[0]=0; // clear the string, so we can test it later.
      
      if (stricmp(gargv[6],"LHA")==0)
      {
        // is there one in the LHA file already ?
        sprintf(outstr,"LHA e %s%s %s T:",gargv[4],gargv[2],histname);
      }
      else
      {
        if (stricmp(gargv[6],"LZX")==0)
        {
          // is there one in the LHA file already ?
          sprintf(outstr,"LZX x %s%s %s T:",gargv[4],gargv[2],histname);
        }
      }
      
      if (outstr[0])
      {
        HBBS_DosCommand(outstr,RDC_NONE,tmpfilename);
        DeleteFile(tmpfilename);
      }

      // create the string to add to the file

      HBBS_GetDate(datestr);
      HBBS_GetTime(timestr);
      sprintf(outstr,"Uploaded to %s by %s at %s on %s\n",BBSGlobal->BBSName,N_ND->User->CallData->Handle,timestr,datestr);

      // add it to the file

      HBBS_AppendStrToFile(fname,outstr);

      // make sure the file is there (no errors)

      if (PathOK(fname))
      {
        // and add it back to the archive.

        if (stricmp(gargv[6],"LHA")==0)
        {
          sprintf(outstr,"LHA <>nil: u -x0 %s%s %s",gargv[4],gargv[2],fname);
          HBBS_RunDOSCMD(outstr,FALSE);
        }
        if (stricmp(gargv[6],"LZX")==0)
        {
          sprintf(outstr,"LZX <>nil: u -x0 %s%s %s",gargv[4],gargv[2],fname);
          HBBS_RunDOSCMD(outstr,FALSE);
        }
        // delete it from t:
        DeleteFile(fname);
      }

      if (UserAround) DOOR_WriteText("\r\n");
      
    }
  }
  else
  {
    DOOR_SysopText("Unsupported file type\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);
  }

  if (stricmp("NOUSER",gargv[gargc-1])==0) UserAround=FALSE;

  init("FileHistory");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
    {
      if (N_ND->OnlineStatus!=OS_ONLINE) UserAround=FALSE;
      DoorMain();
    }
  }
  cleanup(0);
}

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