/*
	CD³²Ctrl - controlls several features of your CD³² games console

	this is just an AmigaDOS frontend for the lowlevel.library function
	SystemControl(). written by Daniel Balster

	example usage:
	
	CD32CTRL NOREQ NOCDREBOOT ; no more requesters & cdeject-resets
	CD32CTRL REQ CDREBOOT     ; the opposite...

	do not offer both (i.e. REQ NOREQ) i won't check that.

	(not much comments in it - forgive me)
*/

#include <exec/exec.h>
#include <dos/dos.h>
#include <libraries/lowlevel.h>
#include <devices/cd.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/lowlevel.h>

#define TEMPLATE "REQ/S,NOREQ/S,CDREBOOT/S,NOCDREBOOT/S,EJECTRESET/S,NOEJECTRESET/S,SPEED/K,QUIET/S"

struct abc {
	ULONG	req,noreq,res,nores,eject,noeject,speed;
	ULONG	quiet;
	ULONG	pads[8]; /* someone said RDArgs must be sized to a 16-longs boundary ??? */
};

struct TagItem CfgSpeed[] =
{
	{ TAGCD_READSPEED, 150 },
	{ TAG_END, 0 }
};

struct TagItem CfgEject[] =
{
	{ TAGCD_EJECTRESET, FALSE },
	{ TAG_END, 0 }
};

int main (void)
{
	struct RDArgs *rdargs;
	struct abc args = {0};
	struct MsgPort *mp;
	struct IOStdReq *ior;
	
	if (rdargs=ReadArgs(TEMPLATE,(LONG*)&(args),NULL))
	{
		if(!args.quiet)
			PutStr("*** CD³²Ctrl Version 1.0.0 *** by Daniel Balster\n");

		if(mp=CreateMsgPort())
		{
			if(ior=(struct IOStdReq*)CreateIORequest(mp,sizeof(struct IOStdReq)))
			{
				if(!OpenDevice("cd.device",0,ior,0))
				{
					/* ok, the following is somehow ugly - but who carez */
				
					if (args.eject)
					{
						CfgEject[0].ti_Data = TRUE;
						ior->io_Command	= CD_CONFIG;
						ior->io_Data	= (APTR) &CfgEject;
						ior->io_Length	= 0;
						DoIO(ior);
					}

					if (args.noeject)
					{
						CfgEject[0].ti_Data = FALSE;
						ior->io_Command	= CD_CONFIG;
						ior->io_Data	= (APTR) &CfgEject;
						ior->io_Length	= 0;
						DoIO(ior);
					}

					if (args.speed)
					{
						CfgSpeed[0].ti_Data = *(ULONG*)args.speed;
						ior->io_Command	= CD_CONFIG;
						ior->io_Data	= (APTR) &CfgSpeed;
						ior->io_Length	= 0;
						DoIO(ior);
					}
				
					CloseDevice((struct IORequest*)ior);
				}
				else PutStr("cannot access cd.device?\n");
				
				DeleteIORequest((struct IORequest*)ior);
			}
			else PutStr("cannot create ioreq?\n");

			DeleteMsgPort(mp);
		}
		else PutStr("cannot create msgport?\n");


		if (args.req) SystemControl(SCON_KillReq,FALSE,TAG_END);
		if (args.noreq) SystemControl(SCON_KillReq,TRUE,TAG_END);
		if (args.res) SystemControl(SCON_CDReboot,CDReboot_On,TAG_END);
		if (args.nores) SystemControl(SCON_CDReboot,CDReboot_Off,TAG_END);

		FreeArgs(rdargs);
	}
	else PrintFault(IoErr(),0);

	return RETURN_OK;
}
