/****************************************/
/* ML-Support                           */
/* Freeware                             */
/* © 1998-2000 Christian Hattemer       */
/* email: chris@riednet.tu-darmstadt.de */
/* 31.08.2000 16:21                     */
/****************************************/

#define VERSIONSTRING "$VER: ML-Support 2.1 (31.08.2000)"

/* stormamiga.lib */
#ifdef USESTORMAMIGA
   #define STORMAMIGA
   #define STORMAMIGA_STACK 15000
   #include <stormamiga.h>
   char __stdiowin[] = "CON:/11/400/100/ML-Support/AUTO/CLOSE/WAIT";
#endif

/* Ansi */
#include <string.h>
#include <stdio.h>

/* Protos */
#include <proto/icon.h>

/* ML-Support */
#include "GUI.h"
#include "Mainwin.h"
#include "ML_Support_Cat.h"

/* Functions */
ULONG        DoSuperNew(Class *cl, Object *obj, ULONG Tag1, ...);
static void  Quit(Object *App, STRPTR Msg);

/* Structures */
       struct Library *MUIMasterBase;
static struct MUI_CustomClass *CL_Mainwin;

/* main function */
void main(int argc, char *argv[])
{
   Object *App, *WI_Main;
   ULONG   Sigs = 0UL;
   TEXT    CreatorName[30];

   /* Program initialization */
   if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME, 14UL)))
   {
      Quit(NULL, "Failed to open muimaster.library V14+");
   }

   if (!(CL_Mainwin = CreateMainwinClass()))
   {
      Quit(NULL, "Failed to init Classes");
   }

   // Look for the name of the creator class

   if (argc == 2)
   {
      stccpy(CreatorName, argv[1], sizeof(CreatorName));
   }
   else
   {
      struct DiskObject *DObj;

      CreatorName[0] = '\0';

      DObj = GetDiskObject("PROGDIR:ML-Support");

      if (DObj)
      {
         STRPTR CreatorPtr;

         CreatorPtr = FindToolType((CONST_STRPTR *)DObj->do_ToolTypes, "CREATOR");

         if (CreatorPtr)
         {
            stccpy(CreatorName, CreatorPtr, sizeof(CreatorName));
         }

         FreeDiskObject(DObj);
      }
   }

   OpenML_SupportCatalog();

   /* Create Application */
   App = ApplicationObject,
      MUIA_Application_Author, "Christian Hattemer",
      MUIA_Application_Base, "ML-SUPPORT",
      MUIA_Application_Title, TITLE_MAINWIN,
      MUIA_Application_Version, VERSIONSTRING,
      MUIA_Application_Copyright, "© 1998-2000 Christian Hattemer",
      MUIA_Application_Description, MSG_APP_DESCRIPTION,
      MUIA_Application_SingleTask, TRUE,

      SubWindow, WI_Main = MainwinObject,
         MUIA_Mainwin_Creator, CreatorName,
      End,
   End;

   if (App)
   {
      STRPTR ErrorStr = NULL;

      // Check if the creator name is valid
      if (strstr(CreatorName, ".mcc") == NULL)
      {
         ErrorStr = MSG_INVALID_CREATOR;
      }

      if (EMPTYSTR(CreatorName))
      {
         ErrorStr = MSG_NO_CREATOR;
      }

      if (ErrorStr)
      {
         MUI_Request(App, NULL, 0,
                     TITLE_MAINWIN,
                     LBL_OK_ACTIVE,
                     ErrorStr,
                     CreatorName
                    );

         Quit(App, NULL);
      }

      set(WI_Main, MUIA_Window_Open, TRUE);
      if (!(xget(WI_Main, MUIA_Window_Open)))
      {
         Quit(App, "Failed to open Mainwindow");
      }

      while (DoMethod(App, MUIM_Application_NewInput, &Sigs) !=
             MUIV_Application_ReturnID_Quit
            )
      {
         if (Sigs)
         {
            Sigs = Wait(Sigs | SIGBREAKF_CTRL_C);
            if (Sigs & SIGBREAKF_CTRL_C)
               break;
         }
      }

      set(WI_Main, MUIA_Window_Open, FALSE);
      Quit(App, NULL);
   }
   else
   {
      Quit(NULL, "Failed to create Application");
   }
}

ULONG DoSuperNew(Class *cl, Object *obj, ULONG Tag1, ...)
{
   return DoSuperMethod(cl, obj, OM_NEW, &Tag1, NULL);
}

static void Quit(Object *App, STRPTR Msg)
{
   if (App)
   {
      MUI_DisposeObject(App);
   }

   CloseML_SupportCatalog();

   if (CL_Mainwin)
   {
      DeleteMainwinClass(CL_Mainwin);
   }

   if (MUIMasterBase)
   {
      CloseLibrary(MUIMasterBase);
   }

   if (Msg)
   {
      puts(Msg);
      exit(20);
   }

   exit(0);
}

