/*

  ModifyDIZ
  =========


  This door displays the .DIZ on the screen
  the user is then asked

  1) Use this DIZ
  2) Enter New DIZ
  3) Modify DIZ

  Pressing return defaults to option 1.

  Options
  =======

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

    none

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

2   <filename>

3   <workdir>

4   <playpen dir>

5   <filename without .extension>

6   <.extension without filename and without the .>

    Note: Hey, I really couldn't think of any other settings that might be usefull..
    you can even write a damn AmigaDOS script to extract file_id's with all those
    parameters! hehehe


*/


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

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..
char filename[1024]; // temp string for displaying text..

  struct List *FileID=NULL;

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 DisplayDIZ( void )
{
  struct Node *node;

  DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "  +------------------------------------------------+\r\n" ANSI_FG_WHITE);
  for (node = FileID->lh_Head ; node->ln_Succ ; node =node->ln_Succ)
  {
//    if (strlen(node->ln_Name)>48) node->ln_Name[48]=0; // 0-47 = 48 chars
    sprintf(outstr,"   %s\r\n",node->ln_Name);
    DOOR_WriteText(outstr);
  }
  DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "  +------------------------------------------------+\r\n" ANSI_FG_WHITE);
}

void EditDIZ( void )
{
  V_SMALLNUM Lines;
  V_BOOL Done=FALSE;

  Lines=N_ND->MaxDIZLines;
  sprintf(outstr,"please enter upto %d lines to describe this file\r\n",N_ND->MaxDIZLines);
  DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "  +------------------------------------------------+\r\n" ANSI_FG_WHITE);
  while ((N_ND->OnlineStatus==OS_ONLINE) && (Lines>0) && (!Done))
  {
    DOOR_WriteText(ANSI_FG_WHITE "  >");
    DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',48,0,NULL);
    if (N_ND->OnlineStatus==OS_ONLINE)
    {
      if (N_ND->CurrentLine[0]==0) // blank line..
      {
        Done=TRUE; // finished entering fileid
      }
      else
      {
        NewStrNode(N_ND->CurrentLine,FileID); // add line to the list
        Lines--;
      }
    }
  }
  DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "  +------------------------------------------------+\r\n" ANSI_FG_WHITE);
}

void SaveID( void ) // *C* add error checking. / create new routine and call it HBBS_SaveFile()
{
  HBBS_SaveFile(filename,FileID);
}

void DoorMain(int argc,char *argv[])
{
  DOOR_WriteText("\r\n[0;36m-=[ [37;44m [0;3;37;44mHydra ModifyDIZ 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");

  if (argc==6 || argc==7)
  {
    sprintf(outstr,"[34mDisplaying DIZ for file [36m%-13s[37m\r\n",argv[2]);
    DOOR_WriteText(outstr);
    sprintf(filename,"%sWork/%s.DIZ",N_ND->NodeLocation,argv[2]);
    if (!(FileID=HBBS_LoadFile(filename)))
    {
      if (FileID=HBBS_CreateList())
      {
        DOOR_WriteText("No FileID contained within uploaded file, please enter a new one\r\n");
        EditDIZ();
      }
    }
    else
    {
      DisplayDIZ();
    }
    if (FileID)
    {
      // Menu Here...
    }
  }
  else DOOR_WriteText(ANSI_RESET ANSI_FG_RED "Invalid Door Options!\r\n");

  if (FileID)
  {
    SaveID();
    FreeStrList(FileID);
  }
}

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

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