/* main() of Reminder */

/* $Id: RemMain.c,v 1.7 1993/04/02 23:30:05 Matti_Rintala Exp $ */

#include <stdlib.h>
#include <stdio.h>
#include <exec/types.h>
#include <dos/dosextens.h>
#include <utility/tagitem.h>
#include <exec/libraries.h>

#include <libraries/reqtools.h>

#include <intuition/intuition.h>

#ifdef __SASC
#include <proto/exec.h>
#include <proto/reqtools.h>
#endif

#ifdef _DCC
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/reqtools_protos.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#include <workbench/startup.h>

struct Library *CxBase = NULL;	/* DICE does not auto open commodities.library */
struct Library *IconBase = NULL; /* Nor icon.library */
#endif

#include "Gadfunc.h"
#include "Globals.h"

char *VersionStr = "$VER: Reminder 1.10";

struct ReqToolsBase *ReqToolsBase = NULL;

/* Variables for setting pr_WindowPtr for ReqTools */
APTR oldwinptr = NULL;
struct Process *myproc;


static void ClearExit(void);

#ifdef _DCC
static struct FileLock *oldlock = NULL;

/* DICE uses different entry point for workbench startup */
int wbmain(struct WBStartup *msg) {

  /* We have to change to correct directory */
  oldlock = CurrentDir(msg->sm_ArgList[0].wa_Lock);
  return main(0, (char **)msg);	/* Simply call main() */
}
#endif

int main(int argc, char **argv) {

  UBYTE **ttypes;		/* For argument parsing */

  atexit(ClearExit);	/* use ClearExit to release all resources at the end */

  /* Open ReqTools */
  if (!(ReqToolsBase =
	(struct ReqToolsBase *)OpenLibrary(REQTOOLSNAME, REQTOOLSVERSION))) {
#ifdef _DCC
    if (argc != 0)		/* DICE doesn't have stdout in Workbench */
#endif
    printf("Needs reqtools.library (version at least %d)!\n", REQTOOLSVERSION);
    exit(-1);
  }

#ifdef _DCC
  /* With DICE we have to open commodities.library and icon.library, too */
  if (!(CxBase = OpenLibrary("commodities.library", 0))) {
    if (argc != 0)		/* DICE doesn't have stdout in Workbench */
      printf("Can't open commodities.library!\n");
    exit(-1);
  }
  if (!(IconBase = OpenLibrary("icon.library", 0))) {
    if (argc != 0)		/* DICE doesn't have stdout in Workbench */
      printf("Can't open icon.library!\n");
    exit(-1);
  }
#endif

  /* Get the pr_WindowPtr */
  myproc = (struct Process *)FindTask(NULL);
  oldwinptr = myproc->pr_WindowPtr;

  /* Set up GadToolsBox screen */
  if (SetupScreen()) {
    rtEZRequestA("Cannot use Workbench screen!", "Quit", NULL, NULL, NULL);
    exit(-1);
  }

  /* Open Main-window and set it as this process's window */
  if (OpenMainWindow()) {
    rtEZRequestA("Cannot open a window!", "Quit", NULL, NULL, NULL);
    exit(-1);
  }
  myproc->pr_WindowPtr = MainWnd;

  /* Parse command line arguments */
  ttypes = ArgArrayInit(argc, argv);
  /* Try to find database filename */
  filename = ArgString(ttypes, FILETYPE, DEFAULTFILE);

  /* Initialize all gadgets */
  InitGadgets();

  do {
    WaitPort(MainWnd->UserPort);
  } while (HandleMainIDCMP());

  /* Save database and do other exit routines */
  ExitGadgets();

  /* Clean up arguments */
  ArgArrayDone();

  exit(0);
}


/* ClearExit closes all resources at exit */
static void ClearExit(void) {

  /* Restore windowpointer */
  if (oldwinptr != NULL)
    myproc->pr_WindowPtr = oldwinptr;

  /* Close Reqtools */
  if (ReqToolsBase != NULL)
    CloseLibrary((struct Library *)ReqToolsBase);

#ifdef _DCC
  /* With DICE we have to close commodities.library and icon.library, too */
  if (CxBase != NULL)
    CloseLibrary(CxBase);

  if (IconBase != NULL)
    CloseLibrary(IconBase);

  /* Return to correct directory */
  if (oldlock != NULL)
    CurrentDir(oldlock);
#endif

  /* Try to close all windows and screens generated by GadToolsBox */
  CloseMainWindow();
  CloseDownScreen();
}
