/*************************************************************************

MUIEmpire Service written by Karl Bellve

   
*************************************************************************/


#include <exec/types.h>
#include <exec/memory.h>
#include <exec/ports.h>

#include <utility/tagitem.h>
/*
#include <workbench/startup.h>
*/
#include <dos/dostags.h>
#include <dos.h>

#include <envoy/nipc.h>
#include <envoy/services.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/nipc_pragmas.h>
#include <pragmas/utility_pragmas.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/nipc_protos.h>
#include <clib/alib_protos.h>
#include <clib/utility_protos.h>

#include "string.h"
#include "stdio.h"

int  __saveds __UserLibInit(void);
void __saveds __UserLibCleanup(void);

struct Library *UtilityBase, *DOSBase;

struct MyMsg  /** Client/Server message to/from MUX and Protocol **/
{
  struct Message msg;
  ULONG  Type;
  ULONG  Length;            
  UBYTE  *Data;
};

#define ASM           __asm
//StartServiceA   (struct TagItem *);
//GetServiceAttrsA(struct TagItem *);

int x;

VOID __saveds ASM LIBRexxReserved() { return; }


/**************************************************************************
StartService() - Starts the service.

    Services Manager calls this function to attempt to start the service.

    INPUTS: TagList passed in by Services Manager
    OUTPUT: Error Code - Non Zero Means Error
**************************************************************************/
ULONG __saveds ASM LIBStartServiceA(register __a0 struct TagItem *st_list)
{
    struct TagItem cptags[3],*tag;
    char   name[30];
    ULONG  MUIEmpireError = 0;
    struct MyMsg *mmsg;
    struct MsgPort *parentport;
    BOOL foo = TRUE;

    x++;
    sprintf(&name[0],"empireserver.%d\0",x);
    if (parentport = CreateMsgPort())
      {
      parentport->mp_Node.ln_Name = "MUIEmpire_service.port";
      parentport->mp_Node.ln_Pri = 0;
      AddPort(parentport);
    }
    cptags[0].ti_Tag  = NP_Name;
    cptags[0].ti_Data = (ULONG) name;
    cptags[1].ti_Tag  = SYS_Asynch;
    cptags[1].ti_Data = TRUE;
    cptags[2].ti_Tag  = TAG_DONE;
    cptags[2].ti_Data = NULL;
            

    MUIEmpireError = SystemTagList("Empire:EnvoyEmpireServer",(struct TagItem *)cptags);
    
    while (foo) {
      while (mmsg = (struct MyMsg *)GetMsg(parentport)) {
        if (mmsg->Type == 1) memcpy(mmsg->Data,name,30);
        if (mmsg->Type == 2) foo = FALSE;
        ReplyMsg((struct Message *)mmsg);
      }
      if (foo) WaitPort (parentport);
    }
    
    if(tag=FindTagItem(SSVC_EntityName,st_list))
      {
      strcpy((char *)tag->ti_Data,name);
      }
    else MUIEmpireError = 1;
    
    if (parentport)   {
       if (parentport->mp_Node.ln_Name)   RemPort(parentport);
       DeletePort(parentport);
    }
 
    return(MUIEmpireError);
}



/*************************************************************************
GetServiceAttrsA() - Get service attributes

    Required to return the name of the service in tag SVCAttrs_Name

    INPUTS: TagList from services manager to process/update
*************************************************************************/
VOID __saveds ASM LIBGetServiceAttrsA(register __a0 struct TagItem *tagList)
{
    struct TagItem *ti;

    if(ti=FindTagItem( SVCAttrs_Name, tagList))
    {
        strcpy((STRPTR)ti->ti_Data,"MUIEmpire_Service");
    }
}


int __saveds __UserLibInit(void) {
  
  UtilityBase = OpenLibrary("utility.library",37);
  DOSBase = OpenLibrary("dos.library",37);
  
  if (!DOSBase) return(1);
  if (!UtilityBase){
     CloseLibrary(DOSBase);
     return (1);
  }
 
  return(0);
}

void __saveds __UserLibCleanup(void) {
  
  
  CloseLibrary(DOSBase);
  CloseLibrary(UtilityBase);
   
  return;
}
