//************************************
//
// Name : main.c
//
//************************************

//**** Header files

//** OS Include files
#include <workbench/startup.h>
#include <workbench/workbench.h>
#include <libraries/dos.h>
#include <libraries/wbstart.h>
#include <libraries/commodities.h>

//** OS function prototypes
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/wb_protos.h>
#include <clib/commodities_protos.h>

//** OS function inline calls
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/wb_pragmas.h>
#include <pragmas/commodities_pragmas.h>

//** ANSI C includes

//** application include
#include "Read.h"
#include "Node.h"
#include "Launch.h"
#include "Janitor.h"
#include "Prefs.h"
#include "Librarian.h"


//**** Local storage

static char VersTag[]="\0$VER: EasyTM v1.1 (28 May 1996)";


//**** Main

void main(int argc, char **argv) {
  struct AppMessage   *appmsg;
  CxMsg *msg;
  ULONG sig_a,sig_n, sret;
  BOOL running=FALSE;
  BOOL Active=TRUE;
  struct NewBroker nb = {
    NB_VERSION,
    "EasyTM",
    &VersTag[7],
    "Easy Tools Menu manager commidity",
    NBU_UNIQUE | NBU_NOTIFY,
    COF_SHOW_HIDE,
    0,
    NULL,
    0
  };
  CxObj *broker=NULL;
  struct Task *t;


  if (1==InitList()) {
    if (1==OpenLibs() ) {
      if (1==ReadIcon(argc,argv) ) {
        if (1==OpenPorts()) {
          nb.nb_Pri = my_cx_pri;
          nb.nb_Port = StarPort[0];
          sig_n = 1 << StarPort[0]->mp_SigBit;
          if (broker = CxBroker(&nb, NULL)) {
            ActivateCxObj(broker, 1L);
            sig_a = 1 << StarPort[1]->mp_SigBit;
            ReadFile(ToolsFileName);
            if (1==BindMenus()) {
              for ( running=TRUE ; TRUE==running ; ) {
                sret = Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | sig_a | sig_n);
                if (sret & sig_a) {
                  while(appmsg=(struct AppMessage *)GetMsg(StarPort[1])) {
                    Launch( (struct ProgNode *)appmsg->am_UserData,
                            (long)appmsg->am_NumArgs,
                            (struct WBArg *)appmsg->am_ArgList);
                    // Let workbench know we're done with the message
                    ReplyMsg((struct Message *)appmsg);
                  } // while GetMsg
                } // if sig_a
                if (sret & sig_n) {
                  while(msg = (CxMsg *)GetMsg(nb.nb_Port)) {
                    switch(CxMsgType(msg)) {
                      case CXM_COMMAND:
                        switch(CxMsgID(msg)) {
                          case CXCMD_DISABLE:
                            if (TRUE==Active) {
                              ActivateCxObj(broker, 0L);
                              Active = FALSE;
                              FreeMenus();
                            }; // if
                            break;
                          case CXCMD_ENABLE:
                            if (FALSE==Active) {
                              ActivateCxObj(broker, 1L);
                              Active = TRUE;
                              if (0==BindMenus()) running=FALSE; // error restarting
                            }; // if
                            break;
                          case CXCMD_APPEAR:
                            Launch(NULL,0,NULL); // launch prefs
                            break;
                          case CXCMD_DISAPPEAR:
                            if (t=FindTask("EasyTM-Prefs")) {
                              Signal(t,SIGBREAKF_CTRL_D);
                            } // if
                            break;
                          case CXCMD_KILL:
                            running=FALSE;
                            break;
                          case CXCMD_UNIQUE:
                            Launch(NULL,0,NULL); // launch prefs
                            break;
                        } // switch
                        break;
                    } // switch
                    ReplyMsg((struct Message *)msg);
                  } // while GetMsg
                } // if sig_n
                if (sret & SIGBREAKF_CTRL_C) running=FALSE;
                if (sret & SIGBREAKF_CTRL_D) {
                  if (TRUE==Active) FreeMenus();
                  if (1==ReadFile(ToolsFileName)) {
                    if (TRUE==Active)
                      if (0==BindMenus()) running=FALSE;
                  } else running=FALSE;
                } // if CTRL_D
              } // while !quit
              FreeMenus();
            } // if BindMenus

            ActivateCxObj(broker, 0L);
            DeleteCxObj(broker);
          } // if CreateBroker
        } // if OpenPort
        ClosePorts(); // yes, this is in the correct place
        FreeVec(ToolsFileName);
        FreeVec(PrefsProgDir);
      } // if ReadIcon
    } // if OpenLibs
    CloseLibs(); // yes, this is in the correct place
    FreeList(1);
  } // if InitList
} // main

//**** End of file
