/* This file is part of the origami distribution. (Amiga Port)
 *
 * Source code written by Thomas Hadig in September 1991
 * Version : 1.6.42 alpha, June 1992
 * Arexx : gamma
 * (C) 1992 by Thomas Hadig
 * may be distributed unchanged ! (see copyright notes)
 */

/*{{{  #includes*/
#include <exec/types.h>
/*{{{  standard includes*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*}}}  */
#undef TRUE
#undef FALSE

#include <local/bool.h>
/*
#include "../h/keys.h"
*/
#include "origami:keybind/keybind.h"
/*{{{  amiga system includes*/
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <exec/io.h>
/*}}}  */
/*{{{  amiga prototype includes*/
#ifdef LATTICE
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#else
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <clib/graphics_protos.h>
#endif
/*}}}  */

#include "origami:amiga/h/arexx.h"

#ifdef LATTICE
#include <dos.h>
#endif
/*}}}  */

/*{{{  variables*/
int i=0;
#define PORT_NAME_DEFAULT "O"
/*{{{  arexx variables*/
  struct FORKENV       forkenv = { 5L, 10000L, NULL, NULL, NULL, NULL};
  struct ProcID        arexx_pid;
  struct MsgPort       *arexxport=NULL,*arexxtaskport = NULL;
  struct My_ArexxMessage  arexxmsg,*arexx_tag_msg=NULL;
  char   arexxtaskarg1[15], portname[20] = PORT_NAME_DEFAULT,
           oas[]="Origami_Arexx";
  bool   arexx_p = FALSE, arexx_tag = FALSE;
  long   arexx_signal = NULL;
  extern TOKEN *arexx_fix_macro_req,*arexx_fix_macro_ocl;
/*}}}  */
short  x=0,y=11;
struct IntuitionBase *IntuitionBase;
struct GfxBase       *GfxBase;
FILE *fd=NULL;
/*}}}  */

/*{{{  i_to_a*/
public char *i_to_a(long x)
{
#  define NBSIZE ((sizeof(long)<<2)+1)
  static char number[NBSIZE];
  char *s=number+NBSIZE-1;
  bool neg;

  if (neg=(x<0)) x= -x;
  *s=0;
  do
   { *(--s)="0123456789"[x%10]; }
  while ((x/=10)>0);
  if (neg) *(--s)='-';
  return(s);
}
/*}}}  */
/*{{{  init and reset amiga*/
/*{{{  reset_amiga*/
int reset_amiga (void)

 {
  if (arexx_p)
    /*{{{  wait till arexx-task has ended*/
    {
     int stop = 0;

     arexxmsg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
     arexxmsg.class = AREXX_COME_DOWN;
     if (arexxtaskport)
      {
       PutMsg (arexxtaskport,(struct Message *)&arexxmsg);
       do
        {
         struct My_ArexxMessage *msg;

         WaitPort (arexxport);
         while ((msg = (struct My_ArexxMessage *)GetMsg (arexxport))&&(!stop))
          {
           if (msg->class == AREXX_CAME_DOWN)
            {
             PutMsg (arexxport,(struct Message *)msg);
             wait (&arexx_pid);
             stop = 1;
            }
           else if (msg->ExecMessage.mn_Node.ln_Type != NT_REPLYMSG)
             ReplyMsg ((struct Message *)msg);
          }
        } while (!stop);
      }
    }
    /*}}}  */
  if (arexx_p) { RemPort (arexxport); arexx_p = FALSE; }
  if (fd) { fclose (fd); fd = NULL; }
  if (GfxBase) { CloseLibrary (GfxBase); GfxBase = NULL; }
  if (IntuitionBase)
   {
    CloseLibrary ((struct Library *)IntuitionBase);
    IntuitionBase = NULL;
   }
  return 0;
 }
/*}}}  */
/*{{{  init_amiga*/
void init_amiga (void )

 {
  /*{{{  open libraries*/
  if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary (
       "intuition.library",NULL)))
      exit (1);
  if (!(GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library",
       NULL)))
   {
    reset_amiga();
    exit (1);
   }
  /*}}}  */
  /*{{{  Open window*/
  if (!(fd = fopen ("CON:0/11/200/80/test","r+")))
  {
    reset_amiga();
    exit (1);
  }
  /*}}}  */
  /*{{{  Create Arexx Port*/
  strcat (portname,i_to_a((long)FindTask(NULL)));
  if (arexxport  = CreatePort (portname,0))
  {
    arexx_p = TRUE;
    arexx_signal = 1 << arexxport->mp_SigBit;
    forkenv.msgport = arexxport;
    if (!(forkl (oas,oas,portname+strlen(PORT_NAME_DEFAULT),NULL,&forkenv,
        &arexx_pid)))
    {
      struct My_ArexxMessage *mam;
      int stop = 0;

      do
      {
        fprintf (fd,"Opened Port and started orirexx\n");
        WaitPort (arexxport);
        while (mam = (struct My_ArexxMessage *)GetMsg (arexxport))
        {
          fprintf (fd,"Got Message Type %d\n",mam->class);
          if (mam->class==AREXX_CAME_UP)
          {
            fprintf (fd,"Got CameUp Message\n");
            arexxtaskport = mam->ExecMessage.mn_ReplyPort;
            strcpy (arexxmsg.my_message,"$PORTNAME");
            arexxmsg.class=AREXX_OUT;
            arexxmsg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
            arexxmsg.ExecMessage.mn_ReplyPort = arexxport;
            PutMsg (arexxtaskport,(struct Message *)&arexxmsg);
            stop=1;
          }
          if (mam->ExecMessage.mn_Node.ln_Type!=NT_REPLYMSG)
            ReplyMsg ((struct Message *)mam);
        }
      } while (!stop);
      /*arexxmsg.ExecMessage.mn_Node.ln_Type = NT_REPLYMSG;*/
    }
  }
  /*}}}  */
 }
/*}}}  */
/*}}}  */

/*{{{  main*/
void main (void)
{
  struct My_ArexxMessage *msg;

  init_amiga ();
  while (!i)
  {
    if (msg=(struct My_ArexxMessage *)GetMsg (arexxport))
    {
      fprintf (fd,"Message : %d",msg->class);
      if (msg->ExecMessage.mn_Node.ln_Type!=NT_REPLYMSG)
        ReplyMsg ((struct Message *)msg);
    }
  }
  reset_amiga ();
}
/*}}}  */
