/*
  These SAS/C autoopen/autoclose functions was supplied by Mattias
  Karlsson. Thanks!
*/

#include <devices/ahi.h>
#include <proto/ahi.h>
#include <proto/exec.h>
#include <dos.h>

extern void __regargs __autoopenfail(char *);

extern long __oslibversion;

struct Library    *AHIBase;
struct MsgPort    *AHImp=NULL;
struct AHIRequest *AHIio=NULL;
BYTE               AHIDevice=-1;

// Autoopen rutin för ahi.
int __stdargs _STI_openahi(void)
{

	if(AHImp=CreateMsgPort())
  	{
		if(AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest)))
		{
			AHIio->ahir_Version=2;  	// Open at least version 2 of 'ahi.device'.

			if(!(AHIDevice=OpenDevice(AHINAME,AHI_NO_UNIT,(struct IORequest *)AHIio,NULL)))
		    {
				AHIBase=(struct Library *)AHIio->ahir_Std.io_Device;
				return 0;
			}
		}
	}

	// Så vi får rätt version angivelse
	__oslibversion=2;
	__autoopenfail("ahi.device");
     return 1;
}

// autoclose rutin finns det oxo :)
void __stdargs _STD_closeahi(void)
{

	if(AHIDevice==0)
	{
        CloseDevice((struct IORequest *)AHIio);
	}

	if(AHIio)
	{
      DeleteIORequest((struct IORequest *)AHIio);
      AHIio=NULL;
    }

	if(AHImp)
	{
    	DeleteMsgPort(AHImp);
	    AHImp=NULL;
	}
}
