/*

  ConfirmUL
  ---------

  Asks user wehter the file that was just uploaded is a) a sysop file
  or b) an incorrect upload
  Also checks the FileID for a / at the start, if one is found it's a
  sysop file..

  options
  =======

    argv[x]
    -------

    2 filename (check <nodelocation+"/WORK/"+filename+".DIZ" for the file id..

  returns
  -------

  SYSOP | INCORRECT

*/

#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: ConfirmUL "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..

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 DoorMain(int argc,char *argv[])
{
  BPTR FH;
  BOOL Continue=FALSE;
  BOOL SysopFile=FALSE;
  UBYTE buffer;

  if (argc>=3)
  {
    sprintf(outstr,"%sWork/%s.DIZ",N_ND->NodeLocation,argv[2]);
    if (FH=Open(outstr,MODE_OLDFILE))
    {
      if (Read(FH,&buffer,1))
      {
        if (buffer=='/') SysopFile=TRUE;
      }
      Close(FH);
    }

    if (UserAround)
    {
      if (!SysopFile)
      {
        do
        {
          Continue=TRUE;
          strcpy(N_ND->CharsAllowed,"PpIiNn");
          DOOR_MenuPrompt(ANSI_RESET "Select [P]rivate, [I]ncorrect, or [N]ormal : ",'N');
          DOOR_GetLine(GL_EDIT|GL_USECHARS|GL_IMMEDIATE|GL_COUNTDOWN,'\0',1,3,NULL);
          if (N_ND->OnlineStatus==OS_ONLINE)
          {
            switch(toupper(N_ND->CurrentLine[0]))
            {
              case 0:
              case 'N':
                DOOR_WriteText("Normal\r\n");
                break;
              case 'P':
                DOOR_WriteText("Private\r\n");
                SysopFile=TRUE;
                break;
              case 'I':
                DOOR_WriteText("Incorrect\r\n");
                DOOR_Return("INCORRECT");
                break;
              default:
                Continue=FALSE;
                break;
            }
          }
          else
          {
            UserAround=FALSE;
          }
        }
        while (Continue==FALSE);
      }
    }
    if (SysopFile)
    {
      if (UserAround) DOOR_WriteText(ANSI_FG_WHITE ANSI_BOLD "Autodetected A SYSOP File.\r\n" ANSI_RESET);
      DOOR_Return("SYSOP");
    }
  }
  else if (UserAround) DOOR_WriteText(ANSI_FG_RED ANSI_BOLD "Incorrect Parameters!\r\n");
}

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

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