/*

  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

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

struct List *FileID=NULL;

long __stack=16384;

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 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)>LEN_DIZWIDTH) node->ln_Name[LEN_DIZWIDTH]=0;
    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))
  {
    // write a > then 45 chars to the right of that write a <
    DOOR_WriteText(ANSI_FG_YELLOW "  >\033[45C<\033[46D" ANSI_FG_WHITE);

    DOOR_GetLine(GL_NODISTURB|GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',LEN_DIZWIDTH,0,NULL);
    if (N_ND->OnlineStatus==OS_ONLINE)
    {
      if (N_ND->CurrentLine[0]==0) // blank line..
      {
        if (Lines==N_ND->MaxDIZLines)
        {
          DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "You "ANSI_BOLD ANSI_FG_RED "Must" ANSI_RESET ANSI_FG_YELLOW " enter a file description\r\n");
        }
        else
        {
          Done=TRUE; // finished entering fileid
          DOOR_WriteText("\033[A"); //move up 1 line
        }
      }
      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[])
{
  if (argc==7 || argc==8)
  {
    if (UserAround)
    {
      sprintf(outstr,ANSI_FG_CYAN "Displaying DIZ for file "ANSI_FG_YELLOW"%-13s\r\n"ANSI_FG_WHITE,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)
      {
        // *C* Put a menu here... (i.e.   do you want to use this diz ??  etc...)
      }
    }
    else
    {
      // we need to do something here...
    }
  }
  else if (UserAround) DOOR_SysopText(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);
  }

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

  init("ModifyDIZ");

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