/*

  AddDIZToList
  ============

  Adds a .DIZ to a file list


  Options
  =======

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

    none

    Door Options (Command Line)
    ---------------------------

2   <confnum> | "BAD" | "HOLD" | "SYSOP"

3   <diz_file>

4   <filename>

5   [NOUSER]

*/


#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: AddDIZToList "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[BIG_STR]; // temp string for displaying text..

struct List *FileID=NULL;
UBYTE *filename=NULL;

BOOL UserAround=TRUE;

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 AddDIZ(int argc,char *argv[])
{
  BPTR FH=NULL,FL;
  struct FileInfoBlock FB;
  LONG count=0;
  char c;
  struct Node *node;
  char datestr[LEN_DATESTR+1];
  char timestr[LEN_TIMESTR+1];


  strcpy(outstr,N_ND->NodeSettings.NodePlayPen);
  strcat(outstr,argv[4]);

  if (FL=Lock(outstr,ACCESS_READ))
  {
    if (Examine(FL,&FB))
    {
      if (FH=Open(filename,MODE_OLDFILE))
      {
        Seek(FH,-1,OFFSET_END);
        Read(FH,&c,1);
        if (c!='\n') Write(FH,"\n",1);
      }
      else
      {
        FH=Open(filename,MODE_NEWFILE);
      }
      if (FH)
      {
        for (node = FileID->lh_Head ; (count<N_ND->MaxDIZLines) && (node->ln_Succ) ; node =node->ln_Succ,count ++)
        {
          switch(count)
          {
            case 0:
              HBBS_GetDate(datestr);
              sprintf(outstr,"F %-12s %8ld %s %s",argv[4],FB.fib_Size,datestr,node->ln_Name);
              break;
            case 1:
              HBBS_GetTime(timestr);
              sprintf(outstr,"T                          %s %s",timestr,node->ln_Name);
              break;
            case 2:
              sprintf(outstr,"N                          Node %3d %s",N_NodeNum,node->ln_Name);
              break;
            default: // 3 or more..
              sprintf(outstr,"I                                   %s",node->ln_Name);
              break;
          }
          strcat(outstr,"\n");
          FPuts(FH,outstr);
        }
        if (N_ND->User->CallData->SentBy[0]) // blank sentby ?
        {
          sprintf(outstr,"U                                   %s\n",N_ND->User->CallData->SentBy);
          FPuts(FH,outstr);
        }
        else
        {
          if (UserAround)
          {
            DOOR_DisplaySpecialScreen("NO_SENTBY");
          }
        }
        Close(FH);
      }
    }
    UnLock(FL);
  }
}


void DoorMain(int argc,char *argv[])
{
  LONG ConfNum,loop;
  struct ConfData *Conf;

  if (argc>=4)
  {
    if (FileID=HBBS_LoadFile(argv[3]))
    {
      if (HBBS_NodesInList(FileID))
      {
        if ((stricmp(argv[2],"HOLD")==0) || (stricmp(argv[2],"SYSOP")==0))
        {
          if (N_ND->CurrentConf)
          {
            filename=N_ND->CurrentConf->HoldFileList;
          }
        }
        else
        {
          if (stricmp(argv[2],"BAD")==0)
          {
            if (N_ND->CurrentConf)
            {
              filename=N_ND->CurrentConf->BadFileList;
            }
          }
          else
          {
            // first we find the conference number.
            if (ConfNum=atoi(argv[2]))
            {
              if (ConfNum>=1 && ConfNum<=BBSGlobal->Conferences) // Valid Conference ?
              {
                // now we get the data structure that points to the conference data
                Conf=(struct ConfData *)GetNode(BBSGlobal->ConfList,ConfNum-1);

                if (Conf->FileList)
                {
                  // then we get a the filename of the list we want to add our .DIZ to
                  // which is the last items in the linked list of file lists..

                  filename=HBBS_ListName(Conf->FileList,Conf->FileLists-1);

                }
              }
            }
          }
        }
        
        if (filename)
        {
          if (UserAround)
          {
            DOOR_SysopText(ANSI_RESET ANSI_FG_CYAN ANSI_BOLD "Adding Diz To: " ANSI_ITALIC);
            DOOR_SysopText(filename);
            DOOR_SysopText(ANSI_RESET "\r\n");
          }

          AddDIZ(argc,argv);
        }
      }
      else
      {
        if (UserAround) DOOR_WriteText("Empty File ID, not adding!\r\n");
        sprintf(outstr,"Empty file ID for file %s",argv[3]);
        HBBS_LogError(N_ND->NodeSettings.NodeLogFile,ERR_GENERAL,outstr,TYPE_WARNING);
      }
      FreeStrList(FileID);
    }
  }
  else
  {
    strcpy(outstr,"Invalid Parameters for AddDIZtoList\n");
    for (loop=0;loop<argc;loop++)
    {
      strcat(outstr,argv[loop]);
      strcat(outstr," ");
    }
    if (UserAround) DOOR_SysopText(outstr);
    HBBS_AddToCallersLog(outstr);
  }
}

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

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

  init("AddDIZToList");

  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(argc,argv);
    }
  }
  cleanup(0);
}
