/*

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

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;

//  DOOR_WriteText("\r\n[0;36m-=[ [37;44m [0;3;37;44mHydra ConfirmUL V1.0[0;37;44m [36;40m ]=-[35m ========= [36m-=[ [37;44m [0;3;37;44m(C) 1995 Deluxe Software Ltd[0;37;44m [36;40m ]=-\r\n\r\n");

//  sprintf(outstr,"args %d\r\n",argc);
//  DOOR_WriteText(outstr);

  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 (!SysopFile)
    {
      do
      {
        Continue=TRUE;
        strcpy(N_ND->CharsAllowed,"SsIiNn");
        DOOR_MenuPrompt(ANSI_RESET "Is this file a [S]ysop File, an [I]ncorrect Upload or [N]ormal > ",'N');
//        DOOR_WriteText(ANSI_RESET ANSI_FG_WHITE "Is this file a [" ANSI_FG_PURPLE "S" ANSI_FG_WHITE "]ysop File, an [" ANSI_FG_PURPLE "I" ANSI_FG_WHITE "]ncorrect Upload or just [" ANSI_FG_PURPLE ANSI_BOLD "N" ANSI_RESET ANSI_FG_WHITE "]ormal ");
        DOOR_GetLine(GL_EDIT|GL_USECHARS|GL_IMMEDIATE,'\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 'S':
              DOOR_WriteText("Sysop\r\n");
              SysopFile=TRUE;
              break;
            case 'I':
              DOOR_WriteText("Incorrect\r\n");
              DOOR_Return("INCORRECT");
              break;
            default:
              Continue=FALSE;
              break;
          }
        }
      }
      while (Continue==FALSE);
    }
    if (SysopFile)
    {
      DOOR_WriteText(ANSI_FG_WHITE ANSI_BOLD "Autodetected A SYSOP File.\r\n" ANSI_RESET);
      DOOR_Return("SYSOP");
    }
  }
  else 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);
  }
  init("ConfirmUL");

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