/*
	CD Stopper Daemon for the CD³² Multimedia Computer

	( Original Idea found on Aminet-5 CD, its called
	  aminet5:aminet/wb/util/cd_stop.lha )

	this has nothing to do with the similar program
	named above - you should use your favorite; note
	that the other one is much shorter but has not
	the ability of being a daemon.
	

	written by Daniel Balster, dbalster@uni-paderborn.de
*/

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

#define DEFAULT_DELAY	500	/* ticks */

struct {
	ULONG	interval;
	BOOL	quiet;
	ULONG	pads[14];
} args = {0};

int main (void)
{
	struct RDArgs *rdargs;
	struct MsgPort *mp;
	struct IOStdReq *ior;
	
	BOOL quit = FALSE;

	if (rdargs=ReadArgs("DELAY/N,QUIET/S",(LONG*)&(args),NULL))
	{
		if(!args.quiet) PutStr("*** CD Stopper *** for the Amiga CD³²® Computer\nwritten by Daniel Balster\n");

		if(mp=CreateMsgPort())
		{
			if(ior=(struct IOStdReq*)CreateIORequest(mp,sizeof(struct IOStdReq)))
			{
				if(!OpenDevice("cd.device",0,ior,0))
				{
					while (!quit)
					{
						if(!args.quiet) PutStr("stopping motor!\n");
						ior->io_Command	= CD_MOTOR;
						ior->io_Length	= 0;		/* motor off */
						DoIO(ior);
			
						Delay (*(ULONG*)args.interval);
			
						if(SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) quit=TRUE;
					
					}
					PutStr("*** BREAK!\n");
				
					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");

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

	return RETURN_OK;
}