/*
**	reboot.c - reboot Amiga
**	$VER: reboot.c 42.2 (28.9.96)
**	Copyright © 1996 Michal Letowski
**
**	42.1 (10.2.96) - initial version
**	42.2 (28.9.96)
**		+ added DELAY option
*/

#define __USE_SYSBASE

#include <exec/types.h>
#include <exec/execbase.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <support/types.h>
#include <support/dos.h>

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

#include "reboot.rev.h"


/*
**	Constants
*/
#define DOS_NAME			"dos.library"
#define DOS_VERN			37L

#define TEMPLATE			"NORES=CLEAR/S,DELAY/N"


/*
**	Private structures
*/
struct Options
{
	LBOOL opt_Clear;															/* NORES=CLEAR/S */
	LONG *opt_Delay;															/* DELAY/N */
};	/* Options */


/*
**	Global data
*/
STATIC CONST TEXT VersionString[]=
	VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);


/*
**	Public functions
*/
/****** C/Reboot ************************************************************
*
*   NAME
*       Reboot -- reboot your computer. (V42)
*
*   SYNOPSIS
*       Reboot [CLEAR] [DELAY=DelayValue]
*
*   TEMPLATE
*       Reboot "NORES=CLEAR/S,DELAY/N"
*
*   FUNCTION
*       Reboots your Amiga by calling exec.library/ColdReboot(). If CLEAR
*       parameter is given, will clear system vectors befor rebooting. This
*       is useful if you want to get rid of some reset-resident programs,
*       like RAD: drive. A DELAY option can be used to delay rebooting.
*
*   INPUTS
*       CLEAR - switch to clear system vectors before rebooting.
*       DELAY - number of seconds to wait before rebooting.
*
*   RESULT
*       RETURN_FAIL  - if 'dos.library' couldn't be opened.
*       RETURN_ERROR - if DELAY value was wrong.
*				RETURN_WARN  - if reboot wasn't performed.
*
*   EXAMPLE
*       Reboot
*           ; Reboots Amiga, doesn't touch resident programs.
*
*       Reboot CLEAR
*           ; Removes resident programs then reboots your Amiga.
*
*       Reboot 10
*           ; Reboots Amiga after a 10 seconds period.
*
*   NOTES
*       Reboot is pure and can be made resident.
*
*****************************************************************************
*
*/
LONG Reboot(VOID)
{
	struct ExecBase *SysBase=INITSYSBASE;
	struct Library *DOSBase;

	struct Options Opts;
	struct RDArgs *Args;
	LONG DelayAmnt;
	LONG RC=RETURN_FAIL;
	
	/* Open DOS */
	unless(DOSBase=OpenLibrary(DOS_NAME,DOS_VERN))
		throw2(SetResult2(ERROR_INVALID_RESIDENT_LIBRARY),	NO_DOS);

	RC=RETURN_ERROR;															/* Working a bit... */

	/* Read arguments */
	clear(&Opts);																	/* Clear out buffer */
	unless(Args=ReadArgs(TEMPLATE,(LONG *)&Opts,NULL))
		throw2(PrintFault(IoErr(),PROG_NAME),	NO_ARGS);

	RC=RETURN_WARN;																/* Set warning code */

	/* Do the thing */
	if(!Opts.opt_Delay || *Opts.opt_Delay<0)
		DelayAmnt=0;
	else
		DelayAmnt=*Opts.opt_Delay*TICKS_PER_SECOND;

	Delay(DelayAmnt);															/* Wait a moment... */
	Disable();																		/* Nothing messing around... */
	if(Opts.opt_Clear)														/* Clearing requested... */
	{																							/* Perform it! */
		SysBase->ColdCapture=NULL;
		SysBase->CoolCapture=NULL;
		SysBase->WarmCapture=NULL;
		SysBase->KickMemPtr=NULL;										/* No allocations */
		SysBase->KickTagPtr=NULL;										/* Only ROM resident modules */
	}
	ColdReboot();																	/* Reboot!!! */
	Enable();																			/* In case ColdReboot() returns ;-) */

	RC=RETURN_OK;

	catch(NO_ARGS,	FreeArgs(Args));							/* Free options */
	catch(NO_DOS,		CloseLibrary(DOSBase));				/* Close DOS */
	return(RC);
}	/* Reboot */
