/*


            callsound2.c     M.E.Schretlen.   88-01-28


            Sends messages to message port "gimme.my.message"

            (this is the message port for "soundproc" )

            Compiled with ye old non-proto Lattice 3.10

*/
#include "soundproc.h"
#include "libraries/dos.h"
#include "exec/memory.h"
#include "workbench/startup.h"
#include "workbench/workbench.h"
#include <stdio.h>
#include <string.h>
#include <time.h>

struct MsgPort *crazyPort;
struct MsgPort *mainPort;
extern struct WBStartup *WBenchMsg; /* def in Lattice _main.c */
extern int argc;                    /* def in Lattice _main.c */

ULONG what;
UBYTE whatid;

#define NOFILE  1
UBYTE nofiles = 0;
extern void getpath();
ULONG IconBase = NULL;
char buffer[256] = "\0";
static char * path = &buffer[0];
char load[256] = "LOADPROC";
char soundproc[256] = "SOUNDPROC";
char command[256] = "";

init_sound()
{
   int success;
   long * timeptr = NULL;
   long timein;

   /* Find out where we started ( CLI or Workbench ?). */

   if ( argc == 0 )          /* then this was started from WB */
      {
         getpath(WBenchMsg->sm_ArgList->wa_Name);
         strins(load,path);
         strins(soundproc,path);
      }

   /* check for loadproc & soundprocess in given path  */

   if ( (access(load,0) == -1 ))
     {
        nofiles = NOFILE;
        printf("no loadproc on this path\n");
        return;
     }
   if ( (access(soundproc,0) == -1 ))
     {
        nofiles = NOFILE;
        printf("no soundproc on this path\n");
        return;
     }
   strins(command,soundproc);
   strins(command," ");
   strins(command,load);

   timein = time(timeptr);
   success = Execute ( command,0,0);
   while (  (crazyPort = (struct MsgPort *)FindPort("gimme.my.message"))  == 0)
      {
         /*  "while" for a half minute then kick out */

         if ( (time(timeptr) - timein) > 30 )
           {
             nofiles = NOFILE;
             printf("Where is the sound message port ?\n");
             return;
           }
      }
   t = (struct test_message *)AllocMem(sizeof(*t),(1<<0));
   if(t == 0)
      {
         printf("not enough memory for message!\n");
         exit(20);
      }
   if((mainPort = CreatePort("callingreplyport",0)) == 0)
      {
         printf("could not create the reply port\n");
         FreeMem(t,sizeof(*t));
         exit(20);
      }

   t->m.mn_Node.ln_Type = NT_MESSAGE;
   t->m.mn_Length = sizeof(struct data);
   t->m.mn_ReplyPort = mainPort;
   t->d.flags = VERBAGE;
   t->d.string = "The child awakes.\n";
   t->d.function = YAHSOUND;
   t->d.id = 0;

   return;
}

void do_sound(thisSOUND,length)
ULONG thisSOUND;
int length;
{
       if (nofiles) return;
       if (thisSOUND == NULL)
              {
                t->d.flags = TURNOFF;
                t->d.id = (UBYTE)TURNOFF;
              }
       else
              {
                t->d.flags = VERBAGE;
                t->d.length = length;
                t->d.function = thisSOUND;
              }

      PutMsg(crazyPort,t);

   /* this expects a reply from a PutMsg() to crazyPort before proceeding */

      Wait(1L << mainPort -> mp_SigBit);
      while((t = (struct test_message *)GetMsg(mainPort)) != 0)
         {
            what = t->d.flags;
            whatid = t->d.id;
            if ( whatid == TURNOFF )
              {
                FreeMem(t,sizeof(*t));
                DeletePort(mainPort);
              }
            return;
         }
}


void getpath(name_of_icon)
char * name_of_icon;
   {
   struct DiskObject *diskobj;
   char **toolarray;
   char * string;
   extern char * path;

   if((IconBase = OpenLibrary("icon.library", 0)))
      {
      diskobj=(struct DiskObject *)GetDiskObject(name_of_icon);
      if(diskobj)
         {
         toolarray = (char **)diskobj->do_ToolTypes;
         string=(char *)FindToolType(toolarray,"PATH");
         strcpy(path,string);
         FreeDiskObject(diskobj);
         }
      CloseLibrary(IconBase);
      }
   return;
   }
