#include "struct.c"
#include "WinPlot.h"
#include "amigawin.h"

/* at least Kick20 is required */
long __oslibversion = 36;
void __regargs __autoopenfail(char *);

/* the library bases */
struct Library       *MUIMasterBase = NULL;
struct FileRequester *FileReq = NULL;

/* some private definitions */
static struct MsgPort *GnuPort = NULL;

int _STI_200_OpenMUI(void)
{
 
 if (!(MUIMasterBase = OpenLibrary("muimaster.library", MUIMASTER_VMIN)))
  {
   __oslibversion = MUIMASTER_VMIN;
   __autoopenfail("mui.library V2.1");
   __oslibversion = 36;
   return 1;
  }
 return 0;
}

void _STD_200_CloseMUI(void)
{
 if (MUIMasterBase)
  CloseLibrary(MUIMasterBase);
}

int _STI_OpenSystem(void)
{
 char Buffer[255];
 int  i;

 for(i=0; i<MAXWINDOW; i++)
  Windows[i].WI_Plot = Windows[i].BT_Plot = Windows[i].BT_Load = Windows[i].BT_Save =
  Windows[i].BT_CD = Windows[i].BT_Set = Windows[i].BT_New = Windows[i].BO_Plot = NULL;
 GetCurrentDirName(Buffer, 255);
 if (!(FileReq = MUI_AllocAslRequestTags(ASL_FileRequest,
					 ASLFR_PrivateIDCMP,  TRUE,
					 ASLFR_RejectIcons,   TRUE,
					 ASLFR_InitialDrawer, Buffer,
					 TAG_END)))
  {
   MUI_Request(NULL, NULL, 0, "WinPlot", "Ok", "Canīt allocate FileRequest structure!\n");
   return 1;
  }

#ifndef _DEBUG
 if (!FindPort(GNUREPLY))
  {
   MUI_Request(NULL, NULL, 0, "WinPlot", "Ok",
	       "This is a GnuPlot terminal driver, Please start GnuPlot first !");
   return 1;
  }
#endif

 return 0;
}

void _STD_CloseSystem(void)
{
 if (FileReq) MUI_FreeAslRequest(FileReq);
}

ULONG main(void)
{
 BOOL                Quit = FALSE;
 ULONG               PortFlag, RexxReplyFlag;
 struct PlotMessage *PlotMsg;
 struct Message     *Msg;

 if (GnuPort = CreatePort(GNUPORT, 0))
  {
   PortFlag = 1 << GnuPort->mp_SigBit;
   RexxReplyFlag = 1 << RexxReplyPort->mp_SigBit;

   if (AP_WinPlot = GetGUI())
   {
    set(Windows[0].WI_Plot, MUIA_Window_Open, TRUE);
    
    while (!Quit)
     {
      LONG  MUI_ID;
      ULONG MUI_Signal, Sig;
      
      MUI_ID = DoMethod(AP_WinPlot, MUIM_Application_Input, &MUI_Signal);
      if (MUI_ID != 0)
       Quit = HandleMUI(MUI_ID);
      if (MUI_Signal)
       Sig = Wait(MUI_Signal|PortFlag|RexxReplyFlag);
      else
       Sig = 0L;
      
      if (Sig & PortFlag)
       while (PlotMsg = (struct PlotMessage *)GetMsg(GnuPort))
	{
	 switch(PlotMsg->PlotCom.gpc_com)
	  {
	  case SET_GRAPHICS:
	   set(AP_WinPlot, MUIA_Application_Sleep, TRUE);
	   break;
	  case SET_TEXT:
	   ncommands = PlotMsg->PlotCom.gpc_ncommands;
	   commands = PlotMsg->PlotCom.gpc_commands;
	   set(AP_WinPlot, MUIA_Application_Sleep, FALSE);
	   Refresh();
	   break;
	  case RESET:
	   Quit = TRUE;
	   break;
	  case SETVAR:
	   Settings((struct SetVar *)PlotMsg->PlotCom.gpc_arg);
	   break;
	  case PAUSE:
	   WaitRequest(PlotMsg->PlotCom.gpc_arg);
	   break;
	  }
	
	 ReplyMsg(PlotMsg);
	}
      if (Sig & RexxReplyFlag)
       if (GetMsg(RexxReplyPort))
	ReplyRexxMsg();
     }

    while (Msg = GetMsg(GnuPort))
     ReplyMsg(Msg);
    while (Msg = GetMsg(RexxReplyPort))
     ReplyMsg(Msg);

    SaveConf();
    MUI_DisposeObject(AP_WinPlot);

   }
   DeletePort(GnuPort);
  }

 return 0;
}
