/*
 * graphics.c
 */

/* $Id: graphics.c,v 1.14 1993/07/04 13:22:02 beust Exp $ */

struct Library *AmigaGuideBase;        

#include <dos/dos.h>
#include <libraries/amigaguide.h>
#include <clib/amigaguide_protos.h>
#include <pragmas/amigaguide_pragmas.h>

#include "ccd.h"

#define D(x) x

#define CCD_AG_PORT "CCDAMIGAGUIDE"

struct PrivateVars {
  AMIGAGUIDECONTEXT context;
};

static int
initDisplay(struct GlobalVars *gv, struct PrivateVars *vars)
{
  int result = 0;

  AmigaGuideBase = OpenLibrary("amigaguide.library", 33);
  if (AmigaGuideBase) {
    struct NewAmigaGuide nag;

    memset(& nag, 0, sizeof(nag));
    nag.nag_Name = CCD_FILE_GUIDE;
    nag.nag_Flags = HTF_LOAD_ALL;
    nag.nag_ClientPort = CCD_AG_PORT;

    if (! (vars -> context = OpenAmigaGuide(& nag, NULL))) {
      result = IoErr();
      fprintf(stderr, "couldn't open the file, error %d\n", result);
    }
  }
  else {
    fprintf(stderr, "*** couldn't open amigaguide.library\n");
    result = 1;
  }

  return result;
}

static int
freeDisplay(struct GlobalVars *gv, struct PrivateVars *vars)
{
  int result = 0;
  if (vars -> context) CloseAmigaGuide(vars -> context); vars -> context = NULL;
  if (AmigaGuideBase) CloseLibrary(AmigaGuideBase); AmigaGuideBase = NULL;

  return result;
}

static int
buildGuideFile(struct GlobalVars *gv, DataBase db)
/* Build a guide file from ccd:.ccdconfig */
{
  FILE *guide;
  int result = 0, i;
  unsigned char *dir;
  Entry entry;

  guide = fopen(CCD_FILE_GUIDE, "w");
  if (! guide) {
    fprintf(stderr, "couldn't write to %s\n", CCD_FILE_GUIDE);
    result = 5;
  }
  else {
    unsigned char spaces[128], node[128], line[256];
    int n;

    DB_Rewind(db);

  /* Create a line corresponding to the directory, with the indentation */
    fprintf(guide, "@database ccd.guide\n\n@node Main\n");
    while (! DB_EndOfDataBase(db)) {
      entry = DB_NextEntry(db);
      fprintf(guide, "%s@{\"%s\" rx \"%s %s\"}\n",
	      entry -> spaces, entry -> name, CCD_FILE_REXX, entry -> name);
    }
    fprintf(guide, "\n@endnode\n");
    fclose(guide);
  }

  return result;
}

static int
buildRexxFile(struct GlobalVars *gv)
/* Build the Rexx file called when the user clicks on a directory */
{
  char *rexxProgram[] = {
    "/**/",
    "",
    "arg dir",
    "ADDRESS CCDAMIGAGUIDE.1 QUIT",
    "if (~ exists('env:ccd')) then",
    "  ADDRESS COMMAND 'Makedir env:ccd'",
    "",
    "ADDRESS COMMAND 'echo ' || dir || ' > ENV:CCD/LAST_DIR'",
    NULL
  };
  int result = 0;
  FILE *f;

  f = fopen(CCD_FILE_REXX, "w");
  if (f) {
    char **p = & rexxProgram[0];
    while (*p) {
      fprintf(f, "%s\n", *p);
      p++;
    }
    fclose(f);
  }
  else {
    fprintf(stderr, "*** couldn't create %s\n", CCD_FILE_REXX);
    result = 5;
  }

  return result;
}


void
displayTree(struct GlobalVars *gv, DataBase db)
{
  struct PrivateVars vars;

  memset(& vars, 0, sizeof(vars));
  buildGuideFile(gv, db);
  buildRexxFile(gv);
  if (initDisplay(gv, & vars) == 0) {
    freeDisplay(gv, & vars);
  }
}


